CSS (Cascading Style Sheets) is the next language you should learn after HTML. Where HTML gives your page structure, CSS provides the layout and style. Cascading Style Sheets give you the ability to alter the font, control the spacing, all the way up to adding animation.
This documentation is intended to be a short, beginner friendly introduction to CSS. Moving forawrd, we will cover some pre-requisites, how to apply your CSS, different selectors, properties and values, and some organizational tips.
Prior to venturing into CSS you should have:
- Basic experience with computers and using the internet
- A basic knowledge of file creation and management, as well as a basic work environment set up
- Basic experience with HTML
CSS (Cascading Style Sheets) is a rule based language that is defined by specifying groups of styles that get applied to specific elements or even groups of elements on your web page. CSS gives you the ability to make amazing looking web pages by specifying how a document is layed out and styled.
There are three ways you can add CSS you your document, the most common and practical is to link an external stylesheet. Next you can apply it in a style section, in the head section of the document itself. Lastly we have inline styles, which is done by adding a style attribute to an individual HTML element, this is NOT considered best practice.
-
External StyleSheets
Let's start with applying CSS with external stylesheets. You will need to add it in the head
(<head></head>)
section. First you will need to add the link tag with a relationship and location in the rel and href properties respectively.It should look like this:
<head> <link rel="stylessheet" href="styles.css" /> </head>
Remember, it must be placed inside the head section. To actually see some style take effect, let's add something to your CSS. Inside your styles.css file, add the following code:
body { background-color: yellow; }
Now, save both files and reload your page, the background should be yellow. If it is not, simply make sure everything is typed correctly and that the CSS file is linked correctly inside the head section.
-
Style Section
Next we will look at adding your styles to a style setion in the head section of the document, also called internal stylesheets.
This will look somethin like this:
<head> <style> body { background-color: yellow; } h1 { color: purple; } </style> </head>
There are times when using an internal stylesheet can be useful, maybe you just need a quick jist of some style to use while working on your project before doing the main stylesheet. There may also be situations where you are blocked from modifying the external stylesheets, like when working with content management systems.
The downside to internal stylesheets is you would need one for every page. For bigger sites, this could quickly become inconvenient for maintenance amd updating.
-
Inline Styles
The last way we will look at add CSS to a project is using inline styles. Inline styles are typically applied with a style attribute, and only affect the HTML element applied to.
Using inline styles will look something like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>My CSS Tech Doc</title> </head> <body> <h1 style="color: white; background-color: charcoal;">Inline Styles</h1> <p style="color: blue;">This is how you apply inline styles</p> </body> </html>
As you can see this gets messy quickly and is NOT considered best practice. You may see this used if you have a very strict work environment, like a content management system that only allows editing of the HTML body. May also be used in emails to achieve compatibility with more clients.
The first part of a CSS rule is the CSS selector. Selectors will tell the browser which HTML element to apply the CSS rule to. There are several different selectors, they are; type, class, ID, attribute, pseudo-class, pseudo-element, and combinators.
-
Type, Class, and ID Selectors
First we will discuss type, class, and ID selectors as these are the most common.
-
Type Selectors
A type selector directly targets an HTML element and will look something like this:
This will target anyh1 { color: blue; }
<h1>
elements within your stylesheets scope. -
Class Selectors
A class selector is case sensitive and will target any HTML element that has the same value for its class attribute. Using a class a class would like like this:
This will target both theHTML: <h1 class="example">Class</h1> <p class="example">Selector</p> CSS: .example { color: purple; background-color; yellow; }
<h1>
and<p>
elements. -
ID Selectors
An ID selector is also case sensitive, but it is a bit special. Each ID has to be unique, meaning two elements can not have the same ID attribute value. Using an ID will look like such:
This will target only theHTML: <h1 id="headline">ID Selector</h1> CSS: #headline { color: red; }
<h1>
element with the id attrivute value of "headline". Note this over rules any type and class selector rules applied.
-
-
Attribute Selectors
Next we have attribute selectors, these target HTML elements with certain attributes. Among atribute selectors, there are presence and value selectors and there are substring matching selectors.
-
Presence and Value Selectors
The presence and value selectors target elements based on the presence of an attribute alone, or matches several elements based on the value of the attribute. Here are some examples of presence and value selectors:
HTML: <h2>Presence and Value Selectors</h2> <p>Item 1</p> <p class="a">Item 2</p> <p class="a b">Item 3</p> <p class="ab">Item 4</p> CSS: p[class] { font-size: 200%; } p[class="a"] { background-color: cyan; } p[class~="a"] { color: charcoal; }
In the example above, the first
<p>
element will not be selected as it has no class attribute. The second<p>
element will have all effects added because it has a class, its class value is "a", and because the third rule applies to elements with a class of "a" but also searches for list rules, separated by white space. The third<p>
element will have the first and third effect applied because it has a class, and it has a listed class value of "a" with white-space, but the second effect will not apply since it will only apply to a class value of "a". Lastly, the fourth<p>
element will only have the the first effect since it has a class but the second rule is only searching for class "a" values and the third rule is looking for listed classes with "a" and whit-space, which "ab" does not have. -
Substring Selectors
Substring selectors allow for more precise matching of substrings inside the value of your attribute. Here are a few examples of substring selectors:
HTML: <h1>Substring Selectors</h1> <p class="a">Item 1</p> <p class="ab">Item 2</p> <p class="bca">Item 3</p> <p class="bcabc">Item 4</p> CSS: p[class^="a"] { font-size: 200%; } p[class$="a"] { background-color: cyan; } p[class*="a"] { color: charcoal; }
In the example above, our first css rule
p[class^="a"]
will apply to any attribute that starts with "a", so this will apply to the first two<p>
elements. The second rulep[class$="a"]
will apply to any attribute that ends with "a", so this will apply to the first and third<p>
elements. Lastly, the third rulep[class*="a"]
applies to any attribute where "a" appears in the string, so this rule will apply to all<p>
elements.
-
-
Pseudo-Class and Pseudo-Element Selectors
The next type of selectors we will look at are called pseudo-classes and pseudo-elements. There are many of these, and they serve specific purposes.
-
Pseudo-Classes
Pseudo-classes select elements in a specific state. Pseudo-classes are identified by keywords beginning with a colon, and they act as if you applied a class to part of your document. Here are some examples of some pseudo-classes:
HTML: <article> <p>first child</p> <p><a href="">Hover Here</a></p> <p>last child</p> </article> CSS: article p:first-child { font-size: 120%; } article p:last-child { font-weight: bold; } a:hover { color: green; }
In the example above, our first CSS rule
article p:first-child { font-size: 120%; }
will make the first<p>
element slightly bigger. Now if we add a new<p>
before it, the new<p>
element will become the effected element because it is now the first-child element. Likewise our second rulearticle p:last-child { font-weight: bold; }
only applies to the last<p>
element inside<article>
and will apply to a new<p>
element if it is applied after the current last<p>
element. Lastly the<a>
element will only change to green when hovered over due to the:hover
class in our last rule,a:hover { color: green; }
. -
Pseudo-Elements
Pseudo-elements start with a double colon, and act as if you added a new element into the document. One example of this would be:
HTML: <article> <p>You could try this with a <span>element wrapped around the presumed first line.</p> <p>However we don't always know how many words will fit on a line, especially on different screens</p> </article> CSS: article p::first-line { font-size: 125%; font-weight: bold; }
This approach will reliably only apply to the first line of each paragraph tag. If you wished to only affect the first line of the first
<p>
element, we could combine selectors, like such:article p:first-child::first-line { font-size: 125%; font-weight: bold; }
There are many pseudo-classes and pseudo-elements, have a look and see what you can do with them.
-
-
Combinators
The last selector type we will look at are combinators. These allow us to select elements based on location of other elements (i.e. child or sibling). For these we have descendant combinator, child combinator, next-sibling combinator, and subsequent-sibling combinator.
-
Descendant Combinator
The descendant combinator is represented by a single space between two selectors. Using a descendant combinator will apply the CSS rule to any element matching the second selectors that are inside elements matching the first selector. This would look something like this:
HTML: <p>I'm not selected</p> <article> <p>But I am</p> </article> <p>I'm not either</p> CSS: article p { color: blue; }
In the exmple above, the first and third
<p>
elements will not be selected because they're not inside the<article>
element, however the second<p>
element will be. -
Child Combinator
The child combinator act similar, but more strict and is used by placing
>
betweem two selectors. This will match elements to the second selector, that are direct children of the first selector. Her is how this might look:HTML: <ol> <li>Item 1</li> <li>Item 2</li> <li> <ul> <li>Unordered Item 1</li> <li>Unordered Item 2</li> </ul> </li> </ol> CSS: ol > li { color: purple; }
In this example, only Item 1 and Item 2 will have purple text, because they are direct children of the
<ol>
element, whereas the Unordered Items are further descendants and will not be affected. -
Next-Sibling Combinator
The next-sibling combinator uses
+
between two selectors and it matches elements matching the second selector that immediately follow an element matching the first selector. An example of this might look like this:HTML: <h1>In this example</h1> <p>This element gets affected</p> <h1>But if you notice</h1> <h2>This element</h2> <p>does not</p> <h1>However as you can see</h1> <p>this element is affected as well</p> CSS: h1 + p { font-weight: bold; background-color: charcoal; color: cyan; }
As you will see, only the first and third
<p>
elements will be affected, that is because the<h2>
element is what immediately follows the secode<h1>
element. -
Subsequent-Sibling Combinator
The last combinator we will look at is the subsequent-sibling combinator. The subsequent-sibling combinator uses
~
between two selectors, and will match all elements of the second selector coming anywhere after the first selector. An example of this might look like this:HTML: <p>Unaffected element</p> <h2>First target selector</h2> <p>First affected element</p> <br> <p>Second affected element</p> CSS: h2 ~ p { font-size: 85%; background-color: charcoal; color: cyan; }
As you can see, the first
<p>
element is not affected because it is before the<h2>
element. However the second and third<p>
elements are affected because they come after the<h2>
element, regardless of the<br>
element.
This is just a brief overview of the use of selectors, for a more in-depth dive on how to use and/or combine selectors click here. For a full list of selectors, click here.
-
All of CSS works by matching declarations, which are property and value pairs, to their intended element of a page. Properties have certain value types they are allowed, some can use multiple types. First we will go over some common value types and units like numeric, color, image, and position. Then we will look at some commonly used CSS properties in which you would see such values.
-
Numeric Types
First we will look at numeric types.
Data Type Description <integer>
A whole number like 2025
or-99
<number>
A decimal number, may or may not have a fractional component like 0.33
,107
, or-7.5
<dimension>
A <number>
with a unit. this is an umbrella category that includes<length>
,<angle>
,<time>
, and<resolution>
. Such as90deg
,10s
, or25px
.<percentage>
Represents a fraction relative to another quality. For example, an elements height would be relative to its parent elements height. -
Color
Color is really only one value type, but there are several options on how to input this value. We will go over a few common ways like using keywords, hexidecimal codes, and
rgb()
values.-
Keywords
The first option is the
<named-color>
value, however this option is not typically used in production applications. Using<named-color>
values is typically used in courses amd tutorials so the learner can focus on coding and knows what color should appear. -
Hexidecimal Values
The next option is hexidecimal RGB values, using a combination of 16 characters from
0
-9
anda
-f
, making the entire range0123456789abdcef
. Every hex color value consists of a hash/pound symbol (#
) and 6 hexidecimal characters, for example#ffc0cb
. These hexidecimal characters are in pairs, representing the different RGB channels of color; Red, Green, Blue. -
The
rgb()
FunctionAnother common option is the
rgb()
function, which takes three parameter values for red, green, and blue, with an optional fourth value for opacity when separated by a slash (/
). Each parameter can be in a range from0
-255
or0%
-100%
but not a combination of the two, for example.
There are several other ways to apply color, if you want to look at more options, click here.
-
-
Image
Another value type is
<image>
s, this can be aurl()
function pointing to an image or a gradient. There are some newer<image>
values, but they currently have poor browser support. -
Position
The
<position>
value type is used to set an item to a set of 2D coordinates. A<position>
value typically consists of two values, the first setting it horizontally and the second vertically. These values can be<length>
s, which represent the offset from the left and top edges of the box, or keywords, which are<center>
,<top>
,<left>
,<bottom>
, and<right>
. If only one axis is specified, the other will default to center. -
Properties
These are your basic value types, and from here we can look at some common properties we would apply these. If you want to look at some of the more advanced value types, click here.
-
Numeric Types
Numeric types have a wide use range, from describing length (i.e.
<width>
,<heigth>
,<margin>
, ect) to defining the amount of color in<rgb()>
color function. It is important to note that when using percentages, that element will be refering to its parent element. -
Color Types
Color types are used to change font
<color>
or<background-color>
,<box-shadow>
and gradient options -
Image Types
Image types can be used to simply add a picture or apply to the background of an element with
<background-image>
. -
Position Types
the position type is used to specify where you want an element for more precise styling
-
There is no right or wrong way to organize your CSS, though inline styles is considered not best practice, the biggest key is consistency. Having a consistent set of rules will help save time and mental overhead, thus deciding on indentation, naming convention, and file structure beforehand can help. For structure, it cam be helpful to comment your code so you can easily see what each section of styles applies to. Also it wouldn't hurt to structure styles with all of your default styles first, then utilities (action styles), then page specific (market page vs settings page), and then item or componjent specific if needed. With this you should have a basic understanding of how to start styling and structuring your page, go experiment with your own code and see what you can do.