Welcome to the fundamental guide on Cascading Style Sheets (CSS)! This is where the magic of visual design for your web pages happens. Without CSS, websites would be plain text documents. CSS allows us to control layout, colors, fonts, and virtually every other visual aspect of your content.
CSS is a stylesheet language used for describing the presentation of a document written in a markup language like HTML. It dictates how HTML elements should be rendered on screen, on paper, in speech, or on other media. CSS is separated from HTML, making your code cleaner and easier to maintain.
At its heart, CSS works by selecting HTML elements and applying styles to them. The basic structure of a CSS rule is a selector followed by a declaration block, which contains one or more declarations. Each declaration consists of a CSS property and its value.
Selectors target the HTML elements you want to style. Some common types include:
p for paragraphs, h1 for headings)..highlight for elements with class="highlight").#main-title for the element with id="main-title").Properties are the specific stylistic attributes you want to change, and values are the settings you assign to them.
selector {
property: value;
another-property: another-value;
}
Let's apply some styles to a paragraph and a heading.
h1 {
color: #3498db; /* A nice shade of blue */
font-size: 2.5em; /* Larger font size */
text-align: center; /* Center the text */
}
p {
color: #555; /* Dark grey for readability */
font-size: 1.1em; /* Slightly larger than default */
line-height: 1.8; /* More spacing between lines */
}
Imagine the h1 above gets styled with a vibrant blue and a large font size, centered on the page. The paragraphs will adopt a more readable dark grey with increased line spacing.
Every HTML element can be thought of as a box. The CSS box model describes how an element's content, padding, border, and margin interact to define its size and spacing on the page.
By combining selectors, properties, and values, you can create sophisticated layouts and unique visual styles. Don't be afraid to experiment!
Feeling inspired? Perhaps you're interested in learning about color theory for web design.