Unit 2 CSS
Unit 2 CSS
CSS
Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to
simplify the process of making web pages presentable.
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.
CSS is easy to learn and understand but it provides powerful control over the presentation of
an HTML document. Most commonly, CSS is combined with the markup languages HTML or
XHTML.
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.
Easy maintenance − To make a global change, simply change the style, and all
elements in all the web pages will be updated automatically.
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.
Who Creates and Maintains CSS?
CSS is created and maintained through a group of people within the W3C called the CSS
Working Group. The CSS Working Group creates documents called specifications. When a
specification has been discussed and officially ratified by the W3C members, it becomes a
recommendation.
These ratified specifications are called recommendations because the W3C has no control
over the actual implementation of the language. Independent companies and organizations
create that software.
NOTE − The World Wide Web Consortium, or W3C is a group that makes recommendations
about how the Internet works and how it should evolve.
CSS Versions
Cascading Style Sheets level 1 (CSS1) came out of W3C as a recommendation in
December 1996. This version describes the CSS language as well as a simple visual
formatting model for all the HTML tags.
CSS2 became a W3C recommendation in May 1998 and builds on CSS1. This version adds
support for media-specific style sheets e.g. printers and aural devices, downloadable fonts,
element positioning and tables.
CSS Syntax
A CSS comprises of style rules that are interpreted by the browser and then applied to the
corresponding elements in your document. A style rule is made of three parts −
Selector − A selector is an HTML tag at which a style will be applied. This could be
any tag like <h1> or <table> etc.
Property − A property is a type of attribute of HTML tag. Put simply, all the HTML
attributes are converted into CSS properties. They could be color, border etc.
Value − Values are assigned to properties. For example, color property can have
value either red or #F1F1F1 etc.
You can put CSS Style Rule Syntax as follows −
selector { property: value }
This rule renders the content of every element in our document in black.
The Descendant Selectors
Suppose you want to apply a style rule to a particular element only when it lies inside a
particular element. As given in the following example, style rule will apply to <em> element
only when it lies inside <ul> tag.
ul em {
color: #000000;
}
The Class Selectors
You can define style rules based on the class attribute of the elements. All the elements
having that class will be formatted according to the defined rule.
.black {
color: #000000;
}
This rule renders the content in black for every element with class attribute set to black in our
document. You can make it a bit more particular. For example −
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class attribute set to black.
You can apply more than one class selectors to given element. Consider the following
example −
<p class = "center bold">
This para will be styled by the classes center and bold.
</p>
The ID Selectors
You can define style rules based on the id attribute of the elements. All the elements having
that id will be formatted according to the defined rule.
#black {
color: #000000;
}
This rule renders the content in black for every element with id attribute set to black in our
document. You can make it a bit more particular. For example −
h1#black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with id attribute set to black.
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.
The Child Selectors
You have seen the descendant selectors. There is one more type of selector, which is very
similar to descendants but have different functionality. Consider the following example −
body > p {
color: #000000;
}
This rule will render all the paragraphs in black if they are direct child of <body> element.
Other paragraphs put inside other elements like <div> or <td> would not have any effect of
this rule.
The advantage to this method is that the <input type = "submit" /> element is unaffected, and
the color applied only to the desired text fields.
There are following rules applied to attribute selector.
p[lang] − Selects all paragraph elements with a lang attribute.
p[lang="fr"] − Selects all paragraph elements whose lang attribute has a value of
exactly "fr".
p[lang~="fr"] − Selects all paragraph elements whose lang attribute contains the
word "fr".
p[lang|="en"] − Selects all paragraph elements whose lang attribute contains values
that are exactly "en", or begin with "en-".
Multiple Style Rules
You may need to define multiple style rules for a single element. You can define these rules
to combine multiple properties and corresponding values into a single block as defined in the
following example −
h1 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Here all the property and value pairs are separated by a semicolon (;). You can keep them
in a single line or multiple lines. For better readability, we keep them in separate lines.
For a while, don't bother about the properties mentioned in the above block. These
properties will be explained in the coming chapters and you can find complete detail about
properties in CSS References
Grouping Selectors
You can apply a style to many selectors if you like. Just separate the selectors with a
comma, as given in the following example −
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
This define style rule will be applicable to h1, h2 and h3 element as well. The order of the list
is irrelevant. All the elements in the selector will have the corresponding declarations applied
to them.
You can combine the various id selectors together as shown below −
#content, #footer, #supplement {
position: absolute;
left: 510px;
width: 200px;
}
There are four ways to associate styles with your HTML document. Most commonly used
methods are inline CSS and External CSS.
media screen
tty
tv
projection
Specifies the device the document will be displayed on. Default value
handheld is all. This is an optional attribute.
print
braille
aural
all
style style rules The value of style attribute is a combination of style declarations
separated by semicolon (;).
Example
Following is the example of inline CSS based on the above syntax −
<html>
<head>
</head>
<body>
<h1 style = "color:#36C;">
This is inline CSS
</h1>
</body>
</html>
href Specifies the style sheet file having Style rules. This attribute
URL
is a required.
media screen
tty
tv
projection
Specifies the device the document will be displayed on.
handheld Default value is all. This is optional attribute.
print
braille
aural
all
Example
Consider a simple style sheet file with a name mystyle.css having the following rules −
h1, h2, h3 {
color: #36C;
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>
Imported CSS - @import Rule
@import is used to import an external stylesheet in a manner similar to the <link> element.
Here is the generic syntax of @import rule.
<head>
@import "URL";
</head>
Here URL is the URL of the style sheet file having style rules. You can use another syntax
as well −
<head>
@import url("URL");
</head>
Example
Following is the example showing you how to import a style sheet file into HTML document −
<head>
@import "mystyle.css";
</head>
CSS Rules Overriding
We have discussed four ways to include style sheet rules in a an HTML document. Here is
the rule to override any Style Sheet Rule.
Any inline style sheet takes highest priority. So, it will override any rule defined in
<style>...</style> tags or rules defined in any external style sheet file.
Any rule defined in <style>...</style> tags will override rules defined in any external
style sheet file.
Any rule defined in external style sheet file takes lowest priority, and rules defined in
this file will be applied only when above two rules are not applicable.
Handling old Browsers
There are still many old browsers who do not support CSS. So, we should take care while
writing our Embedded CSS in an HTML document. The following snippet shows how you
can use comment tags to hide CSS from older browsers −
<style type = "text/css">
<!--
body, td {
color: blue;
}
-->
</style>
CSS Comments
Many times, you may need to put additional comments in your style sheet blocks. So, it is
very easy to comment any part in style sheet. You can simple put your comments inside
/*.....this is a comment in style sheet.....*/.
You can use /* ....*/ to comment multi-line blocks in similar way you do in C and C++
programming languages.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is a multi-line comment */
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
CSS - Colors
CSS uses color values to specify a color. Typically, these are used to set a color either for
the foreground of an element (i.e., its text) or else for the background of the element. They
can also be used to affect the color of borders and other decorative effects.
You can specify your color values in various formats. Following table lists all the possible
formats −
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#C0C0C0
#FFFFFF
#000
#F00
#0F0
#0FF
#FF0
#0FF
#F0F
#FFF
rgb(0,0,0)
rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,255,0)
rgb(0,255,255)
rgb(255,0,255)
rgb(192,192,192)
rgb(255,255,255)
CSS - Backgrounds
You can set the following background properties of an element −
The background-color property is used to set the background color of an element.
The background-image property is used to set the background image of an element.
The background-repeat property is used to control the repetition of an image in the
background.
The background-position property is used to control the position of an image in the
background.
The background-attachment property is used to control the scrolling of an image in
the background.
The background property is used as a shorthand to specify a number of other
background properties.
Set the Background Color
Following is the example which demonstrates how to set the background color for an
element.
Live Demo
<html>
<head>
</head>
<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>Tutorials point</p>
</body>
</html>
<body>
<p>Tutorials point</p>
</body>
</html>
<body>
<p>Tutorials point</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>
<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>
<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>
Shorthand Property
You can use the background property to set all the background properties at once. For
example −
<p style = "background:url(/images/pattern1.gif) repeat fixed;">
This parapgraph has fixed repeated background image.
</p>
CSS - Fonts
You can set following font properties of an element −
The font-family property is used to change the face of a font.
The font-style property is used to make a font italic or oblique.
The font-variant property is used to create a small-caps effect.
The font-weight property is used to increase or decrease how bold or light a font
appears.
The font-size property is used to increase or decrease the size of a font.
The font property is used as shorthand to specify a number of other font properties.
Set the Font Family
Following is the example, which demonstrates how to set the font family of an element.
Possible value could be any font family name.
Live Demo
<html>
<head>
</head>
<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>
<body>
<p style = "font-stretch:ultra-expanded;">
If this doesn't appear to work, it is likely that your computer
doesn't have a <br>condensed or expanded version of the font being used.
</p>
</body>
</html>
Shorthand Property
You can use the font property to set all the font properties at once. For example −
Live Demo
<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>
CSS - Text
You can set following text properties of an element −
The color property is used to set the color of a text.
The direction property is used to set the text direction.
The letter-spacing property is used to add or subtract space between the letters that
make up a word.
The word-spacing property is used to add or subtract space between the words of a
sentence.
The text-indent property is used to indent the text of a paragraph.
The text-align property is used to align the text of a document.
The text-decoration property is used to underline, overline, and strikethrough text.
The text-transform property is used to capitalize text or convert text to uppercase or
lowercase letters.
The white-space property is used to control the flow and formatting of text.
The text-shadow property is used to set the text shadow around a text.
Set the Text Color
The following example demonstrates how to set the text color. Possible value could be any
color name in any valid format.
Live Demo
<html>
<head>
</head>
<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:5px;">
This text is having space between words.
</p>
</body>
</html>
This will produce following result −
<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>
<body>
<p style = "white-space:pre;">
This text has a line break and the white-space pre setting
tells the browser to honor it just like the HTML pre tag.
</p>
</body>
</html>
<body>
<p style = "text-shadow:4px 4px 8px blue;">
If your browser supports the CSS text-shadow property,
this text will have a blue shadow.
</p>
</body>
</html>
<body>
<img style = "border:0px;" src = "/css/images/logo.png" />
<br />
<img style = "border:3px dashed red;" src = "/css/images/logo.png" />
</body>
</html>
<body>
<img style = "border:1px solid red; height:100px;" src = "/css/images/logo.png" />
<br />
<img style = "border:1px solid red; height:50%;" src = "/css/images/logo.png" />
</body>
</html>
<body>
<img style = "border:1px solid red; width:150px;" src = "/css/images/logo.png" />
<br />
<img style = "border:1px solid red; width:100%;" src = "/css/images/logo.png" />
</body>
</html>
<body>
<img style = "border:1px solid red; -moz-opacity:0.4; filter:alpha(opacity=40);" src =
"/css/images/logo.png" />
</body>
</html>
CSS - Links
You can set following properties of a hyper link −
We will revisit the same properties when we will discuss Pseudo-Classes of CSS.
The :link signifies unvisited hyperlinks.
The :visited signifies visited hyperlinks.
The :hover signifies an element that currently has the user's mouse pointer hovering
over it.
The :active signifies an element on which the user is currently clicking.
Usually, all these properties are kept in the header part of the HTML document.
Remember a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective. Also, a:active MUST come after a:hover in the CSS definition as follows −
<style type = "text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
Now, we will see how to use these properties to give different effects to hyperlinks.
<body>
<a href = "">Link</a>
</body>
</html>
<body>
<a href = ""> link</a>
</body>
</html>
It will produce the following link. Once you will click this link, it will change its color to green.
<body>
<a href = "">Link</a>
</body>
</html>
It will produce the following link. Now, you bring your mouse over this link and you will see
that it changes its color to yellow.
<body>
<a href = "">Link</a>
</body>
</html>
It will produce the following link. It will change its color to pink when the user clicks it.
CSS - Tables
A table is an HTML element used to display data in a structured format with rows and
columns. It is created using the <table> tag in HTML and can be styled using CSS
properties.
<head>
<style>
table {
td, th {
tr:nth-child(even){background-color: #f2f2f2;}
th {
</style>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Country</th>
</tr>
<tr>
<td>Nuha Ali</td>
<td>India</td>
</tr>
<tr>
<td>Zara Ali</td>
<td>Students Roosters</td>
<td>England</td>
</tr>
<tr>
<td>Mahira</td>
<td>Orland Park</td>
<td>Chicago</td>
</tr>
</table>
</body>
</html>
<head>
<style>
table {
border-collapse: collapse;
th, td {
</style>
<body>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Country</th>
</tr>
<tr>
<td>Nuha Ali</td>
<td>India</td>
</tr>
<tr>
<td>Zara Ali</td>
<td>Students Roosters</td>
<td>England</td>
</tr>
<tr>
<td>Mahira</td>
<td>Orland Park</td>
<td>Chicago</td>
</tr>
</table>
</body>
</html>
Setting Table Border Spacing
The border-spacing property specifies the distance that separates adjacent cells' borders in
a table. This property may be specified as either one or two values.:
border-spacing: 2px;: If one value is passed, the spacing is applied to both vertical
and horizontal borders.
border-spacing: 1cm 2em;: If two values are passed, the first value defines the
horizontal spacing between cells (i.e., the space between cells in adjacent columns),
and the second value defines the vertical spacing between cells (i.e., the space
between cells in adjacent rows).
Example
Now let's modify the previous example and see the difference −
<html>
<head>
<style>
table {
td {
</style>
<body>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Country</th>
</tr>
<tr>
<td>Nuha Ali</td>
<td>India</td>
</tr>
<tr>
<td>Zara Ali</td>
<td>Students Roosters</td>
<td>England</td>
</tr>
<tr>
<td>Mahira</td>
<td>Orland Park</td>
<td>Chicago</td>
</tr>
</table>
</body>
</html>
The border-spacing property only works when border-collapse is set to separate. If you
set border-collapse to collapse, the border-spacing property will have no effect, and the
borders will collapse into a single line.
<head>
<style>
table {
.top caption {
caption-side: top;
.bottom caption {
caption-side: bottom;
}
table {
td {
</style>
<body>
<table class="top">
<caption>
</caption>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
</tbody>
</table>
<br />
<table class="bottom">
<caption>
</caption>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
</tbody>
</table>
</body>
</html>
The caption-side property only applies to tables that have a <caption> element within
them. The <caption> element is used to provide a title or description for the table. If the
table does not have a caption, the caption-side property won't have any effect.
<head>
<style>
table {
width: 100%;
border-collapse: separate;
empty-cells: hide;
td,th {
padding: 5px;
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
In the following example the empty-cells:show property is used to show borders of empty
cells in the <table> element.
<html>
<head>
<style>
table {
width: 100%;
empty-cells: show;
td,th {
padding: 5px;
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
th, td {
padding: 8px;
text-align: center;
</style>
</head>
<body>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Short content</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
</tr>
</table>
</body>
</html>
The following example shows the use of table-layout: auto:
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
table-layout: auto;
th, td {
padding: 8px;
text-align: center;
</style>
</head>
<body>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Short content</td>
<td>Even longer content that might wrap in Column 3</td>
</tr>
<tr>
</tr>
</table>
</body>
</html>
<html>
<head>
<style>
table {
width: 600px;
height: 200px;
border-collapse: collapse;
th, td {
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
</tbody>
</table>
</body>
</html>
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
td {
text-align:center;
</style>
</head>
<body>
<h2>Text-align Property</h2>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>
Similarly, to align the text to left or right use the property text-align: left or text-align: right,
respectively.
The value text-align: justify ensures that text is aligned both on the left and right sides of
each cell, creating a clean and organized appearance.
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
td {
height: 50px;
vertical-align: middle;
</style>
</head>
<body>
<h2>Vertical-align Property</h2>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>
background-color: #f2f2f2;
}
td {
background-color: #f2f2f2;
tr {
background-color: #ffffff;
}
Example
Let us see an example:
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
td {
height: 50px;
vertical-align: middle;
text-align: center;
</style>
</head>
<body>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>
<head>
<style>
table.one {
border-collapse: collapse;
width: 400px;
th {
font-size: large;
font-family: 'Lucida Sans', sans-serif;
td {
font-size: small;
</style>
</head>
<body>
<h2>font styles</h2>
<div>
<th>Heading</th>
<tr>
<td>Cell value</td>
</tr>
<tr>
<td>Cell value</td>
</tr>
</table>
</div>
</body>
</html>
<head>
<style>
table {
border: 2px solid black;
border-collapse: collapse;
td,th {
padding: 30px;
vertical-align: middle;
height: 50px;
</style>
</head>
<body>
<h2>Padding property</h2>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>
CSS Table Margin
In order to add the margin of table or its cells, use the property margin. Refer the following
example:
<html>
<head>
<style>
table {
border-collapse: collapse;
margin-top: 50px;
td,th {
vertical-align: middle;
height: 50px;
</style>
</head>
<body>
<h2>Border property</h2>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>
CSS Table Dividers (vertical / horizontal)
In order to add vertical or horizontal dividers, you can add the property border-
right or border-bottom to the <th> and <th> elements.
Example
Let us see an example:
<html>
<head>
<style>
table {
border-collapse: collapse;
margin-top: 50px;
th {
td{
height: 50px;
</style>
</head>
<body>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>
Striped Table
In order to make the table look striped, where alternative rows are colored, you can use
the nth-child() selector and add a background-color, to all the odds / even rows of the
table.
Example
Let us see an example:
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
text-align: left;
padding: 8px;
</style>
</head>
<body>
<h2>Striped table</h2>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
</body>
</html>
Responsive Table
Responsive table refers to a table that adjusts and adapts its layout and formatting based on
different screen sizes and resolutions. It ensures that the table is easily readable and
accessible on various devices.
CSS provides features through which we can make a table responsive. You can use the
property overflow-x: auto to add a horizontal scroll bar to the table, when screen in small
and entire content is not seen.
You need to add a container element, such as <div>, with the property overflow-x:
auto around the <table> element, in order to make the table responsive.
Example
Let us see an example. You can resize your screen to see the responsiveness of the table.
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
</style>
</head>
<body>
<h2>Responsive table</h2>
<table>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 6</td>
<td>Data 7</td>
<td>Data 8</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 6</td>
<td>Data 7</td>
<td>Data 8</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 6</td>
<td>Data 7</td>
<td>Data 8</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 6</td>
<td>Data 7</td>
<td>Data 8</td>
</tr>
</table>
</div>
</body>
</html>
border-spacing With separate borders set the spacing between borders. One
value sets vertical and horizontal spacing and two values sets
horizontal and vertical spacing respectively.
CSS - Borders
The border of an HTML element is simply one or more lines that surround the content and padding of
an element. Every border has three aspects: its width, or thickness; its style, or appearance; and its
color.
CSS Borders
The CSS border properties allow you to specify how the border of the box representing an element
should look. There are three properties of a border you can change −
border-style - Specifies whether a border should be solid, dashed line, double line, or one of
the other possible values.
border-color - Specifies the color of a border. The default value is the foreground color of the
element and if element is blank, then color of the parent element
border-width - Specifies the width of a border. The default value is medium.
Now, we will see how to use these properties with examples.
Value Description
none No border
hidden A hidden border, same as 'none' except for table elements
The border-style property can have upto four values in one statement where we can specify the
border style for top, right, bottom and left border.
Example
The following example to show border-style properly with different values −
<html>
<body>
</body>
<html>
border-top-style
border-right-style
border-bottom-style
border-left-style
Example
Let us see an example:
<html>
<head>
<style>
p {
</style>
</head>
<body>
<h2>border-style (single-side)</h2>
</body>
<html>
Value Description
thin a thin border
The border-width property can have upto four values in one statement where we can specify the
border width for top, right, bottom and left border.
We should declare a border-style before declaring border-width, else the border effect will not be
seen.
Example
Let us see an example:
<html>
<body>
</body>
</html>
border-top-width
border-right-width
border-bottom-width
border-left-width
Example
Let us see an example:
<html>
<head>
<style>
p {
</style>
</head>
<body>
<h2>border-width (single-side)</h2>
</body>
<html>
Value Description
You should declare a border-style before declaring border-color, else the border color effect will not
be seen.
The border-color property can have upto four values in one statement where we can specify the border
color for top, right, bottom and left border.
Example
Let us see an example of border-color:
<html>
<body>
</body>
</html>
border-top-color
border-right-color
border-bottom-color
border-left-color
Example
Let us see an example:
<html>
<head>
<style>
p {
</style>
</head>
<body>
<h2>border-color (single-side)</h2>
</body>
</html>
border-top
border-right
border-bottom
border-left
Example
Let us see an example:
<html>
<head>
<style>
p {
padding: 10px;
</style>
</head>
<body>
</body>
</html>
<head>
<style>
p {
</style>
</head>
<body>
</body>
</html>
But you still have the option of overriding the border shorthand property with any individual
property passed exclusively. See the following sample code to clarify this point:
h2 {border: thin solid gray;}
h2 {border-bottom-width: 15px;}
The above code will have a thin, solid and gray border on all the sides except for the bottom where the
width will be of 15px.
Let us see an example:
<html>
<head>
<style>
p {
</style>
</head>
<body>
</body>
</html>
p {
}
The above code will add a rounded border around paragraph element.
Example
<html>
<head>
<style>
p {
</style>
</head>
<body>
<h2>Round Borders</h2>
</body>
</html>
<head>
<style>
strong {
background-color: silver;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
img {
}
Above code will apply a solid, green-colored, 2em wide border around the image.
Example
Let us see an example:
<html>
<head>
<style>
img {
border: 1em solid #40a944;
}
</style>
</head>
<body>
<div>
<p>Check the logo <img src="images/logo.png" alt="logo image"> with
borders altering the line height.</p>
</div>
</body>
</html>
CSS - Margins
Setting up a margin around an HTML element is one of the things that sets CSS so far
above traditional web markup. This article will teach you CSS margin property and its
constituent properties.
CSS Margin
The CSS margin property is a shorthand property to set the margin area around an HTML
element. Consider you want to set a quarter-inch margin on h1 elements then following is
the syntax:
<html>
<head>
<style>
div {
border:1px dotted
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
You can set margin using any units, whether in pixels, inches, millimeters, or ems. The
default value for margin is 0 (zero), so if you don’t set a margin value then no margin should
appear around the element. To set a margin of 20 pixels around h1 element, above code will
be written as follows:
<html>
<head>
<style>
div {
border:1px dotted
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
margin-bottom
margin-top
margin-left
margin-right
Following example demonstrates how we can set different margins around h1 elements:
<html>
<head>
<style>
div {
border:1px dotted
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
<head>
<style>
div{
border:1px dotted
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
<head>
<style>
div {
border:1px dotted
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
<head>
<style>
div {
border:1px dotted
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
We have seen that setting a single value for margin, applies margin equally to all the sides - top, right,
bottom and left. You can check very first example to understand this case.
<head>
<style>
div{
border:1px dotted
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
<head>
<style>
div {
border:1px dotted
h1 {
p {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
h1 {
</style>
</head>
<body>
</div>
</div>;
</body>
</html>
<style>
div {
border:1px dotted
strong {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
Here margin-left created some extra space before the highlited strong text. We can create
same space before and after the element as follows:
<html>
<head>
<style>
div {
border:1px dotted
strong {
</style>
</head>
<body>
<div>
<p>This text has some <strong>strong text</strong> with grey
background</p>
</div>
</body>
</html>
<head>
<style>
div {
h1 {
margin:0 auto;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
Though if you are using an old browser, then above code will not work and with modern
browser, you should use the following code:
<html>
<head>
<style>
div {
}
h1 {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
margin A shorthand property for setting all the margin properties in one
declaration
CSS - Lists
What are Lists?
Lists are useful as they present the information in a structured and organized manner. Lists
improve the readability and comprehension of content on a web page. So, if the content is
listed, it is easy to follow.
Lists are commonly used to display items, steps, options, or any other type of related
information that should be presented sequentially or in a group.
This chapter will discuss how to control list type, position, style, etc., using CSS. Before that
let us understand what are different types of lists in HTML.
HTML Lists
HTML provides mainly two kinds of lists - <ol> (ordered list) and <ul> (unordered list).
Ordered List
Ordered lists are used when the items need to be presented in a specific order marked with
numbers or letters. Following is the syntax to create HTML ordered lists:
<ol>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
Unordered List
Unordered lists are used when the items need to be presented in a specific order marked
with simple bullets. Following is the syntax to create HTML ordered lists:
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
<html>
<head>
<style>
ul.a {
list-style-type: circle;
ul.b {
list-style-type: square;
}
</style>
</head>
<body>
<ul class="a">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
<ul class="b">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</body>
</html>
Ordered Lists Example
<html>
<head>
<style>
ol.a {
list-style-type: decimal;
ol.b {
list-style-type: lower-alpha;
</style>
</head>
<body>
<ol class="a">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
<ol class="b">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
</body>
</html>
You can check CSS property list-style-type detail to check all the possible marker's types
for the lists.
<head>
<style>
ul {
list-style-image: url('/images/icon-bullet.png');
</style>
</head>
<body>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</body>
</html>
none
inside
outside
inherit
Following is an example to show the usage of list-style-position property.
<html>
<head>
<style>
ul.a {
list-style-position: outside;
ul.b {
list-style-position: inside;
</style>
</head>
<body>
<ul class="a">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
<ul class="b">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</body>
</html>
<head>
<style>
ul {
</style>
</head>
<body>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</body>
</html>
<body>
</ul>
</ul>
</ul>
</body>
</html>
<h3>start attribute</h3>
<ol start="4">
<li>Java.</li>
<li>HTML.</li>
<li>CSS.</li>
<li>React.</li>
</ol>
<h3>reverse attribute</h3>
<ol reversed>
<li>Java.</li>
<li>HTML.</li>
<li>CSS.</li>
<li>React.</li>
</ol>
<h3>value attribute</h3>
<ol>
<li value="2">Java.</li>
<li value="3">HTML.</li>
<li value="1">CSS.</li>
<li value="4">React.</li>
</ol>
</body>
</html>
<style>
ol {
ol li {
ul {
ul li {
</style>
</head>
<body>
<ol>
<li>Java.</li>
<li>HTML.</li>
<li>CSS.</li>
<li>React.</li>
</ol>
<ul>
<li>Java.</li>
<li>HTML.</li>
<li>CSS.</li>
<li>React.</li>
</ul>
</body>
</html>
CSS Lists - Related Properties
Property Description
list-style A shorthand property for setting all the list properties in one
declaration.
CSS - Paddings
The CSS padding property is used to specify the space between the content of an element
and its borders. This article will teach you CSS padding property and its constituent
properties.
The value of CSS padding should be either a length, a percentage, or the word inherit. A
padding property does not allow negative values. If the value is inherit, it will have the same
padding as its parent element. If a percentage is used, the percentage is of the containing
box.
CSS Padding
The CSS padding property is a shorthand property to set the padding space around an
HTML element. Let us see a simple example to set padding with a single length value, which
is applied equally to all four padding sides. Here we add 5px padding on h2 element :
<html>
<head>
<style>
div{
h2{
</style>
</head>
<body>
<div>
</div>
</body>
</html>
You can set padding using any units, whether in pixels, inches, millimeters, or ems. The
default value for margin is 0 (zero), so if you don’t set a padding value then no padding
should appear around the element. To set a padding of quarter inche around h2 element,
above code will be written as follows:
<html>
<head>
<style>
div{
h2{
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
div{
h2.a{
h2.b{
background-color: #eee;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
padding-top
padding-right
padding-bottom
padding-left
Following example shows how different padding properties can be set around an h2
element:
<html>
<head>
<style>
div{
h2 {
padding-bottom:10px; padding-left:0px;
background-color: #eee;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
div{
h2 {
background-color: #eee;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
h2 {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
h2 {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
We have already seen an example for a single value passed as padding, which is applied to
all the sides i.e. top, right, bottom and left equally. You can check the very first example to
understand this scenario.
<head>
<style>
h2 {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
CSS does not allow a negative value for either of the padding properties, where as you can set
negative margin values for CSS margin properties.
<head>
<style>
div {
h2 {
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
We can also mix the percentage values with other length units for padding, though the
meaning of percentage padding will remain same as explained above:
<html>
<head>
<style>
div {
h2 {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
strong {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
However, when padding values are set for the left and right side of the element, the case is
different. Here you can see the space or padding applied.
<html>
<head>
<style>
strong {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
img {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
Property Description
padding A shorthand property that is used for setting all the padding
properties in one declaration.
1 auto
Shape of the cursor depends on the context area it is over. For example an I
over text, a hand over a link, and so on...
2
crosshair
A crosshair or plus sign
3
default
An arrow
4
pointer
A pointing hand (in IE 4 this value is hand)
5
move
The I bar
6
e-resize
The cursor indicates that an edge of a box is to be moved right (east)
7
ne-resize
The cursor indicates that an edge of a box is to be moved up and right
(north/east)
8
nw-resize
The cursor indicates that an edge of a box is to be moved up and left
(north/west)
9
n-resize
The cursor indicates that an edge of a box is to be moved up (north)
10
se-resize
The cursor indicates that an edge of a box is to be moved down and right
(south/east)
11
sw-resize
The cursor indicates that an edge of a box is to be moved down and left
(south/west)
12
s-resize
The cursor indicates that an edge of a box is to be moved down (south)
13
w-resize
The cursor indicates that an edge of a box is to be moved left (west)
14
text
The I bar
15
wait
An hour glass
16
help
A question mark or balloon, ideal for use over help buttons
17
<url>
The source of a cursor image file
NOTE − You should try to use only these values to add helpful information for users, and in
places, they would expect to see that cursor. For example, using the crosshair when
someone hovers over a link can confuse visitors.
Here is an example −
Live Demo
<html>
<head>
</head>
<body>
<p>Move the mouse over the words to see the cursor change:</p>
<div style = "cursor:auto">Auto</div>
<div style = "cursor:crosshair">Crosshair</div>
<div style = "cursor:default">Default</div>
CSS - Outlines
CSS Outlines
CSS defines a special sort of element decoration called an outline which is drawn outside
the element's border. CSS outlines are very similar to borders, but there are few major
differences as well −
An outline does not take up space.
Outlines do not have to be rectangular.
Outline is always the same on all sides; you cannot specify different values for
different sides of an element.
Example
Following is rectangle having a black border of 5px and green outline of 30px.
Outline Example
This tutorial will teach us how to set different properties associated with outlines. CSS alows
us to control an outline width, its color, style etc.
thin = 1px
medium = 3px
thick = 5px
A specific size (in px, pt, cm, em, etc)
A width of zero pixels means no outline.
Example
Here is an example to set outline width at different sizes −
<html>
<body>
</p>
</p>
</p>
</p>
</body>
</html>
<body>
</p>
</p>
</p>
</p>
</p>
</p>
</body>
</html>
</p>
</p>
</p>
</p>
</p>
</body>
</html>
Example
Here is an example −
<html>
<body>
<p style = "border:1px solid #000; outline:1px solid red; outline-
offset:20px;margin:25px">
</p>
<br>
</p>
<br>
</p>
</body>
</html>
<body>
</p>
<br />
</p>
<br />
<p style = "outline:5px dotted rgb(13,33,232); padding:5px;">
</p>
</body>
</html>
Outline Border
Outline is a non-rectangular shape that Border is a rectangular shape that is drawn around
surrounds an element, usually with a the content of an element, can be solid, dashed, or
solid color. dotted, and can have different colors and sizes.
It does not take up any space in the It affects the size and position of the element, as it
layout and does not affect the size or adds width to the element.
position of the element.
It can be created using It can be created using the border property in CSS.
the outline property in CSS.
property Description
outline A shorthand property for setting all the outline properties in one declaration
Example
Following example demonstrates using height and width for a div.
<html>
<head>
<style>
div {
</style>
</head>
<body>
</html>
<head>
<style>
div.a {
</style>
</head>
<body>
<div class="a">
</div>
</body>
</html>
<head>
<style>
div.a {
</style>
</head>
<body>
<div class="a">
</div>
</body>
</html>
<head>
<style>
div.a {
</style>
</head>
<body>
<div class="a">
</div>
</body>
</html>
<head>
<style>
div.a {
</style>
</head>
<body>
<div class="a">
</div>
</body>
</html>
<html>
<head>
<style>
div.a {
line-height: 1.0in; background-color: #eee; margin-bottom: 2px;
}
div.b {
line-height: 50px; background-color: #eee; margin-bottom: 2px;
}
div.c {
line-height: 5; background-color: #eee; margin-bottom: 5px;
}
div.d {
line-height: normal; background-color: #eee; margin-bottom: 2px;
}
</style>
</head>
<body>
<h2>Setting up line-height Property</h2>
<div class="a">This div element has a line-height of 1.0 inche.</div>
<div class="b">This div element has a line-height of 50px.</div>
<div class="c">This div element has a line-height of 5 (unitless
number)</div>
<div class="d">This div element has a line-height of normal.</div>
</body>
</html>
CSS - Scrollbars
There may be a case when an element's content might be larger than the amount of space
allocated to it. For example, given width and height properties do not allow enough room to
accommodate the content of the element.
CSS provides a property called overflow which tells the browser what to do if the box's
contents is larger than the box itself. This property can take one of the following values −
1 visible
Allows the content to overflow the borders of its containing element.
2
hidden
The content of the nested element is simply cut off at the border of the
containing element and no scrollbars is visible.
3
scroll
The size of the containing element does not change, but the scrollbars are
added to allow the user to scroll to see the content.
4
auto
The purpose is the same as scroll, but the scrollbar will be shown only if the
content does overflow.
Here is an example −
Live Demo
<html>
<head>
<style type = "text/css">
.scroll {
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:scroll;
}
.auto {
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:auto;
}
</style>
</head>
<body>
<p>Example of scroll value:</p>
<div class = "scroll">
I am going to keep lot of content here just to show you how
scrollbars works if there is an overflow in an element box.
This provides your horizontal as well as vertical scrollbars.
</div>
<br />
CSS - Dropdowns
What is a Dropdown?
A dropdown is a user interface element that includes a list of options. It allows the user to
choose one value from a list by hovering or clicking on a trigger element. It is typically used
in navigation menus, forms, and other interactive components of a website.
Dropdown menus are created using HTML's Unordered List (<ul>) and list item (<li>)
elements.
This chapter will cover the utilization of CSS for styling and arranging dropdown menu
elements, controlling their appearance and behavior.
<html>
<head>
<style>
.dropdown {
.dropdown-button {
.dropdown-menu {
display: block;
</style>
</head>
<body>
<div class="dropdown">
<div class="dropdown-menu">
<p>Option 1</p>
<p>Option 2</p>
<p>Option 3</p>
</div>
</div>
</body>
</html>
In the above example:
Use any HTML element such as <span>, or a <button> to open the dropdown content.
To create the dropdown content or menu, use a container element (like <div>)
Use CSS to wrap and position dropdown content correctly.
The .dropdown class uses position:relative. This property places dropdown menu
right below the dropdown button (using position:absolute).
.dropdown-menu class holds the actual dropdown content. It is hidden by default,
and will be displayed on hover.
The :hover selector shows the dropdown menu when the user moves the mouse over
the dropdown button.
You can separately decorate your list options using CSS styling.
CSS Hoverable Dropdowns
A hoverable dropdown is a user interface element where a dropdown menu appears when
a user hovers the cursor over the trigger element. The dropdown menu usually disappears
when the user moves their cursor away from the trigger element.
Example
Let us see an example. This example uses anchor tags inside the dropdown box and style
them to fit a styled dropdown button:
<html>
<head>
<style>
.dropdown {
.dropdown-button {
.dropdown-menu {
.dropdown-menu li {
.dropdown-menu li a {
.dropdown:hover .dropdown-menu {
display: block;
.dropdown-menu li:hover {
background-color: #82ea32;
</style>
</head>
<body>
<div class="dropdown">
<ul class="dropdown-menu">
</ul>
</div>
</body>
</html>
<head>
<style>
.dropdown-button {
.dropdown-menu {
list-style-type: none;
.dropdown-menu li {
.dropdown-menu li a {
.dropdown-menu li:hover {
background-color: #82ea32;
.show {
display: block;
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
button.addEventListener('click', function () {
dropdownContent.classList.toggle('show');
});
if (!event.target.matches('.dropdown-button') &&
dropdownContent.classList.contains('show')) {
dropdownContent.classList.remove('show');
});
});
</script>
</head>
<body>
<div class="dropdown">
<ul class="dropdown-menu">
</div>
</body>
</html>
<head>
<style>
.dropdown {
.dropdown-button {
.dropdown-menu {
.dropdown-menu li {
.dropdown-menu li a {
.dropdown:hover .dropdown-menu {
display: block;
}
.dropdown-menu li:hover {
background-color: #82ea32;
</style>
</head>
<body>
<div class="dropdown">
<ul class="dropdown-menu">
</ul>
</div>
</body>
</html>
<head>
<style>
.dropdown {
.dropdown-button {
.dropdown-menu {
.dropdown-menu li {
.dropdown-menu li a {
.dropdown:hover .dropdown-menu {
display: block;
.dropdown-menu li:hover {
background-color: #82ea32;
</style>
</head>
<body>
<div class="dropdown">
<ul class="dropdown-menu">
</ul>
</div>
</body>
</html>
<style>
.dropdown {
.dropdown-img-menu {
.dropdown:hover .dropdown-img-menu {
display: block;
.img-descripition {
</style>
</head>
<body>
<div class="dropdown">
<div class="dropdown-img-menu">
</div>
</div>
</body>
</html>
<head>
<style>
ul {
background-color: #40a944;
li {
float: left;
li a, .dropdown-option {
li a:hover,.dropdown:hover .dropdown-option {
background-color: #82ea32;
li.dropdown {
display: inline-block;
.dropdown-menu {
.dropdown-menu a {
.dropdown-menu a:hover {
background-color: #82ea32;
}
.dropdown:hover .dropdown-menu {
display: block;
</style>
</head>
<body>
<ul>
<li><a href="#">Tutorialspoints</a></li>
<li><a href="#">Home</a></li>
<li class="dropdown">
<div class="dropdown-menu">
<a href="#">HTML</a>
<a href="#">CSS</a>
<a href="#">Bootstrap</a>
</div>
</li>
</ul>
</body>
</html>
CSS - Clearfix
What is a Clearfix?
CSS Clearfix is a technique to ensure that a container properly encloses and contains
floated elements within it. It prevents layout issues by adding an empty element to the
container, which clears both left and right floats, allowing the container to expand and
maintain its intended layout.
Clearfix helps to prevent the problems like container collapse, uneven heights, overlapping
content, inconsistent alignment.
This chapter will explore, how the clearfix technique ensures that container elements
properly contains its floated child elements.
CSS Clearfix
As mentioned abbve ,the CSS clearfix fixes the overflow elements from the desired element.
The following three properties can be worked upon for this:
Overflow and Float Property
Height Property
Clear Property
The following diagram demonstrates the clearfix layout for reference:
If an element is taller than the element containing it, and it is floated, it will overflow outside of its
container. We can fix this issue by setting overflow: auto;
<html>
<head>
<style>
div {
.img {
</style>
</head>
<body>
<h2>Without Clearfix</h2>
<div>
</div>
</body>
</html>
To resolve this overflow, we can set overflow: auto; property to the corresponding element,
ensuring that the image is fully contained within the container.
Let us see an example:
<html>
<head>
<style>
div {
.img {
</style>
</head>
<body>
<h2>With Clearfix</h2>
<div>
</div>
</body>
</html>
<head>
<style>
div {
.img {
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<head>
<style>
.main {
.left {
.aqua {
.pink {
p {
width: 50%;
</style>
</head>
<body>
<div class="main">
</div>
</body>
</html>
<head>
<style>
.main {
.right {
.aqua {
.pink {
p {
width: 50%;
</style>
</head>
<body>
<div class="main">
</div>
</body>
</html>
Setting Clear to Both
Following example demonstrates clearfix using clear:both property:
<html>
<head>
<style>
.main {
.both {
.aqua{
.pink {
p {
width: 45%;
</style>
</head>
<body>
<div class="main">
<p class="aqua">There are many variations of passages of Lorem Ipsum
available, but the majority have suffered alteration in some form, by injected
humour, or randomised words which don't look even slightly believable. </p>
</div>
</body>
</html>
CSS - Arrows
What are CSS Arrows?
Arrows are used in user interfaces to guide users and help them understand the flow of
information. They provide visual clues to navigate through different actions.
Arrows are an effective way to improve the user experience. They are used in tooltips,
dropdown menus, navigation elements, and more. This makes it easier to guide users
through a process.
Arrows can be created using CSS properties as listed below:
transform: This property can be used to create arrow icons by rotating elements
using the rotate() function. The rotate() function takes an angle as its argument,
which specifies the direction and amount of rotation.
border: This property allows us to create a triangle by manipulating the width and
height of the element’s border.
CSS Arrow Using Transform
The transform property can be used to create arrow icons by rotating elements using
the rotate() function. The rotate() function takes an angle as its argument, which specifies
the direction and amount of rotation.
Example
Let us see an example for creating an arrow using transform property.
<html>
<head>
<style>
.arrow-container {
display: flex;
align-items: center;
.arrow {
display: inline-block;
margin-right: 30px;
width: 15px;
height: 15px;
.right-arrow {
transform: rotate(45deg);
.left-arrow {
transform: rotate(-135deg);
.up-arrow {
transform: rotate(-45deg);
.down-arrow {
transform: rotate(135deg);
.top-narrow-arrow {
.top-wide-arrow {
.top-left-arrow {
.top-right-arrow {
.bottom-left-arrow {
.bottom-right-arrow {
</style>
</head>
<body>
</body>
</html>
Example
Following example demonstrates use of border property to create arrows:
<html>
<head>
<style>
.arrow-container {
display: flex;
align-items: center;
}
.left-arrow,
.right-arrow,
.up-arrow,
.down-arrow {
width: 0;
height: 0;
margin: 5px;
.left-arrow,
.right-arrow {
.up-arrow,
.down-arrow {
.right-arrow {
.left-arrow {
.up-arrow {
.down-arrow {
</style>
</head>
<body>
<p class="arrow-container"><span class="right-arrow"></span> - This arrow
points to the right.</p>
</body>
</html>
<head>
<style>
.arrow-container {
display: flex;
align-items: center;
.left-arrow,
.right-arrow,
.up-arrow,
.down-arrow {
display: inline-block;
margin: 30px;
width: 15px;
height: 15px;
transform-origin: center;
}
.right-arrow {
transform: rotate(135deg);
.left-arrow {
transform: rotate(-45deg);
.up-arrow {
transform: rotate(45deg);
.down-arrow {
transform: rotate(-135deg);
.right-arrow::after,
.left-arrow::after,
.up-arrow::after,
.down-arrow::after {
content: "";
display: block;
width: 2px;
height: 45px;
background-color: #F10C0C;
</style>
</head>
<body>
</body>
</html>
Dropdown Arrow
You can create a dropdown button with a downward-pointing arrow icon. When you hover
over the button, the dropdown menu appears.
Example
Here is an example −
<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
width:98px;
.dropdown-btn {
background-color: #F10C0C;
color: #ffffff;
padding: 10px;
border: none;
cursor: pointer;
display: flex;
align-items: center;
.dropdown-content {
width:98px;
display: none;
position: absolute;
background-color: #F10C0C;
z-index: 1;
.dropdown-btn::after {
content: "";
width: 0;
height: 0;
border-left: 6px solid transparent;
margin-left: 5px;
.dropdown:hover .dropdown-content {
display: block;
.dropdown-item {
padding: 10px;
text-decoration: none;
color: #ffffff;
display: block;
</style>
</head>
<body>
<div class="dropdown">
<button class="dropdown-btn">Dropdown</button>
<div class="dropdown-content">
</div>
</div>
</body>
</html>
Tooltip Arrow
We can create a tooltip with an upward-pointing triangular arrow using CSS borders and
the transform property. When you hovered over the text tooltip will dispaly and disappears
when the mouse cursor is moved away from the text.
Example
Here is an example −
<html>
<head>
<style>
.tooltip {
position: relative;
display: inline-block;
cursor: pointer;
.tooltipcontent {
display: none;
position: absolute;
background-color: #F10C0C;
color: #fff;
padding: 8px;
border-radius: 4px;
z-index: 1;
font-size: 14px;
white-space: nowrap;
.tooltip:hover .tooltipcontent {
display: block;
.tooltipcontent::before {
content: "";
position: absolute;
border-width: 6px;
border-style: solid;
top: -12px;
left: 50%;
transform: translateX(-50%);
</style>
</head>
<body>
<p>Bring the cursor over Tutorials Point to see the result </p>
<h3 class="tooltip">Tutorials Point
</h3>
</body>
</html>
<head>
<style>
.arrow-container {
display: flex;
align-items: center;
.left-arrow
width: 0;
height: 0;
margin: 5px;
.left-arrow
.left-arrow {
.arrow-move {
position: relative;
@keyframes move {
0% {
transform: translateY(0);
50% {
transform: translateY(-10px);
100% {
transform: translateY(0);
</style>
</head>
<body>
</body>
</html>