31
loading...
This website collects cookies to deliver better user experience
<ul>
<li>Computer Science Eng.</li>
<li>Mathematics</li>
<li>Physics</li>
</ul>
* {
border: 1px solid coral;
}
<body>
element and the <html>
element. div :first-child {
background-color: maroon;
}
div:first-child {
background-color: maroon;
}
<div>
element and the second one selects the <div>
elements that are first children themselves of another element.div *:first-child{
background-color: maroon;
}
<p>A paragraph</p>
<p>Another paragraph</p>
<p>One more paragraph</p>
p {
font-size: 1.5rem;
color: deeppink;
}
<p>
elements. Easy and direct.<div>
<p class="deeppink">This is a deep pink paragraph</p>
<p class="hotpink">This is a hot pink paragraph</p>
<p class="pink">This is a regular pink paragraph</p>
<p class="deeppink-bg">This is a paragraph with a deep pink background</p>
<p class="hotpink-bg">This is a paragraph with a hot pink background</p>
<p class="pink-bg">This is a paragraph with a regular pink background</p>
</div>
.deeppink {
color: deeppink;
}
.hotpink {
color: hotpink;
}
.pink {
color: pink;
}
.deeppink-bg {
background-color: deeppink;
}
.hotpink-bg {
background-color: hotpink;
}
.pink-bg {
background-color: pink;
}
/* Group selector - Stay until the end to understand its
behavior 😉 */
.deeppink-bg, .hotpink-bg, .pink-bg {
color: white;
}
<p>
element depending on the class they contain, styling either their text or their background.<ul>
<li class="list-item">...</li>
<li class="list-item">...</li>
<li class="list-item">...</li>
</ul>
.list-item{
padding: 5px;
}
li.list-item{
padding: 5px;
}
<li>
elements, so appending the element type wouldn't make any difference .<section>
<h1 class="big">List of animals</h1>
<ul>
<li><p class="big">Cat</p></li>
<li><p class="big">Dog</p></li>
<li><p class="big">Monkey</p></li>
<li><p class="big">Dolphin</p></li>
<li><p class="big">Frog</p></li>
</ul>
</section>
h1.big {
font-size: 3rem;
color: coral;
}
p.big {
font-size: 2rem;
color: orange;
}
<div>
<table id="users-table">
<th>Users</th>
<tr>
<td>John Doe</td>
</tr>
<tr>
<td>Jane Doe</td>
</tr>
</table>
<table id="staff-table">
<th>Staff</th>
<tr>
<td>Hannah Legend</td>
</tr>
<tr>
<td>Daniel Oaks</td>
</tr>
</table>
</div>
/* Type selector */
table {
padding: 20px;
}
/* ID selectors */
#users-table {
background-color: black;
color: white;
}
#staff-table {
border: 1px solid black;
}
Important: Even though HTML allows you to assign the same id value to several elements, you shouldn't ever do it.
If you need to apply the same style to a bunch of different elements, always use the class attribute. You'll keep your code clean and will get rid of a possible confusion.
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
/* Type selector */
nav {
background-color: darkslategray;
padding: 10px;
}
/* Attribute selector */
a[href] {
color: white;
text-decoration: none;
}
<a>
element that contains the href
attribute, regardless of its value, and removes the underline.<nav>
element with a background color and some padding by making use of the type selector.<form>
<label for="username">Username</label>
<input id="username" type="text" placeholder="Username" />
<label for="password">Password</label>
<input id="password" type="password" placeholder="Password" />
</form>
input[type="text"] {
color: chocolate;
}
<input>
element that has the type
attribute with an exact value of text.[id="users-table"] {
background-color: black;
color: white;
}
[id="staff-table"] {
border: 1px solid black;
}
Explanation: By using this attribute selector syntax, we are targeting elements that contain an id attribute whose value is exactly users-table or staff-table. We're using this one syntax because an element can only have one idname, so the value needs to be exact (=).
<p lang="en-gb en-us en-ca en-au en-nz">Welcome!</p>
<p lang="fr-fr fr-ca fr-be">Bienvenue!</p>
p[lang~="en-us"] {
color: navy;
}
p[lang~="fr-fr"] {
color: red;
}
<p>
element whose value for lang
is a list that contains the exact string en-us. Same for all <p>
elements whose value for lang
contains fr-fr, applying a red color in this case.[class~="list-item"]{
padding: 5px;
}
Explanation: By using this attribute selector syntax, we are targeting elements that have a class attribute whose value is a list that contains the string list-item.
We're using this specific syntax because an element can contain several classes and we're aiming at just one of them (~=). But even though the element had just one class, the value for the class attribute always acts like a list of values.
<p lang="en-us">Hello</p>
<p lang="en-gb">Hello</p>
p[lang|="en"] {
background-color: cornflowerblue;
color: antiquewhite;
}
<p>
element that has a lang
attribute whose value is exactly en or starts with en-.<a href="#list1">Go to list 1</a>
<a href="#list2">Go to list 2</a>
<section>
<h1>Lists</h1>
<div id="list1">
<h1>List 1</h1>
<ul>
<li>Milk</li>
<li>Butter</li>
<li>Eggs</li>
<li>Sugar</li>
</ul>
</div>
<div id="list2">
<h1>List 2</h1>
<ul>
<li>Oranges</li>
<li>Lemons</li>
<li>Strawberries</li>
<li>Apples</li>
</ul>
</div>
</section>
a[href^="#"] {
color: crimson;
font-size: 2rem;
}
<a href="css-selectors-guide.pdf">CSS Selectors Guide</a>
a[href$=".pdf"] {
color: slateblue;
}
<a>
element that has a href
attribute whose value ends with .pdf.<div class="small-box">
<p>This is a small box</p>
</div>
<div class="big-box">
<p>This is a big box</p>
</div>
div[class*="box"] {
background-color: burlywood;
}
<div>
element that contains a class
attribute whose value has at least one occurrence of the string box.<h1>Computer Science Engineering</h1>
<h2>List of courses</h2>
<ul>
<li>Algebra</li>
<li>Calculus</li>
<li>Physics</li>
<li>Discrete Mathematics</li>
<li>Introduction to Programming</li>
</ul>
h1, h2, li {
color: darkred;
}
<h1>
, <h2>
and <li>
element.<nav>
<ul>
<li><a>Home</a></li>
<li>
<a>People</a>
<ul>
<li><a>Students</a></li>
<li>
<a>Faculty members</a>
<ul>
<a>Discrete Mathematics</a>
<a>Programming</a>
<a>Physics</a>
<a>Algorithms</a>
</ul>
</li>
<li><a>Staff members</a></li>
</ul>
</li>
<li><a>About</a></li>
<li><a>Contact</a></li>
</ul>
</nav>
nav a {
border: 1px solid crimson;
color: darkslateblue;
font-size: 1.5rem;
font-weight: bold;
}
<a>
element that is descendant of a <nav>
element, regardless of how nested they are.<div class="box">
<p>This is a direct child of .box</p>
<div>
<p>This is not a direct child of .box</p>
</div>
<p>This is another direct child of .box</p>
</div>
.box > p {
color: darkgoldenrod;
}
<p>
element that is a direct child of any element that has the class box, so, in this HTML example, the first and last <p>
elements will be selected, but not the one in the middle.<img src="blue-mug.jpeg" alt="a regular blue coffee mug" />
<p>Blue mug</p>
<p>Price: $15</p>
img ~ p {
color: darkblue;
}
<p>
element which is a sibling of any <img>
element and comes after it. In this example, both <p>
elements will be selected.<img src="blue-mug.jpeg" alt="a regular blue coffee mug" />
<p>Blue mug</p>
<p>Price: $15</p>
img + p {
color: darkblue;
}
<p>
element will be selected, since the second one doesn't appear immediately after the <img>
element.<h1>Shopping list</h1>
<ul>
<li>Milk</li>
<li>Butter</li>
<li>Eggs</li>
<li>Sugar</li>
</ul>
li:hover {
background-color: black;
color: white;
}
<li>
element when the cursor hovers over it.:active
, :hover
, :focus
, :disabled
, :checked
, :first-child
, :nth-child
, :first-of-type
.<p>CODE</p>
p::before{
content: "_";
}
<p>
element.::after
(can also be written as :after
), ::before
(can also be written as :before
), ::marker
, ::placeholder
, ::first-letter
.