By:-Mr. Sachin Kumar Sonker
By:-Mr. Sachin Kumar Sonker
By:-Mr. Sachin Kumar Sonker
CSS Example:-
• body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
CSS Syntax
CSS Syntax
• A CSS rule-set consists of a selector and a declaration block:
In this example all <p> elements will be center-aligned, with a red text color:
p{
color: red;
text-align: center;
}
• EXAMPLE EXPLAINED
Example
• External styles are defined within the <link> element, inside the <head>
section of an HTML page:
• <!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type=“text/css” href="mystyle.css">
</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 "mystyle.css" file looks:
• "mystyle.css"
• body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
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>
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
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>
CSS Selector
The CSS element Selector
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;
}
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;
}
CSS Comments
CSS Comments
• body {
background-color: lightblue;
}
Example
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
CSS background-repeat: no-repeat
Example
• Demonstration of the different border widths:
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
CSS Border Color
• The border-color property is used to set the color of the four borders.
• The color can be set by:
• name - specify a color name, like "red"
• HEX - specify a HEX value, like "#ff0000"
• RGB - specify a RGB value, like "rgb(255,0,0)"
• transparent
• Note: If border-color is not set, it inherits the color of the element.
• Example
• Demonstration of the different border colors:
• p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
CSS Border - Individual Sides
• From the examples on the previous pages, you
have seen that it is possible to specify a different
border for each side.
• In CSS, there are also properties for specifying
each of the borders (top, right, bottom, and left):
• Example
• p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
CSS Border - Shorthand Property
• Like you saw in the previous page, there are many
properties to consider when dealing with borders.
• To shorten the code, it is also possible to specify all the
individual border properties in one property.
• The border property is a shorthand property for the
following individual border properties:
• border-width
• border-style (required)
• border-color
Example
p{
border-left: 6px solid red;
background-color: lightgrey;
}
CSS Margins
CSS Margins
• The CSS margin properties are used to create space
around elements, outside of any defined borders.
• With CSS, you have full control over the margins. There
are properties for setting the margin for each side of an
element (top, right, bottom, and left).
Margin - Individual Sides
• CSS has properties for specifying the margin for each
side of an element:
• margin-top
• margin-right
• margin-bottom
• margin-left
All the margin properties can have the following values:
• auto - the browser calculates the margin
• length - specifies a margin in px, pt, cm, etc.
• % - specifies a margin in % of the width of the containing element
• inherit - specifies that the margin should be inherited from the
parent element
Example
• Set different margins for all four sides of a <p> element:
• p{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
CSS Padding
CSS Padding
• The CSS padding properties are used to generate space
around an element's content, inside of any defined borders.
• With CSS, you have full control over the padding. There are
properties for setting the padding for each side of an
element (top, right, bottom, and left).
• Padding - Individual Sides
• CSS has properties for specifying the padding for each side of
an element:
– padding-top
– padding-right
– padding-bottom
– padding-left
All the padding properties can have the following values:
• length - specifies a padding in px, pt, cm, etc.
• % - specifies a padding in % of the width of the containing element
• inherit - specifies that the padding should be inherited from the
parent element
Example
• Set different padding for all four sides of a <div> element:
• div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
CSS Height & Width
CSS Setting height and width
Property Description
height Sets the height of an element
Example
• body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
• A text can be left or right aligned, centered, or justified.
• The following example shows center aligned, and left and right aligned text (left
alignment is default if text direction is left-to-right, and right alignment is default if
text direction is right-to-left):
Example
• h1 {
text-align: center;
}
h2 {
text-align: left;
}
Text Decoration
• The text-decoration property is used to set or remove decorations from
text.
• The value text-decoration: none; is often used to remove underlines from
links:
Example
• h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
Text Transformation
The text-transform property is used to specify uppercase and lowercase
letters in a text.
• It can be used to turn everything into uppercase or lowercase letters, or
capitalize the first letter of each word:
Example
• p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
Text Shadow
Example
• h1 {
text-shadow: 2px 2px;
}
Add a color (red) to the shadow:
• Text shadow effect!
Example
• h1 {
text-shadow: 2px 2px red;
}
In its simplest use, the float property can be used to wrap text
around images.
CSS Layout
CSS Navigation Bar
Navigation Bar
• Example • Example
<ul>
<li><a href="default.asp">Home ul {
</a></li> list-style-type: none;
<li><a href="news.asp">News margin: 0;
</a></li> padding: 0;
<li><a href="contact.asp"> }
Contact</a></li>
<li><a href="about.asp">About li a {
</a></li> display: block;
</ul> }