Introduction to CSS Basics and Syntax
Introduction to CSS Basics and Syntax
LECTURE-9
2.1 Cascading Style Sheet, fondly referred to as CSS, is a simple design language intended
to simplify the process of making web pages presentable.
CSS describes how HTML elements are to be displayed on screen, paper, or in other media.
CSS is used to define styles for your web pages, including the design, layout and variations
in display for different devices and screen sizes.
2.1.3 Where?
CSS allows control over the presentation and styling of HTML documents.
CSS handles the look and feel part of a web page. Using CSS, you can control the color of the
text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out,
what background images or colors are used, layout designs, variations in display for different
devices and screen sizes as well as a variety of other effects.
CSS is easy to learn and understand but it provides powerful control over the presentation
of an HTML document. Most commonly, CSS is combined with the mark-up languages
HTML or XHTML.
∙ CSS saves time − You can write CSS once and then reuse same sheet in
multiple HTML pages. You can define a style for each HTML element and apply
it to as many Web pages as you want.
∙ Pages load faster − If you are using CSS, you do not need to write HTML
tag ,attributes every time. Just write one CSS rule of a tag and apply it to all
the occurrences of that tag. So less code means faster download times.
66
∙ Easy maintenance − To make a global change, simply change the style, and
all elements in all the web pages will be updated automatically.
∙ Superior styles to HTML − CSS has a much wider array of attributes than
HTML, so you can give a far better look to your HTML page in comparison to
HTML attributes.
∙ Multiple Device Compatibility − Style sheets allow content to be optimized
for more than one type of device. By using the same HTML document,
different versions of a website can be presented for handheld devices such as
PDAs and cell phones or for printing.
∙ Global web standards − Now HTML attributes are being deprecated and it is
being recommended to use CSS. So its a good idea to start using CSS in all the
HTML ages to make them compatible to future browsers.
CSS Syntax
67
The selector points to the HTML element you want to style.
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded
by curly braces.
● External CSS
● Internal CSS
● Inline CSS
With an external style sheet, you can change the look of an entire website by changing just one
file!
Each HTML page must include a reference to the external style sheet file inside the <link>
element, inside the head section.
Example
External styles are defined within the <link> element, inside the <head> section of an HTML
page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<h1>Welcome to designing.</h1>
<p> An external style sheet can be written in any text editor, and must be saved with a .css
extension.<br>
The external .css file should not contain any HTML tags
68
.</p>
</body>
</html>
An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags.
"[Link]"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value (20) and the unit (px):
Incorrect (space): margin-left: 20 px;
Correct (no space): margin-left: 20px;
Output:
69
Figure 2.1 External CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style> element, inside the head section.
Example
Internal styles are defined within the <style> element, inside the <head> section of an HTML
page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
70
}
</style>
</head>
<body>
<h1>Welcome to designing.</h1>
<p> An external style sheet can be written in any text editor, and must be saved with a .css
extension.<br>
The external .css file should not contain any HTML tags
.</p>
</body>
</html>
Output:
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain
any CSS property.
Example
71
Inline styles are defined within the "style" attribute of the relevant element:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output:
72
External style sheets have the least priority. If there are no styles defined either in
inline or internal style sheet then external style sheet rules are applied for the HTML
tags.
LECTURE-10
CSS selectors are used to "find" (or select) the HTML elements you want to style.
Select element to style on the basis of the name of tag select .In below example ,we
select a <p>(para tag) on which apply the CSS properties.
Example:
p{
text-align: center;
color: red;
}
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
73
color: red;
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
Output:
And me!
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique
element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element.
#para1 {
text-align: center;
color: red;
}
74
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with class="center" will be red and center-aligned:
75
.center {
text-align: center;
color: blue;
}
Output:
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
*{
text-align: center;
color: blue;
}
Output:
76
Figure 2.5 All center text
The grouping selector selects all the HTML elements with the same style definitions.
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
Output:
77
Figure 2.6 Grouping Selector
[Link]. Descendent Selectors: This Selector targets both direct and indirect children of a parent
tag. The symbol is (space)
Syntax: parent_tag child_tag{property: value;}
[Link]. Child Selectors: This Selector targets the only the direct children of the parent tag. The
symbol is > (greater than)
Syntax: parent_tag > child_tag{property: value;}
[Link]. Adjacent Selectors: This Selector targets the element which is the first sibling of the
targeted HTML tag or element.
This Selector targets the element which is exactly adjacent to the targeted HTML tag or element.
The symbol is + (plus)
78
Syntax: parent_tag + target_tag{property: value;}
[Link]. General Sibling Selectors: This Selector targets all the siblings of the target HTML tag
or element.
This Selector targets all the adjacent tags of the target HTML tag or element.
The symbol is ~ (tilde)
Syntax: parent_tag ~ target_tag{property: value;}
LECTURE-11
1. property-name: property-value
CSS Background-clip The background-clip property in CSS is used to define how to extend
Property the background (color or image) within an element.
79
background-color: color name
Example: background-color:red;
background-image:
Example:
background-image: url('img_tree.gif'), url('[Link]');
background-image: conic-gradient(red, yellow, green);
background-position
background-size
● background-size: auto;
● background-size: contain;
● background-size: cover;
● background-size: 50%;
● background-size: 30px;
● background-size: 60px;
background-repeat
80
background-repeat: repeat;
background-repeat: repeat-x;
background-repeat: repeat-y;
background-repeat: no-repeat;
background-repeat: space;
background-repeat: round;
background-origin
background-origin: padding-box;
background-origin: border-box;
background-origin: content-box;
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
height:200px;
padding: 25px;
background-origin: border-box;
</style>
</head>
81
<body>
<div id="myDIV">
This demo shows how to specify where the background image should be positioned.
</div>
</body>
</html>
background-clip
background-clip: border-box;
background-clip: padding-box;
background-clip: content-box;
[Link] Text Format: CSS Text Formatting refers to applying styles to text elements to control
appearance and layout. This includes properties for color, alignment, decoration, indentation,
justification, shadows, spacing, and direction. These properties enhance readability and aesthetics,
improving the presentation of textual content on web pages.
Property Description
text-color Sets the color of the text using color name, hex value, or RGB value.
text-align Specifies horizontal alignment of text in a block or table-cell element.
text-align-last Sets alignment of last lines occurring in an element.
text-decoration Decorates text content.
text-decoration-color Sets color of text decorations like overlines, underlines, and line-throughs.
text-decoration-line Sets various text decorations like underline, overline, line-through.
text-decoration-style Combines text-decoration-line and text-decoration-color properties.
text-indent Indents first line of paragraph.
text-justify Justifies text by spreading words into complete lines.
text-overflow Specifies hidden overflow text.
82
Property Description
text-transform Controls capitalization of text.
text-shadow Adds shadow to text.
letter-spacing Specifies space between characters of text.
line-height Sets space between lines.
direction Sets text direction.
word-spacing Specifies space between words of line.
The color property is used to set the color of the text. The color is specified by:
Property Description
direction Specifies the text direction/writing direction
text-align Specifies the horizontal alignment of text
text-align-last Specifies how to align the last line of a text
Used together with the direction property to set or return whether
unicode-bidi the text should be overridden to support multiple languages in the
same document
vertical-align Sets the vertical alignment of an element
Property Description
letter-spacing Specifies the space between characters in a text
line-height Specifies the line height
text-indent Specifies the indentation of the first line in a text-block
white-space Specifies how to handle white-space inside an element
83
word-spacing Specifies the space between words in a text
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Advanced Text Formatting</title>
<style>
.text-color {
color: blue;
}
.text-align-center {
text-align: center;
}
.text-decoration {
text-decoration: underline;
}
.text-indent {
text-indent: 20px;
}
.letter-spacing {
letter-spacing: 2px;
}
.line-height {
line-height: 1.5;
}
.text-shadow {
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
.text-transform {
text-transform: uppercase;
}
.word-spacing {
84
word-spacing: 5px;
}
.text-direction {
direction: rtl;
}
</style>
</head>
<body>
<p class="text-color">Changing Text Color</p>
<p class="text-align-center">Aligning Text</p>
<p class="text-decoration">Adding Text Decoration</p>
<p class="text-indent">Setting Text Indentation</p>
<p class="letter-spacing">Adjusting Letter Spacing</p>
</html>
OUTPUT:
85
Figure 2.7 Align Text
1. Serif fonts have a small stroke at the edges of each letter. They create a sense of
formality and elegance.
2. Sans-serif fonts have clean lines (no small strokes attached). They create a modern and
minimalistic look.
3. Monospace fonts - here all the letters have the same fixed width. They create a
mechanical look.
4. Cursive fonts imitate human handwriting.
5. Fantasy fonts are decorative/playful fonts.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.p1 {
.p2 {
.p3 {
86
}
</style>
</head>
<body>
<h1>CSS font-family</h1>
</body>
</html>
OUTPUT:
87
Figure 2.8 Font family
A block-level element always starts on a new line, and the browsers automatically add some
space (a margin) before and after the element.
A block-level element always takes up the full width available (stretches out to the left and right
as far as it can).
88
The <p> element is a block-level element.
<div>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>
<p>The yellow background is added to demonstrate the footprint of the DIV element.</p>
</body>
</html>
OUTPUT:
89
Figure 2.9 Div Example
90
This is a <span> element inside a paragraph.
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<a> <abbr> <acronym> <b> <bdo> <big> <br> <button> <cite> <code> <dfn> <em> <i>
<img> <input> <kbd> <label> <map>
<object> <output> <q> <samp> <script> <select> <small> <span> <strong> <sub>
<sup> <textarea> <time> <tt> <var>
91
LECTURE-12
List-style-type:type name;
Property Description
Example:
<!DOCTYPE html>
<html>
<head>
92
<style>
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
</style>
</head>
<body>
<ul class="b">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ol class="d">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
93
</ol>
</body>
</html>
When you add a border to a table, you also add borders around each table cell:
94
[Link].1 Table Border
To add a border, use the CSS border property on table, th, and td elements:
table, th, td {
border: 1px solid black;
}
Eg:-
table {
width: 100%;
}
To avoid having double borders like in the example above, set the CSS border-
collapse property to collapse.
Eg:-
table {
border-collapse: collapse;
95
Eg:-
table {
width: 100%;
}
th {
height: 70px;
}
The vertical-align property sets the vertical alignment (like top, bottom, or
middle) of the content in <th> or <td>.
Eg:-
td {
height: 50px;
vertical-align: bottom;
}
96
[Link].1.2. border-bottom:- Add the border-bottom property to <th> and <td> for
horizontal dividers. Eg:-
th, td {
border-bottom: 1px solid #ddd;
}
Eg:-
th {
background-color: #04AA6D;
color: white;
}
[Link] CSS Table Properties
Property Description
caption-side Specifies whether or not to display borders and background on empty cells in
a table
empty-cells Sets the layout algorithm to be used for a table
table-layout
Internal css in table
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
97
th, td {
background-color: #96D4D4;
border-radius: 10px;
padding: 15px;
border-spacing: 30px;
}
Example:
!DOCTYPE html>
<html>
<head>
<style>
body
background-color:aqua;
table {
border-collapse: collapse;
width: 100%;
th, td {
98
border:2px soild red;
text-align: left;
padding-top: 10px;
padding-bottom: 20px;
padding-left: 30px;
padding-right: 40px;
tr:nth-child(even) {
background-color: #D6EEEE;
</style>
</head>
<body>
<p>For zebra-striped tables, use the nth-child() selector and add a background-color to all even
(or odd) table rows:</p>
<table>
<tr>
<th>First Name</th>
99
<th>Last Name</th>
<th>Points</th>
</tr>
2.6.1 Class Attribute: The HTML class attribute is used to specify a class for an HTML
element.
Multiple HTML elements can share the same class.
<!DOCTYPE html>
<html>
100
<head>
<style>
.city1,.city3{
background-color: tomato;
color: white;
border: 2px solid black;
margin: 20px;
padding: 20px;
}
.city2
{ background-color: lightpink;
color: white;
border: 2px solid black;
margin: 20px;
padding: 20px;
}
body
{
Background-color:aqua;
Border :5px solid red;
}
</style>
</head>
<body>
<div class="city1">
<h2>London</h2>
<p>London is the capital of England.</p>
</div>
<div class="city2">
<h2>Paris</h2>
<p>Paris is the capital of France.</p>
</div>
<div class="city3">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
</div>
</body>
</html>
101
Figure 2.12 Class Attribute
2.6.2 ID element: he HTML id attribute is used to specify a unique id for an HTML element.
You cannot have more than one element with the same id in an HTML document.
<!DOCTYPE html>
<html>
<head>
<style>
#myHeader {
background-color: lightblue;
102
color: black;
padding: 40px;
text-align: center;
}
</style>
</head>
<body>
</body>
</html>
The HTML <iframe> tag specifies an inline frame.
An inline frame is used to embed another document within the current HTML document.
Syntax:
<iframe src="url" title="description"></iframe>
Example:
<!DOCTYPE html>
<html>
<body>
<h2>HTML Iframes</h2>
<p>You can also use the CSS height and width properties to specify the size of the iframe:</p>
</body>
</html>
103
LECTURE-13
In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of:
content, padding, borders and margins. The image below illustrates the box model:
● Content - The content of the box, where text and images appear
● Padding - Clears an area around the content. The padding is transparent
● Border - A border that goes around the padding and content
● Margin - Clears an area outside the border. The margin is transparent
Example:
<!DOCTYPE html>
<html>
<head>
104
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 100px;
margin: 50px;
}
</style>
</head>
<body>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists
of: borders, padding, margins, and the actual content.</p>
<div>This text is the content of the box. We have added a 50px padding, 20px margin and a
15px green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.</div>
</body>
</html>
105
Figure 2.14 Box Model example
106
● none - Defines no border
● hidden - Defines a hidden border
The border-style property can have from one to four values (for the top border, right border,
bottom border, and the left border).
Example:
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
107
<h2>The border-style Property</h2>
</body>
</html>
108
Figure 2.15 Border Types
109
2.7.2 CSS Border Width
border-width:size;
Example:
border-width: 5px;
The border-color property is used to set the color of the four borders.
Syntax:
Border-color:color name;
The border property is a shorthand property for the following individual border properties:
● border-width
● border-style (required)
● border-color
Property Description
110
border-radius A shorthand property for setting all the four border-*-*- radius
properties
CSS has properties for specifying the padding for each side of an element:
● padding-top
● padding-right
● padding-bottom
● padding-left
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 1px solid black;
background-color: lightblue;
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
111
padding-left: 80px;
}
</style>
</head>
<body>
<div>This div element has a top padding of 50px, a right padding of 30px, a bottom padding of
50px, and a left padding of 80px.</div>
</body>
</html>
2.7.5 Margin:
Margins are used to create space around elements, outside of any defined borders.
CSS has properties for specifying the margin for each side of an element:
● margin-top
● margin-right
● margin-bottom
● margin-left
112
LECTURE-14
2.8.1Grouping
<!DOCTYPE html>
<html lang="en">
<head>
<title>Grouping Selector in CSS</title>
<style>
div, article {
background-color: #04af2f;
color: white;
text-align: center;
font-size: 20px;
}
#id1, #id2 {
padding: 5px;
border: 2px solid black;
}
</style>
</head>
<body>
<h2>
Grouping Selector in CSS
</h2>
<p><strong>
In this example, both div and article
element have same background-color, text
color and aligned to center using
grouping selector.
</strong></p>
<div>
This is a div element
</div>
<br>
113
<article>
This is an article element.
</article>
<p><strong>
In this example, both h4 and span
element have same padding, and
border properties using grouping
selector.
</strong></p>
<h4 id="id1">
This h4 element has padding and border
properties.
</h4>
<span id="id2">
This span element also has padding and
border properties.
</span>
</body>
</html>
Output:
114
2.8.2 Dimension
CSS dimension define the size and space occupied by elements on a webpage. The dimension
properties like height, width, max-height, max-width, line-height and many more are used to
define width, height of HTML elements in every screen sizes. In this tutorial we will learn how
to manage dimension and layout of HTML elements in varying screen sizes.
The height and width properties allow you to set specific height and width for your positioned
element.
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 100px;
width: 80%;
background-color: aqua;
}
img{
height: auto;
width: 180px;
}
</style>
</head>
<body>
<h1>Height and Width Property</h1>
<h2>Length and percentage values</h2>
<div>
115
This div element has a height of 50px and a width
of 80%.
</div>
<h2>Auto value</h2>
<img src="[Link] "/>
<br>
Height is this image adjusted for width 180px so that
aspect ratio is maintained.
</body>
</html>
LECTURE-15
116
2.8.3 Display
The CSS display property specifies an element’s display behavior (the type of rendering box). It
defines how an element is rendered in the layout, determining its positioning and interaction within
the document’s flow and structure.
[Link] Syntax:
display: value;
Value Description
inline It is used to display an element as an inline element.
block It is used to display an element as a block element
contents It is used to disappear the container.
flex It is used to display an element as a block-level flex container.
grid It is used to display an element as a block-level grid container.
inline-block It is used to display an element as an inline-level block container.
inline-flex It is used to display an element as an inline-level flex container.
inline-grid It is used to display an element as an inline-level grid container.
inline-table It is used to display an inline-level table
list-item It is used to display all the elements in <li> element.
It is used to display an element inline or block level, depending on the
run-in
context.
table It is used to set the behavior as <table> for all elements.
table-caption It is used to set the behavior as <caption> for all elements.
table-column-
It is used to set the behavior as <column> for all elements.
group
table-header-group It is used to set the behavior as <header> for all elements.
table-footer-group It is used to set the behavior as <footer> for all elements.
table-row-group It is used to set the behavior as <row> for all elements.
table-cell It is used to set the behavior as <td> for all elements.
table-column It is used to set the behavior as <col> for all elements.
table-row It is used to set the behavior as <tr> for all elements.
none It is used to remove the element.
initial It is used to set the default value.
inherit It is used to inherit property from its parents’ elements.
Example:
<!DOCTYPE html>
117
<html>
<head>
<style>
li {
display: inline;
}
</style>
</head>
<body>
<ul>
<li><a href="/html/[Link]" target="_blank">HTML</a></li>
<li><a href="/css/[Link]" target="_blank">CSS</a></li>
<li><a href="/js/[Link]" target="_blank">JavaScript</a></li>
</ul>
</body>
</html>
2.8.4 Positioning
The position property specifies the type of positioning method used for an element.
● static
● relative
118
● fixed
● absolute
● sticky
[Link] Static : Static positioned elements are not affected by the top, bottom, left, and right
properties.
An element with position: static; is not positioned in any special way; it is always positioned
according to the normal flow of the page.
Syntax: postion:static;
A fixed element does not leave a gap in the page where it would normally have been located.
Syntax: position: fixed;
[Link] Relative: An element with position: relative; is positioned relative to its normal
position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it
to be adjusted away from its normal position.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
[Link] {
119
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
background-image:url("[Link]");
}
[Link] {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
</style>
</head>
<body>
<h2>position: absolute;</h2>
<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor
(instead of positioned relative to the viewport, like fixed):</p>
</body>
</html>
120
Figure 2.19 Position :absolute
2.8.5 Floating
The float property specifies whether an element should float to the left, right, or not at all.
Syntax:
float: none|left|right|initial|inherit;
<!DOCTYPE html>
<html>
<head>
<style>
img {
float: left;
}
</style>
</head>
<body>
121
<h1>The float Property</h1>
<p>In this example, the image will float to the left in the text, and the text in the paragraph will
wrap around the image.</p>
</body>
</html>
2.8.6 Align
CSS alignment refers to the positioning and distribution of elements within their container using
CSS. It controls how content is laid out horizontally and vertically.
Note: Using margin: auto will not work in IE8 unless a !DOCTYPE is declared.
Example 1: This example describes the CSS align using the margin: auto property.
<!DOCTYPE html>
<html>
<head>
<style>
.center {
margin: auto;
width: 60%;
border: 3px solid #73AD21;
padding: 10px;
}
122
</style>
</head>
<body>
<h1 style="color:green;">
Welcome to designing interface
</h1>
<h2>Center Align Elements</h2>
<div class="center">
This is div element on which
margin auto is used to horizontally
align it into center
</div>
</body>
</html>
Output:
The position: absolute property aligns elements relative to their containing block.
Note: The position properties top, bottom, left, and right will not work unless the position
property is set first.
Example 2: This example describes the CSS align using the position: absolute; property.
<!DOCTYPE html>
<html>
<head>
<style>
.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
</style>
</head>
<body>
123
<h1 style="color:green;">
Welcome to designing interface
</h1>
<h2>Right Align</h2>
<div class="right">
<p>
Absolute positioned elements
can overlap other elements.
</p>
</div>
</body>
</html>
Output:
Example 3: This example describes the CSS align using the text-align: center; property.
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
border: 3px solid green;
}
</style>
</head>
<body>
<h1 style="color:green;
text-align: center;">
Welcome to designing interface </h1>
<h2>BOTH TEXTS ARE AT CENTER</h2>
<div class="center">
<p>This text is centered.</p>
</div>
</body>
124
</html>
Example 4: This example describes the CSS align using the padding property.
<!DOCTYPE html>
<html>
<head>
<style>
.center {
padding: 70px 0;
border: 3px solid green;
}
</style>
</head>
<body>
<h1 style="color:green;
text-align:center;">
Welcome to designing interface
</h1>
<h2>Center Vertically</h2>
<div class="center">
<p>This is vertically centered.</p>
</div>
</body>
</html>
Output:
Combining padding and text-align: center to center elements both vertically and horizontally.
Example 5: This example describes the CSS align using the padding & text-align properties.
<!DOCTYPE html>
125
<html>
<head>
<style>
.center {
padding: 70px 0;
border: 3px solid green;
text-align: center;
}
</style>
</head>
<body>
<h1 style="color:green;">
Welcome to designing interface
</h1>
<p>
Here we use padding and
text-align to center the
div element vertically
and horizontally:
</p>
<div class="center">
<p>
This text is vertically
and horizontally centered.
</p>
</div>
</body>
</html>
Center an Image:
To center an image, set left and right margin to auto and make it into a block element:
<!DOCTYPE html>
<html>
<head>
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
126
<body>
<h2>Center an Image</h2>
<p>To center an image, set left and right margin to auto, and make it into a block element.</p>
</body>
</html>
[Link] clearfix :
If an element is taller than the element containing it, and it is floated, it will overflow outside of
its container. You can use the "clearfix hack" to fix this (see example below).
.img1 {
float: right;
127
}
.img2 {
float: right;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
</style>
</head>
<body>
<h2>Without Clearfix</h2>
<p>This image is floated to the right. It is also taller than the element containing it, so it
overflows outside of its container:</p>
<div>
<img class="img1" src="[Link]" alt="Pineapple" width="170" height="170">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet...
</div>
<div class="clearfix">
<img class="img2" src="[Link]" alt="Pineapple" width="170" height="170">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet...
</div>
</body>
</html>
LECTURE-16
A pseudo-class can be defined as a keyword which is combined to a selector that defines the
special state of the selected elements.
128
2.9.2 Why It is added to the selector for adding an effect to the existing elements based on their
states. For example, The ":hover" is used for adding special effects to an element when the user
moves the cursor over the element.
2.9.3 Where
2.9.4 Syntax:
selector:pseudo-class {
property: value;
}
129
2. Visited
3. Hover
4. Active
5. Focus
o Each dynamic pseudo class selector is declared with the single colon :
o Link, Visited only applicable for anchor tag.
o Link is used to style the unvisited link
o Link doesn’t style the already visited link.
o Active applies the style at the time of click event.
o Hover is applicable for all the HTML elements.
o Focus styles when the element is focused.
o Focus selector is applicable for <a> tag and also for the elements which takes user input
that is for <input> and textarea> tag.
o While using all the Link, Visited, Hover and Active selectors. Hover should be declared
after Link and Visited. Along with that the Active should be declared after Hover,
because to make the elements effective.
Example:
a : link {
color: aqua;
background-color : red;
}
a : visited {
color : green;
}
a : hover {
color : yellow;
}
a : active {
color : chocolate;
}
input : focus{
color : red;
background-color : yellow;
}
[Link] Structural Pseudo Classes: The Structural Pseudo Class selectors is used to target the
combination of the HTML elements and to style them.
1. First-child
2. Last-child
3. Nth-child
4. First-of-type
130
5. Last-of-type
[Link].1 First-child: The First-child pseudo class selector targets the first child element of the
targeted parent.
Example: div p : first-child{
color: red;
background-color: yellow;
}
[Link].2 Last-child: The Last-child pseudo class selector targets the last child element of the
targeted parent.
Example: div p : last-child{
color: red;
background-color: yellow;
}
[Link].3 Nth-child: The nth-child( ) pseudo class selector targets the elements according to the
argument passed.
Example: div p : nth-child(2){
color: red;
background-color: yellow;
}
[Link].4 First-of-type: The first-of-type pseudo class selectors targets the first element of the
input type.
The input type should be the first type of all the input types.
It also behaves as first-child selector.
It is applicable for text, submit, reset, button.
It is not applicable for radio, checkbox, textarea,.. so on.
Example: input : first-of-type{
color: red;
background-color: yellow;
}
[Link].5 Last-of-type: The Last-of-type pseudo class selectors targets the last element of the
input.
The input type should be the last type of all the input types.
It also behaves as last-child selector.
It is applicable for text, submit, reset, button.
It is not applicable for radio, checkbox, textarea,.. so on.
Example: input : last-of-type{
color: red;
background-color: yellow;
}
131
[Link].6 Nth-of-type: The nth-of-type pseudo class selectors targets all the element of the input
type.
It also behaves as nth-child selector.
It is applicable for text, submit, reset, button.
It is not applicable for radio, checkbox, textarea,.. so on.
Example: input : nth-of-type(n){
color: red;
background-color: yellow;
Example:
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
/* visited link */
a:visited {
color: green;
a:hover {
color: hotpink;
132
}
/* selected link */
a:active {
color: blue;
</style>
</head>
Example:
<html>
<head>
<style>
p{
display: none;
background-color: yellow;
padding: 20px;
div:hover p {
display: block;
</style>
</head>
133
<body>
</div>
</body>
</html>
2.10.1 First Letter: The first letter styles the very first letter of the content in the targeted
HTML element.
Example: p : : first-letter{
font-size: 30px;
color: black;
134
}
2.10.2 First Line: The first line styles the very first line of the content in the targeted HTML
element.
Example: p : : first-line{
background-color: black;
color: white;
}
2.10.3 Before: The before pseudo element is used to place the specific content before the
targeted HTML element.
Example: p : : before{
content: “&phone”;
background-color: black;
color: white;
}
2.10.4 After: The after pseudo element is used to place the specific content after the targeted
HTML element.
Example: p : : after{
content: “Thank You”;
color: blue;
}
2.10.5 Selection: The selection pseudo element is used to style the content when the cursor is
dragged on the content (when the content is selected) on the targeted
HTML element.
Also copying the text content from the user can also be disabled, by using the property user-
select: none.
Example: p : : selection{
background-color: pink;
color: magenta;
}
2.10.6 Marker: Marker pseudo element is used to style the lists in the HTML document.
Marker is only used to style the list type not the content.
Declaration of selector is not mandatory here.
Background color will not work for Marker.
Example: li : : marker{
color: red;
font-size: 30px;
}
135
A navigation bar is basically a list of links, so using the <ul> and <li> elements makes perfect
sense:
<!DOCTYPE html>
<html>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>Note: We use href="#" for test links. In a real web site this would be URLs.</p>
</body>
</html>
Example for horizontal navigation
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
136
Figure 2.21 Marker
A web page with many images can take a long time to load and generates multiple server
requests.
2.12.2 Why
Using image sprites will reduce the number of server requests and save bandwidth.
2.12.3 Where
In navigation bar
Example;
137
!DOCTYPE html>
<html>
<head>
<style>
#home {
width: 46px;
height: 44px;
background: url(img_navsprites.gif) 0 0;
}
#next {
width: 43px;
height: 44px;
background: url(img_navsprites.gif) -91px 0;
}
</style>
</head>
<body>
</body>
</html>
2.13.1 What
This selector selects elements that have specified attribute, regardless of its value or type of
element.
[Link] Syntax
[attribute]{
property: value;
}
138
The [attribute] selector is used to select elements with a specified attribute.
The following example selects all <a> elements with a target attribute:
<!DOCTYPE html>
<html>
<head>
<style>
a[target] {
background-color: yellow;
</style>
</head>
<body>
<a href="[Link]
</body>
</html>
Output:
139
Figure 2.22 attribute selector
2.13.2 Why
● Flexibility: You don't have to rely on classes or IDs. You can style elements based on
any attribute they have, which can be especially useful when working with elements like
links, form inputs, or images.
● Semantics: They can be more semantically meaningful. For example, styling all links
with href attributes or styling all image elements based on their alt attributes makes your
CSS more logical and accessible.
● Target Specific Elements: You can target a subset of elements that share a common
attribute without needing to add extra markup. This is useful when working with
dynamically generated content or third-party libraries where you can't modify the HTML
directly.
● Avoid Overuse of Classes: Instead of cluttering the markup with additional classes just
for styling, you can leverage attributes to avoid redundant or unnecessary classes in your
HTML.
140
2.13.3 Where
Forms
Responsive design
This selector selects elements that have a specific attribute with a specific value.
Syntax
[attribute="value"]{
property: value;
}
Example
This selector selects all elements with a 'data-toggle' attribute whose value is set to 'yes'.
Open Compiler
<!DOCTYPE html>
<html>
141
<head>
<style>
[data-toggle="yes"] {
background: lightgray;
padding: 10px;
color: black;
}
</style>
</head>
<body>
<div data-toggle="yes">
The div with data-toggle="yes" attribute
</div>
<div>
The div without data-toggle attribute
</div>
<p data-toggle="no">
A paragraph with data-toggle="no" attribute
</p>
<p>
A paragraph without data-toggle attribute
</p>
</body>
</html>
This selector selects elements that have a specific attribute with a value containing a specific
substring.
Syntax
[attribute*="value"]{
property: value;
}
Example
This selector selects all the elements with a "src" attribute which contain an "css" in the path:
Open Compiler
<!DOCTYPE html>
<html>
142
<head>
<style>
img{
height: 50px;
width: 150px;
}
[src*="css"] {
border: 2px solid;
}
</style>
</head>
<body>
<p>
Style applied to src attribute containing string 'css' in it
</p>
<img alt="Logo" src = "/css/images/[Link]" />
<img alt="Logo" src = "/html/images/[Link]" />
</body>
</html>
This selector selects elements that have a specific attribute with a value that starts with a specific
string.
Syntax
[attribute^="value"]{
property: value;
}
Example
This selector selects all elements with a "href" attribute starting with "[Link]
<html>
<head>
<style>
[href^="https"] {
background: lightgray;
color:black;
143
}
</style>
</head>
<body>
<a href="[Link] Link</a>
<br> <br>
<a href="[Link] Link</a>
</body>
</html>
This selector selects elements that have a specific attribute with a value that ends with a specific
string.
Syntax
[attribute$="value"]{
property: value;
}
Example
This selector selects all elements with a "src" attribute which ends with ".png"
Open Compiler
<!DOCTYPE html>
<html>
<head>
<style>
img{
height: 50px;
width: 150px;
}
[src$=".png"] {
border: 2px solid;
}
</style>
</head>
<body>
<p>
144
Style applied to src attribute's value ends with '.png'
</p>
<img alt="Logo" src = "/images/[Link]" />
<img alt="Logo" src = "/html/images/[Link]" />
</body>
</html>
This selector select elements that have a specific attribute with a value that begins with a
specified substring followed by a hyphen (-). This selector is often used for selecting elements
with language-specific attributes, such as lang attributes, which often use hyphens to denote
language sub-codes.
Syntax
[attribute|="value"]{
property: value;
}
Example
This selector selects all the elements with a "lang" attribute that starts with "en" followed by a
hyphen:
Open Compiler
<html>
<head>
<style>
[lang|="en"] {
background: lightgray;
padding: 10px;
color: black;
}
</style>
</head>
<body>
<div lang="en"> This is a div in English! </div>
<p lang="en-US"> This paragraph is in American English. </p>
<p lang="en-GB"> This paragraph is in British English. </p>
<div lang="fr"> Bonjour tout le monde en français! </div>
<div lang="es"> Hola Mundo en español! </div>
</body>
145
</html>
This selector is used to select elements that have a specific attribute with a value containing a
specified word. The word should be a standalone word, surrounded by spaces or at the beginning
or end of the attribute value.
Syntax
[attribute~="value"]{
property: value;
}
Example
This selector select all elements with a "class" attribute containing the word "highlight"
<html>
<head>
<style>
[class~="highlight"] {
background: yellow;
padding: 10px;
color: black;
}
</style>
</head>
<body>
<div class="highlight">
This is a highlighted div!
</div>
<p class="highlight special">
This paragraph is highlighted and special.
</p>
<p class="special">
This paragraph is special but not highlighted.
</p>
<div class="note">
This is just a note.
</div>
<div class="highlight note">
146
This note is highlighted.
</div>
</body>
</html>
Attribute selectors can be used to select input elements based on specific criteria, such as their
type, name, value, or other attributes.
Example
<html>
<head>
<style>
/* Applies to all input tags */
input {
display: block;
margin: 10px;
padding: 5px;
}
/* Applies to input tags with type attribute */
input[type] {
border: 1px solid gray;
}
/* Applies to input tag with placeholder value as name */
input[placeholder="name"] {
border: 1px solid #f00;
}
/* Applies to input tags name attribute value start with "emailid" */
input[name|="emailid"] {
background-color: rgb(236, 178, 233);
}
/* Applies to input type attributes value containing "sub" string in it */
input[type~="sub"] {
background-color: rgb(88, 241, 88);
padding: 10px;
}
</style>
</head>
<body>
<input type="text" placeholder="Username">
<input type="text" placeholder="name">
147
<input type="email" placeholder="Email" name="emailid">
<input type="submit" placeholder="Submit">
</body>
</html>
You can use the lang attribute to select elements based on their language. The lang attribute
specifies the language of the text contained within an element.
Example
Open Compiler
<html>
<head>
<style>
div[lang] {
color: red;
}
div[lang="fr"] {
color: blue;
}
div[lang~="es"] {
color: green;
}
div[lang|="de"] {
color: orange;
}
div[lang^="it"] {
color: purple;
}
div[lang$="ja"] {
color: brown;
}
div[lang*="zh"] {
color: teal;
}
</style>
</head>
<body>
148
<div lang="en">Hello World in English!</div>
<div lang="fr">Bonjour tout le monde en français!</div>
<div lang="es">Hola Mundo en español!</div>
<div lang="ja">こんにちは、日本語で世界!</div>
<div lang="de">Hallo Welt auf Deutsch!</div>
<div lang="it">Ciao Mondo in italiano!</div>
<div lang="zh">你好世界,中文!</div>
</body>
</html>
CSS multiple attribute selectors allow you to select elements based on multiple attribute values.
It is use to target specific element that meet multiple criteria.
Example
<html>
<head>
<style>
/* Remove bullet points from list items */
ul {
list-style: none;
}
/* Target anchor elements with both specific href values and file extension */
a[href="css_backgrounds.htm"][href$=".htm"] {
background-color: lightgray;
padding: 5px;
}
149
<body>
<ul>
<li><a href="css_text.htm">
CSS Text
</a></li>
<li><a href="css_backgrounds.htm">
CSS Background
</a></li>
<li><a href="css_colors.htm">
CSS Color
</a></li>
</ul>
</body>
</html>
2.14 CSS Color: Colors are specified using predefined color names, or RGB, HEX, HSL,
RGBA, HSLA values.
Syntax: color:color name;
Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255.
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and
the others are set to 0.
To display black, set all color parameters to 0, like this: rgb(0, 0, 0).
To display white, set all color parameters to 255, like this: rgb(255, 255, 255).
RGBA color values are an extension of RGB color values with an alpha channel - which
specifies the opacity for a color.
150
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all):
Experiment by mixing the RGBA values below:( rgba(255, 99, 71, 0.5).
A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB
(blue) hexadecimal integers specify the components of the color.
#rrggbb
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff (same as
decimal 0-255).
For example, #ff0000 is displayed as red, because red is set to its highest value (ff) and the others
are set to the lowest value (00).
#ff6347
In CSS, a color can be specified using hue, saturation, and lightness (HSL) in the form:
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and 240 is blue.
Saturation is a percentage value. 0% means a shade of gray, and 100% is the full color.
151
Lightness is also a percentage. 0% is black, 50% is neither light or dark, 100% is white
The lightness of a color can be described as how much light you want to give the color, where
0% means no light (black), 50% means 50% light (neither dark nor light) and 100% means full
lightness (white).
HSLA color values are an extension of HSL color values with an alpha channel - which specifies
the opacity for a color.
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at
all).5, 99, 71, 0.5
Example:
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:Tomato;",”color:red;”>Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
<h1 style="background-color:#ff6347;">#ff6347</h1>
152
<h1 style="background-color:hsl(9, 100%, 64%);">hsl(9, 100%, 64%)</h1><h1
style="background-color:LightGray;">LightGray</h1>
</body>
</html>
LECTURE-18
A website can be divided into various sections comprising of header, menus, content, and footer
based on which there are many different layout designs available for developers. Different layouts
can be created by using a div tag and using CSS property to style it.
A header is usually located at the top of the website (or right below a top navigation menu). It
often contains a logo or the website name.
[Link].Navigation Bar
153
A navigation bar contains a list of links to help visitors navigating through your website.
2.15.1.3Content Section
The content section is the main body of the website. The user can divide the content section in an
n-column layout.
The most common layouts are:
● 2 Column Layout: This website layout is mostly used for tablets or laptops.
154
Figure 2.25 2 column Layout
The user can also create a responsive layout where the layout will get changed as per screen size.
Consider the below example where if the idth of the screen is more than 600px then there will be
a 3-column layout and if the width of the screen is between 400px to 600px then there will be a 2-
column layout and if the screen size is less than 400px then the 1-column layout will display.
[Link].Footer
The footer is placed at the bottom of your page. It often contains information like copyright and
contact info.
155
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*{
box-sizing: border-box;
color:white;
body {
margin: 0;
background-image:url("[Link]");
color:white;
.header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
background-image:url("[Link]");
156
color:white;
.topnav {
overflow: hidden;
background-color: #333;
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
text-decoration: none;
.topnav a:hover {
background-color: #ddd;
color: red;
157
/* Create three equal columns that floats next to each other */
.column {
float: left;
width: 33.33%;
padding: 15px;
.row::after {
content: "";
display: table;
clear: both;
/* Responsive layout - when the screen is less than 800px wide, make the two columns stack on
top of each other instead of next to each other */
.leftcolumn, .rightcolumn {
width: 100%;
padding: 0;
/* Responsive layout - when the screen is less than 400px wide, make the navigation links stack
on top of each other instead of next to each other */
158
@media screen and (max-width: 400px) {
.topnav a {
float: none;
width: 100%;
.footer {
padding: 20px;
align: center;
background: #ddd;
margin-top: 20px;
text-align: center;
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
</div>
159
<div class="topnav">
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</div>
<div class="row">
<div class="column">
<p>Responsive Web design is the approach that suggests that design and development should
respond to the user’s behavior and environment based on screen size, platform and
orientation.</p>
</div>
<div class="column">
<h2>Resposive architecture</h2>
<p>Recently, an emergent discipline called “responsive architecture” has begun asking how
physical spaces can respond to the presence of people passing through them. Through a
combination of embedded robotics and tensile materials, architects are experimenting with art
installations and wall structures that bend, flex, and expand as crowds approach them. Motion
sensors can be paired with climate control systems to adjust a room’s temperature and ambient
lighting as it fills with people. Companies have already produced “smart glass technology” that
can automatically become opaque when a room’s occupants reach a certain density threshold,
giving them an additional layer of privacy.”</p>
</div>
<div class="column">
160
<h2>Flexible Images</h2>
</div>
</div>
<div class="footer">
<h2>Footer</h2>
</div>
</body>
</html>
Output:
161
Figure 2.27 Header
● Fixed Layout: The dimensions are given in fixed units (like pixels) and do not
change with the size of the browser window.
● Fluid Layout: Uses percentages for widths, allowing the layout to adjust to the
browser width.
● Responsive Layout: Uses media queries to adapt to different screen sizes and
orientations, providing optimal viewing across a range of devices.
162
● Grid Layout: Utilizes CSS Grid to define a grid-based layout system, making it
easier to design complex layouts.
IMPORTANT QUESTIONS
Q1: Create a css rule that makes all the text 2 times larger than the base font of the system.
Mention can you integrate css on a web page.
Q2: Describe the role and importance of CSS in web designing. Also Differentiate Class and Id
in CSS.
Q3:Define CSS. Explain the different ways to implement CSS using suitable example.
Q10:Explain the list and table using CSS with suitable example.
163