Introduction to CSS
Introduction to CSS
• CSS is a language that describes the style of an HTML document. CSS describes
how HTML elements should be displayed.
• 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 handles the look and feel part of a web page. Using CSS, you can control
the color of the text, the style of fonts, the spacing between paragraphs, how
columns are sized and laid out, what background images or colors are used,
layout designs, variations in display for different devices and screen sizes as
well as a variety of other effects.
• 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
•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
•Easy maintenance − To make a global change, simply change the style, and all
•Superior styles to HTML − CSS has a much wider array of attributes than HTML, so
you can give a far better look to your HTML page in comparison to HTML attributes.
•
• Multiple Device Compatibility − Style sheets allow content to be optimized for
more than one type of device. By using the same HTML document, different
versions of a website can be presented for handheld devices such as PDAs and
cell phones or for printing.
•Global web standards − Now HTML attributes are being deprecated and it is
being recommended to use CSS. So its a good idea to start using CSS in all the
HTML pages to make them compatible to future browsers.
•Offline Browsing − CSS can store web applications locally with the help of an
offline catche. Using of this, we can view offline websites. The cache also ensures
faster loading and better overall performance of the website.
•Platform Independence − The Script offer consistent platform independence and
can support latest browsers as well.
CSS SYNTAX RULE
</body>
</html>
Out put
Every paragraph will be affected by the style
Me too!
And me!
• Example:
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• #para1 {
• text-align: center; color: red;
•}
• </style>
• </head>
• <body>
• <p id="para1">Hello World!</p>
• <p>This paragraph is not affected by the style.</p>
• </body>
• </html>
• The class Selector
• In the example below, all HTML elements with class="center" will be red and
center- aligned:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center; color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph.</p>
</body>
</html>
The CSS class Selector
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph.</p>
<!DOCTYPE html>
<html>
<head>
Out put
• Red and center-aligned heading
• Red and center-aligned paragraph.
Selector 1
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p.center {
• text-align: center;
• color: red;
• }
• </style>
• </head>
• <body>
• </body>
• </html>
Out put
• This heading will not be affected
• This paragraph will be red and center-aligned.
The CSS Universal Selector
The universal selector (*) selects all HTML element on the page.
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>
• <!DOCTYPE html>
• <html>
• <body>
• <h1 style="color: blue; margin-left: 30px ;"> This is a heading</h1>
• <p>This is a paragraph.</p>
• </body>
• </html>
• Internal Style Sheet
•An internal style sheet may be used if one single page has a unique style.
•Internal styles are defined within the <style> element, inside the <head> section of
an HTML page:
• Example:
<!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>
• External Style Sheet
•With an external style sheet, you can change the look of an entire website by changing just one file!
•Each page must include a reference to the external style sheet file inside the
• <link> element. The <link> element goes inside the <head> section:
Example:
<!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. The file should not contain any html tags. The style
• Generally speaking we can say that all the styles 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 (inside a specific HTML element) has the highest priority,
which means that it will override a style defined inside the <head> tag, or in
an external style sheet, or a browser default value.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
OUT PUT
This is a heading
This is a paragraph.
Internal CSS
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>
OUT PUT
This is a heading
This is a paragraph.
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:
PROGRAM
<!DOCTYPE html>
<html>
<body>
</body>
</html>
out put
This is a heading
This is a paragraph.
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
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.
h1 {
Assume that an external style sheet has the following style for the <h1>
element:
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":
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The style of this document is a combination of an
external stylesheet, and internal style</p>
</body>
</html>
OUT PUT
This is a heading
• The style of this document is a combination of an external
stylesheet, and internal style
Example
However, if the internal style is defined before the link to the external style sheet,
the <h1> elements will be "navy":
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>The style of this document is a combination of an external stylesheet,
and internal style</p>
</body>
</html>
OUT PUT
This is a heading
The style of this document is a combination of an external stylesheet
and internal style
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:
Inline style (inside an HTML element)
External and internal style sheets (in the head section)
Browser default
So, an inline style has the highest
priority, and will override external
and internal styles and browser
defaults.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
body {background-color: linen;}
</style>
</head>
<body style="background-color: lavender">
Here, the background color of the page is set with inline CSS, and also with an
internal CSS, and also with an external CSS.
Try experimenting by removing styles to see how the cascading stylesheets
work (try removing the inline CSS first, then the internal, then the external).
CSS Box Model
• All HTMIn CSS, the term "box model" is used when talking about design and layout.
• The CSS box model is essentially a box that wraps around every HTML element. It
consists of: margins, borders, padding, and the actual content. The image below
illustrates the box model:
• Explanation of the different parts:
•Content - The content of the box, where text and images appear
• Padding - Clears an area around the content. The padding is
transparent
• Border - A border that goes around the padding and content
• Margin - Clears an area outside the border. The margin is transparent
• The box model allows us to add a border around elements, and to define
space between elements.
Margin
Border
padding
Border
Content
Margin
• Css:margin property is used to define the space around elements.It is completely
transparent and doesn’t have any background color.
• Top,Bottom,Left and right margin can be changed independently using separate
properties.
• PROPERTIES:
• margin-bottom
• Margin-top
• Margin-left
• Margin-right
• MARGIN-BOTTOM:-the margin bottom property sets the width of the margin on the
bottom of an element.
• MARGIN-TOP:-margin-top css property sets the margin area an the top of an
element.
• Margin_left:-sets the left margin of an element.
• Margin-right:-sets the margin area on the right side of an element.
• Example:-
• Body{
• color:#000000;
• background-color:#ffffff;
P{
margin-top:40px;
margin-bottom:30px;
margin-left:20px;
margin-right:20px;
margin-style:solod;
Border-style:solod;
Border-color:#000000;
}
Padding:-padding property allows you to specify how much space appear between
the control of an element and its border:
Td{padding:5px;}
Padding-bottom
Padding-top
Padding-left
Padding-right
Padding-bottom:-
The padding-bottom property sets the bottom padding of an elements.
Padding-left:-Sets the width of the padding area to the left of an element.
Padding-right:-sets the width of the padding area to the right of an element.
Padding-top:-sets the height of the padding area on the top of an element.
Example:
H2{
padding-top:20px;
padding-right:30px;
padding-bottom:30px;
padding-left:20px;
}
• Dimensions:
• The following property that allows you to control the dimensions of a box.
• Hight:-sets the height of a box
• Width:-sets the height of aline of text
• P.one{
Line_height:16px;}
p-.two{
line_-height:28px;}
Max-height:-sets a maximum height for a box.
max-height:-sets the minimum height for a box.
Div{min-height:50px;
max-height:200px;
Padding:5px
Border:1px solid#000000;}
• Max width:
• sets the maximum width of a box
• Min width:
• sets the minimum width of a box
Div{
min-height:200px;
max-height:500px;
Padding:5px;
Border:1px solid#000000;
}
Positioning
• The position css property sets how an element is positioned in a document.
• The top ,Right, Bottom and left properties determine the final location of
positioned elements.
Bottom:
• When position is set to absolute or fixed,the bottom property specifies the
distance between elements bottom edge and bottom edge of its containing
block.
Dip:
• The dip css property defines a visible postion of an element.
Left:
• The left property affects horizontal position of a positioned element. this
property has no effect on non positioned elements.
Overflaw:
• Overflow property specifies what should happen if content overflows an
element’s box.
• overflow: visible|hidden|scroll|auto| initial|inherit;
Right:
• The right property specifies distance between elements edge and the right
edge of its containing block.
Top:
• The top css property participates in specifying the vertical position of a
positioned element.
Vertical align:
• To vertically align an inline elements box inside its containing line box. For
example: it could be used to vertically position an image in a line of text.
Z-Index:
• Z-index property is used to displace elements on the z-axis . i.e. in or out of
the screen.
Tab
le
• The table layout allows browsers to speed up layout of a table by using first
width properties it come across for the rest of a column rather than having to
load the whole table before rendering it .
• Properties that are commonly used with the table <table>,<td>,and <th>
elements include the following.
Padding
Border
Text and font
Vertical align
Width
Background –color
Background-image
Border-collapse:
• Indicates whether the browser should control the appearance of adjacent
borders that touch each other or whether each cell should maintain its style.
• Properties:
Collapse
Separate
Ex: #table1 {
border-collapse: separate;
}
#table2 {
border-collapse: collapse;
}
Border-spacing:
• Specifies the width that should appear between table cells.
#table {
border-collapse: separate;
border-spacing:15px;
}
Caption side:
• specifies which side of a table that caption should appear on
Possible values:
Top
Right
Bottom
Left
Syntax:
caption{ caption-side:bottom }
Empty cells:
• Specifies whether the border should be shown if a cell is empty.
Values:
Show
Hide
Inherit
Syntax:
table{
background-color:#efefef;
width:350px;
border-collapse: separate;
empty –cells: hide:
}
Program:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists
of: borders, padding, margins, and the actual content.</p>
<div>This text is the content of the box. We have added a 50px padding, 20px margin and
a 15px green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.</div>
</body>
</html>
Output
Demonstrating the Box Model
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• a{
• color: hotpink;
• }
• </style>
• </head>
• <body>
• <h2>CSS Links</h2>
• <p><b><a href="default.asp" target="_blank">This is a
link</a></b></p>
Output
CSS Links
This is a link
In addition, links can be styled differently depending on what state they are in.
The four links states are:
a:link - a normal, unvisited link
a:visited - a link the user has visited
a:hover - a link when the user mouses over it
a:active - a link the moment it is clicked
Program:
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
<h2>CSS Links</h2>
<p><b><a href="default.asp" target="_blank">This is a link</a></b></p>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in
order to be effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to
be effective.</p>
</body>
</html>
output
CSS Links
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.
Note: a:active MUST come after a:hover in the CSS definition in order to be effective.
When setting the style for several link states, there are some order rules:
a:hover MUST come after a:link and a:visited
a:active MUST come after a:hover
Text Decoration
The text-decoration property is mostly used to remove underlines from links:
Program:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
</style>
</head>
<body>
<h2>CSS Links</h2>
<p><b><a href="default.asp" target="_blank">This is a link</a></b></p>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition
in order to be effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to
be effective.</p>
</body>
</html>
Output
• CSS Links
• This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS
definition in order to be effective.
Note: a:active MUST come after a:hover in the CSS definition
in order to be effective.
Background color
• The background-color property can be used to specify a background
color for links:
Program:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
</style>
</head>
<body>
<h2>CSS Links</h2>
<p><b><a href="default.asp" target="_blank">This is a link</a></b></p>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in
order to be effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to
be effective.</p>
</body>
</html>
Output
• CSS Links
This is a link
Note: a:active
Note: a:hover MUST
MUST come after
come after a:link and a:hover in the
a:visited in the CSS CSS definition in
definition in order to order to be
be effective. effective.
Link Buttons
<h2>Link Button</h2>
<p>A link styled as a button:</p>
<a href="default.asp" target="_blank">This is a link</a>
</body>
</html>
Output
Link Button
A link styled as a button:
This is a link
Program:
• This example demonstrates how to add other styles to hyperlinks:
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• a.one:link {color:#ff0000;}
• a.one:visited {color:#0000ff;}
• a.one:hover {color:#ffcc00;}
• a.two:link {color:#ff0000;}
• a.two:visited {color:#0000ff;}
• a.two:hover {font-size:150%;}
• a.three:link {color:#ff0000;}
• a.three:visited {color:#0000ff;}
• a.three:hover {background:#66ff66;}
• a.four:link {color:#ff0000;}
•a.four:visited {color:#0000ff;}
•a.four:hover {font-family:monospace;}
•a.five:link {color:#ff0000;text-decoration:none;}
•a.five:visited {color:#0000ff;text-decoration:none;}
•a.five:hover {text-decoration:underline;}
•</style>
•</head>
•<body>
•</body>
•</html>
Output
a:hover, a:active {
background-color: green;
color: white;
}
</style>
</head>
<body>
</body>
</html>
Output
This is a link
Program:
This example demonstrates the different types of cursors (can be useful for links):
<!DOCTYPE html>
<html>
<body>
<p>Mouse over the words to change the cursor.</p>
<span style="cursor:auto">auto</span><br>
<span style="cursor:crosshair">crosshair</span><br>
<span style="cursor:default">default</span><br>
<span style="cursor:e-resize">e-resize</span><br>
<span style="cursor:help">help</span><br>
<span style="cursor:move">move</span><br>
<span style="cursor:n-resize">n-resize</span><br>
<span style="cursor:ne-resize">ne-resize</span><br>
<span style="cursor:nw-resize">nw-resize</span><br>
<span style="cursor:pointer">pointer</span><br>
<span style="cursor:progress">progress</span><br>
<span style="cursor:s-resize">s-resize</span><br>
<span style="cursor:se-resize">se-resize</span><br>
<span style="cursor:sw-resize">sw-resize</span><br>
<span style="cursor:text">text</span><br>
<span style="cursor:w-resize">w-resize</span><br>
<span style="cursor:wait">wait</span><br>
</body>
</html>
Output
Mouse over the words to change the cursor.
auto
crosshair
default
e-resize
help
move
n-resize
ne-resize
nw-resize
pointer
progress
s-resize
se-resize
sw-resize
text
w-resize
wait
CSS Lists
oCoffee 1.Coffee
oTea 2.Tea
Coffee i. Coffee
<!DOCTYPE html>
The list-style-type property specifies the type of list item marker.
The following example shows some of the available list item markers:
Program:
<html>
<head>
<style>
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
</style>
</head>
<body>
<h2>Lists</h2>
<p>Example of unordered lists:</p>
<ul class="a">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="b">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ol class="d">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
</body>
</html>
Output
Example of unordered lists:
o Coffee
o Tea
o Coca Cola
Coffee
Tea
Coca Cola
Example of ordered lists:
I. Coffee
II. Tea
III. Coca Cola
a. Coffee
b. Tea
c. Coca Cola
Position The List Item
Markers
The list-style-position property specifies the position of the list-
item markers (bullet points).
Coca-cola
Tea
Coca-cola
Program:
<!DOCTYPE html>
<html>
<head>
<style>
ul.a {
list-style-position: outside;
}
ul.b {
list-style-position: inside;
}
</style>
</head>
<body>
<h1>The list-style-position Property</h1>
<h2>list-style-position: inside:</h2>
<ul class="b">
<li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds
of berries from the Coffea plant</li>
<li>Tea - An aromatic beverage commonly prepared by pouring hot or boiling water
over cured leaves of the Camellia sinensis, an evergreen shrub (bush) native to Asia</li>
<li>Coca Cola - A carbonated soft drink produced by The Coca-Cola Company. The
drink's name refers to two of its original ingredients, which were kola nuts (a source of
caffeine) and coca leaves</li>
</ul>
</body>
</html>
Output
The list-style-position Property
list-style-position: outside (default):
Coffee - A brewed drink prepared from roasted coffee beans,
which are the seeds of berries from the Coffea plant
Tea - An aromatic beverage commonly prepared by pouring hot or
boiling water over cured leaves of the Camellia sinensis, an evergreen
shrub (bush) native to Asia
Coca Cola - A carbonated soft drink produced by The Coca-Cola
Company. The drink's name refers to two of its original ingredients,
which were kola nuts (a source of caffeine) and coca leaves
list-style-position: inside:
Coffee - A brewed drink prepared from roasted coffee beans, which
are the seeds of berries from the Coffea plant
evergreen shrub (bush) native to Asia
Coca Cola - A carbonated soft drink produced by The Coca-Cola
Company. The drink's name refers to two of its original ingredients,
which were kola nuts (a source of caffeine) and coca leaves
An Image as The List Item Marker
The list-style-image property specifies an image as the list
item marker:
Program:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-image: url('sqpurple.gif');
}
</style>
</head>
<body>
<h2>CSS Lists</h2>
<p>The list-style-image property specifies an image as the list item marker:</p>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Output
CSS Lists
The l i s t - s t y l e - i m a g e property specifies an image as the list
item marker:
Coffee
Tea
Coca Cola
List - Shorthand property
The list-style property is a shorthand property. It is used to set
all the list properties in one declaration:
Program:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style: square inside url("sqpurple.gif");
}
</style>
</head>
<body>
<h2>CSS Lists</h2>
<p>The list-style property is a shorthand property, which is used to set all the
list properties in one declaration.</p>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Output
CSS Lists
The list-style property is a shorthand property, which is used to set all the list
properties in one declaration.
Coffee
Tea
Coca Cola
Styling List With Colors
We can also style lists with colors, to make them look a little more interesting.
Anything added to the <ol> or <ul> tag, affects the entire list, while properties added to
the <li> tag will affect the individual list items:
Program:
ol {
background: #ff9999;
padding: 20px;
}
ul {
background: #3399ff;
padding: 20px;
}
ol li {
padding: 5px;
margin-left: 35px;
}
ul li {
background: #cce5ff;
margin: 5px;
}
Result:
Coffee
Tea
Coca Cola
Coffee
Tea
Coca Cola
Css
tables
The look of an HTML table can be greatly improved with CSS:
Output
Add a border to a table:
Firstna Lastna
me me
Peter Griffin
Lois Griffin
Program:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th> First name </th>
<th>Last name </th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
Output
Add a border to a table:
Firstna Lastna
me me
Peter Griffin
Lois Griffin
Full- width table
<table>
First name
output Last name
Peter Griffin
Lois Griffin
Double
Borders
Notice that the
table in the
examples above
have double
borders. This is
because both the
table and the
<th> and <td>
elements have
To remove double borders, take a
look at the example below.
Collapse Table
Borders
• The border-collapse property sets whether the table borders
should be collapsed into a single border:
Firstname Lastname
Peter Griffin
Lois Griffin
Program:
<!DOCTYPE html>
<html> <tr>
<th>Firstname</th>
<head>
<th>Lastname</th>
<style> </tr>
table, td, th { <tr>
border: 1px solid black; <td>Peter</td>
<td>Griffin</td>
}
</tr>
<tr>
table { <td>Lois</td>
width: 100%; <td>Griffin</td>
</tr>
border-collapse: collapse;
</table>
}
</style> </body>
</head> </html>
<body>
Firstname Lastname
peter Graffin
Lois Graffin
If you only want a border around the table, only
specify the border property for <table>:
Firstname Lastname
Peter Graffin
Lios Graffin
Program:
<!DOCTYPE html>
<td>Griffin</td>
<html>
</tr>
<head>
<tr>
<style>
<td>Lois</td>
table {
<td>Griffin</td>
width: 100%;
</tr>
border-collapse: collapse;
</table>
border: 1px solid black;
}
</body>
</style>
</html>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
Output
Firstname Lastname
Peter Graffin
Lios Graffin
CSS Table Size
Table Width and Height
</body>
</html>
Output
The width and height Properties
Set the width of the table, and the height of the table header row:
To create a table that should only span half the
page, use width: 50%:
</body>
</html>
Output
CSS Table Alignment
Horizontal Alignment
• The text-align property sets the
horizontal alignment (like left, right, or
center) of the content in <th> or <td>.
• By default, the content of <th>
elements are center-aligned and the
content of <td> elements are left-
aligned.
• To center-align the content of <td>
elements as well, use text-align: center:
Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Program:
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<!DOCTYPE html> <th>Savings</th>
<html> </tr>
<head>
<tr>
<style>
table, td, th { <td>Peter</td>
border: 1px solid black; <td>Griffin</td>
} <td>$100</td>
</tr>
table { <tr>
border-collapse: collapse; <td>Lois</td>
width: 100%; <td>Griffin</td>
}
<td>$150</td>
td { </tr>
text-align: center; <tr>
} <td>Joe</td>
</style> <td>Swanson</td>
</head> <td>$300</td>
<body> </tr>
<tr>
<h2>The text-align Property</h2>
<p>This property sets the horizontal alignment (like left, right, or center)
of the content in th or td.</p>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Output
The text-align Property
This property sets the horizontal alignment (like left, right, or center) of the
content in th or td.
<table>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Output
The text-align Property
This property sets the horizontal alignment (like left, right, or center) of the content in th
or td.
</body>
</html>
Output
The vertical-align Property
This property sets the vertical alignment
(like top, bottom, or middle) of the content in th
or td.
By default, the vertical alignment of the content in a table is middle (for both <th> and
<td> elements).
The following example sets the vertical text alignment to bottom for <td> elements:
Firstnam Lastnam
Savings
e e
Peter Griffin $100
</body>
</html>
Output
The vertical-align Property
This property sets the vertical alignment (like top,
bottom, or middle) of the content in th or td.
Firstname Lastname Savings
Table Padding
To control the space between the border and the content in a table,
use the padding property on <td> and <th> elements:
</body>
</html>
Output
The padding Property
This property adds space between the
border and the content in a table.
Firstname Lastname Savings
border-
Joe Swanson $300
Add the
</body>
</html>
</body>
</html>
Output
Hoverable Table
Move the mouse over the table rows to see the
effect.
First Name Last Name Points
</body>
</html>
Output
Striped Table
</body>
</html>
Colored Table
Output
Header
Firstname Lastname Savings
p.ex3 {
border: 2px solid black;
outline-style: outset;
outline-color: grey;
}
Output:
CSS Outline Shorthand
</body>
</html>
Output
The outline Property
The look of an HTML table can be greatly improved with CSS:
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #04AA6D;
color: white;
}
</style>
</head>
<body>
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>North/South</td>
<td>Simon Crowther</td>
<td>UK</td>
</tr>
<tr>
<td>Paris spécialités</td>
<td>Marie Bertrand</td>
<td>France</td>
</tr>
</table>
</body>
</html>
Output:
Lois Griffin
Program:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
Full- width table
table {
width: 100%;
}
</style>
</head>
<body>
<h2>Full-width Table</h2>
<table>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
output
First name Last name
Peter Griffin
Lois Griffin
Program:
When an <input type="text"> gets focus, gradually change the
width from 100px to 250px:
Search: <input type="text"
<!DOCTYPE html>
name="search">
<html>
<head>
</body>
<style>
</html>
input[type=text] {
width: 100px;
-webkit-transition: width .35s ease-in-out;
transition: width .35s ease-in-out;
}
input[type=text]:focus {
width: 250px;
}
</style>
</head>
<body>
<h1>The width Property</h1>
<p>Set the width of the input field to 100 pixels. However, when the input
Output
The width
Property
Set the width of the input field to 100 pixels. However, when the
input field gets focus, make it 250 pixels wide:
Search:
Focus
Program:
<!DOCTYPE html>
<html> Last name: <input type="text"
name="lastname">
<head> </form>
<style>
input:focus { </body>
background-color: yellow; </html>
}
</style>
</head>
<body>
<form>
First name: <input type="text" name="firstname"><br>
Definition and Usage
The :focus selector is used to select the element that has focus.
Tip: The :focus
selector is allowed on
elements that accept
keyboard events or other
user inputs.
Browser Support
The numbers in the table specifies
the first browser version that fully
Selector
Selector
<p>Set the width of the input field to 100 pixels. However, when the input
Output
The width
Property
Set the width of the input field to 100 pixels. However, when the
input field gets focus, make it 250 pixels wide:
Search:
Active
Program:
<!DOCTYPE html>
<html> <p><b>Note:</b> The :active selector styles
the active link.</p>
<head>
<style> </body>
a:active { </html>
background-color: yellow;
}
</style>
</head>
<body>
<a href="https://www.w3schools.com">w3schools.com</a>
<a href="http://www.wikipedia.org">wikipedia.org</a>
Output
w3schools.comwikipedia.org
Selector
<!DOCTYPE html>
<html>
<head>
<style>
.alias {cursor: alias;}
.all-scroll {cursor: all-scroll;}
.auto {cursor: auto;}
.cell {cursor: cell;}
.context-menu {cursor: context-menu;}
.col-resize {cursor: col-resize;}
.copy {cursor: copy;}
.crosshair {cursor: crosshair;}
.default {cursor: default;}
.e-resize {cursor: e-resize;}
.ew-resize {cursor: ew-resize;}
.grab {cursor: -webkit-grab; cursor: grab;}
.grabbing {cursor: -webkit-grabbing; cursor: grabbing;}
.help {cursor: help;}
.move {cursor: move;}
.n-resize {cursor: n-resize;}
.ne-resize {cursor: ne-resize;}
.nesw-resize {cursor: nesw-resize;}
.ns-resize {cursor: ns-resize;}
.nw-resize {cursor: nw-resize;}
.nwse-resize {cursor: nwse-resize;}
.no-drop {cursor: no-drop;}
.none {cursor: none;}
.not-allowed {cursor: not-allowed;}
.pointer {cursor: pointer;}
.progress {cursor: progress;}
.row-resize {cursor: row-resize;}
.s-resize {cursor: s-resize;}
.se-resize {cursor: se-resize;}
.sw-resize {cursor: sw-resize;}
.text {cursor: text;}
.url {cursor: url(myBall.cur),auto;}
.w-resize {cursor: w-resize;}
.wait {cursor: wait;}
.zoom-in {cursor: zoom-in;}
.zoom-out {cursor: zoom-out;}
</style>
</head>
<body>
<h1>The cursor Property</h1>
<p>Mouse over the words to change the mouse cursor.</p>
<p class="none">none</p>
<p class="alias">alias</p>
<p class="not-allowed">not-allowed</p>
<p class="all-scroll">all-scroll</p>
<p class="pointer">pointer</p>
<p class="auto">auto</p>
<p class="progress">progress</p>
<p class="cell">cell</p>
<p class="context-menu">context-menu</p> <p class="row-resize">row-resize</p>
<p class="s-resize">s-resize</p>
<p class="col-resize">col-resize</p>
<p class="se-resize">se-resize</p>
<p class="copy">copy</p>
<p class="sw-resize">sw-resize</p>
<p class="crosshair">crosshair</p>
<p class="text">text</p>
<p class="default">default</p>
<p class="url">url</p>
<p class="e-resize">e-resize</p>
<p class="w-resize">w-resize</p>
<p class="ew-resize">ew-resize</p>
<p class="wait">wait</p>
<p class="grab">grab</p>
<p class="zoom-in">zoom-in</p>
<p class="grabbing">grabbing</p>
<p class="zoom-out">zoom-out</p>
<p class="help">help</p>
<p class="move">move</p>
</body>
<p class="n-resize">n-resize</p>
</html>
<p class="ne-resize">ne-resize</p>
<p class="nesw-resize">nesw-resize</p>
<p class="ns-resize">ns-resize</p>
<p class="nw-resize">nw-resize</p>
<p class="nwse-resize">nwse-resize</p>
<p class="no-drop">no-drop</p>
Output
The cursor Property
Mouse over the words to change the mouse cursor.
alias
all-scroll ns-resize
auto nw-resize
cell nwse-resize
context-menu no-drop
col-resize none
copy not-allowed
crosshair pointer
default progress
e-resize row-resize
ew-resize s-resize
grab se-resize
grabbing sw-resize
help text
move url
n-resize w-resize
ne-resize wait
nesw-resize zoom-in
zoom-out
Value Description Play it
<h2>display: none:</h2>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at
pulvinar felis blandit. <p class="ex1">HELLO WORLD!</p> Vestibulum volutpat tellus diam,
consequat gravida libero rhoncus ut.
</div>
<h2>display: inline:</h2>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at
pulvinar felis blandit. <p class="ex2">HELLO WORLD!</p> Vestibulum volutpat tellus diam,
consequat gravida libero rhoncus ut.
</div>
<h2>display: block:</h2>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at
pulvinar felis blandit. <p class="ex3">HELLO WORLD!</p> Vestibulum volutpat tellus diam,
consequat gravida libero rhoncus ut.
</div>
<h2>display: inline-block:</h2>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at
pulvinar felis blandit. <p class="ex4">HELLO WORLD!</p> Vestibulum volutpat tellus diam,
consequat gravida libero rhoncus ut.
</div>
</body>
</html>
Output
The display Property
display: none:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam semper diam at erat pulvinar, at pulvinar felis
blandit. Vestibulum volutpat tellus diam, consequat
gravida libero rhoncus ut.
display: inline:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam semper diam at erat pulvinar, at pulvinar felis
blandit. HELLO WORLD!
Vestibulum volutpat tellus diam, consequat gravida libero
rhoncus ut.
display: block:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam semper diam at erat pulvinar, at pulvinar felis
blandit. HELLO WORLD!
The visibility property
<!DOCTYPE html>
<html> <p>Notice that the hidden heading still
<head> takes up space on the page.</p>
<style>
h2.a { </body>
visibility: visible; </html>
}
h2.b {
visibility: hidden;
}
</style>
</head>
<body>
<h2>position: static;</h2>
<p>An element with position: static; is not positioned in any special way; it is
always positioned according to the normal flow of the page:</p>
<div class="static">
This div element has position: static;
</div>
Output:
position: static;
A fixed element
Notice the fixed
does not leave a
element in the
gap in the page
lower-right
where it would
corner of the
normally have
page.
been located.
<h2>position: fixed;</h2>
Output
• position: fixed;
• An element with position: fixed; is positioned relative to the
viewport, which means it always stays in the same place even if the
page is scrolled:
• This div element has position: fixed;
Note: Absolute
positioned
elements are
removed from the
normal flow, and
can overlap
elements.
}
</style>
</head>
Program: <body>
<h2>position: absolute;</h2>
<!DOCTYPE html>
<html> <p>An element with position: absolute; is positioned
<head> relative to the nearest positioned ancestor (instead of
<style> positioned relative to the viewport, like fixed):</p>
div.relative {
position: relative; <div class="relative">This div element has position:
width: 400px; relative;
height: 200px; <div class="absolute">This div element has position:
border: 3px solid #73AD21;
absolute;</div>
}
</div>
div.absolute {
position: absolute; </body>
top: 80px; </html>
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
Output
• position: absolute;
• An element with position: absolute; is positioned
relative to the nearest positioned ancestor (instead of
positioned relative to the viewport, like fixed):
This div
element
has
position:
relative;
position: sticky;
• An element with position: sticky; is positioned based on the
user's scroll position.
<p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo,
maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam
omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no
molestiae voluptatibus.</p>
</div>
</body>
</html>
Output
Try to scroll inside this frame to understand how sticky positioning works.
I am sticky!
In this example, the sticky element sticks to the top of the page (top: 0), when you reach its
scroll position.
Scroll back up to remove the stickyness.
Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset
concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis
evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no
molestiae voluptatibus.
Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset
concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis
evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no
molestiae voluptatibus.
• When elements are positioned, they can overlap other elements.
Overlapping Elements
• The z-index property specifies the stack order of an element (which element
should be placed in front of, or behind, the others).
• An element can have a positive or negative stack order:
Program:
<!DOCTYPE html>
<img src="w3css.gif" width="100"
<html>
height="140">
<head> <p>Because the image has a z-index of -
<style> 1, it will be placed behind the text.</p>
img {
</body>
position: absolute; </html>
left: 0px;
top: 0px;
z-index: -1;
}
</style>
</head>
<body>
An element with greater stack
order is always in front of an
element with a lower stack order.
Note: If two positioned elements overlap without a z-
index specified, the element positioned last in the HTML code will be
shown on top.
Navagation:
• Basic Navigation
• The w3-bar class is a container for displaying HTML elements
horizontally.
• The w3-bar-item class defines the container elements.
• It is a perfect tool for creating navigation bars:
• HomeLink 1Link 2Link 3
Program:
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-container">
<h2>Navigation Bars</h2>
<p>The <strong>w3-bar</strong> class is a container for displaying
HTML elements horizontally.</p>
<p>The <strong>w3-bar-item</strong> class defines the container
elements.</p>
<p>It it a perfect tool for creating navigation bars:</p>
</div>
<div class="w3-bar w3-black">
<a href="#" class="w3-bar-item w3-button">Home</a>
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#" class="w3-bar-item w3-button">Link 3</a>
</div>
</body>
</html>
Output
Navigation Bars
The w3-bar class is a container for displaying HTML elements horizontally.
The w3-bar-item class defines the container elements.
It it a perfect tool for creating navigation bars:
<h2>Active/Current Link</h2>
<p>Add a w3-color class, or the w3-text-color class to the current link to let the user know
which page he/she is on:</p>
</div>
</body>
</html>
CSS FORMS
<!DOCTYPE html> cursor: pointer;
<html> }
<style>
input[type=text], select { input[type=submit]:hover {
width: 100%; background-color: #45a049;
padding: 12px 20px; }
margin: 8px 0;
display: inline-block; div {
border: 1px solid #ccc; border-radius: 5px;
border-radius: 4px; background-color: #f2f2f2;
box-sizing: border-box; padding: 20px;
} }
</style>
input[type=submit] { <body>
width: 100%;
background-color: #4CAF50; <h3>Using CSS to style an HTML Form</h3>
color: white;
padding: 14px 20px; <div>
margin: 8px 0; <form action="/action_page.php">
border: none; <label for="fname">First Name</label>
border-radius: 4px;
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="country">Country</label>
<select id="country" name="country">
<option value="australia">Australia</option>
<option value="canada">Canada</option>
<option value="usa">USA</option>
</select>
</body>
</html>
OUTPUT
• Using CSS to style an HTML Form
• FIRST NAME
YOUR NAME
• LAST NAME
• COUNTRY
YOUR LAST NAME
INDIA
SUBMIT
Font
TEXT