Unit 3 Web Tech
Unit 3 Web Tech
CSS 3
Advantages of CSS
• CSS saves time - You can write CSS once and then reuse same sheet
in multiple HTML pages. You can define a style for each HTML
element and apply it to as many Web pages as you want.
• Pages load faster - If you are using CSS, you do not need to write
HTML tag attributes every time. Just write one CSS rule of a tag and
apply it to all the occurrences of that tag. So less code means faster
download times.
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.
• The style rule below will be applied to the HTML element with id=”para1″:
#para1 {
text-align: center;
color: red;
}
The true power of id selectors is when they are used as the foundation for
descendant selectors, For example:
#black h2 {
color: #000000;
}
• In this example all level 2 headings will be displayed in black color when
those headings will lie with in tags having id attribute set to black.
ul em
{ color: Red}
</body>
</html>
<body>
<h1 style = "color:blue">
This is inline CSS
</h1>
</body>
</html>
h1, h2, h3 {
color: blue;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Now you can include this file mystyle.css in any HTML document as follows −
<head>
<link type = "text/css" href = "mystyle.css" media = " all" />
</head>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
Rajat Kumar Web Tech Unit Number:03 CSS 31
01/13/2024
Cascading Style Sheets (CSS)
(CO 3 )
Inline CSS Example
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS</title>
</head>
<body>
<p style = "color:#009900; font-size:50px;
font-style:italic; text-align:center;">
WELCOME
</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Embedded CSS</title>
<style>
.main {
text-align:center;
}
.GFG {
color:#009900;
font-size:50px;
font-weight:bold;
}
.wd {
font-style:bold;
font-size:20px;
Rajat Kumar Web Tech Unit Number:03 CSS 33
}
01/13/2024
Cascading Style Sheets (CSS)
(CO 3 )
External CSS Example
body {
background-color: powderblue;
}
.main {
text-align:center; save with mystyle.css
}
.GFG {
color:#009900;
font-size:50px;
font-weight:bold;
}
#wd {
font-style:bold;
font-size:20px;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css"/>
</head>
<body>
<div class = "main">
<div class ="GFG">Elective subject</div>
<div id ="wd">
Web Designing
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>HTML External CSS</title>
<link rel = "stylesheet" type = "text/css" href = "s.css">
</head>
<body>
<p class = "red">This is red</p>
<p class = "thick">This is thick</p>
<p class = "green">This is green</p>
<p class = "thick green">This is thick and green</p>
</body>
</html>
<head>
<title>HTML Internal CSS</title>
<body>
<p class = "red">This is red</p>
<p class = "thick">This is thick</p>
<p class = "green">This is green</p>
<p class = "thick green">This is thick and green</p>
</body>
<head>
<title>HTML Inline CSS</title>
</head>
<body>
<p style = "color:red;">This is red</p>
<p style = "font-size:20px;">This is thick</p>
<p style = "color:green;">This is green</p>
<p style = "color:green;font-size:20px;">This is thick and green</p>
</body>
</html>
<body>
<p>Hello World!</p>
</body>
</html>
<body>
<p style = "background-color:yellow;">
This text has a yellow background color.
</p>
</body>
</html>
<body>
<h1>Hello World!</h1>
</body>
<html>
<body>
<p>Welcome</p>
</body>
</html>
<body>
<p>Tutorials point</p>
</body>
</html>
<body>
<p>Tutorials point</p>
</body>
</html>
<body>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
</body>
</html>
<body>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
</body>
</html>
<body>
<p style = "font-family:georgia,garamond,serif;">
This text is rendered in either georgia, garamond, or the
default serif font depending on which font you have at your system.
</p>
</body>
</html>
<body>
<p style = "font-style:italic;">
This text will be rendered in italic style
</p>
</body>
</html>
<body>
<p style = "font-variant:small-caps;">
This text will be rendered as small caps
</p>
</body>
</html>
<body>
<p style = "font-weight:bold;">
This font is bold.
</p>
<body>
<p style = "font-size:20px;">
This font size is 20 pixels
</p>
<body>
<p style = "font-size-adjust:0.61;">
This text is using a font-size-adjust value.
</p>
</body>
</html>
Rajat Kumar Web Tech Unit Number:03 CSS 59
01/13/2024
Cascading Style Sheets (CSS)
(CO 3 )
Shorthand Property
You can use the font property to set all the font properties at once.
<html>
<head>
</head>
<body>
<p style = "font:italic small-caps bold 15px georgia;">
Applying all the properties on the text at once.
</p>
</body>
</html>
<body>
<p style = "color:red;">
This text will be written in red.
</p>
</body>
</html>
<body>
<p style = "direction:rtl;">
This text will be rendered from right to left
</p>
</body>
</html>
<body>
<p style = "letter-spacing:5px;">
This text is having space between letters.
</p>
</body>
</html>
<body>
<p style = "word-spacing:10px;">
This text is having space between words.
</p>
</body>
</html>
<body>
<p style = "text-indent:1cm;">
This text will have first line indented by 1cm and this line will remain at
its actual position this is done by CSS text-indent property.
</p>
</body>
</html>
<body>
<p style = "text-align:right;">
This will be right aligned.
</p>
<body>
<p style = "text-decoration:underline;">
This will be underlined
</p>
<body>
<p style = "text-transform:capitalize;">
This will be capitalized
</p>
td.a {
border-style:dotted;
border-width:3px;
border-color:#000000;
padding: 10px;
}
td.b {
border-style:solid;
border-width:3px;
border-color:#333333;
padding:10px;
}
</style>
</head>
<body>
Rajat Kumar Web Tech Unit Number:03 CSS 71
01/13/2024
<table class = "one">
Cascading Style Sheets (CSS)
(CO 3 )
The border-spacing Property
• The border-spacing property specifies the distance that separates
adjacent cells'. borders. It can take either one or two values; these
should be units of length.
• If you provide one value, it will applies to both vertical and
horizontal borders. Or you can specify two values, in which case, the
first refers to the horizontal spacing and the second to the vertical
spacing
<html>
<head>
<style type = "text/css">
table.one {
border-collapse:separate;
width:400px;
border-spacing:10px;
}
table.two {
border-collapse:separate;
width:400px;
border-spacing:10px 50px;
}
Rajat Kumar Web Tech Unit Number:03 CSS 72
</style>
01/13/2024
Cascading Style Sheets (CSS)
(CO 3 )
The caption-side Property
• The caption-side property allows you to specify where the content
of a <caption> element should be placed in relationship to the table.
This property can have one of the four values top, bottom,
left or right.
<html>
<head>
<style type = "text/css">
caption.top {caption-side:top}
caption.bottom {caption-side:bottom}
</style>
</head>
<body>
<body>
<body>
<div class="main">CSS Box-Model Property</div>
<div id="box">NIET</div>
</body>
</html>
<body>
<p style = "width:400px; height:100px; border:1px solid red; padding:5px;
margin:10px;">
This paragraph is 400pixels wide and 100 pixels high
</p>
</body>
</html>
Rajat Kumar Web Tech Unit Number:03 CSS 81
01/13/2024
CSS – Dimension
(CO 3 )
The line-height Property
The line-height property allows you to increase the space between
lines of text. The value of the line-height property can be a number,
a length, or a percentage.
<html>
<head>
</head>
<body>
<p style = "width:400px; height:100px; border:1px solid red; padding:5px; margin:10px;
line-height:30px;">
This paragraph is 400pixels wide and 100 pixels high and here line height is 30pixels.
This paragraph is 400 pixels wide and 100 pixels high and here line height is 30pixels.
</p>
</body>
</html>
<body>
<p style = "width:400px; min-height:200px; border:1px solid red; padding:5px; margin:10px;">
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
</p>
</body>
</html>
Rajat Kumar Web Tech Unit Number:03 CSS 84
01/13/2024
CSS – Dimension
(CO 3 )
The max-width Property
The max-width property allows you to specify maximum width of a
box. The value of the max-width property can be a number, a length,
or a percentage.
<html>
<head>
</head>
<body>
<p style = "max-width:100px; height:200px; border:1px solid red; padding:5px;
margin:10px;">
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
</p>
</body> Rajat Kumar Web Tech Unit Number:03 CSS 85
01/13/2024
CSS – Dimension
(CO 3 )
The min-width Property
The min-width property allows you to specify minimum width of a
box. The value of the min-width property can be a number, a length,
or a percentage.
<html>
<head>
</head>
<body>
<p style = "min-width:400px; height:100px; border:1px solid red; padding:5px;
margin:10px;">
This paragraph is 100px high and min width is 400px
This paragraph is 100px high and min width is 400px
</p>
</body>
</html>
</body>
</html> 89
Rajat Kumar Web Tech Unit Number:03 CSS
01/13/2024
CSS - Positioning
(CO 3 )
CSS helps you to position your HTML element. You can put any HTML
element at whatever location you like. You can specify whether you
want the element positioned relative to its natural position in the
page or absolute.
Relative Positioning
• Relative positioning changes the position of the HTML element
relative to where it normally appears. So "left:20" adds 20 pixels to
the element's LEFT position.
<html>
<head>
</head>
<body>
<div style = "position:relative; left:80px; top:2px; background-color:yellow;">
This div has relative positioning.
</div>
Rajat Kumar Web Tech Unit Number:03 CSS 90
</body>
01/13/2024
CSS - Positioning
(CO 3 )
Absolute Positioning
• An element with position: absolute is positioned at the specified
coordinates relative to your screen top-left corner.
<html>
<head>
</head>
<body>
<div style = "position:absolute; left:80px; top:20px; background-color:yellow;">
This div has absolute positioning.
</div>
</body>
</html>
<body>
<div style = "position:fixed; left:80px; top:20px; background-color:yellow;">
This div has fixed positioning.
</div>
</body>
</html>
It will produce the link. When a user clicks it, the color changes to pink.
<body>
<div>
<p>First paragraph in div. This paragraph will be indented</p>
<p>Second paragraph in div. This paragraph will not be indented</p>
102
</div>
01/13/2024
Rajat Kumar Web Tech Unit Number:03 CSS
Pseudo Elements
(CO 3 )
Pseudo Elements
CSS pseudo-elements are used to add special effects to some
selectors. You do not need to use JavaScript or any other script to
use those effects. A simple syntax of pseudo-element is as follows:
<html>
<head>
<style type = "text/css">
p:first-line { text-decoration: underline; }
p.noline:first-line { text-decoration: none; }
</style>
</head>
<body>
<p class = "noline">
This line would not have any underline because this belongs to nline class.
</p>
<p>
The first line of this paragraph will be underlined as defined in the
CSS rule above. Rest of the lines in this paragraph will remain normal.
This example shows how to use :first-line pseduo element to give effect
to the first line of any HTML element.
</p>
</body>
</html>
<body>
<p class = "normal">
First character of this paragraph will be normal and will have font size 10 px;
</p>
<p>
The first character of this paragraph will be 5em big as defined in the
CSS rule above. Rest of the characters in this paragraph will remain
normal. This example shows how to use :first-letter pseduo element
to give effect to the first characters of any HTML element.
</p>
</body>
</html>
<h1>This is a heading</h1>
<p>The ::before pseudo-element inserts content before the content of an element.</p>
<h1>This is a heading</h1>
<p><b>Note:</b> IE8 supports the content property only if a !DOCTYPE is specified.</p>
</body>
</html>
<body>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
</body>
</html>
<p>In this example, the image will float to the right in the text, and the text in the paragraph will wrap around the image.</p>
</body>
</html>
h2 {
text-align: left;
}
h3 {
text-align: right;
}
</style>
</head>
<body>
<h1>Heading 1 (center)</h1>
<h2>Heading 2 (left)</h2>
<h3>Heading 3 (right)</h3>
<p>The three headings above are aligned center, left and right.</p>
</body>
</html>
img.middle {
vertical-align: middle;
}
img.bottom {
vertical-align: bottom;
}
</style>
</head>
<body>
<p>An <img src="smiley.gif" alt="smiley" width="270" height="50"> image with a default alignment.</p><br>
<p>An <img class="top" src="smiley.gif" alt="smiely" width="270" height="50"> image with a top alignment.</p><br>
<p>An <img class="middle" src="smiley.gif" alt="smiely" width="270" height="50"> image with a middle alignment.</p><br> 113
Rajat Kumar Web Tech Unit Number:03 CSS
01/13/2024
CSS Navigation Bar
(CO 3 )
• Having easy-to-use navigation is important for any web site.
• With CSS you can transform boring HTML menus into good-looking navigation
bars.
<p>Note: We use href="#" for test links. In a real web site this would be URLs.</p>
<p>In this example, we remove the bullets from the list, and its default padding and margin.</p>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
<style>
/* Style the header */
.header {
background-color: yellow;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
</div>
Rajat Kumar Web Tech Unit Number:03 CSS 117
01/13/2024
CSS Website Layout
(CO 3 )
Navigation Bar
A navigation bar contains a list of links to help visitors navigating
through your website.
<html>
<head>
<title>CSS Website Layout</title>
<style>
<style>
/* Style the header */
.header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
}