SlideShare a Scribd company logo
1 of 121
Download to read offline
CSS
Meenakshi Patel
CSS
● CSS stands for Cascading Style Sheets
● CSS describes how HTML elements are to be displayed on screen, paper,
or in other media
● CSS saves a lot of work. It can control the layout of multiple web pages
all at once
● External stylesheets are stored in CSS files
Advantages of Using CSS
CSS Save Lots of Time
Easy Maintenance
Pages Load Faster
Superior Styles to HTML
Multiple Device Compatibility
Including CSS in HTML Documents
● Inline styles — Using the style attribute in the HTML start tag.
● Embedded styles — Using the <style> element in the head section of a document.
● External style sheets — Using the <link> element, pointing to an external CSS file.
Inline Styles
Inline styles are used to apply the unique style rules to an element by putting the CSS rules
directly into the start tag.
It can be attached to an element using the style attribute.
The style attribute includes a series of CSS property and value pairs. Each "property:
value" pair is separated by a semicolon (;)
<h1 style="color:red; font-size:30px;">This is a heading</h1>
<p style="color:green; font-size:22px;">This is a paragraph.</p>
<div style="color:blue; font-size:14px;">This is some text content.</div>
Embedded Style Sheets
Embedded or internal style sheets only affect the document they are embedded in.
Embedded style sheets are defined in the <head> section of an HTML document using the <style> element.
<!DOCTYPE html>
<html lang="en">
<head>
<title>My HTML Document</title>
<style>
body { background-color: YellowGreen; }
p { color: #fff; }
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph of text. </p>
</body>
</html>
External Style Sheets
An external style sheet is ideal when the style is applied to many pages of the website.
An external style sheet holds all the style rules in a separate document that you can link from any HTML file on
your site.
Linking External Style Sheets
body {
background: lightyellow;
font: 18px Arial, sans-serif;
}
h1 {
color: orange;
}
An external style sheet can be linked to an HTML document using the <link> tag.
The <link> tag goes inside the <head> section.
<!DOCTYPE html>
<html lang="en">
<head>
<title>My HTML Document</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</body>
</html>
Importing External Style Sheets
The @import rule is another way of loading an external style sheet.
The @import statement instructs the browser to load an external style sheet and use its styles.
<style>
@import url("css/style.css") ;
p {
color: blue;
font-size: 16px;
}
</style>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS Imported
Style Sheet</title>
<style>
@import url("style.css");
p {
color: blue;
font-size: 16px;
}
</style>
</head>
<body>
<h1>The styles for this
heading are defined in the imported
style sheet</h1>
<p>The styles for this
paragraph are defined in the
embedded style sheet.</p>
</body>
</html>
CSS Syntax
h1 {
color: blue;
text-align: center;
}
Writing Comments in CSS
A CSS comment begins with /*, and ends with */, as shown in the example below:
/* This is a CSS comment */
h1 {
color: blue;
text-align: center;
}
/* This is a multi-line CSS comment
that spans across more than one line */
p {
font-size: 18px;
text-transform: uppercase;
}
CSS Selectors
A CSS selector is a pattern to match the elements on a web page.
The style rules associated with that selector will be applied to the elements that match the selector pattern.
Universal Selector
The universal selector, denoted by an asterisk (*), matches every single element on the page.
This selector is often used to remove the default margins and paddings from the elements for quick testing
purpose.
The universal selector may be omitted if other conditions exist on the element.
The style rules inside the * selector will be applied to every element in a document.
* {
margin: 0;
padding: 0;
}
Element Type Selectors
An element type selector matches all instance of the element in the document with the corresponding element
type name.
p {
color: blue;
}
Id Selectors
The id selector is used to define style rules for a single or unique element.
The id selector is defined with a hash sign (#) immediately followed by the id value.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS id
selector</title>
<style>
#error {
color: #ff0000;
}
</style>
</head>
<body>
<p id="error">This is a warning!</p>
</body>
</html>
Class Selectors
The class selectors can be used to select any HTML element that has a class attribute.
All the elements having that class will be formatted according to the defined rule.
The class selector is defined with a period sign (.) immediately followed by the class value.
The style rule inside the selector p.blue renders the text in blue of only those <p> elements that has class
attribute set to blue, and has no effect on other paragraphs.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS class
selector</title>
<style>
.blue {
color: #0000ff;
}
</style>
</head>
<body>
<h1 class="blue">This is a heading</h1>
<p class="blue">This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS class
selector</title>
<style>
p.blue {
color: #0000ff;
}
</style>
</head>
<body>
<h1 class="blue">This is a heading</h1>
<p class="blue">This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
Descendant Selectors
You can use these selectors when you need to select an element that is the descendant of another element, for
example, if you want to target only those anchors that are contained within an unordered list, rather than targeting
all anchor elements.
The style rules inside the selector ul.menu li a applied to only those <a> elements that contained inside an
<ul> element having the class .menu, and has no effect on other links inside the document.
Similarly, the style rules inside the h1 em selector will be applied to only those <em> elements that contained
inside the <h1> element and has not effect on other <em> elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS Descendant
Selectors</title>
<style>
h1 em {
color: green;
}
ul.menu {
padding: 0;
list-style: none;
}
ul.menu li{
display: inline;
}
ul.menu li a {
margin: 10px;
text-decoration: none;
}
</style>
</head>
<body>
<h1>This is a
<em>heading</em></h1>
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a
href="#">Services</a></li>
<li><a
href="#">Contact</a></li>
</ul>
</body>
</html>
Child Selectors
A child selector is used to select only those elements that are the direct children of some element.
A child selector is made up of two or more selectors separated by a greater than symbol (>).
You can use this selector, for instance, to select the first level of list elements inside a nested list that has more
than one level.
The style rule inside the selector ul > li applied to only those <li> elements that are direct children of the <ul>
elements, and has no effect on other list elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS Child
Selectors</title>
<style>
ul > li {
list-style: square;
}
ul > li ol {
list-style: none;
}
</style>
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li>
<a href="#">Services</a>
<ol>
<li><a
href="#">Design</a></li>
<li><a
href="#">Development</a></li>
</ol>
</li>
<li><a
href="#">Contact</a></li>
</ul>
</body>
</html>
Adjacent Sibling Selectors
The adjacent sibling selectors can be used to select sibling elements (i.e. elements at the same level).
This selector has the syntax like: E1 + E2, where E2 is the target of the selector.
The selector h1 + p in the following example will select the <p> elements only if both the <h1> and <p> elements
share the same parent in the document tree and <h1> is immediately precedes the <p> element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS Adjacent Sibling
Selectors</title>
<style>
h1 + p {
color: yellow;
font-size: 60px;
}
ul.task + p {
color: #f0f;
text-indent: 30px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ul class="task">
<li>Task 1</li>
<li>Task 2</li>
<li>Task 3</li>
</ul>
<p>This is one more paragraph.</p>
<p>This is also a paragraph.</p>
</body>
</html>
General Sibling Selectors
The general sibling selector is similar to the adjacent sibling selector (E1 + E2), but it is less strict.
A general sibling selector is made up of two simple selectors separated by the tilde (∼) character.
It can be written like: E1 ∼ E2, where E2 is the target of the selector.
The selector h1 ∼ p in the example below will select all the <p> elements that preceded by the <h1> element,
where all the elements share the same parent in the document tree.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS General Sibling
Selectors</title>
<style>
h1 ~ p {
color: blue;
font-size: 18px;
}
ul.task ~ p {
color: #f0f;
text-indent: 30px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ul class="task">
<li>Task 1</li>
<li>Task 2</li>
<li>Task 3</li>
</ul>
<p>This is one more paragraph.</p>
<p>This is also a paragraph.</p>
</body>
</html>
Grouping Selectors
Often several selectors in a style sheet share the same style rules declarations.
You can group them into a comma-separated list to minimize the code in your style sheet.
It also prevents you from repeating the same style rules over and over again.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS Selectors without
Grouping</title>
<style>
h1 {
font-size: 36px;
font-weight: normal;
}
h2 {
font-size: 28px;
font-weight: normal;
}
h3 {
font-size: 22px;
font-weight: normal;
}
</style>
</head>
<body>
<h1>This is a heading of level 1</h1>
<h2>This is a heading of level 2</h2>
<h3>This is a heading of level 3</h3>
</body>
</html>
As you can see in the above example, the same style rule font-weight: normal; is shared by the
selectors h1, h2 and h3, so it can be grouped in a comma-separated list, like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS Grouping
Selectors</title>
<style>
h1, h2, h3 {
font-weight: normal;
}
h1 {
font-size: 36px;
}
h2 {
font-size: 28px;
}
h3 {
font-size: 22px;
}
</style>
</head>
<body>
<h1>This is a heading of level 1</h1>
<h2>This is a heading of level 2</h2>
<h3>This is a heading of level 3</h3>
</body>
</html>
Color Property
The color property defines the text color (foreground color in general) of an element.
For instance, the color property specified in the body selector defines the default text color for the whole page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Text Color in CSS</title>
<style>
body {
color: #ff5722;
}
.text-green {
color: #008000;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<div class="text-green">This is a simple
piece of text.</div>
</body>
</html>
Defining Color Values
Colors in CSS most often specified in the following formats:
● a color keyword - like "red", "green", "blue", "transparent", etc.
● a HEX value - like "#ff0000", "#00ff00", etc.
● an RGB value - like "rgb(255, 0, 0)"
Affect of Color Property on Borders and Outlines
The color property is not just for text content, but for anything in the foreground that takes a color value.
For instance, if border-color or outline-color value hasn't been defined explicitly for the element, the color
value will be used instead.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Foreground Color for
Elements</title>
<style>
p.one {
color: #0000ff;
border: 2px solid;
}
p.two {
color: #00ff00;
outline: 2px solid;
}
</style>
</head>
<body>
<p class="one">The border color of this
paragraph is same as the element's text
color.</p>
<p class="two">The outline color of this
paragraph is same as the element's text
color.</p>
</body>
</html>
CSS Background
CSS provide several properties for styling the background of an element, including
coloring the background, placing images in the background and managing their
positioning, etc.
The background properties are
background-color,
background-image,
background-repeat, repeat or no-repeat
background-attachment fixed or scroll
background-position.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of CSS individual
background properties</title>
<style>
body {
background-color: #98effc;
background-image:
url("/examples/images/smiley.png");
background-repeat: repeat;
background-attachment: fixed;
background-position: 20px
205px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Fonts
CSS provide several properties for styling the font of the text, including changing their
face, controlling their size and boldness, managing variant, and so on.
The font properties are:
font-family, serif, sans-serif, monospace, cursive and fantasy.
font-style, normal, italic or oblique
font-weight,
font-size, percentage, pixels, ems
Font-variant.
If the name of a font family contains more than one word, it must be placed inside
quotation marks, like "Times New Roman", "Courier New", "Segoe UI", etc.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting font-family in
CSS</title>
<style>
body {
font-family: Arial, Helvetica,
sans-serif;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Setting Font Size with EM
The em unit refers to the font size of the parent element.
So, if you set a font-size of 20px on the body element, then 1em = 20px and
2em = 40px.
However, if you haven't set the font size anywhere on the page, then it is the
browser default, which is normally 16px. Therefore, by default 1em = 16px, and
2em = 32px.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Defining Font Size Using Percentage
and EM</title>
<style>
body {
font-size: 62.5%; /* font-size 1em =
10px */
}
p {
font-size: 1.4em; /* 1.4em = 14px */
}
p span {
font-size: 2em; /* 2em = 28px */
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a
<span>paragraph</span>.</p>
</body>
</html>
Setting Font Size with Root EM
1rem is equivalent to the font size of the html element, which is 16px by default in most
browsers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Defining Font Size Using REM</title>
<style>
html {
font-size: 62.5%; /* font-size 1em =
10px */
}
body {
font-size: 1.6rem; /* 1.6rem = 16px */
}
p {
font-size: 1.4rem; /* 1.4rem = 14px */
}
p span {
font-size: 2rem; /* 2rem = 20px (not
28px) */
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a
<span>paragraph</span>.</p>
</body>
</html>
Setting Font Size with Keywords
xx-small,
x-small,
small,
medium,
large,
x-large,
xx-large
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Defining Font Size Using Keywords</title>
<style>
body {
font-size: large;
}
h1 {
font-size: larger;
}
p {
font-size: smaller;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Setting Font Size with Viewport Units
The font sizes can be specified using viewport units such as vw or vh.
Viewport units refer to a percentage of the browser's viewport dimensions, where 1vw = 1% of
viewport width, and 1vh = 1% of viewport height.
Hence, if the viewport is 1600px wide, 1vw is 16px.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Defining Font Size Using
Viewport Units</title>
<style>
body {
font-size: 16vw;
}
h1 {
font-size: 3vw;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
However, there is a problem with the viewport units.
On small screens fonts become so small that they are hardly readable.
To prevent this you can utilize CSS calc() function, like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fixing Font Size Issue with Viewport
Units</title>
<style>
html {
font-size: calc(1em + 1vw);
}
h1 {
font-size: 3rem;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Font Weight
The font-weight property specifies the weight or boldness of the font.
This property can take one of the following values: normal, bold, bolder, lighter, 100,
200, 300, 400, 500, 600, 700, 800, 900 and inherit.
● The numeric values 100-900 specify the font weights, where each number represents a
weight greater than its predecessor. 400 is same as normal & 700 is same as bold.
● The bolder and lighter values are relative to the inherited font weight, while the
other values such as normal and bold are absolute font weights.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting font-weight in
CSS</title>
<style>
p {
font-weight: bold;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph in bold
font.</p>
</body>
</html>
Font Variant
The font-variant property allows the text to be displayed in a special small-caps
variation.
Small-caps or small capital letters are slightly different to normal capital letters, in which
lowercase letters appear as smaller versions of the corresponding uppercase letters.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting font-variant in
CSS</title>
<style>
p {
font-variant: small-caps;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Text
The commonly used text properties are:
text-align, left, right, centre or justified
text-decoration, underline, overline, line-through, and none
text-transform,
text-indent,
line-height,
letter-spacing,
word-spacing,
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Text Decoration using
CSS</title>
<style>
h1 {
text-decoration:overline;
}
h2 {
text-decoration:line-through;
}
h3 {
text-decoration:underline;
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>
</html>
Removing the Default Underline from Links
The text-decoration property is extensively used to remove the default underline from the
HTML hyperlinks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Removing the Default Underline from
HTML Links</title>
<style>
a {
text-decoration: none;
border-bottom: 1px dotted;
}
a:hover {
border-bottom: none;
}
</style>
</head>
<body>
<p>Place your mouse pointer <a
href="#">over me!</a></p>
</body>
</html>
Text Transformation
Using this property you can change an element's text content into uppercase or lowercase
letters, or capitalize the first letter of each word without modifying the original text.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Text Transformation using
CSS</title>
<style>
h1 {
text-transform: uppercase;
}
h2 {
text-transform: capitalize;
}
h3 {
text-transform: lowercase;
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>
</html>
Text Indentation
The text-indent property is used to set the indentation of the first line of text within a block of
text. It is typically done by inserting the empty space before the first line of text.
The size of the indentation can be specified using percentage (%), length values in pixels, ems,
etc.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Text Indentation using
CSS</title>
<style>
p {
text-indent: 100px;
}
</style>
</head>
<body>
<h1>CSS Text Indentation</h1>
<p>Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Nam eu sem
tempor, varius quam at, luctus dui. Mauris
magna metus, dapibus nec turpis vel,
semper malesuada ante. Vestibulum id
metus ac nisl bibendum scelerisque non
non purus. Suspendisse varius nibh non
aliquet sagittis.</p>
</body>
</html>
Letter Spacing
The letter-spacing property is used to set extra spacing between the characters of text.
This property can take a length value in pixels, ems, etc.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Letter Spacing using
CSS</title>
<style>
h1 {
letter-spacing: -3px;
}
p {
letter-spacing: 10px;
}
</style>
</head>
<body>
<h1>This is a heading.</h1>
<p>This is a simple paragraph of
text.</p>
</body>
</html>
Word Spacing
The word-spacing property is used to specify additional spacing between the words.
This property can accept a length value in pixels, ems, etc. Negative values are also allowed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting word spacing using
CSS</title>
<style>
p.one {
word-spacing: 20px;
}
p.two {
width: 150px;
word-spacing: 20px;
text-align: justify;
}
p.three {
word-spacing: 20px;
white-space: pre;
}
</style>
</head>
<body>
<p class="one">This is a normal
paragraph.</p>
<hr>
<p class="two">Note that spacing
between the words of this paragraph are
varying in order to justify the text even if the
value of word-spacing property is set to
20px.</p>
<hr>
<p class="three">Note that spacing
between the words
of this paragraph are higher than the
normal spacing
even if whitespace are
preserved.</p>
</body>
</html>
Line Height
The line-height property is used to set the height of the text line.
It is also called leading and commonly used to set the distance between lines of text.
The value of this property can be a number, a percentage (%), or a length in pixels, ems, etc.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Line Height using CSS</title>
<style>
p {
line-height: 1.2;
border: 1px solid #00ff00;
}
div {
line-height: 200%;
border: 1px solid #ff0000;
}
</style>
</head>
<body>
<h1>Change the values to see how it
works</h1>
<p>The <code>line-height</code> property
sets the height between lines of text.<br> The
line height of this paragraph is specified using
number.</p>
<div>The <code>line-height</code> property
sets the height between lines of text.<br> The
line height of this paragraph is specified using
percentage.</div>
</body>
</html>
If the value of the line-height property is greater than the value of the font-size for an
element, this difference (called the "leading") is cut in half (called the "half-leading") and
distributed evenly on the top and bottom of the in-line box.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Line Height using
CSS</title>
<style>
p {
font-size: 14px;
line-height: 50px;
background-color: #f0e68c;
}
</style>
</head>
<body>
<h1>Change the values to see how
it works.</h1>
<p>The <code>line-height</code>
property sets the height between lines
of text.<br> The line height of this
paragraph is specified using pixels
value.</p>
</body>
</html>
Styling Links with CSS
A link has four different states
● a:link — define styles for normal or unvisited links.
● a:visited — define styles for links that the user has already visited.
● a:hover — define styles for a link when the user place the mouse pointer over it.
● a:active — define styles for links when they are being clicked.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Styling Different Link States using CSS</title>
<style>
/* unvisited link */
a:link {
color: #ff0000;
text-decoration: none;
border-bottom: 1px solid;
}
/* visited link */
a:visited {
color: #ff00ff;
}
/* mouse over link */
a:hover {
color: #00ff00;
border-bottom: none;
}
/* active link */
a:active {
color: #00ffff;
}
</style>
</head>
<body>
<p><a href="https://www.tutorialrepublic.com/"
target="_top">Visit Tutorial Republic</a></p>
</body>
</html>
Modifying Standard Link Styles
By default, text links will appear as follow in most of the browsers:
● An unvisited link as underlined blue text.
● A visited link as underlined purple text.
● An active link as underlined red text.
Removing the Default Underline from Links
you can simply use the CSS text-decoration property to get rid of it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Remove Default Underline from Links
using CSS</title>
<style>
a:link, a:visited {
text-decoration: none;
}
a:hover, a:active {
text-decoration: underline;
}
</style>
</head>
<body>
<p><a href="#">Place mouse pointer
over me</a></p>
</body>
</html>
Making Text Links Look Like Buttons
CSS properties such as background-color, border, display, padding, etc.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Customize a Link as Button using
CSS</title>
<style>
a:link, a:visited {
color: white;
background-color: #1ebba3;
display: inline-block;
padding: 10px 20px;
border: 2px solid #099983;
text-decoration: none;
text-align: center;
font: 14px Arial, sans-serif;
}
a:hover, a:active {
background-color: #9c6ae1;
border-color: #7443b6;
}
</style>
</head>
<body>
<p><a href="#">CSS Link
Button</a></p>
</body>
</html>
Types of HTML Lists
There are three different types of list in HTML:
● Unordered lists — A list of items, where every list items are marked with bullets.
● Ordered lists — A list of items, where each list items are marked with numbers.
● Definition list — A list of items, with a description of each item.
Styling Lists with CSS
CSS provides the several properties for styling and formatting the most commonly used
unordered and ordered lists.
These CSS list properties typically allow to:
● Control the shape or appearance of the marker.
● Specify an image for the marker rather than a bullet point or number.
● Set the distance between a marker and the text in the list.
● Specify whether the marker would appear inside or outside of the box containing the list
items.
Changing the Marker Type of Lists
By default, items in an ordered list are numbered with Arabic numerals (1, 2, 3, 5, and so
on), whereas in an unordered list, items are marked with round bullets (•).
But, you can change this default list marker type to any other type such as roman numerals,
latin letters, circle, square, and so on using the list-style-type property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Numbering or Bullet
Point Style of Lists</title>
<style>
ul {
list-style-type: square;
}
ol {
list-style-type: upper-roman;
}
</style>
</head>
<body>
<h2>Unordered List</h2>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
<h2>Ordered List</h2>
<ol>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ol>
</body>
</html>
Changing the Position of List Markers
By default, markers of each list items are positioned outside of their display boxes.
list-style-positionproperty along with the value inside.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting the Position of List Marker</title>
<style>
body{
font-size: 14px;
font-family: Arial,sans-serif;
}
ol li {
background: #ddd;
padding: 5px;
margin: 5px;
}
ol.in li {
list-style-position: inside;
}
ol.out li {
list-style-position: outside;
}
</style>
</head>
<body>
<h2>List Marker Position - Inside</h2>
<ol class="in">
<li>Fasten your seatbelt</li>
<li>Start the car's engine and take a closer
look the instrument cluster for any warning
sign</li>
<li>Look around carefully and go</li>
</ol>
<h2>List Marker Position - Outside</h2>
<ol class="out">
<li>Fasten your seatbelt</li>
<li>Start the car's engine and take a closer
look the instrument cluster for any warning
sign</li>
<li>Look around carefully and go</li>
</ol>
</body>
</html>
Using Images as List Markers
You can also set an image as a list marker using the list-style-image property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting an Image as List Marker with
CSS</title>
<style>
ul li {
list-style-image:
url("/examples/images/bullet.png");
margin: 5px;
}
</style>
</head>
<body>
<h2>Using Image as Bullet Point</h2>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Setting Image Marker in Background
with CSS</title>
<style>
ul {
list-style-type: none;
}
ul li {
background-image:
url("/examples/images/bullet.png");
background-position: 0px 3px;
background-repeat: no-repeat;
padding-left: 20px;
margin: 5px
}
</style>
</head>
<body>
<h1>Using Image as Bullet Point</h1>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
</body>
</html>
Setting All List Properties At Once
The list-style property is a shorthand property for defining all the three properties
list-style-type , list-style-image , and list-style-position of a list in one place.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS list-style Shorthand
Property</title>
<style>
ul {
list-style: square inside
url("/examples/images/bullet.png");
background: #9ddfef;
}
ul li {
background: #ededed;
margin: 5px 0;
padding: 5px;
}
</style>
</head>
<body>
<h2>The <code>list-style</code>
Shorthand Property</h2>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
</body>
</html>
Creating Navigation Menus Using Lists
HTML lists are frequently used to create horizontal navigation bar or menu that typically
appear at the top of a website.
But since the list items are block elements, so to display them inline we need to use the
CSS display property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Building Navigation Bar with HTML List and
CSS</title>
<style>
body{
font-size: 14px;
font-family: Arial,sans-serif;
}
ul {
padding: 0;
list-style: none;
background: #f2f2f2;
}
ul li {
display: inline-block;
}
ul li a {
display: block;
padding: 10px 25px;
color: #333;
text-decoration: none;
}
ul li a:hover {
color: #fff;
background: #939393;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<p><strong>Note:</strong> Place mouse pointer
over the menu link to see the hover effect.</p>
</body>
Styling Tables with CSS
Adding Borders to Tables
The CSS border property is the best way to define the borders for the tables.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of Setting Table Borders</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>johncarter@mail.com</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>peterparker@mail.com</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>johnrambo@mail.com</td>
</tr>
<tr>
<td>4</td>
<td>Harry</td>
<td>Potter</td>
<td>harrypotter@mail.com</td>
</tr>
</table>
</body>
</html>
Collapsing Table Borders
There are two distinct models for setting borders on table cells in CSS: separate and
collapse.
<style>
table {
border-collapse: saparate;
}
table, th, td {
border: 1px solid black;
}
</style>
Adjusting Space inside Tables
To add more space between the table cell contents and the cell borders, you can simply use the CSS
padding property.
<style>
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 15px;
}
</style>
CSS border-spacing
spacing between the borders of the cells using the CSS border-spacing property.
<style>
table {
border-spacing: 10px;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 10px;
}
</style>
Setting Table Width and Height
you can also set the width and height of the table as well as its cells explicitly using the
width and height CSS property.
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
border: 1px solid #dee2e6;
}
th {
height: 70px;
text-align: left;
}
</style>
Controlling the Table Layout
A table expands and contracts to accommodate the data contained inside it.
This property takes one of two values:
● auto — Uses an automatic table layout algorithm. With this algorithm, the widths of the
table and its cells are adjusted to fit the content. This is the default value.
● fixed — Uses the fixed table layout algorithm. With this algorithm, the horizontal layout
of the table does not depend on the contents of the cells; it only depends on the table's
width, the width of the columns, and borders or cell spacing. It is normally faster than
auto.
Example
<style>
table {
width: 250px;
border-collapse: separate;
}
table, tr, th, td{
border: 1px solid #000000;
}
.auto {
table-layout: auto;
}
.fixed {
table-layout: auto;
}
td{
width: 50%;
}
</style>
Aligning the Text Inside Table Cells
Horizontal Alignment of Cell Contents
For horizontal alignment of text inside the table cells you can use the text-align property
in the same way as you use with other elements. You align text to either left, right, center or
justify.
Example
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
border: 1px solid #dee2e6;
}
th {
text-align: left;
}
</style>
<table>
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Clark</td>
<td>Kent</td>
<td>clarkkent@mail.com</td>
</tr>
</tbody>
</table>
Vertical Alignment of Cell Contents
vertically align the content inside the <th> and <td> elements to top, bottom, or middle
using the CSS vertical-align property. The default vertical alignment is middle.
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
border: 1px solid #dee2e6;
}
th {
height: 400px;
vertical-align: top;
}
</style>
Controlling the Position of Table Caption
set the vertical position of a table caption using the CSS caption-side property.
The caption can be placed either at the top or bottom of the table.
<style>
table, td, th {
border: 1px solid gray;
}
caption {
caption-side: bottom;
}
</style>
Handling Empty Cells
This property accepts a value of either show or hide.
The default value is show, which renders empty cells like normal cells, but if the
value hide is specified no borders or backgrounds are drawn around the empty
cells.
<style>
table {
width: 300px;
border-collapse: separate;
}
table, th, td{
border: 1px solid #000000;
}
table.empty-show {
empty-cells: show;
}
table.empty-hide {
empty-cells: hide;
}
<h2>Table with Empty-cells</h2>
<table class="empty-show">
<tr>
<th>Name</th>
<td>John Carter</td>
</tr>
<tr>
<th>Email</th>
<td></td>
</tr>
</table>
<h2>Table with Hidden
Empty-cells</h2>
<table class="empty-hide">
<tr>
<th>Name</th>
<td>Peter Parker</td>
</tr>
<tr>
<th>Email</th>
<td></td>
</tr>
</table>
Creating Zebra-striped Tables
Setting different background colors for alternate rows is a popular technique to improve the
readability of tables that has large amount of data. This is commonly known as zebra-striping a
table.
You can simply achieve this effect by using the CSS :nth-child() pseudo-class selector.
Example
<style>
table {
width: 100%;
font-family: arial, sans-serif;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-top: 1px solid #dee2e6;
}
tbody tr:nth-child(odd) {
background-color: #f2f2f2;
}
</style>
Making a Table Responsive
Tables are not responsive in nature.
However, to support mobile devices you can add responsiveness to your tables by
enabling horizontal scrolling on small screens.
To do this simply wrap your table with a <div> element and apply the style
overflow-x: auto;
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border: 1px solid #dee2e6;
white-space: nowrap; /* to prevent
text wrapping */
}
.responsive-table {
overflow-x: auto;
}
</style>
<div class="responsive-table">
<table>
<thead>
<tr>
<th>ID</th>
<th>Supplier</th>
<th>Contact Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
<th>Phone</th>
</tr>
</thead>
</tbody>
</table>
</div>
CSS Box Model
Every element that can be displayed on a web page is comprised of one or more rectangular boxes.
CSS box model typically describes how these rectangular boxes are laid out on a web page.
These boxes can have different properties and can interact with each other in different ways, but every box has a
content area and optional surrounding padding, border, and margin areas.
Width and Height of the Elements
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Box Formatting Model</title>
<style>
div {
width: 300px;
height: 200px;
padding: 15px;
border: 10px solid black;
margin: 20px auto;
}
</style>
</head>
<body>
<div>Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Nam eu sem
tempor, varius quam at, luctus dui. Mauris
magna metus, dapibus nec turpis vel,
semper malesuada ante. Vestibulum id
metus ac nisl bibendum scelerisque non
non purus. Suspendisse varius nibh non
aliquet sagittis. In tincidunt orci sit amet
elementum vestibulum. Vivamus
fermentum in arcu in aliquam. Quisque
aliquam porta odio in fringilla. Vivamus nisl
leo, blandit at bibendum eu, tristique
eget.</div>
</body>
</html>
CSS Dimension
Setting Element Dimensions
CSS has several dimension properties, such as width, height, max-width, min-width, max-height, and
min-height that allows you to control the width and height of an element.
Setting the Width and Height
The width and height property defines the width and height of the content area of an element.
This width and height does not include paddings, borders, or margins.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Width and Height of an
Element</title>
<style>
div {
width: 300px;
height: 200px;
background: #eee8aa;
}
</style>
</head>
<body>
<div>Play with the values to see how
it works!</div>
</body>
</html>
Setting Maximum Width and Height
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Maximum Width of an Element</title>
<style>
div {
width: 300px;
max-width: 200px;
background: #bbb3ff;
}
p {
float: left;
max-width: 400px;
background: #eee8aa;
}
</style>
</head>
<body>
<div>The maximum width of this div element is set to
200px, so it can't be wider than that.</div>
<p>Enter some text to see how it works.</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Maximum Height of an Element</title>
<style>
div {
height: 200px;
max-height: 100px;
background: #bbb3ff;
}
p {
max-height: 100px;
background: #eee8aa;
}
</style>
</head>
<body>
<div>The maximum height of this div element is set to
100px, so it can't be taller than that.</div>
<p>Enter some more line of text to see how it works.</p>
</body>
</html>
Setting Minimum Width and Height
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Minimum Width of an Element</title>
<style>
div {
width: 200px;
min-width: 300px;
background: #bbb3ff;
}
p {
float: left;
min-width: 400px;
background: #eee8aa;
}
</style>
</head>
<body>
<div>The minimum width of this div element is set to
300px, so it can't be narrower than that.</div>
<p>Enter some text to see how it works.</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set minimum Height of an Element</title>
<style>
div {
height: 200px;
min-height: 300px;
background: #bbb3ff;
}
p {
min-height: 100px;
background: #eee8aa;
}
</style>
</head>
<body>
<div>The minimum height of this div element is set to
300px, so it can't be smaller than that.</div>
<p>Enter some more line of text to see how it works.</p>
</body>
</html>
Setting a Width and Height Range
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Width Range for an Element</title>
<style>
div {
float: left;
min-width: 300px;
max-width: 500px;
height: 100px;
background: #eee8aa;
}
</style>
</head>
<body>
<p><strong>Note:</strong> The minimum width
of the following div element will be 300px, and it can
stretches horizontally up to 500px. Enter few lines of
text inside the div element to understand how it
works.</p>
<div></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Height Range for an Element</title>
<style>
div {
min-height: 300px;
max-height: 500px;
background: #eee8aa;
}
</style>
</head>
<body>
<p><strong>Note:</strong> The minimum
height of the following div element will be 300px, and it
can stretches vertically up to 500px. Enter few lines of
text inside the div element to understand how it
works.</p>
<div></div>
</body>
</html>
Setting a Width and Height Range
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Width Range for an Element</title>
<style>
div {
float: left;
min-width: 300px;
max-width: 500px;
height: 100px;
background: #eee8aa;
}
</style>
</head>
<body>
<p><strong>Note:</strong> The minimum
width of the following div element will be 300px,
and it can stretches horizontally up to 500px. Enter
few lines of text inside the div element to
understand how it works.</p>
<div></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Set Height Range for an
Element</title>
<style>
div {
min-height: 300px;
max-height: 500px;
background: #eee8aa;
}
</style>
</head>
<body>
<p><strong>Note:</strong> The minimum
height of the following div element will be 300px,
and it can stretches vertically up to 500px. Enter
few lines of text inside the div element to
understand how it works.</p>
<div></div>
</body>
</html>
CSS Padding Properties
Define Paddings for Individual Sides
CSS padding-top, padding-right, padding-bottom, and the padding-left properties, respectively.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Padding for Individual
Sides</title>
<style>
h1 {
padding-top: 50px;
padding-bottom: 100px;
background: lime;
}
p {
padding-left: 75px;
padding-right: 75px;
background: lime;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a simple paragraph of
text.</p>
<p><strong>Note:</strong> Play with the
padding property value to see how it
works.</p>
</body>
</html>
The Padding Shorthand Property
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Set Padding for All Sides At Once</title>
<style>
h1 {
padding: 50px; /* apply to all four sides */
}
p {
padding: 25px 75px; /* vertical | horizontal
*/
}
div {
padding: 25px 50px 75px; /* top |
horizontal | bottom */
}
pre {
padding: 25px 50px 75px 100px; /* top |
right | bottom | left */
}
h1, p, div, pre {
background: lime;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a simple paragraph of text.</p>
<div>This is a DIV element.</div>
<pre>This is a piece of preformatted
text.</pre>
<p><strong>Note:</strong> Play with the
padding property value to see how it
works.</p>
</body>
</html>
Effect of Padding and Border on Layout
When creating web page layouts, adding a padding or border to the elements sometimes produce unexpected
result, because padding and border is added to the width and height of the box generated by the element.
For instance, if you set the width of a <div> element to 100% and also apply left right padding or border on it, the
horizontal scrollbar will appear.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Effect of CSS Padding on Element
Box Size</title>
<style>
div {
width: 100%;
padding: 25px;
background: violet;
}
</style>
</head>
<body>
<div>
<h1>This is a DIV Box</h1>
</div>
<p><strong>Notice</strong>, the
scrollbar at the bottom of the viewport.</p>
</body>
</html>
Effect of Padding and Border on Layout
To prevent padding and border from changing element's box width and height, you can use the CSS box-sizing
property.
In the following example the width and height of the <div> box will remain unchanged, however, its content area
will decrease with increasing padding or border.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Preventing Change in Element
Box Dimensions</title>
<style>
div {
width: 100%;
padding: 25px;
box-sizing: border-box;
background: violet;
}
</style>
</head>
<body>
<div>
<h1>This is a DIV Box</h1>
</div>
<p><strong>Notice</strong>, this time
there is no scrollbar at the bottom of the
viewport.</p>
</body>
</html>
CSS Border Properties
The CSS border properties allow you to define the border area of an element's box.
Borders appear directly between the margin and padding of an element.
The border can either be a predefined style like, solid line, dotted line, double line, etc. or an image.
Understanding the Different Border Styles
The border-style property can have the following values: none, hidden, solid, dashed, dotted, double,
inset, outset, groove, and ridge.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS border-style Property</title>
<style>
p {
border-width: 15px;
background: yellow;
padding: 20px;
margin: 20px;
}
p.none {
border-style: none;
}
p.hidden {
border-style: hidden;
}
p.dotted {
border-style: dotted;
}
p.dashed {
border-style: dashed;
}
p.solid {
border-style: solid;
}
p.double {
border-style: double;
}
p.groove {
border-style: groove;
}
p.ridge {
border-style: ridge;
}
p.inset {
border-style: inset;
}
p.outset {
border-style: outset;
}
</style>
</head>
<body>
<p class="none">No border.</p>
<p class="hidden">Hidden border.</p>
<p class="dotted">A dotted border.</p>
<p class="dashed">A dashed border.</p>
<p class="solid">A solid border.</p>
<p class="double">A double border.</p>
<p class="inset">An inset border.</p>
<p class="outset">An outset border.</p>
<p class="groove">A groove border.</p>
<p class="ridge">A ridge border.</p>
</body>
</html>
Setting the Border Width
The border-width property specifies the width of the border area.
It is a shorthand property for setting the thickness of all the four sides of an element's border at the same time.
border-width: 10px;
Specifying the Border Color
The border-color property specifies the color of the border area.
border-color: #ff0000;
The Border Shorthand Property
border-width, border-style and border-color in a single rule.
border: 5px solid #00ff00;
For instance, if the value for the border-color property is missing or not specified when setting the border, the
element's color property will be used as the value for the border color.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Omitting border-color Property</title>
<style>
p {
color: red;
background: yellow;
border: 5px solid;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<p>The border color of this paragraph is
same as the text color.</p>
</body>
</html>
But, in the case of border-style, omitting the value will cause no border to show at all, because the default value
for this property is none.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ignoring border-style Property</title>
<style>
p {
border: 5px #00ff00;
background: yellow;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<p>This paragraph has no border.</p>
</body>
</html>
CSS Margin Properties
The CSS margin properties allow you to set the spacing around the border of an element's box.
An element's margin is not affected by its background-color, it is always transparent.
Setting Margins for Individual Sides
CSS margin-top, margin-right, margin-bottom, and the margin-left properties, respectively.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Margin for Individual
Sides</title>
<style>
h1 {
margin-top: 50px;
margin-bottom: 100px;
background: yellow;
}
p {
margin-left: 75px;
margin-right: 75px;
background: yellow;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a simple paragraph of
text.</p>
<p><strong>Note:</strong> Play with the
margin property value to see how it
works.</p>
</body>
</html>
The Margin Shorthand Property
The margin property is a shorthand property to avoid setting margin of each side separately, i.e., margin-top,
margin-right, margin-bottom and margin-left.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Set Margin for All Sides At Once</title>
<style>
h1 {
margin: 50px; /* apply to all four sides */
}
p {
margin: 25px 75px; /* vertical | horizontal
*/
}
div {
margin: 25px 50px 75px; /* top |
horizontal | bottom */
}
hr {
margin: 25px 50px 75px 100px; /* top |
right | bottom | left */
}
h1, p, div {
background: yellow;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a simple paragraph of text.</p>
<div>This is a DIV element.</div>
<hr>
<p><strong>Note:</strong> Play with the
margin property value to see how it works.</p>
</body>
</html>
Horizontal Centering with Auto Margins
The auto value for the margin property tells the web browser to automatically calculate the margin.
This is commonly used to center an element horizontally within a larger container.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Centering an Element with CSS Auto
Margin</title>
<style>
.container {
width: 300px;
height: 200px;
background: #ddd;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</div>
</body>
</html>

More Related Content

Similar to Unit III CSS & JAVA Script.pdf (20)

Html tutorial
Html tutorialHtml tutorial
Html tutorial
 
Css
CssCss
Css
 
CSS
CSSCSS
CSS
 
CSS.pdf
CSS.pdfCSS.pdf
CSS.pdf
 
Chapter 4a cascade style sheet css
Chapter 4a cascade style sheet cssChapter 4a cascade style sheet css
Chapter 4a cascade style sheet css
 
Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1Web Design Course: CSS lecture 1
Web Design Course: CSS lecture 1
 
Html2
Html2Html2
Html2
 
TUTORIAL DE CSS 2.0
TUTORIAL DE CSS 2.0TUTORIAL DE CSS 2.0
TUTORIAL DE CSS 2.0
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Css basics
Css basicsCss basics
Css basics
 
IP - Lecture 6, 7 Chapter-3 (3).ppt
IP - Lecture 6, 7 Chapter-3 (3).pptIP - Lecture 6, 7 Chapter-3 (3).ppt
IP - Lecture 6, 7 Chapter-3 (3).ppt
 
Workshop 2 Slides.pptx
Workshop 2 Slides.pptxWorkshop 2 Slides.pptx
Workshop 2 Slides.pptx
 
Java script and html new
Java script and html newJava script and html new
Java script and html new
 
Java script and html
Java script and htmlJava script and html
Java script and html
 
Html tutorials by www.dmdiploma.com
Html tutorials by www.dmdiploma.comHtml tutorials by www.dmdiploma.com
Html tutorials by www.dmdiploma.com
 
Web technology Unit-II Part-C
Web technology Unit-II Part-CWeb technology Unit-II Part-C
Web technology Unit-II Part-C
 
Ifi7174 lesson2
Ifi7174 lesson2Ifi7174 lesson2
Ifi7174 lesson2
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
Css tutorial
Css tutorialCss tutorial
Css tutorial
 
Html Workshop
Html WorkshopHtml Workshop
Html Workshop
 

More from meghana092

Internal_Research_Supervisors_June.2022.pdf
Internal_Research_Supervisors_June.2022.pdfInternal_Research_Supervisors_June.2022.pdf
Internal_Research_Supervisors_June.2022.pdfmeghana092
 
Unit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptxUnit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptxmeghana092
 
ACA-Lect10.pptx
ACA-Lect10.pptxACA-Lect10.pptx
ACA-Lect10.pptxmeghana092
 
Week 1 Lec 1-5 with watermarking.pdf
Week 1 Lec 1-5 with watermarking.pdfWeek 1 Lec 1-5 with watermarking.pdf
Week 1 Lec 1-5 with watermarking.pdfmeghana092
 

More from meghana092 (6)

Internal_Research_Supervisors_June.2022.pdf
Internal_Research_Supervisors_June.2022.pdfInternal_Research_Supervisors_June.2022.pdf
Internal_Research_Supervisors_June.2022.pdf
 
Unit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptxUnit 3 JAVA Script.pptx
Unit 3 JAVA Script.pptx
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
ch01.ppt
ch01.pptch01.ppt
ch01.ppt
 
ACA-Lect10.pptx
ACA-Lect10.pptxACA-Lect10.pptx
ACA-Lect10.pptx
 
Week 1 Lec 1-5 with watermarking.pdf
Week 1 Lec 1-5 with watermarking.pdfWeek 1 Lec 1-5 with watermarking.pdf
Week 1 Lec 1-5 with watermarking.pdf
 

Recently uploaded

Low Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)
(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)
(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)kojalkojal131
 
定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一
定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一
定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一zul5vf0pq
 
VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...Call Girls in Nagpur High Profile
 
Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...
Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...
Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...nagunakhan
 
《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...
《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...
《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...ur8mqw8e
 
Top Rated Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Call Girls in Nagpur High Profile
 
Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...
Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...
Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...nagunakhan
 
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Naicy mandal
 
Pallawi 9167673311 Call Girls in Thane , Independent Escort Service Thane
Pallawi 9167673311  Call Girls in Thane , Independent Escort Service ThanePallawi 9167673311  Call Girls in Thane , Independent Escort Service Thane
Pallawi 9167673311 Call Girls in Thane , Independent Escort Service ThanePooja Nehwal
 
VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...
VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...
VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...Call Girls in Nagpur High Profile
 
Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...
Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...
Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...Pooja Nehwal
 
Call Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up Number
Call Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up NumberCall Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up Number
Call Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up NumberMs Riya
 
Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...
Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...
Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...anilsa9823
 
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...Pooja Nehwal
 
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Call Girls Dubai Slut Wife O525547819 Call Girls Dubai Gaped
Call Girls Dubai Slut Wife O525547819 Call Girls Dubai GapedCall Girls Dubai Slut Wife O525547819 Call Girls Dubai Gaped
Call Girls Dubai Slut Wife O525547819 Call Girls Dubai Gapedkojalkojal131
 
(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 

Recently uploaded (20)

Low Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Vedika 7001305949 Independent Escort Service Nashik
 
(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)
(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)
(=Towel) Dubai Call Girls O525547819 Call Girls In Dubai (Fav0r)
 
定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一
定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一
定制加拿大滑铁卢大学毕业证(Waterloo毕业证书)成绩单(文凭)原版一比一
 
VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Kalyani Nagar (7001035870) Pune Escorts Nearby with Comp...
 
Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...
Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...
Russian Call Girls In South Delhi Delhi 9711199012 💋✔💕😘 Independent Escorts D...
 
《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...
《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...
《伯明翰城市大学毕业证成绩单购买》学历证书学位证书区别《复刻原版1:1伯明翰城市大学毕业证书|修改BCU成绩单PDF版》Q微信741003700《BCU学...
 
Top Rated Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Chakan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
🔝 9953056974🔝 Delhi Call Girls in Ajmeri Gate
🔝 9953056974🔝 Delhi Call Girls in Ajmeri Gate🔝 9953056974🔝 Delhi Call Girls in Ajmeri Gate
🔝 9953056974🔝 Delhi Call Girls in Ajmeri Gate
 
Top Rated Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
 
Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...
Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...
Russian Escorts in lucknow 💗 9719455033 💥 Lovely Lasses: Radiant Beauties Shi...
 
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
Pallawi 9167673311 Call Girls in Thane , Independent Escort Service Thane
Pallawi 9167673311  Call Girls in Thane , Independent Escort Service ThanePallawi 9167673311  Call Girls in Thane , Independent Escort Service Thane
Pallawi 9167673311 Call Girls in Thane , Independent Escort Service Thane
 
VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...
VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...
VVIP Pune Call Girls Warje (7001035870) Pune Escorts Nearby with Complete Sat...
 
Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...
Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...
Kalyan callg Girls, { 07738631006 } || Call Girl In Kalyan Women Seeking Men ...
 
Call Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up Number
Call Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up NumberCall Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up Number
Call Girls Delhi {Rs-10000 Laxmi Nagar] 9711199012 Whats Up Number
 
Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...
Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...
Lucknow 💋 Call Girls Adil Nagar | ₹,9500 Pay Cash 8923113531 Free Home Delive...
 
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...High Profile Call Girls In Andheri 7738631006 Call girls in mumbai  Mumbai ...
High Profile Call Girls In Andheri 7738631006 Call girls in mumbai Mumbai ...
 
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls Kolkata Chhaya 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls Kolkata Chhaya 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Call Girls Dubai Slut Wife O525547819 Call Girls Dubai Gaped
Call Girls Dubai Slut Wife O525547819 Call Girls Dubai GapedCall Girls Dubai Slut Wife O525547819 Call Girls Dubai Gaped
Call Girls Dubai Slut Wife O525547819 Call Girls Dubai Gaped
 
(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(PARI) Alandi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 

Unit III CSS & JAVA Script.pdf

  • 2. CSS ● CSS stands for Cascading Style Sheets ● CSS describes how HTML elements are to be displayed on screen, paper, or in other media ● CSS saves a lot of work. It can control the layout of multiple web pages all at once ● External stylesheets are stored in CSS files
  • 3. Advantages of Using CSS CSS Save Lots of Time Easy Maintenance Pages Load Faster Superior Styles to HTML Multiple Device Compatibility
  • 4. Including CSS in HTML Documents ● Inline styles — Using the style attribute in the HTML start tag. ● Embedded styles — Using the <style> element in the head section of a document. ● External style sheets — Using the <link> element, pointing to an external CSS file.
  • 5. Inline Styles Inline styles are used to apply the unique style rules to an element by putting the CSS rules directly into the start tag. It can be attached to an element using the style attribute. The style attribute includes a series of CSS property and value pairs. Each "property: value" pair is separated by a semicolon (;) <h1 style="color:red; font-size:30px;">This is a heading</h1> <p style="color:green; font-size:22px;">This is a paragraph.</p> <div style="color:blue; font-size:14px;">This is some text content.</div>
  • 6. Embedded Style Sheets Embedded or internal style sheets only affect the document they are embedded in. Embedded style sheets are defined in the <head> section of an HTML document using the <style> element. <!DOCTYPE html> <html lang="en"> <head> <title>My HTML Document</title> <style> body { background-color: YellowGreen; } p { color: #fff; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph of text. </p> </body> </html>
  • 7. External Style Sheets An external style sheet is ideal when the style is applied to many pages of the website. An external style sheet holds all the style rules in a separate document that you can link from any HTML file on your site.
  • 8. Linking External Style Sheets body { background: lightyellow; font: 18px Arial, sans-serif; } h1 { color: orange; }
  • 9. An external style sheet can be linked to an HTML document using the <link> tag. The <link> tag goes inside the <head> section. <!DOCTYPE html> <html lang="en"> <head> <title>My HTML Document</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph of text.</p> </body> </html>
  • 10. Importing External Style Sheets The @import rule is another way of loading an external style sheet. The @import statement instructs the browser to load an external style sheet and use its styles. <style> @import url("css/style.css") ; p { color: blue; font-size: 16px; } </style>
  • 11. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS Imported Style Sheet</title> <style> @import url("style.css"); p { color: blue; font-size: 16px; } </style> </head> <body> <h1>The styles for this heading are defined in the imported style sheet</h1> <p>The styles for this paragraph are defined in the embedded style sheet.</p> </body> </html>
  • 12. CSS Syntax h1 { color: blue; text-align: center; }
  • 13. Writing Comments in CSS A CSS comment begins with /*, and ends with */, as shown in the example below: /* This is a CSS comment */ h1 { color: blue; text-align: center; } /* This is a multi-line CSS comment that spans across more than one line */ p { font-size: 18px; text-transform: uppercase; }
  • 14. CSS Selectors A CSS selector is a pattern to match the elements on a web page. The style rules associated with that selector will be applied to the elements that match the selector pattern.
  • 15. Universal Selector The universal selector, denoted by an asterisk (*), matches every single element on the page. This selector is often used to remove the default margins and paddings from the elements for quick testing purpose. The universal selector may be omitted if other conditions exist on the element. The style rules inside the * selector will be applied to every element in a document. * { margin: 0; padding: 0; }
  • 16. Element Type Selectors An element type selector matches all instance of the element in the document with the corresponding element type name. p { color: blue; }
  • 17. Id Selectors The id selector is used to define style rules for a single or unique element. The id selector is defined with a hash sign (#) immediately followed by the id value. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS id selector</title> <style> #error { color: #ff0000; } </style> </head> <body> <p id="error">This is a warning!</p> </body> </html>
  • 18. Class Selectors The class selectors can be used to select any HTML element that has a class attribute. All the elements having that class will be formatted according to the defined rule. The class selector is defined with a period sign (.) immediately followed by the class value. The style rule inside the selector p.blue renders the text in blue of only those <p> elements that has class attribute set to blue, and has no effect on other paragraphs.
  • 19. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS class selector</title> <style> .blue { color: #0000ff; } </style> </head> <body> <h1 class="blue">This is a heading</h1> <p class="blue">This is a paragraph.</p> <p>This is another paragraph.</p> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS class selector</title> <style> p.blue { color: #0000ff; } </style> </head> <body> <h1 class="blue">This is a heading</h1> <p class="blue">This is a paragraph.</p> <p>This is another paragraph.</p> </body> </html>
  • 20. Descendant Selectors You can use these selectors when you need to select an element that is the descendant of another element, for example, if you want to target only those anchors that are contained within an unordered list, rather than targeting all anchor elements. The style rules inside the selector ul.menu li a applied to only those <a> elements that contained inside an <ul> element having the class .menu, and has no effect on other links inside the document. Similarly, the style rules inside the h1 em selector will be applied to only those <em> elements that contained inside the <h1> element and has not effect on other <em> elements.
  • 21. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS Descendant Selectors</title> <style> h1 em { color: green; } ul.menu { padding: 0; list-style: none; } ul.menu li{ display: inline; } ul.menu li a { margin: 10px; text-decoration: none; } </style> </head> <body> <h1>This is a <em>heading</em></h1> <ul class="menu"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </body> </html>
  • 22. Child Selectors A child selector is used to select only those elements that are the direct children of some element. A child selector is made up of two or more selectors separated by a greater than symbol (>). You can use this selector, for instance, to select the first level of list elements inside a nested list that has more than one level. The style rule inside the selector ul > li applied to only those <li> elements that are direct children of the <ul> elements, and has no effect on other list elements.
  • 23. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS Child Selectors</title> <style> ul > li { list-style: square; } ul > li ol { list-style: none; } </style> </head> <body> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li> <a href="#">Services</a> <ol> <li><a href="#">Design</a></li> <li><a href="#">Development</a></li> </ol> </li> <li><a href="#">Contact</a></li> </ul> </body> </html>
  • 24. Adjacent Sibling Selectors The adjacent sibling selectors can be used to select sibling elements (i.e. elements at the same level). This selector has the syntax like: E1 + E2, where E2 is the target of the selector. The selector h1 + p in the following example will select the <p> elements only if both the <h1> and <p> elements share the same parent in the document tree and <h1> is immediately precedes the <p> element.
  • 25. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS Adjacent Sibling Selectors</title> <style> h1 + p { color: yellow; font-size: 60px; } ul.task + p { color: #f0f; text-indent: 30px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <ul class="task"> <li>Task 1</li> <li>Task 2</li> <li>Task 3</li> </ul> <p>This is one more paragraph.</p> <p>This is also a paragraph.</p> </body> </html>
  • 26. General Sibling Selectors The general sibling selector is similar to the adjacent sibling selector (E1 + E2), but it is less strict. A general sibling selector is made up of two simple selectors separated by the tilde (∼) character. It can be written like: E1 ∼ E2, where E2 is the target of the selector. The selector h1 ∼ p in the example below will select all the <p> elements that preceded by the <h1> element, where all the elements share the same parent in the document tree.
  • 27. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS General Sibling Selectors</title> <style> h1 ~ p { color: blue; font-size: 18px; } ul.task ~ p { color: #f0f; text-indent: 30px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <ul class="task"> <li>Task 1</li> <li>Task 2</li> <li>Task 3</li> </ul> <p>This is one more paragraph.</p> <p>This is also a paragraph.</p> </body> </html>
  • 28. Grouping Selectors Often several selectors in a style sheet share the same style rules declarations. You can group them into a comma-separated list to minimize the code in your style sheet. It also prevents you from repeating the same style rules over and over again.
  • 29. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS Selectors without Grouping</title> <style> h1 { font-size: 36px; font-weight: normal; } h2 { font-size: 28px; font-weight: normal; } h3 { font-size: 22px; font-weight: normal; } </style> </head> <body> <h1>This is a heading of level 1</h1> <h2>This is a heading of level 2</h2> <h3>This is a heading of level 3</h3> </body> </html>
  • 30. As you can see in the above example, the same style rule font-weight: normal; is shared by the selectors h1, h2 and h3, so it can be grouped in a comma-separated list, like this: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS Grouping Selectors</title> <style> h1, h2, h3 { font-weight: normal; } h1 { font-size: 36px; } h2 { font-size: 28px; } h3 { font-size: 22px; } </style> </head> <body> <h1>This is a heading of level 1</h1> <h2>This is a heading of level 2</h2> <h3>This is a heading of level 3</h3> </body> </html>
  • 31. Color Property The color property defines the text color (foreground color in general) of an element. For instance, the color property specified in the body selector defines the default text color for the whole page. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Text Color in CSS</title> <style> body { color: #ff5722; } .text-green { color: #008000; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <div class="text-green">This is a simple piece of text.</div> </body> </html>
  • 32. Defining Color Values Colors in CSS most often specified in the following formats: ● a color keyword - like "red", "green", "blue", "transparent", etc. ● a HEX value - like "#ff0000", "#00ff00", etc. ● an RGB value - like "rgb(255, 0, 0)"
  • 33. Affect of Color Property on Borders and Outlines The color property is not just for text content, but for anything in the foreground that takes a color value. For instance, if border-color or outline-color value hasn't been defined explicitly for the element, the color value will be used instead. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Foreground Color for Elements</title> <style> p.one { color: #0000ff; border: 2px solid; } p.two { color: #00ff00; outline: 2px solid; } </style> </head> <body> <p class="one">The border color of this paragraph is same as the element's text color.</p> <p class="two">The outline color of this paragraph is same as the element's text color.</p> </body> </html>
  • 34. CSS Background CSS provide several properties for styling the background of an element, including coloring the background, placing images in the background and managing their positioning, etc. The background properties are background-color, background-image, background-repeat, repeat or no-repeat background-attachment fixed or scroll background-position.
  • 35. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of CSS individual background properties</title> <style> body { background-color: #98effc; background-image: url("/examples/images/smiley.png"); background-repeat: repeat; background-attachment: fixed; background-position: 20px 205px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
  • 36. CSS Fonts CSS provide several properties for styling the font of the text, including changing their face, controlling their size and boldness, managing variant, and so on. The font properties are: font-family, serif, sans-serif, monospace, cursive and fantasy. font-style, normal, italic or oblique font-weight, font-size, percentage, pixels, ems Font-variant. If the name of a font family contains more than one word, it must be placed inside quotation marks, like "Times New Roman", "Courier New", "Segoe UI", etc.
  • 37. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting font-family in CSS</title> <style> body { font-family: Arial, Helvetica, sans-serif; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
  • 38. Setting Font Size with EM The em unit refers to the font size of the parent element. So, if you set a font-size of 20px on the body element, then 1em = 20px and 2em = 40px. However, if you haven't set the font size anywhere on the page, then it is the browser default, which is normally 16px. Therefore, by default 1em = 16px, and 2em = 32px.
  • 39. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Defining Font Size Using Percentage and EM</title> <style> body { font-size: 62.5%; /* font-size 1em = 10px */ } p { font-size: 1.4em; /* 1.4em = 14px */ } p span { font-size: 2em; /* 2em = 28px */ } </style> </head> <body> <h1>This is a heading</h1> <p>This is a <span>paragraph</span>.</p> </body> </html>
  • 40. Setting Font Size with Root EM 1rem is equivalent to the font size of the html element, which is 16px by default in most browsers. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Defining Font Size Using REM</title> <style> html { font-size: 62.5%; /* font-size 1em = 10px */ } body { font-size: 1.6rem; /* 1.6rem = 16px */ } p { font-size: 1.4rem; /* 1.4rem = 14px */ } p span { font-size: 2rem; /* 2rem = 20px (not 28px) */ } </style> </head> <body> <h1>This is a heading</h1> <p>This is a <span>paragraph</span>.</p> </body> </html>
  • 41. Setting Font Size with Keywords xx-small, x-small, small, medium, large, x-large, xx-large
  • 42. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Defining Font Size Using Keywords</title> <style> body { font-size: large; } h1 { font-size: larger; } p { font-size: smaller; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
  • 43. Setting Font Size with Viewport Units The font sizes can be specified using viewport units such as vw or vh. Viewport units refer to a percentage of the browser's viewport dimensions, where 1vw = 1% of viewport width, and 1vh = 1% of viewport height. Hence, if the viewport is 1600px wide, 1vw is 16px.
  • 44. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Defining Font Size Using Viewport Units</title> <style> body { font-size: 16vw; } h1 { font-size: 3vw; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
  • 45. However, there is a problem with the viewport units. On small screens fonts become so small that they are hardly readable. To prevent this you can utilize CSS calc() function, like this: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Fixing Font Size Issue with Viewport Units</title> <style> html { font-size: calc(1em + 1vw); } h1 { font-size: 3rem; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
  • 46. Font Weight The font-weight property specifies the weight or boldness of the font. This property can take one of the following values: normal, bold, bolder, lighter, 100, 200, 300, 400, 500, 600, 700, 800, 900 and inherit. ● The numeric values 100-900 specify the font weights, where each number represents a weight greater than its predecessor. 400 is same as normal & 700 is same as bold. ● The bolder and lighter values are relative to the inherited font weight, while the other values such as normal and bold are absolute font weights.
  • 47. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting font-weight in CSS</title> <style> p { font-weight: bold; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph in bold font.</p> </body> </html>
  • 48. Font Variant The font-variant property allows the text to be displayed in a special small-caps variation. Small-caps or small capital letters are slightly different to normal capital letters, in which lowercase letters appear as smaller versions of the corresponding uppercase letters.
  • 49. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting font-variant in CSS</title> <style> p { font-variant: small-caps; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>
  • 50. CSS Text The commonly used text properties are: text-align, left, right, centre or justified text-decoration, underline, overline, line-through, and none text-transform, text-indent, line-height, letter-spacing, word-spacing,
  • 51. Example <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Text Decoration using CSS</title> <style> h1 { text-decoration:overline; } h2 { text-decoration:line-through; } h3 { text-decoration:underline; } </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <h3>This is heading 3</h3> </body> </html>
  • 52. Removing the Default Underline from Links The text-decoration property is extensively used to remove the default underline from the HTML hyperlinks. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Removing the Default Underline from HTML Links</title> <style> a { text-decoration: none; border-bottom: 1px dotted; } a:hover { border-bottom: none; } </style> </head> <body> <p>Place your mouse pointer <a href="#">over me!</a></p> </body> </html>
  • 53. Text Transformation Using this property you can change an element's text content into uppercase or lowercase letters, or capitalize the first letter of each word without modifying the original text. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Text Transformation using CSS</title> <style> h1 { text-transform: uppercase; } h2 { text-transform: capitalize; } h3 { text-transform: lowercase; } </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <h3>This is heading 3</h3> </body> </html>
  • 54. Text Indentation The text-indent property is used to set the indentation of the first line of text within a block of text. It is typically done by inserting the empty space before the first line of text. The size of the indentation can be specified using percentage (%), length values in pixels, ems, etc. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Text Indentation using CSS</title> <style> p { text-indent: 100px; } </style> </head> <body> <h1>CSS Text Indentation</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh non aliquet sagittis.</p> </body> </html>
  • 55. Letter Spacing The letter-spacing property is used to set extra spacing between the characters of text. This property can take a length value in pixels, ems, etc. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Letter Spacing using CSS</title> <style> h1 { letter-spacing: -3px; } p { letter-spacing: 10px; } </style> </head> <body> <h1>This is a heading.</h1> <p>This is a simple paragraph of text.</p> </body> </html>
  • 56. Word Spacing The word-spacing property is used to specify additional spacing between the words. This property can accept a length value in pixels, ems, etc. Negative values are also allowed.
  • 57. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting word spacing using CSS</title> <style> p.one { word-spacing: 20px; } p.two { width: 150px; word-spacing: 20px; text-align: justify; } p.three { word-spacing: 20px; white-space: pre; } </style> </head> <body> <p class="one">This is a normal paragraph.</p> <hr> <p class="two">Note that spacing between the words of this paragraph are varying in order to justify the text even if the value of word-spacing property is set to 20px.</p> <hr> <p class="three">Note that spacing between the words of this paragraph are higher than the normal spacing even if whitespace are preserved.</p> </body> </html>
  • 58. Line Height The line-height property is used to set the height of the text line. It is also called leading and commonly used to set the distance between lines of text. The value of this property can be a number, a percentage (%), or a length in pixels, ems, etc.
  • 59. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Line Height using CSS</title> <style> p { line-height: 1.2; border: 1px solid #00ff00; } div { line-height: 200%; border: 1px solid #ff0000; } </style> </head> <body> <h1>Change the values to see how it works</h1> <p>The <code>line-height</code> property sets the height between lines of text.<br> The line height of this paragraph is specified using number.</p> <div>The <code>line-height</code> property sets the height between lines of text.<br> The line height of this paragraph is specified using percentage.</div> </body> </html>
  • 60. If the value of the line-height property is greater than the value of the font-size for an element, this difference (called the "leading") is cut in half (called the "half-leading") and distributed evenly on the top and bottom of the in-line box. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Line Height using CSS</title> <style> p { font-size: 14px; line-height: 50px; background-color: #f0e68c; } </style> </head> <body> <h1>Change the values to see how it works.</h1> <p>The <code>line-height</code> property sets the height between lines of text.<br> The line height of this paragraph is specified using pixels value.</p> </body> </html>
  • 61. Styling Links with CSS A link has four different states ● a:link — define styles for normal or unvisited links. ● a:visited — define styles for links that the user has already visited. ● a:hover — define styles for a link when the user place the mouse pointer over it. ● a:active — define styles for links when they are being clicked.
  • 62. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Styling Different Link States using CSS</title> <style> /* unvisited link */ a:link { color: #ff0000; text-decoration: none; border-bottom: 1px solid; } /* visited link */ a:visited { color: #ff00ff; } /* mouse over link */ a:hover { color: #00ff00; border-bottom: none; } /* active link */ a:active { color: #00ffff; } </style> </head> <body> <p><a href="https://www.tutorialrepublic.com/" target="_top">Visit Tutorial Republic</a></p> </body> </html>
  • 63. Modifying Standard Link Styles By default, text links will appear as follow in most of the browsers: ● An unvisited link as underlined blue text. ● A visited link as underlined purple text. ● An active link as underlined red text.
  • 64. Removing the Default Underline from Links you can simply use the CSS text-decoration property to get rid of it. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Remove Default Underline from Links using CSS</title> <style> a:link, a:visited { text-decoration: none; } a:hover, a:active { text-decoration: underline; } </style> </head> <body> <p><a href="#">Place mouse pointer over me</a></p> </body> </html>
  • 65. Making Text Links Look Like Buttons CSS properties such as background-color, border, display, padding, etc. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Customize a Link as Button using CSS</title> <style> a:link, a:visited { color: white; background-color: #1ebba3; display: inline-block; padding: 10px 20px; border: 2px solid #099983; text-decoration: none; text-align: center; font: 14px Arial, sans-serif; } a:hover, a:active { background-color: #9c6ae1; border-color: #7443b6; } </style> </head> <body> <p><a href="#">CSS Link Button</a></p> </body> </html>
  • 66. Types of HTML Lists There are three different types of list in HTML: ● Unordered lists — A list of items, where every list items are marked with bullets. ● Ordered lists — A list of items, where each list items are marked with numbers. ● Definition list — A list of items, with a description of each item.
  • 67. Styling Lists with CSS CSS provides the several properties for styling and formatting the most commonly used unordered and ordered lists. These CSS list properties typically allow to: ● Control the shape or appearance of the marker. ● Specify an image for the marker rather than a bullet point or number. ● Set the distance between a marker and the text in the list. ● Specify whether the marker would appear inside or outside of the box containing the list items.
  • 68. Changing the Marker Type of Lists By default, items in an ordered list are numbered with Arabic numerals (1, 2, 3, 5, and so on), whereas in an unordered list, items are marked with round bullets (•). But, you can change this default list marker type to any other type such as roman numerals, latin letters, circle, square, and so on using the list-style-type property.
  • 69. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Numbering or Bullet Point Style of Lists</title> <style> ul { list-style-type: square; } ol { list-style-type: upper-roman; } </style> </head> <body> <h2>Unordered List</h2> <ul> <li>List Item 1</li> <li>List Item 2</li> <li>List Item 3</li> </ul> <h2>Ordered List</h2> <ol> <li>List Item 1</li> <li>List Item 2</li> <li>List Item 3</li> </ol> </body> </html>
  • 70. Changing the Position of List Markers By default, markers of each list items are positioned outside of their display boxes. list-style-positionproperty along with the value inside.
  • 71. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting the Position of List Marker</title> <style> body{ font-size: 14px; font-family: Arial,sans-serif; } ol li { background: #ddd; padding: 5px; margin: 5px; } ol.in li { list-style-position: inside; } ol.out li { list-style-position: outside; } </style> </head> <body> <h2>List Marker Position - Inside</h2> <ol class="in"> <li>Fasten your seatbelt</li> <li>Start the car's engine and take a closer look the instrument cluster for any warning sign</li> <li>Look around carefully and go</li> </ol> <h2>List Marker Position - Outside</h2> <ol class="out"> <li>Fasten your seatbelt</li> <li>Start the car's engine and take a closer look the instrument cluster for any warning sign</li> <li>Look around carefully and go</li> </ol> </body> </html>
  • 72. Using Images as List Markers You can also set an image as a list marker using the list-style-image property. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting an Image as List Marker with CSS</title> <style> ul li { list-style-image: url("/examples/images/bullet.png"); margin: 5px; } </style> </head> <body> <h2>Using Image as Bullet Point</h2> <ul> <li>List Item 1</li> <li>List Item 2</li> <li>List Item 3</li> </ul> </body> </html>
  • 73. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Setting Image Marker in Background with CSS</title> <style> ul { list-style-type: none; } ul li { background-image: url("/examples/images/bullet.png"); background-position: 0px 3px; background-repeat: no-repeat; padding-left: 20px; margin: 5px } </style> </head> <body> <h1>Using Image as Bullet Point</h1> <ul> <li>List Item 1</li> <li>List Item 2</li> <li>List Item 3</li> </ul> </body> </html>
  • 74. Setting All List Properties At Once The list-style property is a shorthand property for defining all the three properties list-style-type , list-style-image , and list-style-position of a list in one place. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS list-style Shorthand Property</title> <style> ul { list-style: square inside url("/examples/images/bullet.png"); background: #9ddfef; } ul li { background: #ededed; margin: 5px 0; padding: 5px; } </style> </head> <body> <h2>The <code>list-style</code> Shorthand Property</h2> <ul> <li>List Item 1</li> <li>List Item 2</li> <li>List Item 3</li> </ul> </body> </html>
  • 75. Creating Navigation Menus Using Lists HTML lists are frequently used to create horizontal navigation bar or menu that typically appear at the top of a website. But since the list items are block elements, so to display them inline we need to use the CSS display property.
  • 76. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Building Navigation Bar with HTML List and CSS</title> <style> body{ font-size: 14px; font-family: Arial,sans-serif; } ul { padding: 0; list-style: none; background: #f2f2f2; } ul li { display: inline-block; } ul li a { display: block; padding: 10px 25px; color: #333; text-decoration: none; } ul li a:hover { color: #fff; background: #939393; } </style> </head> <body> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About Us</a></li> <li><a href="#">Products</a></li> <li><a href="#">Services</a></li> <li><a href="#">Portfolio</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <p><strong>Note:</strong> Place mouse pointer over the menu link to see the hover effect.</p> </body>
  • 77. Styling Tables with CSS Adding Borders to Tables The CSS border property is the best way to define the borders for the tables.
  • 78. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of Setting Table Borders</title> <style> table, th, td { border: 1px solid black; } </style> </head> <body> <table> <tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <tr> <td>1</td> <td>John</td> <td>Carter</td> <td>johncarter@mail.com</td> </tr> <tr> <td>2</td> <td>Peter</td> <td>Parker</td> <td>peterparker@mail.com</td> </tr> <tr> <td>3</td> <td>John</td> <td>Rambo</td> <td>johnrambo@mail.com</td> </tr> <tr> <td>4</td> <td>Harry</td> <td>Potter</td> <td>harrypotter@mail.com</td> </tr> </table> </body> </html>
  • 79. Collapsing Table Borders There are two distinct models for setting borders on table cells in CSS: separate and collapse. <style> table { border-collapse: saparate; } table, th, td { border: 1px solid black; } </style>
  • 80. Adjusting Space inside Tables To add more space between the table cell contents and the cell borders, you can simply use the CSS padding property. <style> table { border-collapse: collapse; } table, th, td { border: 1px solid black; } th, td { padding: 15px; } </style>
  • 81. CSS border-spacing spacing between the borders of the cells using the CSS border-spacing property. <style> table { border-spacing: 10px; } table, th, td { border: 1px solid black; } th, td { padding: 10px; } </style>
  • 82. Setting Table Width and Height you can also set the width and height of the table as well as its cells explicitly using the width and height CSS property. <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; border: 1px solid #dee2e6; } th { height: 70px; text-align: left; } </style>
  • 83. Controlling the Table Layout A table expands and contracts to accommodate the data contained inside it. This property takes one of two values: ● auto — Uses an automatic table layout algorithm. With this algorithm, the widths of the table and its cells are adjusted to fit the content. This is the default value. ● fixed — Uses the fixed table layout algorithm. With this algorithm, the horizontal layout of the table does not depend on the contents of the cells; it only depends on the table's width, the width of the columns, and borders or cell spacing. It is normally faster than auto.
  • 84. Example <style> table { width: 250px; border-collapse: separate; } table, tr, th, td{ border: 1px solid #000000; } .auto { table-layout: auto; } .fixed { table-layout: auto; } td{ width: 50%; } </style>
  • 85. Aligning the Text Inside Table Cells Horizontal Alignment of Cell Contents For horizontal alignment of text inside the table cells you can use the text-align property in the same way as you use with other elements. You align text to either left, right, center or justify.
  • 86. Example <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; border: 1px solid #dee2e6; } th { text-align: left; } </style> <table> <thead> <tr> <th>Row</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Clark</td> <td>Kent</td> <td>clarkkent@mail.com</td> </tr> </tbody> </table>
  • 87. Vertical Alignment of Cell Contents vertically align the content inside the <th> and <td> elements to top, bottom, or middle using the CSS vertical-align property. The default vertical alignment is middle. <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; border: 1px solid #dee2e6; } th { height: 400px; vertical-align: top; } </style>
  • 88. Controlling the Position of Table Caption set the vertical position of a table caption using the CSS caption-side property. The caption can be placed either at the top or bottom of the table. <style> table, td, th { border: 1px solid gray; } caption { caption-side: bottom; } </style>
  • 89. Handling Empty Cells This property accepts a value of either show or hide. The default value is show, which renders empty cells like normal cells, but if the value hide is specified no borders or backgrounds are drawn around the empty cells. <style> table { width: 300px; border-collapse: separate; } table, th, td{ border: 1px solid #000000; } table.empty-show { empty-cells: show; } table.empty-hide { empty-cells: hide; }
  • 90. <h2>Table with Empty-cells</h2> <table class="empty-show"> <tr> <th>Name</th> <td>John Carter</td> </tr> <tr> <th>Email</th> <td></td> </tr> </table> <h2>Table with Hidden Empty-cells</h2> <table class="empty-hide"> <tr> <th>Name</th> <td>Peter Parker</td> </tr> <tr> <th>Email</th> <td></td> </tr> </table>
  • 91. Creating Zebra-striped Tables Setting different background colors for alternate rows is a popular technique to improve the readability of tables that has large amount of data. This is commonly known as zebra-striping a table. You can simply achieve this effect by using the CSS :nth-child() pseudo-class selector.
  • 92. Example <style> table { width: 100%; font-family: arial, sans-serif; border-collapse: collapse; } th, td { padding: 8px; text-align: left; border-top: 1px solid #dee2e6; } tbody tr:nth-child(odd) { background-color: #f2f2f2; } </style>
  • 93. Making a Table Responsive Tables are not responsive in nature. However, to support mobile devices you can add responsiveness to your tables by enabling horizontal scrolling on small screens. To do this simply wrap your table with a <div> element and apply the style overflow-x: auto;
  • 94. <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; text-align: left; border: 1px solid #dee2e6; white-space: nowrap; /* to prevent text wrapping */ } .responsive-table { overflow-x: auto; } </style> <div class="responsive-table"> <table> <thead> <tr> <th>ID</th> <th>Supplier</th> <th>Contact Name</th> <th>Address</th> <th>City</th> <th>Postal Code</th> <th>Country</th> <th>Phone</th> </tr> </thead> </tbody> </table> </div>
  • 95. CSS Box Model Every element that can be displayed on a web page is comprised of one or more rectangular boxes. CSS box model typically describes how these rectangular boxes are laid out on a web page. These boxes can have different properties and can interact with each other in different ways, but every box has a content area and optional surrounding padding, border, and margin areas.
  • 96. Width and Height of the Elements
  • 97. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Box Formatting Model</title> <style> div { width: 300px; height: 200px; padding: 15px; border: 10px solid black; margin: 20px auto; } </style> </head> <body> <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh non aliquet sagittis. In tincidunt orci sit amet elementum vestibulum. Vivamus fermentum in arcu in aliquam. Quisque aliquam porta odio in fringilla. Vivamus nisl leo, blandit at bibendum eu, tristique eget.</div> </body> </html>
  • 98.
  • 99. CSS Dimension Setting Element Dimensions CSS has several dimension properties, such as width, height, max-width, min-width, max-height, and min-height that allows you to control the width and height of an element.
  • 100. Setting the Width and Height The width and height property defines the width and height of the content area of an element. This width and height does not include paddings, borders, or margins. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Width and Height of an Element</title> <style> div { width: 300px; height: 200px; background: #eee8aa; } </style> </head> <body> <div>Play with the values to see how it works!</div> </body> </html>
  • 101. Setting Maximum Width and Height <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Maximum Width of an Element</title> <style> div { width: 300px; max-width: 200px; background: #bbb3ff; } p { float: left; max-width: 400px; background: #eee8aa; } </style> </head> <body> <div>The maximum width of this div element is set to 200px, so it can't be wider than that.</div> <p>Enter some text to see how it works.</p> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Maximum Height of an Element</title> <style> div { height: 200px; max-height: 100px; background: #bbb3ff; } p { max-height: 100px; background: #eee8aa; } </style> </head> <body> <div>The maximum height of this div element is set to 100px, so it can't be taller than that.</div> <p>Enter some more line of text to see how it works.</p> </body> </html>
  • 102. Setting Minimum Width and Height <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Minimum Width of an Element</title> <style> div { width: 200px; min-width: 300px; background: #bbb3ff; } p { float: left; min-width: 400px; background: #eee8aa; } </style> </head> <body> <div>The minimum width of this div element is set to 300px, so it can't be narrower than that.</div> <p>Enter some text to see how it works.</p> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set minimum Height of an Element</title> <style> div { height: 200px; min-height: 300px; background: #bbb3ff; } p { min-height: 100px; background: #eee8aa; } </style> </head> <body> <div>The minimum height of this div element is set to 300px, so it can't be smaller than that.</div> <p>Enter some more line of text to see how it works.</p> </body> </html>
  • 103. Setting a Width and Height Range <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Width Range for an Element</title> <style> div { float: left; min-width: 300px; max-width: 500px; height: 100px; background: #eee8aa; } </style> </head> <body> <p><strong>Note:</strong> The minimum width of the following div element will be 300px, and it can stretches horizontally up to 500px. Enter few lines of text inside the div element to understand how it works.</p> <div></div> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Height Range for an Element</title> <style> div { min-height: 300px; max-height: 500px; background: #eee8aa; } </style> </head> <body> <p><strong>Note:</strong> The minimum height of the following div element will be 300px, and it can stretches vertically up to 500px. Enter few lines of text inside the div element to understand how it works.</p> <div></div> </body> </html>
  • 104. Setting a Width and Height Range <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Width Range for an Element</title> <style> div { float: left; min-width: 300px; max-width: 500px; height: 100px; background: #eee8aa; } </style> </head> <body> <p><strong>Note:</strong> The minimum width of the following div element will be 300px, and it can stretches horizontally up to 500px. Enter few lines of text inside the div element to understand how it works.</p> <div></div> </body> </html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Set Height Range for an Element</title> <style> div { min-height: 300px; max-height: 500px; background: #eee8aa; } </style> </head> <body> <p><strong>Note:</strong> The minimum height of the following div element will be 300px, and it can stretches vertically up to 500px. Enter few lines of text inside the div element to understand how it works.</p> <div></div> </body> </html>
  • 105. CSS Padding Properties Define Paddings for Individual Sides CSS padding-top, padding-right, padding-bottom, and the padding-left properties, respectively.
  • 106. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Padding for Individual Sides</title> <style> h1 { padding-top: 50px; padding-bottom: 100px; background: lime; } p { padding-left: 75px; padding-right: 75px; background: lime; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a simple paragraph of text.</p> <p><strong>Note:</strong> Play with the padding property value to see how it works.</p> </body> </html>
  • 107. The Padding Shorthand Property <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Set Padding for All Sides At Once</title> <style> h1 { padding: 50px; /* apply to all four sides */ } p { padding: 25px 75px; /* vertical | horizontal */ } div { padding: 25px 50px 75px; /* top | horizontal | bottom */ } pre { padding: 25px 50px 75px 100px; /* top | right | bottom | left */ } h1, p, div, pre { background: lime; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a simple paragraph of text.</p> <div>This is a DIV element.</div> <pre>This is a piece of preformatted text.</pre> <p><strong>Note:</strong> Play with the padding property value to see how it works.</p> </body> </html>
  • 108. Effect of Padding and Border on Layout When creating web page layouts, adding a padding or border to the elements sometimes produce unexpected result, because padding and border is added to the width and height of the box generated by the element. For instance, if you set the width of a <div> element to 100% and also apply left right padding or border on it, the horizontal scrollbar will appear. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Effect of CSS Padding on Element Box Size</title> <style> div { width: 100%; padding: 25px; background: violet; } </style> </head> <body> <div> <h1>This is a DIV Box</h1> </div> <p><strong>Notice</strong>, the scrollbar at the bottom of the viewport.</p> </body> </html>
  • 109. Effect of Padding and Border on Layout To prevent padding and border from changing element's box width and height, you can use the CSS box-sizing property. In the following example the width and height of the <div> box will remain unchanged, however, its content area will decrease with increasing padding or border. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Preventing Change in Element Box Dimensions</title> <style> div { width: 100%; padding: 25px; box-sizing: border-box; background: violet; } </style> </head> <body> <div> <h1>This is a DIV Box</h1> </div> <p><strong>Notice</strong>, this time there is no scrollbar at the bottom of the viewport.</p> </body> </html>
  • 110. CSS Border Properties The CSS border properties allow you to define the border area of an element's box. Borders appear directly between the margin and padding of an element. The border can either be a predefined style like, solid line, dotted line, double line, etc. or an image.
  • 111. Understanding the Different Border Styles The border-style property can have the following values: none, hidden, solid, dashed, dotted, double, inset, outset, groove, and ridge.
  • 112. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS border-style Property</title> <style> p { border-width: 15px; background: yellow; padding: 20px; margin: 20px; } p.none { border-style: none; } p.hidden { border-style: hidden; } p.dotted { border-style: dotted; } p.dashed { border-style: dashed; } p.solid { border-style: solid; } p.double { border-style: double; } p.groove { border-style: groove; } p.ridge { border-style: ridge; } p.inset { border-style: inset; } p.outset { border-style: outset; } </style> </head> <body> <p class="none">No border.</p> <p class="hidden">Hidden border.</p> <p class="dotted">A dotted border.</p> <p class="dashed">A dashed border.</p> <p class="solid">A solid border.</p> <p class="double">A double border.</p> <p class="inset">An inset border.</p> <p class="outset">An outset border.</p> <p class="groove">A groove border.</p> <p class="ridge">A ridge border.</p> </body> </html>
  • 113. Setting the Border Width The border-width property specifies the width of the border area. It is a shorthand property for setting the thickness of all the four sides of an element's border at the same time. border-width: 10px;
  • 114. Specifying the Border Color The border-color property specifies the color of the border area. border-color: #ff0000;
  • 115. The Border Shorthand Property border-width, border-style and border-color in a single rule. border: 5px solid #00ff00;
  • 116. For instance, if the value for the border-color property is missing or not specified when setting the border, the element's color property will be used as the value for the border color. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Omitting border-color Property</title> <style> p { color: red; background: yellow; border: 5px solid; padding: 20px; margin: 20px; } </style> </head> <body> <p>The border color of this paragraph is same as the text color.</p> </body> </html>
  • 117. But, in the case of border-style, omitting the value will cause no border to show at all, because the default value for this property is none. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Ignoring border-style Property</title> <style> p { border: 5px #00ff00; background: yellow; padding: 20px; margin: 20px; } </style> </head> <body> <p>This paragraph has no border.</p> </body> </html>
  • 118. CSS Margin Properties The CSS margin properties allow you to set the spacing around the border of an element's box. An element's margin is not affected by its background-color, it is always transparent.
  • 119. Setting Margins for Individual Sides CSS margin-top, margin-right, margin-bottom, and the margin-left properties, respectively. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CSS Margin for Individual Sides</title> <style> h1 { margin-top: 50px; margin-bottom: 100px; background: yellow; } p { margin-left: 75px; margin-right: 75px; background: yellow; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a simple paragraph of text.</p> <p><strong>Note:</strong> Play with the margin property value to see how it works.</p> </body> </html>
  • 120. The Margin Shorthand Property The margin property is a shorthand property to avoid setting margin of each side separately, i.e., margin-top, margin-right, margin-bottom and margin-left. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Set Margin for All Sides At Once</title> <style> h1 { margin: 50px; /* apply to all four sides */ } p { margin: 25px 75px; /* vertical | horizontal */ } div { margin: 25px 50px 75px; /* top | horizontal | bottom */ } hr { margin: 25px 50px 75px 100px; /* top | right | bottom | left */ } h1, p, div { background: yellow; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a simple paragraph of text.</p> <div>This is a DIV element.</div> <hr> <p><strong>Note:</strong> Play with the margin property value to see how it works.</p> </body> </html>
  • 121. Horizontal Centering with Auto Margins The auto value for the margin property tells the web browser to automatically calculate the margin. This is commonly used to center an element horizontally within a larger container. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Centering an Element with CSS Auto Margin</title> <style> .container { width: 300px; height: 200px; background: #ddd; margin: 0 auto; } </style> </head> <body> <div class="container"> <h1>This is a heading</h1> <p>This is a paragraph of text.</p> </div> </body> </html>