7 September 2023
CSS basics
CSS (Cascading Style Sheets) is the code that styles web content.
Like HTML, CSS is not a programming language. It's not a markup language either. CSS
is a style sheet language. CSS is what you use to selectively style HTML elements. For
example, this CSS selects paragraph text, setting the color to red
Unset
p{
color: red;
}
The whole structure is called a ruleset. (The term ruleset is often referred to as just
rule.) Note the names of the individual parts:
Selector
This is the HTML element name at the start of the ruleset. It defines the element(s) to
be styled (in this example, <p> elements). To style a different element, change the
selector.
Declaration
This is a single rule like color: red;. It specifies which of the element's properties you
want to style.
Properties
These are ways in which you can style an HTML element. (In this example, color is a
property of the <p> elements.) In CSS, you choose which properties you want to affect in
the rule.
Property value
To the right of the property—after the colon—there is the property value. This chooses
one out of many possible appearances for a given property. (For example, there are
many color values in addition to red.)
Note the other important parts of the syntax:
● Apart from the selector, each ruleset must be wrapped in curly braces. ({})
● Within each declaration, you must use a colon (:) to separate the property from
its value or values.
● Within each ruleset, you must use a semicolon (;) to separate each declaration
from the next one.
Unset
p{
color: red;
width: 500px;
border: 1px solid black;
}
You can also select multiple elements and apply a single ruleset to all of them. Separate
multiple selectors by commas. For example:
Unset
p,
li,
h1 {
color: red;
}
CSS layout is mostly based on the box model. Each box taking up space on your page
has properties like:
● padding, the space around the content. In the example below, it is the space
around the paragraph text.
● border, the solid line that is just outside the padding.
● margin, the space around the outside of the border.
● width (of an element).
● background-color, the color behind an element's content and padding.
● color, the color of an element's content (usually text).
● text-shadow sets a drop shadow on the text inside an element.
● display sets the display mode of an element. (keep reading to learn more)
What is the difference between padding and margin in css
Padding: top right bottom left