- Getting started with CSS
- Starting with some HTML
- Adding CSS to our document
- Styling HTML elements
- Changing the default behavior of elements
- Adding a class
- Styling things based on their location in a document
- Styling things based on state
- Combining selectors and combinators
- Summary
- Found a content problem with this page?
- CSS Selectors
- CSS Selectors
- The CSS element Selector
- Example
- The CSS id Selector
- Example
- The CSS class Selector
- Example
- Example
- Example
- The CSS Universal Selector
- Example
- The CSS Grouping Selector
- Example
- All CSS Simple Selectors
Getting started with CSS
In this article, we will take a simple HTML document and apply CSS to it, learning some practical things about the language along the way.
Prerequisites: | Basic computer literacy, basic software installed, basic knowledge of working with files, and HTML basics (study Introduction to HTML.) |
---|---|
Objective: | To understand the basics of linking a CSS document to an HTML file, and be able to do simple text formatting with CSS. |
Starting with some HTML
Our starting point is an HTML document. You can copy the code from below if you want to work on your own computer. Save the code below as index.html in a folder on your machine.
doctype html> html lang="en"> head> meta charset="utf-8" /> title>Getting started with CSStitle> head> body> h1>I am a level one headingh1> p> This is a paragraph of text. In the text is a span>span elementspan> and also a a href="https://example.com">linka>. p> p> This is the second paragraph. It contains an em>emphasizedem> element. p> ul> li>Item span>onespan>li> li>Item twoli> li>Item em>threeem>li> ul> body> html>
Note: If you are reading this on a device or an environment where you can’t easily create files, then don’t worry — live code editors are provided below to allow you to write example code right here in the page.
Adding CSS to our document
The very first thing we need to do is to tell the HTML document that we have some CSS rules we want it to use. There are three different ways to apply CSS to an HTML document that you’ll commonly come across, however, for now, we will look at the most usual and useful way of doing so — linking CSS from the head of your document.
Create a file in the same folder as your HTML document and save it as styles.css . The .css extension shows that this is a CSS file.
To link styles.css to index.html , add the following line somewhere inside the of the HTML document:
link rel="stylesheet" href="styles.css" />
Save your HTML and CSS files and reload the page in a web browser. The level one heading at the top of the document should now be red. If that happens, congratulations — you have successfully applied some CSS to an HTML document. If that doesn’t happen, carefully check that you’ve typed everything correctly.
You can continue to work in styles.css locally, or you can use our interactive editor below to continue with this tutorial. The interactive editor acts as if the CSS in the first panel is linked to the HTML document, just as we have with our document above.
Styling HTML elements
By making our heading red, we have already demonstrated that we can target and style an HTML element. We do this by targeting an element selector — this is a selector that directly matches an HTML element name. To target all paragraphs in the document, you would use the selector p . To turn all paragraphs green, you would use:
You can target multiple selectors at the same time by separating the selectors with a comma. If you want all paragraphs and all list items to be green, your rule would look like this:
Try this out in the interactive editor below (edit the code boxes) or in your local CSS document.
Changing the default behavior of elements
When we look at a well-marked up HTML document, even something as simple as our example, we can see how the browser is making the HTML readable by adding some default styling. Headings are large and bold and our list has bullets. This happens because browsers have internal stylesheets containing default styles, which they apply to all pages by default; without them all of the text would run together in a clump and we would have to style everything from scratch. All modern browsers display HTML content by default in pretty much the same way.
- , an unordered list. It has list bullets. If you don’t want those bullets, you can remove them like so:
Try adding this to your CSS now.
The list-style-type property is a good property to look at on MDN to see which values are supported. Take a look at the page for list-style-type and you will find an interactive example at the top of the page to try some different values in, then all allowable values are detailed further down the page.
Looking at that page you will discover that in addition to removing the list bullets, you can change them — try changing them to square bullets by using a value of square .
Adding a class
So far, we have styled elements based on their HTML element names. This works as long as you want all of the elements of that type in your document to look the same. To select a subset of the elements without changing the others, you can add a class to your HTML element and target that class in your CSS.
- In your HTML document, add a class attribute to the second list item. Your list will now look like this:
ul> li>Item oneli> li class="special">Item twoli> li>Item em>threeem>li> ul>
.special color: orange; font-weight: bold; >
You can apply the class of special to any element on your page that you want to have the same look as this list item. For example, you might want the in the paragraph to also be orange and bold. Try adding a class of special to it, then reload your page and see what happens.
Sometimes you will see rules with a selector that lists the HTML element selector along with the class:
li.special color: orange; font-weight: bold; >
This syntax means «target any li element that has a class of special». If you were to do this, then you would no longer be able to apply the class to a or another element by adding the class to it; you would have to add that element to the list of selectors:
li.special, span.special color: orange; font-weight: bold; >
As you can imagine, some classes might be applied to many elements and you don’t want to have to keep editing your CSS every time something new needs to take on that style. Therefore, it is sometimes best to bypass the element and refer to the class, unless you know that you want to create some special rules for one element alone, and perhaps want to make sure they are not applied to other things.
Styling things based on their location in a document
Add the following rule to your stylesheet:
li em color: rebeccapurple; >
Something else you might like to try is styling a paragraph when it comes directly after a heading at the same hierarchy level in the HTML. To do so, place a + (an adjacent sibling combinator) between the selectors.
Try adding this rule to your stylesheet as well:
The live example below includes the two rules above. Try adding a rule to make a span red if it is inside a paragraph. You will know if you have it right because the span in the first paragraph will be red, but the one in the first list item will not change color.
Note: As you can see, CSS gives us several ways to target elements, and we’ve only scratched the surface so far! We will be taking a proper look at all of these selectors and many more in our Selectors articles later on in the course.
Styling things based on state
a:link color: pink; > a:visited color: green; >
You can change the way the link looks when the user hovers over it, for example by removing the underline, which is achieved by the next rule:
a:hover text-decoration: none; >
In the live example below, you can play with different values for the various states of a link. We have added the rules above to it, and now realize that the pink color is quite light and hard to read — why not change that to a better color? Can you make the links bold?
We have removed the underline on our link on hover. You could remove the underline from all states of a link. It is worth remembering however that in a real site, you want to ensure that visitors know that a link is a link. Leaving the underline in place can be an important clue for people to realize that some text inside a paragraph can be clicked on — this is the behavior they are used to. As with everything in CSS, there is the potential to make the document less accessible with your changes — we will aim to highlight potential pitfalls in appropriate places.
Note: you will often see mention of accessibility in these lessons and across MDN. When we talk about accessibility we are referring to the requirement for our webpages to be understandable and usable by everyone.
Your visitor may well be on a computer with a mouse or trackpad, or a phone with a touchscreen. Or they might be using a screen reader, which reads out the content of the document, or they may need to use much larger text, or be navigating the site using the keyboard only.
A plain HTML document is generally accessible to everyone — as you start to style that document it is important that you don’t make it less accessible.
Combining selectors and combinators
It is worth noting that you can combine multiple selectors and combinators together. For example:
- , which comes directly after an */h1 + ul + p>
You can combine multiple types together, too. Try adding the following into your code:
body h1 + p .special color: yellow; background-color: black; padding: 5px; >
This will style any element with a class of special , which is inside a
, which comes just after an , which is inside a . Phew!
In the original HTML we provided, the only element styled is .
Don’t worry if this seems complicated at the moment — you’ll soon start to get the hang of it as you write more CSS.
Summary
In this article, we have taken a look at a number of ways in which you can style a document using CSS. We will be developing this knowledge as we move through the rest of the lessons. However, you now already know enough to style text, apply CSS based on different ways of targeting elements in the document, and look up properties and values in the MDN documentation.
In the next lesson, we’ll be taking a look at how CSS is structured.
Found a content problem with this page?
This page was last modified on Jul 7, 2023 by MDN contributors.
CSS Selectors
A CSS selector selects the HTML element(s) you want to style.
CSS Selectors
CSS selectors are used to «find» (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
- Simple selectors (select elements based on name, id, class)
- Combinator selectors (select elements based on a specific relationship between them)
- Pseudo-class selectors (select elements based on a certain state)
- Pseudo-elements selectors (select and style a part of an element)
- Attribute selectors (select elements based on an attribute or attribute value)
This page will explain the most basic CSS selectors.
The CSS element Selector
The element selector selects HTML elements based on the element name.
Example
Here, all
elements on the page will be center-aligned, with a red text color:
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Example
The CSS rule below will be applied to the HTML element with
Note: An id name cannot start with a number!
The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with will be red and center-aligned:
You can also specify that only specific HTML elements should be affected by a class.
Example
In this example only
elements with will be red and center-aligned:
HTML elements can also refer to more than one class.
Example
In this example the
element will be styled according to and to
This paragraph refers to two classes.
Note: A class name cannot start with a number!
The CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
h2 text-align: center;
color: red;
>
p text-align: center;
color: red;
>
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example
In this example we have grouped the selectors from the code above:
All CSS Simple Selectors
Selector | Example | Example description |
---|---|---|
#id | #firstname | Selects the element with > |
.class | .intro | Selects all elements with > |
element.class | p.intro | Selects only elements with > |
* | * | Selects all elements |
element | p | Selects all elements |
element,element. | div, p | Selects all elements and all elements |