CSS
- CSS is used to modify the style (appearance) of elements on the page.
- A CSS file, also called a stylesheet, is a plain text file ending in
.css. - Like HTML files, stylesheets can be edited in any plain text editor or in an IDE.
- CSS stands for Cascading Style Sheets, which refers to the order in which styles are applied to elements and is outside the scope of this brief intro.
- CSS consists of comments, rules, selectors, and media queries.
Adding styles to a web page
- The
styleattribute can be used to modify the style of a single element, like so:<div style="color: red;">Warning!</div> - Excessive use of the
styleattribute can make your HTML difficult to read and modify. For this reason, it's better to use a stylesheet. - Stylesheets must be added to the
headelement and can be inline or external. - An inline stylesheet is added with the
styleelement:The<head> <meta charset="utf-8" /> <title>My Notes</title> <style> ... </style> </head>...between thestyletags is just a placeholder. On a real web page, it would be replaced with CSS code. - An external stylesheet is added with the
linkelement:In order for this example to work, the<head> <meta charset="utf-8" /> <title>My Notes</title> <link href="style.css" rel="stylesheet" /> </head>style.cssfile must be located in the same directory as the HTML file that references it.
Comments
- Like in HTML, comments in CSS are intended for developers and are ignored by the browser.
- Here's an example of a single-line comment:
/* This is a comment */ - Here's an example of a multi-line comment:
/* The greatest danger facing us is ourselves, and irrational fear of the unknown. There is no such thing as the unknown. Only things temporarily hidden, temporarily not understood. */
Rules
- A rule looks like this:
color: red; - In the example above, the
colorproperty is assigned a value ofred. - Other properties include:
background-colorborder-colorborder-widthfont-familyfont-sizemarginpadding
- While not strictly required, it's recommended to end every rule with a semi-colon in order to reduce bugs.
- For a complete list of all available properties and documentation, including examples, see CSS reference.
Selectors
- A selector looks like this:
body > div:first-child - The example above would match the first
divelement that is a child of thebodyelement. - Selectors group one or more rules and apply them to all elements that match, like this:
In this example, the selector would match alla { color: red; text-decoration: none; }aelements, modifying theircolorandtext-decorationproperties. - Selectors can be extremely broad or specific. Here are a few examples:
- All elements on the page:
* - All
divelements:div - All
divelements with atitleattribute:div[title] - All
divelements with atitleattribute of Products:div[title=Products] - All
divelements that are descendants of thebodyelement:body div - All
divelements that are children of thebodyelement:body > div - The first
divelement that is a child of thebodyelement:body > div:first-child - All elements with a
classattribute ofproduct:.product - The element with an
idattribute ofproducts:#products
- All elements on the page:
- See CSS selectors and combinators for more info.
Pseudo-classes
- When added to a selector, a pseudo-class selects elements based on their state. Here are some examples:
- The
aelement that is under the mouse pointer:a:hover- Keep in mind this doesn't work on devices without a mouse, including most phones and tablets.
- The
aelement that is selected by clicking, tapping, or pressing the Tab key (until another element is selected):a:focus - The
aelement that is activated by clicking or tapping (until the mouse button or the user's finger is released):a:active
- The
- See Pseudo-classes for more info.
Media queries
- A media query allows us to apply styles only on certain device types or only when certain device features are present.
- In this example, the rules are only applied when the page is printed (a device type):
@media print { body { background-color: white; color: black; } } - In this example, all
spanelements that are children of aheaderelement would be displayed vertically (stacked) on screens smaller than 800 pixels wide (a device feature):@media (max-width: 800px) { header > span { display: block; } } - In this example, all
divelements that are children of afooterelement would be displayed horizontally (side-by-side) on screens larger than 800 pixels wide (a device feature):@media (min-width: 800px) { footer > div { display: inline; } } - See Using media queries for more info