0% found this document useful (0 votes)
2 views16 pages

Css

CSS, or Cascading Style Sheets, is a language used to style HTML documents, allowing for control over layout and design across multiple web pages. It includes various selectors, such as element, id, and class selectors, to apply styles, and can be implemented through external, internal, or inline methods. Proper syntax and organization are crucial to avoid common errors that can lead to unexpected styling issues.

Uploaded by

abaasbogere6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Css

CSS, or Cascading Style Sheets, is a language used to style HTML documents, allowing for control over layout and design across multiple web pages. It includes various selectors, such as element, id, and class selectors, to apply styles, and can be implemented through external, internal, or inline methods. Proper syntax and organization are crucial to avoid common errors that can lead to unexpected styling issues.

Uploaded by

abaasbogere6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CSS

CSS is the language we use to style an HTML document.


CSS describes how HTML elements should be displayed.

CSS Introduction
What is CSS?
CSS is the language we use to style a Web page.
 CSS stands for Cascading Style Sheets
 CSS describes how HTML elements are to be displayed on screen,
paper, or in other media
 CSS saves a lot of work. It can control the layout of multiple web
pages all at once
 External stylesheets are stored in CSS files
CSS Demo - One HTML Page - Multiple Styles!
Here we will show one HTML page displayed with four different
stylesheets. Click on the "Stylesheet 1", "Stylesheet 2", "Stylesheet 3",
"Stylesheet 4" links below to see the different styles:
Why Use CSS?
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 Example
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}

p{
font-family: verdana;
font-size: 20px;
}
CSS Saves a Lot of Work!
The CSS definitions are normally saved in an external .css file.
With an external stylesheet file, you can change the look of an entire
website by changing just one file!
CSS Syntax
CSS Syntax
A CSS rule consists of a selector and a declaration block:

The 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 CSS property name and a value, separated
by a colon.
Multiple CSS declarations are separated with semicolons, and
declaration blocks are surrounded by curly braces.
Example
In this example all <p> elements will be center-aligned, with a red
text color:
p{
color: red;
text-align: center;
}
Example Explained
 p is a selector in CSS (it points to the HTML element you want to
style: <p>).
 color is a property, and red is the property value
 text-align is a property, and center is the property value
CSS Selectors

CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you
want to style.
We can divide CSS selectors into five categories:
 Simple selectors (select elements based on name, id, class)
 Combinator selectors (select elements based on a specific
relationship between them)
 Pseudo-class selectors (select elements based on a certain state)
 Pseudo-elements selectors (select and style a part of an element)
 Attribute selectors (select elements based on an attribute or
attribute value)
This page will explain the most basic CSS selectors.
The CSS element Selector
The element selector selects HTML elements based on the element
name.
Example
Here, all <p> elements on the page will be center-aligned, with a red
text color:
p{
text-align: center;
color: red;
}
The CSS id Selector
The 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
The CSS rule below will be applied to the HTML element with
id="para1":
#para1 {
text-align: center;
color: red;
}
Note: An id name cannot start with a number!
The CSS 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.
Example
In this example all HTML elements with class="center" will be red
and center-aligned:
.center {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be
affected by a class.
Example
In this example only <p> elements with class="center" will be red
and center-aligned:
[Link] {
text-align: center;
color: red;
}
HTML elements can also refer to more than one class.
Example
In this example the <p> element will be styled according to
class="center" and to class="large":
<p class="center large">This paragraph refers to two classes.</p>
Note: A class name cannot start with a number!
Exercise?
Which CSS selector is used to select an element with a specific id?
CSS Grouping Selectors
The CSS 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: center;
color: blue;
}
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style
definitions.
Look at the following CSS code (the h1, h2, and p elements have the
same style definitions):
h1 {
text-align: center;
color: red;
}

h2 {
text-align: center;
color: red;
}

p{
text-align: center;
color: red;
}
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
All CSS Simple Selectors

Selector Example Example description

#id #firstname Selects the element with id="firstname"

.class .intro Selects all elements with class="intro"

* * Selects all elements

element p Selects all <p> elements

element,element,. div, p Selects all <div> elements and all <p> eleme
.

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.
There are three ways of inserting a style sheet:
1. External CSS - link to an external .css file
2. Internal CSS - use the <style> element in the head section
3. Inline CSS - use the style attribute on HTML elements
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
External styles are defined within the <link> element, inside the <head>
section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="[Link]">
</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.
Here is how the "[Link]" file looks:
"[Link]"
body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value (20) and the unit
(px):
Incorrect (space): margin-left: 20 px;
Correct (no space): margin-left: 20px;
How To Add Internal CSS
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
Internal styles are defined within the <style> element, inside the <head>
section of an HTML page:
<!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>
How To Add Inline CSS
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
Inline styles are defined within the "style" attribute of the relevant
element:
<!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>
Tip: An inline style loses many of the advantages of a style sheet (by
mixing content with presentation). Use this method sparingly.
Multiple Style Sheets & Cascading Order
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.
Assume that an external style sheet has the following style for the <h1>
element:
h1 {
color: navy;
}
Then, assume that an internal style sheet also has the following style
for the <h1> element:
h1 {
color: orange;
}
Example
If the internal style is defined after the link to the external style sheet,
the <h1> elements will be "orange":
<head>
<link rel="stylesheet" type="text/css" href="[Link]">
<style>
h1 {
color: orange;
}
</style>
</head>
Example
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="[Link]">
</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:
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
CSS Comments
Comments are used to explain the CSS code, and may help when you
edit the source code at a later date.
Comments are also used to temporarily disable sections of CSS code
within a stylesheet.
Comments are ignored by browsers!
A CSS comment is placed inside the HTML <style> element, and starts
with /* and ends with */:
Example
/* This is a single-line comment */
p{
color: red;
}
You can add comments wherever you want in the code:
Example
p{
color: red; /* Set text color to red */
}
Even in the middle of a code line:
Example
p{
color: /*red*/blue;
}
Comments can also span multiple lines:
Example
/* This is
a multi-line
comment */

p{
color: red;
}

HTML and CSS Comments


From the HTML tutorial, you learned that you can add comments to
your HTML source by using the <!--...--> syntax.
In the following example, we use a combination of HTML and CSS
comments:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red; /* Set text color to red */
}
</style>
</head>
<body>

<h2>My Heading</h2>

<!-- These paragraphs will be red -->


<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
<p>HTML and CSS comments are not shown in the output.</p>

</body>
</html>
CSS Errors
CSS Errors
Errors in CSS can lead to unexpected behavior or styles not being
applied correctly.
This page shows common CSS mistakes, and how to avoid them.
Missing Semicolons
Forgetting a semicolon at the end of a property declaration can break the
style rule.
Example
.bad {
color: red
background-color: yellow;
}
Invalid Property Names
Using a property name that does not exist will simply be ignored by the
browser.
Example
.bad {
colr: blue;
font-size: 16px;
}

Invalid Values
Correct properties but invalid values will also be ignored.
Example
.bad {
width: -100px;
color: green;
}
Unclosed Braces
If you forget to close a brace }, the entire rule may be ignored.
Example
.bad {
padding: 20px;
margin: 10px;
Extra Colons or Braces
Typos like extra colons or misplaced braces can cause rules to break.
Example
.bad {
color:: blue;
}
Tips to Avoid CSS Errors
 Use a code editor with syntax highlighting.
 Validate your CSS with a CSS linter or validator.
 Write CSS in small sections and test frequently.

You might also like