8:00p EST / 7:00p CST / 5:00p PST
Visual Studio Code or a code editor of your choice
The selector is used to select or find which elements on the HTML page will be given the styles inside the curly braces.
selector {
property: value;
property: value;
}
p {
color: yellow;
background-color: black;
}
Element or Type selector
Class selector
ID selector
Selects all elements of the same type
This example below selects all p
tags or paragraph elements
p {
color: purple;
}
Class
selectors are prefixed with a "."
dot, then followed by the class name
.product-text {
color: purple;
}
On the HTML page, the class name is associated with one or more target element or content
<p class="product-text">This is some product text.</p>
<p>This is another paragraph of text.</p>
<h4 class="product-text">This is headline text.</h4>
When we want multiple elements to share a style, we give them the same class name.
The CSS code below selects all elements with the class
of .product-text
in the
markup and applies the style
.product-text {
color: purple;
}
<p class="product-text">This is some product text.</p>
<p>This is another paragraph of text.</p>
<h4 class="product-text">This is headline text.</h4>
Elements can have more than one class
name or selector applied to them.
.product-text {
color: purple;
}
.product-container {
background-color: yellow;
}
<p class="product-text product-container">Some product detail.</p>
style.css
, create a CSS rule that adds the color red
to that class name ID
selectors are prefixed with a "#"
symbol, then followed by a name
#product-name {
background: tomato;
}
Unlike the class selector, the ID selector is associated with only ONE target element on the HTML page
<h2 id="product-name">First Product Title</h2>
<h2 id="needs-a-new-id">Second Product Title</h2>
IDs cannot be shared among elements
An HTML element can have both a class name and an ID
<h2 class="product-text" id="product-name">First Product Title</h2>
style.css
, create a CSS rule that adds font-size: 24px;
to that ID
Properly naming selectors is important.
Semantic CSS naming conveys meaning.
<h2 class="product-heading">My First Start Up App</h2>
Non-semantic CSS tends not to convey meaning; it usually conveys what an element might look like.
<h2 class="large-text">My First Start Up App</h2>
Non-semantic CSS naming is common in CSS frameworks such as Bootstrap and Tailwind.
<div class="row mb-10 h-4">My First Start Up App</div>
A few methodologies to research and explore in your web design or web development journey:
Dev.to Article: CSS Naming conventions
Commenting our code also works in CSS.
/* Sample comment */
p {
color: purple;
}
CSS properties allow us to apply visual design, layout and even animation to our HTML code.
We can apply color, play with text appearance, add fonts, work with images, create unique layouts, and more.
Although the majority of HTML elements and CSS properties are standard and work across browsers, this is not always the case for a variety of reasons.
caniuse.com is a popular site/tool used to check for browser support for HTML and CSS features.
What CSS rules apply the following styles?
Browsers can accept colors in different ways:
Color name: | red |
Hexadecimal value: | #FF0000 |
RGB value: | rgb(255, 0, 0) |
HSL value | hsl(0, 100%, 50%) |
Web Safe Fonts are font families that all browsers can display by default. We don't have to load font files or link to external font libraries to use them in our projects.
p {
font-family: "Times New Roman"; /* Specific font name */
font-family: serif; /* Generic name */
font-family: "Arial", sans-serif; /* Comma-separated list */
}
To apply custom fonts, we either load font files into our project directory or we link to external font libraries such as Google Fonts.
class
namesIDs
if you're creating bookmarks