Introduction
CSS (Cascading Style Sheets) is used to style and lay out web pages — for example, to alter the font, color, size, and spacing of your content, split it into multiple columns, or add animations and other decorative features. This module provides a gentle beginning to your path towards CSS mastery with the basics of how it works, what the syntax looks like, and how you can start using it to add styling to HTML.
Prerequisites
This guide assumes you have the following basic background:
- Basic familiarity with using computers and using the Web passively (i.e. looking at it, consuming the content.)
- A basic work environment set up (text editor, web browser), and an understanding of how to create and manage files.
- Basic familiarity with HTML.
What is CSS
CSS is a language for specifying how documents are presented to users — how they are styled, laid out, etc. Using CSS, you can control exactly how HTML elements look in the browser, presenting your markup using whatever design you like.
A document is usually a text file structured using a markup language — HTML is the most common markup language, but you may also come across other markup languages such as SVG or XML.
Presenting a document to a user means converting it into a form usable by your audience. Browsers, like Firefox, Chrome, or Edge, are designed to present documents visually, for example, on a computer screen, projector, or printer.
CSS can be used for very basic document text styling — for example, for changing the color and size of headings and links. It can be used to create a layout — for example, turning a single column of text into a layout with a main content area and a sidebar for related information. It can even be used for effects such as animation.
CSS syntax
CSS is a rule-based language — you define the rules by specifying groups of styles that should be applied to particular elements or groups of elements on your web page.
For example, you can decide to have the main heading on your page to be shown as large red text. The following code shows a very simple CSS rule that would achieve the styling described above:
h1 {
color: red;
font-size: 5em;
}
- In the above example, the CSS rule opens with a selector. This selects the HTML element that we are going to style. In this case, we are styling level one headings (
<h1>). - We then have a set of curly braces
{ }. - Inside the braces will be one or more declarations, which take the form of property and value pairs. We specify the property (
colorin the above example) before the colon, and we specify the value of the property after the colon (redin this example). - This example contains two declarations, one for
colorand the other forfont-size. Each pair specifies a property of the element(s) we are selecting (<h1>in this case), then a value that we'd like to give the property.
A CSS stylesheet will contain many such rules, written one after the other.
h1 {
color: red;
font-size: 5em;
}
p {
color: black;
}
Reference
All the documentation in this page is taken from MDN.