CSS - basics

revision:


Content

CSS stands for "Cascading Style Sheets". Anatomy of a CSS syntax Three ways to insert CSS: external CSS, internal CSS, inline CSS CSS comments are used to explain the code. The CSS box model is essentially a box that wraps around every HTML element.


CSS stands for "Cascading Style Sheets".

top

CSS describes how HTML elements are to be displayed on screen, paper, or in other media.
CSS saves a lot of work, as it can control the layout of multiple web pages all at once. External stylesheets are stored in CSS files.
CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.

CSS solved a big problem.

HTML was NEVER intended to contain tags for formatting a web page! HTML was created to describe the content of a web page.
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers.
Development of large websites, where fonts and color information were added to every single page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS. CSS removed the style formatting from the HTML page!

CSS saves a lot of work!

The style definitions are normally saved in external .css files. With an external stylesheet file, you can change the look of an entire website by changing just one file!


Anatomy of a CSS syntax

top

A CSS rule-set consists of a selector and a declaration block.

example: p{color: red;}

The selector points to the HTML element you want to style. To style a different element, change the selector. CSS selectors are used to "find" (or select) the HTML elements you want to style.

The declaration block contains one or more declarations separated by semicolons. . It specifies which of the element's properties you want to style.

Each declaration includes a CSS "property name" and a "value", separated by a colon. Within each declaration, you must use a colon (:) to separate the property from its value or values.

Multiple CSS declarations are separated with semicolons. Within each ruleset, you must use a semicolon (;) to separate each declaration from the next one.

Declaration blocks are surrounded by curly braces ({}).

Example: p {color: blue; text-align: center;}

CSS selectors can be devided into five categories:

1/ simple/element selectors : select elements based on name, id, class. Sometimes also called a "tag" or "type" selector.

2/ combinator selectors : select elements based on a specific relationship between them.

3/ pseudo-class selectors : select elements based on a certain state. It selects the specified element(s), but only when in the specified state. (For example, when a cursor hovers over a link.).

4/ pseudo-elements selectors : select and style a part of an element.

5/ attribute selectors : select elements based on an attribute or attribute value. For example, the element on the page with the specified ID or the element(s) with the specified class.

The CSS element selector selects HTML elements based on the element name.

Example 2: p {color: blue; text-align: center;}
explanation: all <p> elements on the page will be center-aligned, with a blue text color.

The CSS "id" selector uses the id attribute of an HTML 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!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Example 3: #para1 {text-align: center; color: blue;}
explanation: the CSS rule will be applied to the HTML element with id="para1".

Note: an id name cannot start with a number!

The CSS "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.

Example 4: .center {text-align: center; color: blue;}
explanation: all HTML elements with class="center" will be blue and center-aligned.

You can also specify that only specific HTML elements should be affected by a class.

Example 5: p.center {text-align: center; color: blue;}
explanation: only <p> elements with class="center" will be center-aligned.

HTML elements can also refer to more than one class.

Example 6: p.center {text-align: center; color: blue;} p.large {font-size: 300%;}
explanation: the <p> element will be styled according to class="center" and to class="large".

Note: a class name cannot start with a number!

The CSS "universal" selector (*) selects all HTML elements on the page.

Example 7: * {text-align: center; color: blue;}
explanation: this CSS rule will affect every HTML element on the page.

The CSS "grouping" selector selects all the HTML elements with the same style definitions.

To group selectors, separate each selector with a comma.

Example 8: h1, h2, p {text-align: center; color: blue;}
explanation: the h1, h2, and p elements have the same style definitions.


Three ways to insert CSS: external CSS, internal CSS, inline CSS

top

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.
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.

Example 9: <head> <link rel="stylesheet" href="mystyle.css></head>
explanation: external styles are defined within the <link> element, inside the <head> section of an HTML page:

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 10: <head><style>body {background-color: linen;} h1 {color: maroon; margin-left: 40px;}</style></head>

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 11: <h1 style="color:blue;text-align:center;">This is a heading</h1>
explanation: inline styles are defined within the "style" attribute of the relevant element:

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.

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:
1. Inline style (inside an HTML element),
2. External and internal style sheets (in the head section),
3. Browser default.

So, an inline style has the highest priority, and will override external and internal styles and browser defaults.


CSS comments are used to explain the code.

top

They may help when you edit the source code at a later date.

Comments are ignored by browsers.
A CSS comment is placed inside the <style> element, and starts with /* and ends with */.
Comments can also span multiple lines.


The CSS box model is essentially a box that wraps around every HTML element.

top

It consists of: margins, borders, padding, and the actual content.

content : the content of the box, where text and images appear.

padding : clears an area around the content; the padding is transparent.

border: a border that goes around the padding and content.

margin: clears an area outside the border; the margin is transparent.

The box model allows us to add a border around elements, and to define space between elements.

Demonstrating the box model

This text is the content of the box. We have added a 5vw padding, 2vw margin and a 1.5vw green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
code:
                <style>
                    .text {
                        background-color: lightgrey;
                        width: 25vw;
                        border: 1.5vw solid green;
                        padding: 5vw;
                        margin: 2vw;
                    }
                </style>
            

The content area, bounded by the content edge, contains the "real" content of the element.

The content can be text, an image, or a video player.

Its dimensions are the content width (or content-box width) and the content height (or content-box height). It often has a background color or background image.

If the box-sizing property is set to "content-box" (default) and if the element is a block element, the content area's size can be explicitly defined with the "width", "min-width", "max-width", "height", "min-height", and "max-height" properties.

The padding area, bounded by the padding edge, extends the content area to include the element's padding.

Its dimensions are the padding-box width and the padding-box height.
The thickness of the padding is determined by the "padding-top", "padding-right", "padding-bottom", "padding-left", and shorthand "padding" properties.

The border area, bounded by the border edge, extends the padding area to include the element's borders.

Its dimensions are the border-box width and the border-box height.

The thickness of the borders are determined by the "border-width" and shorthand "border" properties.

If the box-sizing property is set to border-box, the border area's size can be explicitly defined with the "width", "min-width", "max-width", "height", "min-height", and "max-height" properties.

When there is a background (background-color or background-image) set on a box, it extends to the outer edge of the border (i.e. extends underneath the border in z-ordering). This default behavior can be altered with the "background-clip" css property.

The margin area, bounded by the margin edge, extends the border area to include an empty area used to separate the element from its neighbors.

Its dimensions are the margin-box width and the margin-box height.

The size of the margin area is determined by the "margin-top", "margin-right", "margin-bottom", "margin-left", and shorthand "margin" properties.

When margin collapsing occurs, the margin area is not clearly defined since margins are shared between boxes. For non-replaced inline elements, the amount of space taken up (the contribution to the height of the line) is determined by the "line-height" property, even though the borders and padding are still displayed around the content.

When you set the width and height properties of an element with CSS, you just set the width and height of the content area.

To calculate the full size of an element, you must also add padding, borders and margins.

The total width of an element should be calculated like this: total element width = width + left padding + right padding + left border + right border + left margin + right margin.

The total height of an element should be calculated like this: total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin.

For the example here above: 30vw (=width) + 10vw (= left and right padding) + 3vw (= left and right border) + 4vw (= left and right margin) = 47vw width.