CSS (1)
CSS (1)
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 Syntax
EXAMPLE
In this example all <p> elements will be center-aligned, with a red text color:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Output-
Hello World!
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 are used to "find" (or select) the HTML elements you want to
style.
Selectors makes it easy for us to easily target single\multiple HTML elements
in the markup .
CSS selectors categories:-
1)Css element selector
2)Css id selector
3)Css class selector
4)Css group selector
The element selector selects HTML elements based on the element name.
!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>Every paragraph will be affected by the style.</p>
<p>Me too!</p>
<p>And me!</p>
</body>
</html>
OUTPUT-
Me too!
And me!
Example
The CSS rule below will be applied to the HTML element with id="para1":
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
Output-
Hello World!
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:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
Output-
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;
}
Example
In this example we have grouped the selectors from the code above:
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
Example
The CSS rule below will affect every HTML element on the page:
<!DOCTYPE html>
<html>
<head>
<style>
*{
text-align: center;
color: blue;
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>And me!</p>
</body>
</html>
Me too!
And me!
External CSS
Internal CSS
Inline CSS
1.)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.
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output-
This is a heading
This is a paragraph.
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>
Output-
This is a heading
This is a paragraph.
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>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output-
This is a heading
This is a paragraph.
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.
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;
CSS Comments
Comments are used to explain the code, and may help when you edit the
source code at a later date.
A CSS comment is placed inside the <style> element, and starts with /* and
ends with */:
<!DOCTYPE html>
<html>
<head>
<style>
/* This is a single-line comment */
p {
color: red;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
<p>CSS comments are not shown in the output.</p>
</body>
</html>
OUTPUT-
Hello World!
Example
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red; /* Set text color to red */ css comments
}
</style>
</head>
<body>
<h2>My Heading</h2>
<!-- These paragraphs will be red --> html comments
<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 Color Names
In CSS, a color can be specified by using a predefined color name:
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
<h1 style="background-color:Gray;">Gray</h1>
<h1 style="background-color:SlateBlue;">SlateBlue</h1>
<h1 style="background-color:Violet;">Violet</h1>
</body>
</html>
Output-
Tomato
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
CSS Border Style
The border-style property specifies what kind of border to display.
The border-style property can have from one to four values (for the top
border, right border, bottom border, and the left border).
Example
Demonstration of the different border styles:
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
</body></html>
The border-style Property
This property specifies what kind of border to display:
A dotted border.
A dashed border.
A solid border.
A double border.
A groove border.
A ridge border.
An inset border.
An outset border.
No border.
A hidden border.
A mixed border.