CSS basics
Computer Science · Web development
Question 1 of 10
0CSS styles:
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
CSS basics explained
CSS, which stands for Cascading Style Sheets, controls how a web page looks. It sets colours, fonts, spacing, sizes and layout. HTML supplies the content and structure, while CSS is kept separate, so one stylesheet can style many pages.
A CSS rule has a selector, which chooses the elements to style, and a block of declarations in curly braces. Each declaration is a property and a value, separated by a colon and ended with a semicolon. Selectors can match elements by tag name, such as p, by class, written with a full stop such as .highlight, or by id, written with a hash such as #header. When several rules apply to the same element, the cascade decides which one wins, using factors such as specificity and the order of the rules in the file.
A common mistake is to write a hash for a class or a full stop for an id, so the rule matches nothing. Another is to leave out a semicolon, which can cause the next declaration to be ignored.
Worked example
Consider these two rules:
h1 {
color: navy;
}
.highlight {
background-color: yellow;
}
- The first rule selects every h1 heading and sets its text colour to navy.
- The second rule selects any element with the class highlight and gives it a yellow background.
- The paragraph written as p with class highlight matches the second rule, so it gets a yellow background.
What you will practise
105 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.
Example questions
- CSS styles:
- A selector:
- Properties like color:
- The cascade determines:
- Media queries:
- In CSS, a class selector is written with which symbol before the name?
- An ID selector is written with which symbol?
- The CSS box model, from the innermost layer outwards, is:
…and 97 more.