CSS Basics


Chapter: 1

 What is CSS ?


Welcome to CSS!


CSS stands for Cascading Style Sheets.

Cascading refers to the way CSS applies one style on top of another.
Style Sheets control the look and feel of web documents.

CSS and HTML work hand in hand:
- HTML sorts out the page structure.
- CSS defines how HTML elements are displayed.
To understand CSS, you should already have a basic knowledge of HTML.
If you want to study HTML, check out our HTML course.


Welcome to CSS!

What are style sheets used for?

to control the look and feel of web documents
to define the structure of web documents
to store the keywords of web pages
to script web pages


Why Use CSS?


CSS allows you to apply specific styles to specific HTML elements.

The main benefit of CSS is that it allows you to separate style from content.

Using just HTML, all the styles and formatting are in the same place, which becomes rather difficult to maintain as the page grows.
All formatting can (and should) be removed from the HTML document and stored in a separate CSS file.


Why Use CSS?

Why use CSS?

it helps make the web page's browser independent
it helps create unique web pages
it allows for the separation of style and content


Chapter: 2

    Inline, Embedded and External CSS 


Inline CSS


Using an inline style is one of the ways to insert a style sheet. With an inline style, a unique style is applied to a single element.

In order to use an inline style, add the style attribute to the relevant tag.

The example below shows how to create a paragraph with a gray background and white text:
<p style="color:white; background-color:gray;">
This is an example of inline styling.
</p>
CSS
Result:contentImage
The style attribute can contain any CSS property.


Inline CSS

Select the attribute that organizes the inline styling:

style
id
class


Embedded/Internal CSS


Internal styles are defined within the <style> element, inside the head section of an HTML page.
For example, the following code styles all paragraphs:
<html>
<head>
<style>
p {
color:white;
background-color:gray;
}
</style>
</head>
<body>
<p>This is my first paragraph. </p>
<p>This is my second paragraph. </p>
</body>
</html>
CSS
All paragraphs have a white font and a gray background:contentImage
An internal style sheet may be used if one single page has a unique style.


Embedded/Internal CSS

Where should the style tag be declared to organize an internal CSS?

body
external file
head


External CSS


With this method, all styling rules are contained in a single text file, which is saved with the .css extension.

This CSS file is then referenced in the HTML using the <link> tag. The <link> element goes inside the head section.

Here is an example:
The HTML:
<head>
<link rel="stylesheet" href="example.css">
</head>
<body>
<p>This is my first paragraph.</p>
<p>This is my second paragraph. </p>
<p>This is my third paragraph. </p>
</body>
HTML
The CSS:
p {
color:white;
background-color:gray;
}
CSS
Result:contentImage
Both relative and absolute paths can be used to define the href for the CSS file. In our example, the path is relative, as the CSS file is in the same directory as the HTML file.


External CSS

Fill in the blanks to call an external stylesheet called "test.css":

<head>

<
rel="stylesheet" href="test.css">

</head>


Chapter: 3

   CSS Rules and Selector  


CSS Syntax


CSS is composed of style rules that the browser interprets and then applies to the corresponding elements in your document.
A style rule has three parts: selectorproperty, and value.

For example, the headline color can be defined as:
h1 { color: orange; }
CSS
Where:contentImageThe selector points to the HTML element you want to style.
The declaration block contains one or more declarations, separated by semicolons.
Each declaration includes a property name and a value, separated by a colon.


CSS Syntax

In the rule, the "selector":

allows to substitute the selected attribute
selects which element to style
serves as a property


Type Selectors


The most common and easy to understand selectors are type selectors. This selector targets element types on the page.

For example, to target all the paragraphs on the page:
p {
color: red;
font-size:130%;
}
CSS
A CSS declaration always ends with a semicolon, and declaration groups are surrounded by curly braces.


Type Selectors

Rearrange the code to create a valid CSS style rule:

p {
color: blue;
}


id and class Selectors



id selectors allow you to style an HTML element that has an id attribute, regardless of their position in the document tree. Here is an example of an id selector:

The HTML:
<div id="intro">
<p> This paragraph is in the intro section.</p>
</div>
<p> This paragraph is not in the intro section.</p>
HTML
The CSS:
#intro {
color: white;
background-color: gray;
}
CSS
To select an element with a specific id, use a hash character, and then follow it with the id of the element.
Result:contentImageClass selectors work in a similar way. The major difference is that IDs can only be applied once per page, while classes can be used as many times on a page as needed.

In the example below, both paragraphs having the class "first" will be affected by the CSS:

The HTML:
<div>
<p class="first">This is a paragraph</p>
<p> This is the second paragraph. </p>
</div>
<p class="first"> This is not in the intro section</p>
<p> The second paragraph is not in the intro section. </p>
HTML
The CSS:
.first {font-size: 200%;}
CSS
To select elements with a specific class, use a period character, followed by the name of the class.
Do NOT start a class or id name with a number!


id and class Selectors

Fill in the blanks to give yellow background color to the element with id="intro", and black text color to the class="mytext":

{

background-color: yellow;

}

{

color: black;

}


Descendant Selectors


These selectors are used to select elements that are descendants of another element. When selecting levels, you can select as many levels deep as you need to.

For example, to target only <em> elements in the first paragraph of the "intro" section:

The HTML:
<div id="intro">
<p class="first">This is a <em> paragraph.</em></p>
<p> This is the second paragraph. </p>
</div>
<p class="first"> This is not in the intro section.</p>
<p> The second paragraph is not in the intro section. </p>
HTML
The CSS:
#intro .first em {
color: pink;
background-color:gray;
}
CSS
As a result, only the elements selected will be affected:contentImage
code repo icon
Design Your Resume!
Build an eye-catching design step by step for your resume!
The descendant selector matches all elements that are descendants of a specified element.


Descendant Selectors

Drag and drop from the options below to create a style rule for all paragraphs belonging to the element with id="test":

#test

p

{


color: red;


}


Chapter: 4

CSS Comments


Comments


Comments are used to explain your code, and may help you when you edit the source code later. Comments are ignored by browsers.

A CSS comment look like this:
/* Comment goes here */
CSS
Example:
p {
color: green;
/* This is a comment */
font-size: 150%;
}
CSS
The comment does not appear in the browser:contentImage
Comments can also span multiple lines.


Comments

Turn the text into a comment in CSS:

This is a comment


Chapter: 5

Style Cascade and Inheritance


Cascade


The final appearance of a web page is a result of different styling rules.

The three main sources of style information that form a cascade are:

- The stylesheet created by the author of the page
- The browser's default styles
- Styles specified by the user
CSS is an acronym for Cascading Style Sheets.



Inheritance


Inheritance refers to the way properties flow through the page. A child element will usually take on the characteristics of the parent element unless otherwise defined.

For example:
<html>
<head>
<style>
body {
color: green;
font-family: Arial;
}
</style>
</head>
<body>
<p>
This is a text inside the paragraph.
</p>
</body>
</html>
HTML
Result:contentImage
code repo icon
A Little Intro
Customize your Intro section, including your name and surname, profession, and summary.
Since the paragraph tag (child element) is inside the body tag (parent element), it takes on any styles assigned to the body tag.

Inheritance

What color does the paragraph have? <style> body {color: green; } .mydiv {color: red; } </style> <body> <div class="mydiv"> <p>Some text</p> </div> </body>

black
red
green


Chapter: 6

Module 1 Quiz


From the three types of styling, which one is the most useful in terms of website optimization?

Inline
Internal
External


What is the "style", when creating an internal CSS?

value
tag
property


The Style definition rule consists of selector, property and:


Fill in the blank to apply white text color to the paragraph.

p {
#FFF;}


Comments