0% found this document useful (0 votes)
12 views

Unit 2 CSS

Uploaded by

Hardik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit 2 CSS

Uploaded by

Hardik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 133

Unit 2

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 }

Example − You can define a table border as follows −


table{ border :1px solid #C00; }
Here table is a selector and border is a property and given value 1px solid #C00 is the value
of that property.
You can define selectors in various simple ways based on your comfort. Let me put these
selectors one by one.

The Type Selectors


This is the same selector we have seen above. Again, one more example to give a color to
all level 1 headings −
h1 {
color: #36CFFF;
}
The Universal Selectors
Rather than selecting elements of a specific type, the universal selector quite simply
matches the name of any element type −
*{
color: #000000;
}

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 Attribute Selectors


You can also apply styles to HTML elements with particular attributes. The style rule below
will match all the input elements having a type attribute with a value of text −
input[type = "text"] {
color: #000000;
}

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.

Embedded CSS - The <style> Element


You can put your CSS rules into an HTML document using the <style> element. This tag is
placed inside the <head>...</head> tags. Rules defined using this syntax will be applied to
all the elements available in the document. Here is the generic syntax −
<!DOCTYPE html>
<html>
<head>
<style type = "text/css" media = "all">
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>

It will produce the following result −


Attributes
Attributes associated with <style> elements are −

Attribut Value Description


e
type text/css Specifies the style sheet language as a content-type (MIME type). This
is required attribute.

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

Inline CSS - The style Attribute


You can use style attribute of any HTML element to define style rules. These rules will be
applied to that element only. Here is the generic syntax −
<element style = "...style rules....">
Attributes

Attribut Value Description


e

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>

It will produce the following result −

External CSS - The <link> Element


The <link> element can be used to include an external stylesheet file in your HTML
document.
An external style sheet is a separate text file with .css extension. You define all the Style
rules within this text file and then you can include this file in any HTML document using
<link> element.
Here is the generic syntax of including external CSS file −
<head>
<link type = "text/css" href = "..." media = "..." />
</head>
Attributes
Attributes associated with <style> elements are −

Attribute Value Description

type Specifies the style sheet language as a content-type (MIME


text css
type). This attribute is required.

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 - Measurement Units


CSS supports a number of measurements including absolute units such as inches,
centimeters, points, and so on, as well as relative measures such as percentages and em
units. You need these values while specifying various measurements in your Style rules
e.g. border = "1px solid red".
We have listed out all the CSS Measurement Units along with proper Examples −

Unit Description Example

% Defines a measurement as a percentage p {font-size: 16pt; line-height: 125%;}


relative to another value, typically an
enclosing element.

cm Defines a measurement in centimeters. div {margin-bottom: 2cm;}

em A relative measurement for the height of a font


in em spaces. Because an em unit is
equivalent to the size of a given font, if you p {letter-spacing: 7em;}
assign a font to 12pt, each "em" unit would be
12pt; thus, 2em would be 24pt.

ex This value defines a measurement relative to


a font's x-height. The x-height is determined p {font-size: 24pt; line-height: 3ex;}
by the height of the font's lowercase letter x.

in Defines a measurement in inches. p {word-spacing: .15in;}

mm Defines a measurement in millimeters. p {word-spacing: 15mm;}

pc Defines a measurement in picas. A pica is


equivalent to 12 points; thus, there are 6 picas p {font-size: 20pc;}
per inch.

pt Defines a measurement in points. A point is body {font-size: 18pt;}


defined as 1/72nd of an inch.

px Defines a measurement in screen pixels. p {padding: 25px;}

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 −

Format Syntax Example

Hex Code #RRGGBB p{color:#FF0000;}

Short Hex Code #RGB p{color:#6A7;}

RGB % rgb(rrr%,ggg%,bbb%) p{color:rgb(50%,50%,50%);}

RGB Absolute rgb(rrr,ggg,bbb) p{color:rgb(0,0,255);}

keyword aqua, black, etc. p{color:teal;}

These formats are explained in more detail in the following sections −

CSS Colors - Hex Codes


A hexadecimal is a 6 digit representation of a color. The first two digits(RR) represent a red
value, the next two are a green value(GG), and the last are the blue value(BB).
A hexadecimal value can be taken from any graphics software like Adobe Photoshop, Jasc
Paintshop Pro, or even using Advanced Paint Brush.
Each hexadecimal code will be preceded by a pound or hash sign '#'. Following are the
examples to use Hexadecimal notation.

Color Color HEX

#000000

#FF0000

#00FF00

#0000FF
#FFFF00

#00FFFF

#FF00FF

#C0C0C0

#FFFFFF

CSS Colors - Short Hex Codes


This is a shorter form of the six-digit notation. In this format, each digit is replicated to arrive
at an equivalent six-digit value. For example: #6A7 becomes #66AA77.
A hexadecimal value can be taken from any graphics software like Adobe Photoshop, Jasc
Paintshop Pro, or even using Advanced Paint Brush.
Each hexadecimal code will be preceded by a pound or hash sign '#'. Following are the
examples to use Hexadecimal notation.

Color Color HEX

#000

#F00

#0F0

#0FF

#FF0

#0FF

#F0F

#FFF

CSS Colors - RGB Values


This color value is specified using the rgb( ) property. This property takes three values, one
each for red, green, and blue. The value can be an integer between 0 and 255 or a
percentage.
NOTE − All the browsers does not support rgb() property of color so it is recommended not
to use it.
Following is the example to show few colors using RGB values.

Color Color RGB

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)

Building Color Codes


You can build millions of color codes using our Color Code Builder. Check our HTML Color
Code Builder. To use this tool, you would need a Java Enabled Browser.

Browser Safe Colors


Here is the list of 216 colors which are supposed to be most safe and computer independent
colors. These colors vary from hexa code 000000 to FFFFFF. These colors are safe to use
because they ensure that all computers would display the colors correctly when running a
256 color palette −

000000 000033 000066 000099 0000CC 0000FF

003300 003333 003366 003399 0033CC 0033FF

006600 006633 006666 006699 0066CC 0066FF

009900 009933 009966 009999 0099CC 0099FF

00CC00 00CC33 00CC66 00CC99 00CCCC 00CCFF

00FF00 00FF33 00FF66 00FF99 00FFCC 00FFFF


330000 330033 330066 330099 3300CC 3300FF

333300 333333 333366 333399 3333CC 3333FF

336600 336633 336666 336699 3366CC 3366FF

339900 339933 339966 339999 3399CC 3399FF

33CC00 33CC33 33CC66 33CC99 33CCCC 33CCFF

33FF00 33FF33 33FF66 33FF99 33FFCC 33FFFF

660000 660033 660066 660099 6600CC 6600FF

663300 663333 663366 663399 6633CC 6633FF

666600 666633 666666 666699 6666CC 6666FF

669900 669933 669966 669999 6699CC 6699FF

66CC00 66CC33 66CC66 66CC99 66CCCC 66CCFF

66FF00 66FF33 66FF66 66FF99 66FFCC 66FFFF

990000 990033 990066 990099 9900CC 9900FF

993300 993333 993366 993399 9933CC 9933FF

996600 996633 996666 996699 9966CC 9966FF

999900 999933 999966 999999 9999CC 9999FF

99CC00 99CC33 99CC66 99CC99 99CCCC 99CCFF

99FF00 99FF33 99FF66 99FF99 99FFCC 99FFFF

CC0000 CC0033 CC0066 CC0099 CC00CC CC00FF

CC3300 CC3333 CC3366 CC3399 CC33CC CC33FF


CC6600 CC6633 CC6666 CC6699 CC66CC CC66FF

CC9900 CC9933 CC9966 CC9999 CC99CC CC99FF

CCCC00 CCCC33 CCCC66 CCCC99 CCCCCC CCCCFF

CCFF00 CCFF33 CCFF66 CCFF99 CCFFCC CCFFFF

FF0000 FF0033 FF0066 FF0099 FF00CC FF00FF

FF3300 FF3333 FF3366 FF3399 FF33CC FF33FF

FF6600 FF6633 FF6666 FF6699 FF66CC FF66FF

FF9900 FF9933 FF9966 FF9999 FF99CC FF99FF

FFCC00 FFCC33 FFCC66 FFCC99 FFCCCC FFCCFF

FFFF00 FFFF33 FFFF66 FFFF99 FFFFCC FFFFFF

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>

This will produce following result −

Set the Background Image


We can set the background image by calling local stored images as shown below −
Live Demo
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-color: #cccccc;
}
</style>
</head>

<body>
<h1>Hello World!</h1>
</body>
<html>

It will produce the following result −

Repeat the Background Image


The following example demonstrates how to repeat the background image if an image is
small. You can use no-repeat value for background-repeat property if you don't want to
repeat an image, in this case image will display only once.
By default background-repeat property will have repeat value.
Live Demo
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat;
}
</style>
</head>

<body>
<p>Tutorials point</p>
</body>
</html>

It will produce the following result −


The following example which demonstrates how to repeat the background image vertically.
Live Demo
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-y;
}
</style>
</head>

<body>
<p>Tutorials point</p>
</body>
</html>

It will produce the following result −


The following example demonstrates how to repeat the background image horizontally.
Live Demo
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-x;
}
</style>
</head>

<body>
<p>Tutorials point</p>
</body>
</html>

It will produce the following result −

Set the Background Image Position


The following example demonstrates how to set the background image position 100 pixels
away from the left side.
Live Demo
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-position:100px;
}
</style>
</head>

<body>
<p>Tutorials point</p>
</body>
</html>

It will produce the following result −


The following example demonstrates how to set the background image position 100 pixels
away from the left side and 200 pixels down from the top.
Live Demo
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-position:100px 200px;
}
</style>
</head>

<body>
<p>Tutorials point</p>
</body>
</html>

It will produce the following result −

Set the Background Attachment


Background attachment determines whether a background image is fixed or scrolls with the
rest of the page.
The following example demonstrates how to set the fixed background image.
Live Demo
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
}
</style>
</head>

<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>

It will produce the following result −


The following example demonstrates how to set the scrolling background image.
Live Demo
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-attachment:scroll;
}
</style>
</head>

<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>

It will produce the following result −

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>

This will produce following result −

Set the Font Style


Following is the example, which demonstrates how to set the font style of an element.
Possible values are normal, italic and oblique.
Live Demo
<html>
<head>
</head>

<body>
<p style = "font-style:italic;">
This text will be rendered in italic style
</p>
</body>
</html>

This will produce following result −

Set the Font Variant


The following example demonstrates how to set the font variant of an element. Possible
values are normal and small-caps.
Live Demo
<html>
<head>
</head>

<body>
<p style = "font-variant:small-caps;">
This text will be rendered as small caps
</p>
</body>
</html>

This will produce following result −

Set the Font Weight


The following example demonstrates how to set the font weight of an element. The font-
weight property provides the functionality to specify how bold a font is. Possible values could
be normal, bold, bolder, lighter, 100, 200, 300, 400, 500, 600, 700, 800, 900.
Live Demo
<html>
<head>
</head>

<body>
<p style = "font-weight:bold;">
This font is bold.
</p>

<p style = "font-weight:bolder;">


This font is bolder.
</p>

<p style = "font-weight:500;">


This font is 500 weight.
</p>
</body>
</html>
This will produce following result −

Set the Font Size


The following example demonstrates how to set the font size of an element. The font-size
property is used to control the size of fonts. Possible values could be xx-small, x-small,
small, medium, large, x-large, xx-large, smaller, larger, size in pixels or in %.
Live Demo
<html>
<head>
</head>

<body>
<p style = "font-size:20px;">
This font size is 20 pixels
</p>

<p style = "font-size:small;">


This font size is small
</p>

<p style = "font-size:large;">


This font size is large
</p>
</body>
</html>

This will produce following result −

Set the Font Size Adjust


The following example demonstrates how to set the font size adjust of an element. This
property enables you to adjust the x-height to make fonts more legible. Possible value could
be any number.
Live Demo
<html>
<head>
</head>

<body>
<p style = "font-size-adjust:0.61;">
This text is using a font-size-adjust value.
</p>
</body>
</html>

This will produce following result −

Set the Font Stretch


The following example demonstrates how to set the font stretch of an element. This property
relies on the user's computer to have an expanded or condensed version of the font being
used.
Possible values could be normal, wider, narrower, ultra-condensed, extra-condensed,
condensed, semi-condensed, semi-expanded, expanded, extra-expanded, ultra-expanded.
Live Demo
<html>
<head>
</head>

<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>

This will produce following result −

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>

It will produce the following result −

Set the Text Direction


The following example demonstrates how to set the direction of a text. Possible values are ltr
or rtl.
Live Demo
<html>
<head>
</head>

<body>
<p style = "direction:rtl;">
This text will be rendered from right to left
</p>
</body>
</html>

It will produce the following result −

Set the Space between Characters


The following example demonstrates how to set the space between characters. Possible
values are normal or a number specifying space..
Live Demo
<html>
<head>
</head>

<body>
<p style = "letter-spacing:5px;">
This text is having space between letters.
</p>
</body>
</html>

It will produce the following result −

Set the Space between Words


The following example demonstrates how to set the space between words. Possible values
are normal or a number specifying space.
Live Demo
<html>
<head>
</head>

<body>
<p style = "word-spacing:5px;">
This text is having space between words.
</p>
</body>
</html>
This will produce following result −

Set the Text Indent


The following example demonstrates how to indent the first line of a paragraph. Possible
values are % or a number specifying indent space.
Live Demo
<html>
<head>
</head>

<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>

It will produce the following result −

Set the Text Alignment


The following example demonstrates how to align a text. Possible values are left, right,
center, justify.
Live Demo
<html>
<head>
</head>

<body>
<p style = "text-align:right;">
This will be right aligned.
</p>

<p style = "text-align:center;">


This will be center aligned.
</p>

<p style = "text-align:left;">


This will be left aligned.
</p>
</body>
</html>

This will produce following result −

Decorating the Text


The following example demonstrates how to decorate a text. Possible values are none,
underline, overline, line-through, blink.
Live Demo
<html>
<head>
</head>

<body>
<p style = "text-decoration:underline;">
This will be underlined
</p>

<p style = "text-decoration:line-through;">


This will be striked through.
</p>

<p style = "text-decoration:overline;">


This will have a over line.
</p>

<p style = "text-decoration:blink;">


This text will have blinking effect
</p>
</body>
</html>

This will produce following result −

Set the Text Cases


The following example demonstrates how to set the cases for a text. Possible values
are none, capitalize, uppercase, lowercase.
Live Demo
<html>
<head>
</head>

<body>
<p style = "text-transform:capitalize;">
This will be capitalized
</p>

<p style = "text-transform:uppercase;">


This will be in uppercase
</p>

<p style = "text-transform:lowercase;">


This will be in lowercase
</p>
</body>
</html>

This will produce following result −

Set the White Space between Text


The following example demonstrates how white space inside an element is handled.
Possible values are normal, pre, nowrap.
Live Demo
<html>
<head>
</head>

<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>

This will produce following result −

Set the Text Shadow


The following example demonstrates how to set the shadow around a text. This may not be
supported by all the browsers.
Live Demo
<html>
<head>
</head>

<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>

CSS - Using Images


Images play an important role in any webpage. Though it is not recommended to include a
lot of images, but it is still important to use good images wherever required.
CSS plays a good role to control image display. You can set the following image properties
using CSS.
 The border property is used to set the width of an image border.
 The height property is used to set the height of an image.
 The width property is used to set the width of an image.
 The -moz-opacity property is used to set the opacity of an image.
The Image Border Property
The border property of an image is used to set the width of an image border. This property
can have a value in length or in %.
A width of zero pixels means no border.
Here is the example −
Live Demo
<html>
<head>
</head>

<body>
<img style = "border:0px;" src = "/css/images/logo.png" />
<br />
<img style = "border:3px dashed red;" src = "/css/images/logo.png" />
</body>
</html>

It will produce the following result −

The Image Height Property


The height property of an image is used to set the height of an image. This property can
have a value in length or in %. While giving value in %, it applies it in respect of the box in
which an image is available.
Here is an example −
Live Demo
<html>
<head>
</head>

<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>

It will produce the following result −

The Image Width Property


The width property of an image is used to set the width of an image. This property can have
a value in length or in %. While giving value in %, it applies it in respect of the box in which
an image is available.
Here is an example −
Live Demo
<html>
<head>
</head>

<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>

It will produce the following result −

The -moz-opacity Property


The -moz-opacity property of an image is used to set the opacity of an image. This property
is used to create a transparent image in Mozilla. IE uses filter:alpha(opacity=x) to create
transparent images.
In Mozilla (-moz-opacity:x) x can be a value from 0.0 - 1.0. A lower value makes the element
more transparent (The same things goes for the CSS3-valid syntax opacity:x).
In IE (filter:alpha(opacity=x)) x can be a value from 0 - 100. A lower value makes the
element more transparent.
Here is an example −
Live Demo
<html>
<head>
</head>

<body>
<img style = "border:1px solid red; -moz-opacity:0.4; filter:alpha(opacity=40);" src =
"/css/images/logo.png" />
</body>
</html>

It will produce the following result −

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.

Set the Color of Links


The following example demonstrates how to set the link color. Possible values could be any
color name in any valid format.
Live Demo
<html>
<head>
<style type = "text/css">
a:link {color:#000000}
</style>
</head>

<body>
<a href = "">Link</a>
</body>
</html>

It will produce the following black link −

Set the Color of Visited Links


The following example demonstrates how to set the color of visited links. Possible values
could be any color name in any valid format.
Live Demo
<html>
<head>
<style type = "text/css">
a:visited {color: #006600}
</style>
</head>

<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.

Change the Color of Links when Mouse is Over


The following example demonstrates how to change the color of links when we bring a
mouse pointer over that link. Possible values could be any color name in any valid format.
Live Demo
<html>
<head>
<style type = "text/css">
a:hover {color: #FFCC00}
</style>
</head>

<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.

Change the Color of Active Links


The following example demonstrates how to change the color of active links. Possible values
could be any color name in any valid format.
Live Demo
<html>
<head>
<style type = "text/css">
a:active {color: #FF00CC}
</style>
</head>

<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.

Styling Tables using CSS


Following is a simple HTML table which has been styled using CSS :
<html>

<head>

<style>

table {

font-family: Arial, Helvetica, sans-serif;

border-collapse: collapse; width: 100%;

td, th {

border: 1px solid #ddd; padding: 8px;

tr:nth-child(even){background-color: #f2f2f2;}

tr:hover {background-color: #ddd;}

th {

padding-top: 12px; padding-bottom: 12px;

text-align: left; background-color: #40a944; color: white;

</style>

</head>

<body>

<h1>CSS Styled Table</h1>

<table>

<tr>

<th>Name</th>

<th>Address</th>

<th>Country</th>

</tr>

<tr>

<td>Nuha Ali</td>

<td>My Home Bhooja</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>

CSS Properties of a Table


You can set the following CSS properties of a table −
 The border-collapse specifies whether the browser should control the appearance of
the adjacent borders that touch each other or whether each cell should maintain its
style.
 The border-spacing specifies the width that should appear between table cells.
 The caption-side specifies where the table caption should be displayed (top or
bottom).
 The empty-cells specifies whether the border should be shown if a cell is empty.
 The table-layout allows browsers to speed up layout of a table by using the first width
properties it comes across for the rest of a column rather than having to load the
whole table before rendering it.
 The width and height properties set the height and width of a table.
 The text-align property sets the horizontal alignment of the text content within table
cells.
 The border property can be used to set border to table and its cells.
 The background-color property can be applied to the table itself, the table cells, or
table rows.
 The font-size, font-family, font-weight, font-coloretc style the table font.
Collapse Table Border using CSS
The property border-collapse ensures that borders between table cells collapse into a
single border, creating a cleaner look. Property border-collapse can have
values collapse or separate (default).
Example
The following example shows how we can use border-collapse property to merge the
borders to remove the spacing between the table cells.
<html>

<head>

<style>
table {

border-collapse: collapse;

border: 3px solid;

width: 100%; padding:5px;

th, td {

border: 1px solid black;

</style>

<body>

<table>

<tr>

<th>Name</th>

<th>Address</th>

<th>Country</th>

</tr>

<tr>

<td>Nuha Ali</td>

<td>My Home Bhooja</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 {

border-collapse: separate; border-spacing: 1em; width: 100%;

padding: 5px; border: 3px solid #40a944;

td {

width: 1.5em; height: 1.5em; background: #d2d2d2;

text-align: center; vertical-align: middle;

</style>

<body>

<table>

<tr>

<th>Name</th>

<th>Address</th>

<th>Country</th>

</tr>

<tr>

<td>Nuha Ali</td>

<td>My Home Bhooja</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.

Setting Table Caption using CSS


The caption-side property in CSS is used to control the placement or alignment of the table
caption in relation to the table. The caption-side property can have one of following values:
 top: The caption placed above the table.
 bottom: The caption placed below the table.
 block-start: The caption box is positioned at the block start edge of the table.
 block-end: The caption box sis positioned at the block end edge of the table.
 inline-start: The caption box is positioned at the inline start edge of the table.
 inline-end: The caption box is positioned at the inline end edge of the table.
 inherit: The value is inherited from the caption-side property of the parent element.
Example
Let us see an example:
<html>

<head>

<style>

table {

border-collapse: separate; border-spacing: 1em; width: 100%;

padding: 5px; border: 3px solid #40a944;

.top caption {

caption-side: top;

.bottom caption {

caption-side: bottom;
}

table {

border: 1px solid red;

td {

border: 1px solid blue;

</style>

<body>

<table class="top">

<caption>

Caption ABOVE the table

</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 BELOW the table

</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.

Hide Empty Cells in a Table


The empty-cells property in CSS is used to control the rendering of cells in a table that have
no content or are otherwise considered "empty." It only applies to tables and table cells.
This property can have one of the two values:
 show: It indicates that empty cells should be shown with borders and spacing as if
they contained content. It is the default value.
 hide: It indicates that empty cells should be hidden and not take up any space.
Borders and spacing for empty cells will not be displayed, contributing to a more
compact layout.
Example
Here is the empty-cells property used to hide borders of empty cells in the <table> element.
<html>

<head>

<style>

table {

width: 100%;

border:1px solid #aaa;

border-collapse: separate;
empty-cells: hide;

td,th {

padding: 5px;

border: 1px solid #aaa;

</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%;

border:1px solid #aaa;


border-collapse: separate;

empty-cells: show;

td,th {

padding: 5px;

border: 1px solid #aaa;

</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>

Setting Table Layout using CSS


The table-layout property helps you control how a browser should render or lay out a table.
This property can have one of the following values:
 auto: When table-layout is set to auto, the browser will calculate the width of
columns and cells based on their content.
 fixed: When table-layout is set to fixed, the browser will allocate a fixed width to
each column based on the first row of the table. This means that all subsequent rows
will adhere to the same column widths, regardless of their content.
Using table-layout: fixed can be beneficial when you want to create tables with consistent
column widths, especially when dealing with large amounts of data or when you want to
maintain a specific design.
Example
The following example shows the use of table-layout: fixed:
<html>

<head>

<style>

table {

width: 100%;

border:1px solid #aaa;

border-collapse: collapse;

table-layout: fixed;

th, td {

border: 1px solid black;

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>This is some longer content in Column 1</td>

<td>Short content</td>

<td>Even longer content that might wrap in Column 3</td>

</tr>

<tr>
<td>Row 2, Column 1</td>

<td>Row 2, Column 2</td>

<td>Row 2, Column 3</td>

</tr>

</table>

</body>

</html>
The following example shows the use of table-layout: auto:
<html>

<head>

<style>

table {

width: 100%;

border:1px solid #aaa;

border-collapse: collapse;

table-layout: auto;

th, td {

border: 1px solid black;

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>This is some longer content in Column 1</td>

<td>Short content</td>
<td>Even longer content that might wrap in Column 3</td>

</tr>

<tr>

<td>Row 2, Column 1</td>

<td>Row 2, Column 2</td>

<td>Row 2, Column 3</td>

</tr>

</table>

</body>

</html>

Setting Table Height and Width


CSS provides height and width properties to set a table height and width respectively.
Example

<html>

<head>

<style>

table {

width: 600px;

height: 200px;

border-collapse: collapse;

border: 1px solid #aaa;

th, td {

border: 1px solid #aaa;

</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>

Setting Table Text Align


The text-align property sets the horizontal alignment of the text content within table cells
(<th> or <td>). It can take following values:
 left
 right
 center
 justify
The content of <th> element is center-aligned and that of <td> element is left-aligned, by
default.
Example
Let us see an example of text-align: center:
<html>

<head>

<style>

table, td, th {

border: 1px solid black;

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.

Setting Table Vertical Align


The property vertical-align sets the vertical alignment of content in <th> or <td>.
It can take following values:
 top
 middle
 bottom
The vertical alignment of content of <th> and <td> elements is middle, by default.
Example
Let us see an example for setting the vertical-alignment to top:
<html>

<head>

<style>

table, td, th {

border: 2px solid black;

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>

CSS Table Background color


The background-color property can be applied to the table itself, the table cells, or table
rows.
In order to set the background color use the following code:
table {

background-color: #f2f2f2;

}
td {

background-color: #f2f2f2;

tr {

background-color: #ffffff;

}
Example
Let us see an example:
<html>

<head>

<style>

table {

background-color: rgb(237, 181, 237);

border: 2px solid black;

border-collapse: collapse;

width: 100%;

td {

height: 50px;

vertical-align: middle;

text-align: center;

</style>

</head>

<body>

<h2>Background color 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 Font styles


The font of the content of a table can be styled using the font-related properties, such
as font-size, font-family, font-weight, etc. on the <th> and <td> elements.
Example
Let us see following example:
<html>

<head>

<style>

table.one {

border-collapse: collapse;

width: 400px;

th {

font-size: large;
font-family: 'Lucida Sans', sans-serif;

td {

font-size: small;

font-family: Verdana, Geneva, Tahoma, sans-serif;

</style>

</head>

<body>

<h2>font styles</h2>

<div>

<table class = "one" border = "1">

<th>Heading</th>

<tr>

<td>Cell value</td>

</tr>

<tr>

<td>Cell value</td>

</tr>

</table>

</div>

</body>

</html>

CSS Table Other Styles


Various CSS properties are available to style the table design further. For example, you can
add padding, margin, dividers, etc. to a table.
Example
Let us see few examples:
Padding
In order to add padding to the table or its cells, use the property padding. Refer the example
below:
<html>

<head>

<style>

table {
border: 2px solid black;

background-color: rgb(175, 239, 194);

border-collapse: collapse;

td,th {

border: 2px solid black;

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: 2px solid black;

background-color: rgba(237, 181, 237);

border-collapse: collapse;

margin-top: 50px;

td,th {

border: 2px solid black;

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: 2px solid black;

background-color: rgb(175, 239, 194);

border-collapse: collapse;

margin-top: 50px;

th {

border-bottom: 2px solid black;

td{

border-right: 2px solid black;


vertical-align: middle;

height: 50px;

</style>

</head>

<body>

<h2>horizontal & vertical divider</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>

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;

tr:nth-child(odd) {background-color: rgb(230,125,111);}

</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;

tr:nth-child(odd) {background-color: rgb(230,125,111);}

</style>

</head>

<body>

<h2>Responsive table</h2>

<div style="overflow-x: auto;">

<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>

CSS Table - Related Properties


Following is the list of CSS properties for styling tables:
Property Description

border-collapse Sets the table border rendering algorithm.

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.

caption-side Sets the position of a table caption.

empty-cells With separate borders, hides empty cells in a table.

table-layout Determines the table-rendering algorithm.

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.

CSS Border Style


The border-style property specifies what kind of border to display. Following values can be passed to
border-style:

Value Description

none No border
hidden A hidden border, same as 'none' except for table elements

dotted A series of dots

dashes A series of short dashes

solid A single solid line

double Two parallel lines with a small gap between them

groove A border that appears to be carved into the page

ridge A border that appears to be slightly raised above the page

inset A border that appears embedded into the page

outset A border that appears slightly raised out of the page

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>

<p style="border-style: none;">No border.</p>

<p style="border-style: hidden;">Hidden border.</p>

<p style="border-style: dotted;">Dotted border.</p>

<p style="border-style: dashed;">Dashed border.</p>

<p style="border-style: solid;">Solid border.</p>

<p style="border-style: double;">Double border.</p>

<p style="border-style: groove;">Groove border.</p>


<p style="border-style: ridge;">Ridge border.</p>

<p style="border-style: inset;">Inset border.</p>

<p style="border-style: outset;">Outset border.</p>

<p style="border-style: none dashed solid dotted;">A mixed border.</p>

</body>

<html>

CSS Border Style - Single Side


CSS provided following four properties to control the style of each single-side of an element.

 border-top-style
 border-right-style
 border-bottom-style
 border-left-style
Example
Let us see an example:
<html>

<head>

<style>

p {

border-top-style: dotted; border-right-style: solid;

border-bottom-style: dashed; border-left-style: double;

border-width:5px; padding: 2em;

</style>

</head>

<body>

<h2>border-style (single-side)</h2>

<p>Different border styles on all the sides.</p>

</body>

<html>

CSS Border Width


The border-width property is next in line after setting border style and this property is used to set the
border width. Following values can be passed to border-width:

Value Description
thin a thin border

medium a medium width border

thick a thick border

length any length specified in px, pt, cm, em, etc

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>

<h2>border-width with different values</h2>

<p style="border-style: double; border-width: thin;">Thin width.</p>

<p style="border-style: dashed; border-width: medium;">Medium width.</p>

<p style="border-style: solid; border-width: thick;">Thick width.</p>

<p style="border-style: dotted; border-width: 5px">Specific length width


border.</p>

<p style="border-style: solid; border-width: 2px 4px 5px 10px">Mixed length


width border.</p>

</body>

</html>

CSS Border Width - Single Side


The property border-width can be set independently for each single-side. The same set of values can
be passed to each single-side for border-width:

 border-top-width
 border-right-width
 border-bottom-width
 border-left-width
Example
Let us see an example:
<html>

<head>

<style>

p {

border-style: solid; padding: 2em;

border-top-width: thin; border-right-width: thick;

border-bottom-width: medium; border-left-width: 10px;

</style>

</head>

<body>

<h2>border-width (single-side)</h2>

<p>Different border styles on all the sides.</p>

</body>

<html>

CSS Border Color


The border-color property is the third constituent property of border. It sets the color of the border.
 border-color property can have one, two, three or all four values.
 If you don’t declare a color, the default color is the foreground color of the element.
 Any type of color value can be passed, such as Name, RGB, RGBA, Hexadecimal, etc.
Following values can be passed to border-color:

Value Description

color The border will be of the color specified

transparent The border will be transparent

inherit The parent element's value is inherited

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>

<h2>border-color with different values</h2>

<p style="border-style: double; border-color: red;">Red Color Border.</p>

<p style="border-style: dashed; border-color: #40a944;">Green Color


Border</p>

<p style="border-style: solid; border-color: rgb(255,200,0);">Yellow Color


Border</p>

<p style="border-style: dotted; border-color: hsl(0, 50%, 50%)">Brown Color


Border</p>

<p style="border-style: solid; border-color: red blue green yellow">Mixed


Color Borders</p>

</body>

</html>

CSS Border Color - Single Side


The property border-color can be set independently for each single-side. The same set of values can
be passed to each single-side for border-color:

 border-top-color
 border-right-color
 border-bottom-color
 border-left-color
Example
Let us see an example:
<html>

<head>

<style>

p {

border-style: solid; padding: 2em;

order-top-color: red; border-right-color: #0000ff;

border-bottom-color: rgb(100,123,111); border-left-color:


rgba(50,123,111,0.4)

</style>
</head>

<body>

<h2>border-color (single-side)</h2>

<p>Different border colors on all the sides.</p>

</body>

</html>

CSS Border - Single-Side Shorthand Properties


In case you want to apply all the border properties, such as style, width and color, along just one side
of an element, you can make use of the border shorthand properties.
For example, if the border properties are to be applied to top side of an h2 element, you can use the
following syntax:
h2 {border-top: thin solid red;}
Four shorthand properties, based on each side of any element, are as follows:

 border-top
 border-right
 border-bottom
 border-left
Example
Let us see an example:
<html>

<head>

<style>

p {

border-top: red dashed thick;

border-right: solid #0000ff medium;

border-bottom: 10px double rgb(100,123,111);

border-left: rgba(50,123,111,0.4) 15px solid;

padding: 10px;

</style>

</head>

<body>

<h2>Shorthand single-side border properties</h2>

<p>Check the borders!!!</p>

</body>
</html>

CSS Border - Global Shorthand Property


The smallest possible shorthand property for border for all sides of an element is border.
Syntax

h2 {border: thick dotted green;}


The above code will add a green, dotted and thick border on all the four sides of h2 element.
Example
Let us see an example:
<html>

<head>

<style>

p {

border: green dashed thick; padding: 10px;

</style>

</head>

<body>

<h2>Border Shorthand Property</h2>

<p>Check the borders!!!</p>

</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 {

border: #40a944 dashed thick; border-bottom-width: 15px; padding: 10px;

</style>
</head>

<body>

<h2>Border Shorthand Property</h2>

<p>Check the borders!!!</p>

</body>

</html>

CSS Rounded Borders


We can use border-radius property to add rounded borders to an element.
Syntax

p {

border: 2px solid #40a944; border-radius: 5px;

}
The above code will add a rounded border around paragraph element.
Example

<html>

<head>

<style>

p {

border: 2px solid #40a944; border-radius: 5px; padding:10px;

</style>

</head>

<body>

<h2>Round Borders</h2>

<p>Check the borders!!!</p>

</body>

</html>

CSS Borders - Inline Elements


Borders can be given to any inline HTML element. The border's thickness will not have any effect on
the line height of element. If left and right borders are specified in an inline element, it will displace
the text around the border.
Syntax

strong {border-top: thin solid green; border-bottom: 5px dotted #ff00ff;}


Above code will apply the border to strong text in the paragraph as green, thin and solid border on the
top and magenta-colored, 5px dotted border on the bottom side.
Example
Let us see an example:
<html>

<head>

<style>

strong {

border-top: thick solid green; border-bottom: 5px dashed #ff00ff;

background-color: silver;

</style>

</head>

<body>

<div>

<h2>CSS Borders on Inline Elements</h2>

<p>Check the <strong>inline elements with borders</strong> and rest has


no border.</p>

</div>

</body>

</html>

CSS Borders - Replaced Elements


We can apply border around replaced elements such as an image but the line height will get affected
by applying border on such elements.
Syntax

img {

border: 2em solid #40a944;

}
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 {

margin: 0.25in; background-color: #eee;

</style>

</head>

<body>

<div>

<h1>This h1 element has quarter-inch margin around it!</h1>

</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 {

margin: 20px; background-color: #eee;

</style>

</head>

<body>

<div>

<h1>This h1 element has 20 pixels margin around it!</h1>

</div>

</body>

</html>

CSS Margins - Single-Side Properties


CSS provides four separate properties to set margins for left, right, top and bottom for an
element.

 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 {

margin-top: 20px; margin-right:40px; margin-bottom:10px;

margin-left:0px; background-color: #eee;

</style>

</head>
<body>

<div>

<h1>This h1 element has different margins around it!</h1>

</div>

</body>

CSS Margins - Shorthand Property


You can use margin property to set all the sides margin at different values. Following is the
syntax to use margin as shorthand property:
h1 {margin: top right bottom left}
Here the value of top, right, bottom and left could be in pixels, inches, ems, or centimeters
etc. So using the above syntax we can write our previous HTML code as follows:
<html>

<head>

<style>

div{

border:1px dotted

h1 {

margin: 20px 40px 10px 0px; background-color: #eee;

</style>

</head>

<body>

<div>

<h1>This h1 element has different margins around it!</h1>

</div>

</body>

CSS Margins - Set Three Values:


We can set margin at three values as margin: 20px 40px 10px, in this case top margin will
be 20px, right and left margins will be 40px and bottom margin will be 10px. Following is the
example. You should try to compare the output with previous example:
<html>

<head>

<style>

div {
border:1px dotted

h1 {

margin: 20px 40px 10px; background-color: #eee;

</style>

</head>

<body>

<div>

<h1>This h1 element has different margins around it!</h1>

</div>

</body>

CSS Margins - Set Two Values:


We can set margin at two values as margin: 20px 40px, in this case top and bottom
margins will be 20px, right and left margins will be 40px. Following is the example. You
should try to compare the output with previous example:
<html>

<head>

<style>

div {

border:1px dotted

h1 {

margin: 20px 40px; background-color: #eee;

</style>

</head>

<body>

<div>

<h1>An h1 element with grey background!</h1>

</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.

CSS Margins - Mix up Units:


CSS allows to mix up the types of length value you use while specifying different margins in
a shorthand property. You are not restricted to using a single length type in a given rule as
shown below:
<html>

<head>

<style>

div{

border:1px dotted

h1 {

margin: 20px 5em .5in 4ex; background-color: #eee;

</style>

</head>

<body>

<div>

<h1>An h1 element with grey background!</h1>

</div>

</body>

CSS Negative Margins


CSS allows to specify negative margins for an element. This will cause the element’s box to
stick out of its parent or to overlap other elements.
<html>

<head>

<style>

div {

border:1px dotted

h1 {

margin: 20px 40px; background-color: #eee;

p {

margin: -20px 40px; background-color: #eee;

</style>
</head>

<body>

<div>

<h1>An h1 element with grey background!</h1>

<p>This paragraph has negative margin.</p>

</div>

</body>

</html>

CSS Margins - Percentages


It is very much possible to set percentage values for the margins of an element. Percentage
margins values are computed in relation to the width of the parent element’s content area, so
they can change if the parent element’s width changes.
<html>

<head>

<style>

h1 {

margin:10%; background-color: #eee;

</style>

</head>

<body>

<div style="width: 600px; border: 1px dotted;">

<h1>An h1 element with grey background!</h1>

</div>

<div style="width: 400px; border: 1px dotted;">

<h1>An h1 element with grey background!</h1>

</div>;

</body>

</html>

CSS Margins - Inline Elements


Margins can also be applied to inline elements but top and bottom margins do not have any
effect on the line height of these nonreplaced elements and these margins are always
transparent. Though when you apply margins to the left and right sides of an inline
nonreplaced element then it will show the effect as shown in the following example.
<html>
<head>

<style>

div {

border:1px dotted

strong {

margin-top: 25px; margin-bottom: 50px;

margin-left: 25px; background-color: #eee;

</style>

</head>

<body>

<div>

<p>This text has some <strong>strong text</strong> with grey


background</p>

</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 {

margin: 25px; background-color: #eee;

</style>

</head>

<body>

<div>
<p>This text has some <strong>strong text</strong> with grey
background</p>

</div>

</body>

</html>

CSS Margins - Auto Value


In order to center an element inside its parent, use margin: 0 auto as shown in the following
example:
<html>

<head>

<style>

div {

width:600px; border:1px dotted

h1 {

margin:0 auto;

</style>

</head>

<body>

<div>

<h1>An h1 element with center position!</h1>

</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 {

width:600px; border:1px dotted

}
h1 {

display: flex; justify-content:center;

</style>

</head>

<body>

<div>

<h1>An h1 element with center position!</h1>

</div>

</body>

</html>

CSS Margins - Related Properties


Property Description

margin A shorthand property for setting all the margin properties in one
declaration

margin-bottom Sets the bottom margin of an element

margin-left Sets the left margin of an element

margin-right Sets the right margin of an element

margin-top Sets the top margin of an element

margin-trim Allows the container to trim the margins of its children

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>

CSS Lists - Properties


CSS provides a set of properties that can be applied to any list, which are as follows:
 The list-style-type allows you to control the shape or appearance of the list markers
(bullet points).
 The list-style-position allows to specify the position of the list-item markers.
 The list-style-image specifies an image for the marker rather than a bullet point or
number.
 The list-style serves as shorthand property for to manage the markers.
CSS Lists - Marker Types
CSS allows you to control the shape of the list markers (bullet points). The following example
shows how CSS property list-style-type sets different markers for the ordered and
unordered lists. The same property can be used for both ordered and unordered lists.
Unordered Lists Example

<html>

<head>

<style>

ul.a {

list-style-type: circle;

ul.b {

list-style-type: square;
}

</style>

</head>

<body>

<h2>Unordered List Example</h2>

<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>

<h2>Ordered List Example</h2>

<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.

CSS Lists - Image Marker


You might prefer to use an image as an item list marker. The CSS list-style-image property
can be used to to specify an image as an item list marker.
You can use your own bullet style. If no image is found, then default bullets are printed.
Following is an example to show the usage of list-style-image property.
<html>

<head>

<style>

ul {

list-style-image: url('/images/icon-bullet.png');

</style>

</head>

<body>

<h2>CSS Lists - Image Marker</h2>

<ul>

<li>First</li>

<li>Second</li>

<li>Third</li>

</ul>
</body>

</html>

CSS Lists - Marker Position


The CSS list-style-position property indicates whether the marker should appear inside or
outside of the box containing the bullet points. It can have one of the following values −

 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>

<h2>CSS Lists - Marker Position</h2>

<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>

CSS Lists - Default Marker Position


If the CSS list-style-position property is set to none, then it will remove the markers/bullets.
While setting this propert to none, then we need to set margin:0 and padding:0 −
Following is an example to show the usage of list-style-position=none property.
<html>

<head>

<style>

ul {

list-style-position: none; margin:0; padding:0;

</style>

</head>

<body>

<h2>CSS Lists - Default Marker Position</h2>

<ul>

<li>First</li>

<li>Second</li>

<li>Third</li>

</ul>

</body>

</html>

CSS Lists - Shorthand Property


The CSS list-style property allows you to specify all the three list properties into a single
expression. −
Following are the values that list-style can hold:
 <list-style-type>
 <list-style-image>
 <list-style-position>
The order of the values passed to the list-style is not fixed and any of the three values can be omitted.
If any of the value(s) is missing, it will be filled by the default value for the same. But there has to be
minimum one value passed.
Following is an example to show the usage of list-style property.
<html>

<body>

<h2>CSS Lists - Shorthand Property</h2>

<h3>Three values passed</h3>

<ul style = "list-style: url('/images/icon-bullet.png') circle outside;">

<li>All the three values have been provided</li>

<li>The item marker is an image, position as outside.</li>

<li>It is used in cases we do not need to follow a sequence.</li>

</ul>

<h3>Two values passed</h3>

<ul style = "list-style: square inside">

<li>The item marker is square, with style position as inside and no


image.</li>

<li>It is used in cases we do nit need to follow a sequence.</li>

</ul>

<h3>One value passed</h3>

<ul style = "list-style: disc">

<li>The item marker is disc, with style position as outside (default)


and no image (default - none).</li>

<li>It is used in cases where we need not follow a sequence.</li>

</ul>

</body>

</html>

CSS Lists - Controlled Counting


Some time we might want to count differently on an ordered list — e.g., starting from a
number other than 1, or counting backwards, or counting in steps of more than 1.
There are following three CSS attributes which helps in controlling the list numbering.
 <start> - allows you to start the list counting from a number other than 1.
 <reversed> - starts the list counting reverse or down instead of up.
 <value> - allows you to set your list items to specific numerical values.
Following is an example to show the usage of all these three attributes.
<html>
<body>

<h2>CSS Lists - Controlled Counting</h2>

<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>

CSS Lists - Setting Colors


We can make lists look more stylish and colorful using CSS styling as demonstared in the
following example. As we see any styling added to the <ul> or <ol> tag will affect the entire
list, whereas the addition to the individual <li> tag will affect only the items of the
corresponding list.
Following is an example to show the usage of setting up different CSS properties for a list.
<html>
<head>

<style>

ol {

list-style: upper-latin; background: Aquamarine; padding:20px;

ol li {

background: silver; padding:10px; font-size:20px; margin:10px;

ul {

list-style: square inside; background: teal; padding:20px;

ul li {

background: olive; color:white; padding:10px; font-size:20px;


margin:10px;

</style>

</head>

<body>

<h2>CSS Lists - Setting Colors</h2>

<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.

list-style-image Sets an image as a list item's marker.

list-style-position Sets the position of the list-item markers (bullet points)

list-style-type Sets the type of list-item marker

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{

border: 1px solid #aaa;

h2{

padding: 5px; background-color: #eee;

</style>

</head>
<body>

<div>

<h2>The padding can be seen around the text.</h2>

</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{

border: 1px solid #aaa;

h2{

padding: 0.25in; background-color: #eee;

</style>

</head>

<body>

<div>

<h2>A 0.25 inch padding can be seen around the text.</h2>

</div>

</body>

</html>

CSS Padding vs No-padding


Consider the following example where first h2 element is having a padding around it where
as next h2 element does not have any padding around it. Check out the difference by seeing
its output:
<html>

<head>

<style>

div{

border: 1px solid #aaa;


}

h2.a{

padding: 10px; background-color: #eee;

h2.b{

background-color: #eee;

</style>

</head>

<body>

<div>

<h2 class="a">The padding can be seen around this text.</h2>

<h2 class="b">There is no padding around this text.</h2>

</div>

</body>

</html>

CSS Paddings - Single-side Properties


CSS provides four separate properties to set padding for top, right, bottom, and left for an
element. These properties are:

 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{

border:1px solid #aaa

h2 {

padding-top: 20px; padding-right:40px;

padding-bottom:10px; padding-left:0px;

background-color: #eee;
}

</style>

</head>

<body>

<div>

<h2>This h2 element has different paddings around it!</h2>

</div>

</body>

</html>

CSS Paddings - Shorthand Property


CSS allows to set all the paddings using single statement. Following is the syntax to use
padding as shorthand property:
h2 {padding: top right bottom left}
The value for padding properties can be in pixels, inches, ems, or centimeters, apart from
percentage. So using the above syntax we can write our previous HTML code as follows:
<html>

<head>

<style>

div{

border:1px solid #aaa

h2 {

padding: 20px 40px 10px 0px;

background-color: #eee;

</style>

</head>

<body>

<div>

<h2>This h2 element has different paddings around it!</h2>

</div>

</body>

</html>

CSS Paddings - Set Three Values


Three values can be passed to the padding as padding: 20px 40px 10px. In this case top
padding will be 20px, right and left paddings will be 40px and bottom padding will be 10px.
Following is the example for the same:
<html>

<head>

<style>

h2 {

padding: 20px 40px 10px; background-color: #eee;

</style>

</head>

<body>

<div>

<h2>This h2 element has different paddings around it!</h2>

</div>

</body>

</html>

CSS Paddings - Set Two Values


Two values can be passed to the padding as padding: 20px 40px. In this case top and
bottom paddings will be 20px, right and left paddings will be 40px.
Following is the example for the same:
<html>

<head>

<style>

h2 {

padding: 20px 40px; background-color: #eee;

</style>

</head>

<body>

<div>

<h2>This h2 element has different paddings around it!</h2>

</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.

CSS Paddings - Mix up Units


CSS does not restrict usage of multiple units for specifying the values of padding, while
specifying in shorthand property. That means one can pass values of length as pixel along
with ems or inches, etc.
<html>

<head>

<style>

h2 {

padding: 20px .5in 3em 4ex; background-color: #eee;

</style>

</head>

<body>

<div>

<h2>This h2 element has different paddings around it!</h2>

</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.

CSS Paddings - Percentages


CSS Padding property can have a percentage value. Percentages are calculated in relation
to the width of the parent element’s content area. For example a parent width is 500px and
we set 10% padding for its child element then CSS will calculate 10% padding of 500px, ie.
50px pixles padding for the child element. Following is an example for the same:
<html>

<head>

<style>

div {

width: 500px; border:1px solid #aaa;

h2 {

padding: 10%; background-color: #eee;

}
</style>

</head>

<body>

<div>

<h2>H2 element with some paddings around it!</h2>

</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 {

width: 500px; border:1px solid #aaa;

h2 {

padding-top: 0.5in; padding-bottom: 10%; background-color: #eee;

</style>

</head>

<body>

<div>

<h2>H2 element with some paddings around it!</h2>

</div>

</body>

</html>

Padding - Inline Elements


Padding can also be applied to inline elements but it will have absolutely no effect on the line
height of these non-replaced elements.
An inline non-replaced element with a background color and padding will have a background
that extends above and below the element, which can be observed in the following example:
<html>

<head>

<style>
strong {

padding-top: 0.5em; padding-bottom: 10%; background-color: #eee;

</style>

</head>

<body>

<div>

<h2>This example is to demonstrate the padding applied for


<strong>inline non-replaced elements, which is 0.5em</strong>. This will not
have an effect on the line height.</h2>

</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 {

padding-left: 50px; padding-right: 50px; background-color: #eee;

</style>

</head>

<body>

<div>

<h2>This example is to demonstrate the padding left and right applied


for <strong>inline non-replaced elements, which is 50px</strong>. This will
not have an effect on the line height.</h2>

</div>

</body>

</html>

Padding and Replaced Elements


Padding can be applied to replaced elements whether it is block-level or inline, such as an
image, iframe, or video elements. Padding of an inline replaced element does have an effect
on the line height.
Replaced elements are those whose representation is outside the scope of CSS. Some of
the common replaced elements are:
 <iframe>
 <video>
 <embed>
 <img>
Let us see an example for padding applied to a replaced element (<img>):
<html>

<head>

<style>

img {

padding: 25px; background:#eee; border:1px solid #aaa

</style>

</head>

<body>

<div>

<h5>This example demonstrates the padding applied to a replaced


elements.</h5>

<img src="/images/logo.png" alt="logo">

</div>

</body>

</html>

Padding - Related Properties


You can explore more examples on padding properties by visiting the sub topics listed in the
following table:

Property Description

padding A shorthand property that is used for setting all the padding
properties in one declaration.

padding-top Sets the top padding of an element.

padding-right Sets the right padding of an element.

padding-bottom Sets the bottom padding of an element.

padding-left Sets the left padding of an element.


CSS - Cursors
The cursor property of CSS allows you to specify the type of cursor that should be displayed
to the user.
One good usage of this property is in using images for submit buttons on forms. By default,
when a cursor hovers over a link, the cursor changes from a pointer to a hand. However, it
does not change form for a submit button on a form. Therefore, whenever someone hovers
over an image that is a submit button, it provides a visual clue that the image is clickable.
The following table shows the possible values for the cursor property −

Sr.No Value & Description


.

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>

<div style = "cursor:pointer">Pointer</div>


<div style = "cursor:move">Move</div>
<div style = "cursor:e-resize">e-resize</div>
<div style = "cursor:ne-resize">ne-resize</div>
<div style = "cursor:nw-resize">nw-resize</div>

<div style = "cursor:n-resize">n-resize</div>


<div style = "cursor:se-resize">se-resize</div>
<div style = "cursor:sw-resize">sw-resize</div>
<div style = "cursor:s-resize">s-resize</div>
<div style = "cursor:w-resize">w-resize</div>

<div style = "cursor:text">text</div>


<div style = "cursor:wait">wait</div>
<div style = "cursor:help">help</div>
</body>
</html>

It will produce the following result −

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.

CSS Outline Width


The outline-width property specifies the width of the outline to be added to the box. Its
value should be a length or one of the values thin, medium, or thick, just like the border-
width attribute.

 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 style = "outline-width: thin; outline-style:solid; padding: 5px">

This text is having thin outline.

</p>

<p style = "outline-width: medium; outline-style:solid; padding: 5px">

This text is having thick outline.

</p>

<p style = "outline-width: thick; outline-style:solid; padding: 5px">

This text is having 5px outline.

</p>

<p style = "outline-width: 7px; outline-style:solid; padding: 5px">

This text is having 7px outline.

</p>

</body>

</html>

CSS Outline Style


The outline-style property specifies the style for the line (solid, dotted, or dashed) that goes
around an element. It can take one of the following values −

 auto − Default outline provided by the browser.


 none − No outline is used. The outline-width is 0.
 solid − Outline is a single solid line.
 dotted − Outline is a series of dots.
 dashed − Outline is a series of short line segments.
 double − Outline is two solid lines.
 groove − Outline looks as though it is carved into the page.
 ridge − Outline looks the opposite of groove.
 inset − Outline makes the box look like it is embedded in the page.
 outset − Outline makes the box look like it is coming out of the canvas.
Example
Here is an example −
<html>

<body>

<p style = "outline-width:thick; outline-style:solid; padding:5px;">

This text is having solid outline.

</p>

<p style = "outline-width:thick; outline-style:dotted; padding:5px;">

This text is having dotted outline.

</p>

<p style = "outline-width:thick; outline-style:dashed; padding:5px;">

This text is having dashed outline.

</p>

<p style = "outline-width:thick; outline-style:double; padding:5px;">

This text is having double outline.

</p>

<p style = "outline-width:thick; outline-style:groove; padding:5px;">

This text is having groove outline.

</p>

<p style = "outline-width:thick; outline-style:ridge; padding:5px;">

This text is having ridge outline.

</p>

</body>

</html>

CSS Outline Color


The outline-color property is used to specify the color of the outline. Its value should either
be a color name, a hex color, or an RGB value, as with the color and border-color properties.

 Name - Example red, blue or green


 HEX - Example #ff0000
 RGB - Example rgb(255,0,0)
 HSL - Example hsl(0, 100%, 50%)
 invert - inverts the color with background
Example
Here is an example −
<html>
<body>

<p style = "outline-width:thick; outline-style:solid; outline-


color:red;padding:5px;">

Outline color set with name "red".

</p>

<p style = "outline-width:thick; outline-style:solid; outline-


color:#40a944; padding:5px;">

Outline color set with Hex code "#40a944".

</p>

<p style = "outline-width:thick; outline-style:solid; outline-


color:rgb(255,200,0); padding:5px;">

Outline color set with RGB code "rgb(255,200,0)".

</p>

<p style = "outline-width:thick; outline-style:solid; outline-color:hsl(0,


50%, 50%); padding:5px;">

Outline color set with HSL code "hsl(0, 50%, 50%)".

</p>

<p style = "outline-width:thick; outline-style:solid; outline-color:invert;


padding:5px;">

Outline color set with invert option.

</p>

</body>

</html>

CSS Outline Offset


The outline-offset property is used to specify the space between an outline and the border
edge of an element. The space between the outline and the element is transparent.
The following example show an outline 20px outside the border of the element:

Outline Offset Example


The above example shows that the space between an element's border and its outline is
transparent.

Example
Here is an example −
<html>

<body>
<p style = "border:1px solid #000; outline:1px solid red; outline-
offset:20px;margin:25px">

Outline offset 20px;

</p>

<br>

<p style = "border:1px solid #000; outline:1px solid red; outline-


offset:10px;margin:15px">

Outline offset 10px;

</p>

<br>

<p style = "border:1px solid #000; outline:1px solid red; outline-


offset:5px;margin:10px">

Outline offset 5px;

</p>

</body>

</html>

CSS Outline Shorthand


The outline property is a shorthand property that allows you to specify values for any of the
three properties, style, color and width. You can specify these properties in any order using
the follwing syntax.
Syntax

outline: width style color;


Example
Here is an example −
<html>

<body>

<p style = "outline:thin solid red; padding:5px;">

This text is having thin solid red outline.

</p>

<br />

<p style = "outline:thick dashed #009900; padding:5px;">

This text is having thick dashed green outline.

</p>

<br />
<p style = "outline:5px dotted rgb(13,33,232); padding:5px;">

This text is having 5x dotted blue outline.

</p>

</body>

</html>

CSS Outline vs Border


The two obvious differences are that outlines cannot have a hidden style where as a
borders can be hidden second an outline can have auto style where as border can't have it.
Following table illustrates more differences between outline and border:

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 is typically used to highlight or It can be used for various purposes, such as


emphasize an element, such as when an separating elements, creating boxes, and adding
element is focused or activated. visual emphasis.

It can be created using It can be created using the border property in CSS.
the outline property in CSS.

CSS Outline - Related Properties


You can explore more examples on CSS outline properties by visiting the sub topics listed in
the following table:

property Description

outline A shorthand property for setting all the outline properties in one declaration

outline-color Sets the outline color of an element


outline-style Sets the outline style of an element

outline-width Sets the outline width of an element

outline-offset Sets the outline offset of an element

CSS - Height & Width (Dimension)


The dimesions of HTML elements is often specified with CSS width and height properties and we
can use these properties to set the dimension of the elements.
CSS also provides properties like max-width, min-width, max-height and min-height to set the
maximum/minimum width and height of an element.

Setting Height and Width using CSS


The height and width properties allow you to set the height and width of an element. These properties
can hold following values:
 length: The height and width of an element can be of any unit of length (px, pt, em, in, etc.)
 percentage (%): The value can be passed as a percentage value, which is in percent of the
containing block.
 auto: Browser calculates the height and width of the element. It is the default value.
 initial: Sets the value of height and width to its default value.
 inherit: The value of height and width is inherited from its parent value.
The height and width properties do not add anything to the layout of the element i.e they do not
include padding, margin or borders. They set the height and width of the area inside the padding,
border, and margin of the element.

Example
Following example demonstrates using height and width for a div.
<html>

<head>

<style>

div {

height: 100px; width: 80%; background-color: #eee;

</style>

</head>

<body>

<h2>Setting Height and Width Properties</h2>

<div>This div element has a height of 100px and a width of 80%.</div>


</body>

</html>

Setting max-height using CSS


CSS can limit the maximum height of an element using max-height property. This property allows to
specify maximum height of an element. The value of the max-height property can be:
 none: No maximum height value is set. This is the default value.
 length: Sets the maximum height in terms of length units (px, pt, em, in, etc.)
 percentage (%): The value is relative to the percent of containing block.
 initial: Sets the value of height and width to its default value.
 inherit: The value is inherited from its parent value.
Example
Here is an example to set max-height using CSS −
<html>

<head>

<style>

div.a {

max-height: 100px; width: 80%; overflow: auto;

background-color: #eee; padding:10px;

</style>

</head>

<body>

<div class="a">

<h2>max-height: 100px and width:80%</h2>

<p>The <i>max-height</i> property allows you to specify maximum height


of an element. The value of the max-height property can be various, but this
div can be maximum 100px high and so it is creating a scrollbar to fit the
content.</p>

</div>

</body>

</html>

Setting min-height using CSS


CSS can limit the minimum height of an element using min-height property. This property allows to
specify minimum height of an element. It specifies the smallest height that an element can have,
ensuring that it will never shrink below that value. The value of the min-height property can be:
 length: Sets the minimum height in terms of length units (px, pt, em, in, etc.)
 percentage (%): The value is relative to the percent of containing block.
 initial: Sets the value of height and width to its default value.
 inherit: The value is inherited from its parent value.
The minimum height will be applied, when the content is smaller than the minimum height. And
when the content is larger than the mimimum height, the value of min-height has no effect on the
element.
Example
Here is an example to set min-height using CSS −
<html>

<head>

<style>

div.a {

min-height:200px; width: 80%; overflow: auto;

background-color: #eee; padding:10px;

</style>

</head>

<body>

<div class="a">

<h2>min-height: 200px and width:80%</h2>

<p>The <i>min-height</i> property allows you to specify minimum height


of an element. The value of the min-height property can be various, but this
div can not shrink below to 200px even if you reduce the screen height less
than 200px.</p>

</div>

</body>

</html>

Setting max-width using CSS


CSS can limit the maximum width of an element using max-width property. This property allows to
specify maximum width of an element. The value of the max-width property can be:
 none: No maximum width value is set. This is the default value.
 length: Sets the maximum width in terms of length units (px, pt, em, in, etc.)
 percentage (%): The value is relative to the percent of containing block.
 initial: Sets the value of height and width to its default value.
 inherit: The value is inherited from its parent value.
The max-width value overrides the value of width property. If the content within the element is
larger than the specified max-width, it will automatically adjust the height of the element to
accommodate the content within the element. If the content within the element is smaller than the
specified max-width, the max-width property has no effect.
Example
Here is an example to set max-width using CSS −
<html>

<head>

<style>

div.a {

max-width: 600px; overflow: auto;

background-color: #eee; padding:10px;

</style>

</head>

<body>

<div class="a">

<h2>max-width: 600px </h2>

<p>The <i>max-width</i> property allows you to specify maximum width of


an element. This div can have maxmum width of 600px and if it has larger
content than its width then it will adjust the height to fit the content.</p>

</div>

</body>

</html>

Setting min-width using CSS


CSS can limit the minimum width of an element using min-width property. This property allows to
specify minimum width of an element. It specifies the smallest width that an element can have,
ensuring that it will never shrink below that value. The value of the min-width property can be:
 length: Sets the minimum width in terms of length units (px, pt, em, in, etc.)
 percentage (%): The value is relative to the percent of containing block.
 initial: Sets the value of height and width to its default value.
 inherit: The value is inherited from its parent value.
If the content with the element is larger than the min-width, the min-width property has no effect but
if the content with the element is smaller than the specified min-width, the minimum width will be
applied.
Example
Here is an example to set min-width using CSS −
<html>

<head>

<style>
div.a {

min-width:400px; width: 80%; overflow: auto;

background-color: #eee; padding:10px;

</style>

</head>

<body>

<div class="a">

<h2>min-width: 400px and width:80%</h2>

<p>The <i>min-width</i> property allows you to specify minimum width of


an element. This div can not shrink below to 400px even if you reduce the
screen width less than 400px.</p>

</div>

</body>

</html>

Setting line-height using CSS


The line-height property allows you to set the space between lines of text. The value of the line-
height property can be:
 length: The value passed is used in the calculation of line box height (px, pt, em, in, etc.)
 percentage (%): The value is relative to the font size of the element.
 number: It is a unitless number, that is multiplied by the element's own font-size.
 normal: It is a keyword. The default value is 1.2, but it depends on the element's font-family..
Example
Here is an example to set line-height using CSS −

<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 −

Sr.No Value & Description


.

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 />

<p>Example of auto value:</p>

<div class = "auto">


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>
</body>
</html>

It will produce the following result −

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.

Create Dropdowns using CSS


Let us see a simple example of a CSS dropdowns with list of options. Try to click Edit &
Run button to see the result of this code. When you hover over the "Select an Option" text, a
dropdown menu appears with different options.
Example

<html>

<head>

<style>
.dropdown {

position: relative; display: inline-block;

.dropdown-button {

background-color: #40a944; padding: 10px 20px; border: none;

cursor: pointer; color: #ffffff; margin: 0; width:155px;

.dropdown-menu {

display: none; position: absolute; background-color: #fff;

border: 1px solid #ccc; margin: 0; min-width: 160px;

padding: 12px 16px; z-index: 1;

.dropdown-button, .dropdown:hover .dropdown-menu {

display: block;

</style>

</head>

<body>

<div class="dropdown">

<span class="dropdown-button">Select an Option</span>

<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 {

position: relative; display: inline-block;

.dropdown-button {

background-color: #40a944; padding: 15px; border: none;

cursor: pointer; color: #ffffff; margin: 0; width: 162px;

.dropdown-menu {

display: none; position: absolute; background-color: #fff;

border: 1px solid #ccc; padding: 0; margin: 0;

min-width: 160px; list-style-type: none;

.dropdown-menu li {

padding: 10px; background-color: #15AD39;

.dropdown-menu li a {

display: block; text-decoration: none; color: #ffffff;

.dropdown:hover .dropdown-menu {

display: block;

.dropdown-menu li:hover {
background-color: #82ea32;

</style>

</head>

<body>

<div class="dropdown">

<button class="dropdown-button">Select an Option</button>

<ul class="dropdown-menu">

<li><a href="#">Option 1</a></li>

<li><a href="#">Option 2</a></li>

<li><a href="#">Option 3</a></li>

</ul>

</div>

</body>

</html>

CSS Clickable Dropdowns


When you click on a dropdown, it expands to show the available options, and you can select
one of these options by clicking on it.
Example
Let us see an example:
<html>

<head>

<style>

.dropdown-button {

background-color: #40a944; padding: 15px; border: none; cursor: pointer;

color: #ffffff; margin: 0; width: 162px;

.dropdown-menu {

display: none; position: absolute; background-color: #fff;

border: 1px solid #ccc; margin: 0; padding: 0; min-width: 160px;

list-style-type: none;

.dropdown-menu li {

padding: 10px; background-color: #15AD39;


}

.dropdown-menu li a {

display: block; text-decoration: none; color: #ffffff;

.dropdown-menu li:hover {

background-color: #82ea32;

.show {

display: block;

</style>

<script>

document.addEventListener('DOMContentLoaded', function () {

const button = document.querySelector('.dropdown-button');

const dropdownContent = document.querySelector('.dropdown-menu');

button.addEventListener('click', function () {

dropdownContent.classList.toggle('show');

});

document.addEventListener('click', function (event) {

if (!event.target.matches('.dropdown-button') &&
dropdownContent.classList.contains('show')) {

dropdownContent.classList.remove('show');

});

});

</script>

</head>

<body>

<div class="dropdown">

<button class="dropdown-button">Select an Option</button>

<ul class="dropdown-menu">

<li><a href="#">Option 1</a></li>

<li><a href="#">Option 2</a></li>

<li><a href="#">Option 3</a></li>


</ul>

</div>

</body>

</html>

CSS Dropdown Menu - Right-aligned


You can place a dropdown menu on the right side of the screen by applying a float:
right style to the <div> that contains the dropdown menu. This arrangement shifts the menu
to the right-hand side of the screen.
Example
Let us see an example:
<html>

<head>

<style>

.dropdown {

position: relative; display: inline-block; float: right;

.dropdown-button {

background-color: #40a944; padding: 15px; border: none;

cursor: pointer; color: #ffffff; margin: 0; width: 162px;

.dropdown-menu {

display: none; position: absolute; background-color: #fff;

border: 1px solid #ccc; padding: 0; margin: 0;

min-width: 160px; list-style-type: none;

.dropdown-menu li {

padding: 10px; background-color: #15AD39;

.dropdown-menu li a {

display: block; text-decoration: none; color: #ffffff;

.dropdown:hover .dropdown-menu {

display: block;

}
.dropdown-menu li:hover {

background-color: #82ea32;

</style>

</head>

<body>

<div class="dropdown">

<button class="dropdown-button">Select an Option</button>

<ul class="dropdown-menu">

<li><a href="#">Option 1</a></li>

<li><a href="#">Option 2</a></li>

<li><a href="#">Option 3</a></li>

</ul>

</div>

</body>

</html>

CSS Dropdown Menu - Left-aligned


You can achieve a left-aligned dropdown menu by adding the float: left style to
the <div> containing the dropdown menu. This arrangement shifts the menu to the leftmost
part of the screen.
Example
Let us see an example:
<html>

<head>

<style>

.dropdown {

position: relative; display: inline-block; float: left;

.dropdown-button {

background-color: #40a944; padding: 15px; border: none;

cursor: pointer; color: #ffffff; margin: 0; width: 162px;

.dropdown-menu {

display: none; position: absolute; background-color: #fff;

border: 1px solid #ccc; padding: 0; margin: 0; min-width: 160px;


list-style-type: none;

.dropdown-menu li {

padding: 10px; background-color: #15AD39;

.dropdown-menu li a {

display: block; text-decoration: none; color: #ffffff;

.dropdown:hover .dropdown-menu {

display: block;

.dropdown-menu li:hover {

background-color: #82ea32;

</style>

</head>

<body>

<div class="dropdown">

<button class="dropdown-button">Select an Option</button>

<ul class="dropdown-menu">

<li><a href="#">Option 1</a></li>

<li><a href="#">Option 2</a></li>

<li><a href="#">Option 3</a></li>

</ul>

</div>

</body>

</html>

CSS Dropdown Menu with Images


You can enhance the dropdown by including images along with the textual options. When
you hover over small image or menu button, a larger size image appears along with a
description.
Example
Let us see an example:
<html>
<head>

<style>

.dropdown {

position: relative; display: inline-block;

.dropdown-img-menu {

display: none; position: absolute;background-color: #fff;border: 1px


solid #ccc;

.dropdown:hover .dropdown-img-menu {

display: block;

.img-descripition {

padding: 15px; background-color: rgb(38, 222, 53);text-align: center;

</style>

</head>

<body>

<div class="dropdown">

<img src="images/red-flower.jpg" alt="Red Flower" width="100"


height="50">

<div class="dropdown-img-menu">

<div class="img-descripition">Beautiful Red Flower</div>

<img src="images/red-flower.jpg" alt="Red Flower" width="200"


height="150">

</div>

</div>

</body>

</html>

CSS Dropdown Menu - Navbar


A dropdown navbar is commonly found in website navigation menus. It consists of a
horizontal bar that contains a list of links. When users hover over or click on a specific link, a
dropdown menu appears, display additional navigation options or content related to the
selected link.
Now we will apply our dropdown menu knowledge to create a beautiful navigation bar using
CSS.
Example
Let us see an example:
<html>

<head>

<style>

ul {

list-style-type: none; margin: 0; padding: 0; overflow: hidden;

background-color: #40a944;

li {

float: left;

li a, .dropdown-option {

display: inline-block; color: white; text-align: center; padding: 14px


16px;

text-decoration: none; z-index: 99;

li a:hover,.dropdown:hover .dropdown-option {

background-color: #82ea32;

li.dropdown {

display: inline-block;

.dropdown-menu {

display: none; position: absolute; min-width: 160px;

box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);

min-width: 160px; background-color: #f9f9f9; z-index: 1;

.dropdown-menu a {

color: black; text-decoration: none; display: block; text-align: left;

.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">

<a href="#" class="dropdown-option">Articles</a>

<div class="dropdown-menu">

<a href="#">HTML</a>

<a href="#">CSS</a>

<a href="#">Bootstrap</a>

</div>

</li>

</ul>

<h2>Dropdown Menu inside a Navigation Bar</h2>

<p>Hover over the "Articles" link to see the dropdown menu.</p>

</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;

Overflow and Float Property


Let us see an example where an image is larger than its container's height, causing the
image to extend beyond the boundaries of its container and potentially disrupting the layout.
Example

<html>

<head>

<style>

div {

border: 2px solid #f0610e; padding: 5px; background-color: #40a944;

.img {

float: right; border: 3px solid #40a944;


}

</style>

</head>

<body>

<h2>Without Clearfix</h2>

<div>

<img class="img" src="images/tutimg.png" width="200" height="200">

<p>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. If you are going
to use a passage of Lorem Ipsum, you need to be sure there isn't anything
embarrassing hidden in the middle of text. All the Lorem Ipsum generators on
the Internet tend to repeat predefined chunks as necessary, making this the
first true generator on the Internet.</p>

</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 {

border: 2px solid #f0610e; padding: 5px;

background-color: #40a944; overflow: auto;

.img {

float: right; border: 3px solid #40a944;

</style>

</head>

<body>

<h2>With Clearfix</h2>

<div>

<img class="img" src="images/tutimg.png" width="200" height="200">


<p>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. If you are going
to use a passage of Lorem Ipsum, you need to be sure there isn't anything
embarrassing hidden in the middle of text. All the Lorem Ipsum generators on
the Internet tend to repeat predefined chunks as necessary, making this the
first true generator on the Internet.</p>

</div>

</body>

</html>

Setting CSS Height Property


You can also acheive clearfix by setting the height of <div> element similar to the height of
the floated image.
Example
Let us see an example:
<html>

<head>

<style>

div {

border: 2px solid #f0610e; padding: 10px;

height: 120px; background-color: #40a944;

.img {

float: right; border: 3px solid #f0610e;

</style>

</head>

<body>

<div>

<img class="img" src="images/tutimg.png" width="120" height="120">

<p>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. If you are going
to use a passage of Lorem Ipsum, you need to be sure there isn't anything
embarrassing hidden in the middle of text. All the Lorem Ipsum generators on
the Internet tend to repeat predefined chunks as necessary, making this the
first true generator on the Internet.</p>

</div>
</body>

</html>

Setting CSS Clear Property


The CSS Clear property applies to floating and non-floating elements. This sets whether an
element must be moved below (cleared) floating elements that precede it.
The Clear property can have one of the following values:
 none: Is a keyword indicating that the element is not moved down to clear past
floating elements.
 left: Is a keyword indicating that the element is moved down to clear past left floats.
 right: Is a keyword indicating that the element is moved down to clear past right floats.
 both: Is a keyword indicating that the element is moved down to clear past both left
and right floats.
 inline-start: Is a keyword indicating that the element is moved down to clear floats on
start side of its containing block, that is the left floats on ltr scripts and the right floats
on rtl scripts.
 inline-end: Is a keyword indicating that the element is moved down to clear floats on
end side of its containing block, that is the right floats on ltr scripts and the left floats
on rtl scripts.
Setting Clear to Left
Following example demonstrates clearfix using clear:left property:
<html>

<head>

<style>

.main {

border: 1px solid black; padding: 10px;

.left {

border: 1px solid black; clear: left;

.aqua {

float: left; margin: 0; background-color: aqua; color: #000; width: 20%;

.pink {

float: left; margin: 0; background-color: pink; width: 20%;

p {

width: 50%;

</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>

<p class="pink">Lorem ipsum dolor sit amet, consectetuer adipiscing


elit.</p>

<p class="left">This paragraph clears left.</p>

</div>

</body>

</html>

Setting Clear to Right


Following example demonstrates clearfix using clear:right property:
<html>

<head>

<style>

.main {

border: 1px solid black; padding: 10px;

.right {

border: 1px solid black; clear: right;

.aqua {

float: right; margin: 0; background-color: aqua; color: #000; width:


20%;

.pink {

float: right; margin: 0; background-color: pink; width: 20%;

p {

width: 50%;

</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>

<p class="pink">Lorem ipsum dolor sit amet, consectetuer adipiscing


elit.</p>

<p class="right">This paragraph clears right.</p>

</div>

</body>

</html>
Setting Clear to Both
Following example demonstrates clearfix using clear:both property:
<html>

<head>

<style>

.main {

border: 1px solid black; padding: 10px;

.both {

border: 1px solid black; clear: both;

.aqua{

float: left; margin: 0; background-color: aqua; color: #000; width: 20%;

.pink {

float: right; margin: 0; background-color: pink; width: 20%;

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>

<p class="pink">Lorem ipsum dolor sit amet, consectetuer adipiscing


elit.</p>

<p class="both">This paragraph clears both.</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;

border-top: 2px solid #000;

border-right: 2px solid #000;

.right-arrow {

transform: rotate(45deg);

.left-arrow {

transform: rotate(-135deg);

.up-arrow {

transform: rotate(-45deg);

.down-arrow {

transform: rotate(135deg);

.top-narrow-arrow {

transform: rotate(-45deg) skew(-15deg, -15deg);

.top-wide-arrow {

transform: rotate(-45deg) skew(7deg, 7deg);

.top-left-arrow {

transform: rotate(-90deg) skew(-10deg, -10deg);

.top-right-arrow {

transform: rotate(0) skew(-10deg, -10deg);

.bottom-left-arrow {

transform: rotate(180deg) skew(-10deg, -10deg);

.bottom-right-arrow {

transform: rotate(90deg) skew(-10deg, -10deg);


}

</style>

</head>

<body>

<p class="arrow-container"><span class="arrow right-arrow"></span> - This


arrow points to the right.</p>

<p class="arrow-container"><span class="arrow left-arrow"></span> - This


arrow points to the left.</p>

<p class="arrow-container"><span class="arrow up-arrow"></span> - This


arrow points upwards.</p>

<p class="arrow-container"><span class="arrow down-arrow"></span> - This


arrow points downwards.</p>

<p class="arrow-container"><span class="arrow top-narrow-arrow"></span> -


This arrow points top and is narrow.</p>

<p class="arrow-container"><span class="arrow top-wide-arrow"></span> -


This arrow points top and is wide.</p>

<p class="arrow-container"><span class="arrow top-left-arrow"></span> -


This arrow points top left.</p>

<p class="arrow-container"><span class="arrow top-right-arrow"></span> -


This arrow points top right.</p>

<p class="arrow-container"><span class="arrow bottom-left-arrow"></span> -


This arrow points bottom left.</p>

<p class="arrow-container"><span class="arrow bottom-right-arrow"></span> -


This arrow points bottom right.</p>

</body>

</html>

CSS Arrow Using Border


The border property allows us to create a triangle by manipulating the width and height of
the element’s border and hence resulting in an arrow.

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 {

border-top: 18px solid transparent;

border-bottom: 18px solid transparent;

.up-arrow,

.down-arrow {

border-left: 15px solid transparent;

border-right: 15px solid transparent;

.right-arrow {

border-left: 25px solid #F10C0C;

.left-arrow {

border-right: 25px solid #F10C0C;

.up-arrow {

border-bottom: 25px solid #F10C0C;

.down-arrow {

border-top: 25px solid #F10C0C;

</style>

</head>

<body>
<p class="arrow-container"><span class="right-arrow"></span> - This arrow
points to the right.</p>

<p class="arrow-container"><span class="left-arrow"></span> - This arrow


points to the left.</p>

<p class="arrow-container"><span class="up-arrow"></span> - This arrow


points to the upwards.</p>

<p class="arrow-container"><span class="down-arrow"></span> - This arrow


points to the downwards.</p>

</body>

</html>

CSS Arrows Styling


We can make arrows look more stylish using CSS transformations and border properties
as demonstared in the following example.
The transform-origin: center property ensures that the rotation of each arrow occurs
around its center point.
Example
Here is an example −
<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;

border-top: 2px solid #F10C0C;

border-left: 2px solid #F10C0C;

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;

transform: rotate(-45deg) translate(15px, 4px);

</style>

</head>

<body>

<p class="arrow-container">Right Arrow - <span class="right-


arrow"></span></p>

<p class="arrow-container">Left Arrow - <span class="left-


arrow"></span></p>

<p class="arrow-container">Up Arrow - <span class="up-arrow"></span></p>

<p class="arrow-container">Down Arrow - <span class="down-


arrow"></span></p>

</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;

box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);

z-index: 1;

.dropdown-btn::after {

content: "";

width: 0;

height: 0;
border-left: 6px solid transparent;

border-right: 6px solid transparent;

border-top: 6px solid white;

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">

<a href="#" class="dropdown-item">Item 1</a>

<a href="#" class="dropdown-item">Item 2</a>

<a href="#" class="dropdown-item">Item 3</a>

</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;

border-color: transparent transparent #F10C0C transparent;

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

<span class="tooltipcontent">CSS - Arrow</span>

</h3>

</body>

</html>

Animated CSS Arrows


By using CSS animations, we can create arrows that move and pulse, adding a dynamic
effect to the web pages. The following example demonstrates an animated arrow that moves
up and down. To create an animated arrow, we have used the @keyframes rule in CSS to
define a set of animations that will be applied to the arrow.
<html>

<head>

<style>

.arrow-container {

display: flex;

align-items: center;

.left-arrow

width: 0;

height: 0;

margin: 5px;

.left-arrow

border-top: 18px solid transparent;

border-bottom: 18px solid transparent;

.left-arrow {

border-right: 25px solid #F10C0C;

.arrow-move {

position: relative;

animation: move 2s ease-in-out infinite;


}

@keyframes move {

0% {

transform: translateY(0);

50% {

transform: translateY(-10px);

100% {

transform: translateY(0);

</style>

</head>

<body>

<p class="arrow-container"><span class="left-arrow arrow-move"></span> -


This arrow points to the left.</p>

</body>

</html>

You might also like