CSS - Overview
Stylesheets
Let's create a stylesheet for our Portfolio Project that applies to all of our pages!
First, we need to create a separate file with the .css file extension. Let's go ahead and call it style.css. To keep things organized, let's create that file inside its own css directory (since we might want to add multiple stylesheets to the same document). HINT:
mkdir css
touch style.cssNext, we need to link that stylesheet to each of our pages. In the <head> of each page, add the following:
<!-- for your landing page -->
<link rel="stylesheet" href="css/style.css">
<!-- for all other pages -->
<link rel="stylesheet" href="../css/style.css">Do you know why we need different href values for our landing page and our projects and blog pages?
At this point, there shouldn't be any difference in the way your pages look or behave, since there's nothing in style.css. For now, you should stage, commit, push, and deploy your page to make sure that everything looks the same in the browser.
Concepts
The CSS Box Model
Every HTML element has a box around it, even if you cannot see it. We can show a visible border around every HTML element on the page using the CSS border property and the "match all" selector:
Try putting a border around each "boxed" element while you try out a few of the following properties:
The CSS padding property defines the extra space inside the border:
You can also assign different values to the
paddingon each side of an element with specific properties (e.g.padding-left,padding-right, etc.) or through the shorthandpaddingproperty. As an example, the following threepaddingconstructs compile to the same appearance:The CSS margin property defines the extra space outside the border:
The same shorthand rules that worked for
paddingalso work formargin.We can also manually set the width and height of the element itself.
What's the difference? Check it out in the Elements panel of Chrome Dev Tools to inspect the spacing around the element.
NOTE: you can open the Elements panel with
CMD + Option + Iin macOS or withCTRL + Iin Linux or Windows
Complex CSS Selectors & Combinators
As our documents grow, we'll need to leverage more complex CSS selection syntax. Here are a few important selectors to cherish and to keep:
The Universal Selector
The Direct Decendant Selector
The Descendant Selector
In the HTML shown in 2 👆🏾, this selector would grab
<p>regardless of whether it was a direct child or not - as long as it's a child.The Direct Sibling Selector
The General Sibling Selector
In the HTML code 👆🏾, ~ will select the <footer> b/c it is a sibling - doesn't matter if it's adjacent or not.
These rules might seem like overkill right now, but they're extremely useful for when you have large stylesheets of CSS rules that apply across multiple pages. Speaking of which...
Pseudo Selectors
There are some selectors that can be used to make some very basic calculations about the state of your markup, and add styles accordingly. These aren't real selectors, since they aren't able to select anything on their own. Rather, these are used to filter the results of a selector into a useful subset of HTML elements to style. Here are a few of the most useful:
:first-of-type:last-of-type:nth-of-type(n):nth-of-type(an):hover
Last updated