Css (Cascading Style Sheet)
Css (Cascading Style Sheet)
STYLE SHEET)
CSS was introduced together with HTML 4, to provide a better way
to style HTML elements. Styles were added to HTML 4.0 to solve a
problem
What is CSS?
CSS stands for Cascading Style Sheets
CSS is used to control the style of a web document in a simple and easy
way.
•
•Why Use CSS?
Each declaration includes a CSS property name and a value, separated by a colon.
A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly
braces.
Example
CSS selectors are used to "find" (or select) HTML elements based on their
element name, id, class, attribute, and more.
The element Selector
The element selector selects elements based on the element name.
Example
You can select all <p> elements on a page like this (here, all <p> elements will be center-
aligned, with a red text color):
p{
text-align: center;
color: red;
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be 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 style rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
To select elements with a specific class, write a period (.) character, followed by the name of
the class.
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 center-aligned:
p.center {
text-align: center;
color: red;
}
Example
}
h2 {
p{
text-align: center;
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a later date.
p{
color: red;
/* This is a single-line comment */
text-align: center;
}
Note:
CSS can be added to HTML in the following ways:
Inline - using the style attribute in HTML elements
The preferred way to add CSS to HTML is to put CSS syntax in separate CSS files.
INLINE STYLES
An inline style can be used if a unique style is to be applied to one single occurrence of
an element.
To use inline styles, use the style attribute in the relevant tag. The style attribute can
contain any CSS property.
The example below shows how to change the text color and the left margin of a paragraph:
<!DOCTYPE html>
<html>
<body style="background-color:yellow;">
<h2 style="background-color:red;">This is a heading</h2>
<p style="background-color:green;"> This is a paragraph.</p>
</body>
</html>
Internal Style Sheet
An internal style sheet can be used if one single document has a unique style. Internal styles are defined
in the <head> section of an HTML page, by using the <style> tag, like this:
<head>
<style>
body {background-color:yellow;}
p {color:blue;}
</head>
External Style Sheet
An external style sheet is ideal when the style is applied to many pages.
External Style Sheets can save a lot of work
External Style Sheets are stored in CSS files
With an external style sheet, you can change the look of an entire Web site by changing one file.
Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the <head>
section:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
Example mystle.css
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}