Read a CSS rule and predict which part of an HTML page will change.
CSS, or Cascading Style Sheets, describes the presentation of an HTML document. A heading stays a heading when you change its size or color. The HTML names the content, and CSS tells the browser how to present it.
body {
background-color: lightblue;
}Read from the outside in. The body selector chooses the body of the page. The braces hold a declaration, background-color: lightblue;. Open a page with this rule and an otherwise unstyled, transparent root: the light blue fills the page canvas.
HTML gives CSS something to select. Here are the tags for two headings, a paragraph, a link, and an image:
<h1>Main Heading</h1>
<h2>Subheading</h2>
<p>This is a paragraph.</p>
<a href="https://example.com">Visit Example</a>
<img src="image.jpg" alt="Sample Image">An element can contain other elements. In the HTML tab, the heading and paragraph are both children of the same div. The class attribute gives that parent a name CSS can match.
.container {
color: blue;
}The rule matches the parent. Its children also have blue text because color is inherited when the child has no text color of its own. When you read the paired tabs in this course, style.css holds the rules and index.html supplies the elements. The link in the document head connects the two files.
A class can identify several elements, even if they use different tags. An ID identifies one element in the document. Predict which text will have a yellow background and which will be bold.
.highlight {
background-color: yellow;
}
#main {
font-weight: bold;
}The dot in .highlight matches the class on both the first div and the paragraph. The hash in #main matches the ID on the last division. Only that div gets bold text.
This pattern shows the three parts you will read in a rule. The names here stand for the selector, property, and value that a real rule needs.
selector {
property: value;
}h1 {
color: blue;
font-size: 20px;
}Here h1 selects the top-level headings. The first declaration sets their text color. The second sets their font size to 20 CSS pixels.
.button {
background-color: green;
color: white;
border-radius: 5px;
padding: 10px;
}Look at padding: 10px;. It puts ten pixels of space between the button’s content and each inside edge. The green background extends through that space. All four declarations belong to the same class selector.
A comment begins with /* and ends with */. The browser skips the text between them, even when it spans several lines.
/* Style a class */
.highlight {
background-color: yellow;
}
/*
This is a longer comment
that spans multiple lines.
*/The comment labels the rule. The yellow background comes from the declaration inside the braces.
Read the first rule and predict whether the heading will turn red.
h1 {
color: red
font-size: 16px;
}A line break does not end a declaration. The browser reads the next line as part of the color value and discards that invalid declaration. Restoring the separator lets both changes take effect.
h1 {
color: red;
font-size: 16px;
}Try it yourself
HTML + CSS