CSS (Cascading Style Sheets)

Introduction
CSS stands for Cascading Style Sheets. It is used to describe how HTML elements are to be displayed on screen,paper, or in other media. It saves a lot of work. It ca control the layout of multiple web pages all at once. External style sheets are stored in CSS files. It is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.
CSS Syntax
A CSS rule-set consists of a selector and a declaration block
h1 {color:aquamarine; font-size:40px}
Where; h1 is the selector. color/font-size is the property. aquamarine/40px is the value.
The selector points to the HTML element you want to style.The declaration block contains one or more declarations separated by semicolons.Each declaration always ends with a semicolon, and declaration blocks are surrounded by curly braces.
Example;
This h1
elements will have a font size of 40px and an aquamarine text color, then:
p {
color: aquamarine;
font-size: 40px;
}
CSS Comments
CSS comments are used to explain the code, and may help when you edit the source code at a later date.Comments are ignored by the browser therefore they are not displayed.A CSS comment starts with / and ends with /. Comments can also span through multiple lines.
Example;
h1 {
color: aquamarine;
/* This is a single-line comment */
font-size: 40px;
}
/* This is
a multi-line
comment */
CSS Selectors
CSS selectors are used to find or select the HTML elements you want to style.
CSS selectors are five in number;
- Simple selectors which select elements based on name, id, class, etc.
- Combinator selectors which select elements based on a specific relationship between them.
- Pseudo-class selectors which select elements based on a certain state.
- Pseudo-elements selectors which select and style a part of an element .
- Attribute selectors which select elements based on an attribute or attribute value.
But I will explain the most basic CSS selectors which are the element, id, class,universal, grouping selectors.
The element Selector
The element selector selects HTML elements based on th element name.
Example;
Below, all <p>
elements will be to the left and have blue text color:
p {
text-align: left;
color: blue;
}
The id Selector
The id selector uses the id attribute of an 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. The id is given to the element in its opening tag in the HTML document. To select an element with a specific id, write a hash (#) character, followed by the id of the element. Also know that an id name cannot start with a number
Example;
The CSS rule below will be applied to the HTML element with id=wrapper:
#wrapper {
text-align: left;
color: blue;
}
The 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.Just like id the class is given to the element in the start tag of the element in the HTML document.You should know that HTML elements can also refer to more than one class.
Example;
The HTML element with class="col-2" will be blue and to the left:
.col-2 {
text-align: left;
color: blue;
}
You can also specify that only specific HTML elements should be affected by a class.
Example;
In this example only <p>
elements with class="col-2" will be to the left and red:
p.col-2 {
text-align: left;
color: blue;
}
And also a class name cannot start with a number!
The 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:
* {
text-align: justify;
font-family: Poppins, sans-serif;
}
The grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
Look at the following CSS code (the h1, section, and aside elements have the same style definitions):
h1 {
text-align: center;
color: red;
}
section {
text-align: center;
color: red;
}
aside {
text-align: center;
color: red;
}
If we use group selectors the that is by writing the selectors together and separate each of them with a comma. It would look like this:
h1, section, aside {
text-align: center;
color: red
;
}
How To Add CSS
When a browser reads a style sheet, it will format the HTML document according to the information in the style sheet.
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
- External CSS
- Internal CSS
- Inline CSS
I will be briefly explaining this three ways;
External CSS
With an external style sheet, you can change the look of an entire website by changing just one file!
Each HTML page must include a reference to the external style sheet file inside the <link>
element, inside the head section.
Example;
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags.
Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style>
element, inside the head section.
Example;
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the "style" attribute to the relevant element. The style attribute can contain any CSS property.
Example;
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
An inline style loses many of the advantages of a style sheet (by mixing content with presentation). Use this method sparingly.
Multiple Style Sheets
If some properties have been defined for the same selector (element) in different style sheets, the value from the last read style sheet will be used.
Example;
If an external stylesheet has the following style for the <h1>
element:
h1 {
color: navy;
}
And an internal style sheet also has the following style for the <h1>
element:
h1 {
color: orange;
}
If the internal style is defined after the link to the external style sheet, the <h1>
elements will be "orange":
<head>
<link rel="stylesheet" href="style.css">
<style>
h1 {
color: orange;
}
</style>
</head>
However, if the internal style is defined before the link to the external style sheet, the <h1>
elements will be "navy":
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the following rules, where number one has the highest priority:
- Inline style (inside an HTML element)
- External and internal style sheets (in the head section)
- Browser default So, an inline style has the highest priority, and will override external and internal styles and browser defaults.
Subscribe to my newsletter
Read articles from Richard Offodile directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
