Introduction to CSS1
Introduction to CSS1
styling web pages. It works hand in hand with HTML to control the visual
What is CSS?
CSS stands for Cascading Style Sheets. It's a style sheet language that
page. By using CSS, you can control things like colors, fonts, spacing, and
positioning of elements.
The word cascading means that a style applied to a parent element will
also apply to all children elements within the parent. So, if you set the color of
the body text to "blue", all headings, paragraphs, and other text elements within
the body will also get the same color (unless you specify something else)!
These rules specify how the content should be displayed in the browser.
internally within the HTML file using the <style> tag, or externally by linking to
selector
property: value;
Selectors:
Selectors are patterns used to select the HTML elements you want to style.
They can target elements based on their tag names, classes, IDs, attributes, or
CSS properties define the visual appearance of HTML elements, and each
Comments:
You can add comments in CSS using /* */. Comments are ignored by
/* This is a comment */
External CSS:
To keep your CSS separate from your HTML code and to apply styles
across multiple pages, you can create an external CSS file and link it to your
<head>
</head>
Inheritance and Specificity:
CSS styles can be inherited from parent elements, but they can also be
Responsive Design:
With CSS, you can create responsive designs that adapt to different screen
sizes and devices. Techniques like media queries allow you to apply different
There are CSS frameworks like Bootstrap and Foundation that provide
pre-written CSS and HTML templates to help you quickly build responsive
websites. Additionally, preprocessors like Sass and Less extend the functionality
-------------------------------------------------------------
CSS COLORS:
CSS (Cascading Style Sheets) offers various ways to specify colors for styling
1. Color Names: CSS supports a set of predefined color names, such as "red",
characters (0-9 and A-F) preceded by a hash (#). Each pair of characters
represents the intensity of red, green, and blue (RGB) components. For
example:
3. RGB Function: Colors can also be specified using the rgb() function,
which allows you to define colors using the intensity of the red, green, and
is green, and 240 is blu ) color model, which provides a more intuitive
way to specify colors. The hsl() function allows you to define colors based
/* Blue */
6. HSLA Function: Similar to hsl(), the hsla() function allows you to specify
color: hsla(0, 100%, 50%, 0.5); /* Red with 50% opacity */ background-color:
CSS3 BACKGROUND:
properties for an element. Here are some of the most commonly used sub-
properties of background:
background of an element.
background-image: url('background.jpg');
Values can be keywords like top, bottom, left, right, or percentage values.
scrolls with the content or remains fixed. Values can be scroll or fixed.
*/
box */
image.
For example:
div
size: cover;
This CSS rule would set a gray background color with a background image
Example Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3 Background Example</title>
<style>
body {
margin: 0;
padding: 0;
height: 100vh; /* Set the height of the body to the viewport height */
background-color: #f0f0f0; /* Fallback background color */
background-image: linear-gradient(to bottom, #ffffff, #f0f0f0); /* Linear gradient
background */
font-family: Arial, sans-serif; /* Specify font */
display: flex; /* Center content vertically */
justify-content: center; /* Center content horizontally */
align-items: center; /* Center content vertically */
}
.container {
width: 80%;
max-width: 600px; /* Maximum width of the container */
padding: 20px;
background-color: rgba(255, 255, 255, 0.9); /* Semi-transparent background color */
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); /* Box shadow for a subtle depth effect */
border-radius: 10px; /* Rounded corners */
text-align: center;
}
h1 {
color: #333333; /* Text color */
margin-bottom: 20px;
}
p{
color: #666666; /* Text color */
line-height: 1.6; /* Line height for better readability */
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to CSS3 Background Example</h1>
<p>This is a demonstration of how you can use CSS3 to style backgrounds in HTML.</p>
</div>
</body>
</html>
CSS BORDER:
1. border-style: Sets the style of the border. Common values include solid,
2. border-width: Sets the width of the border. You can specify it in pixels
3. border-color: Sets the color of the border. You can use color names,
4. border: A shorthand property that allows you to set all border properties
corners. You can specify values in pixels (px), ems (em), or percentages
(%).
CSS Padding:
CSS Padding property is used to define the space between the element
It is different from CSS margin in the way that CSS margin defines the
the content.
Top, bottom, left and right padding can be changed independently using
separate properties. You can also change all properties at once by using shorthand
padding property.
Property Description
Value Description
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: pink;
}
p.padding {
padding-top: 50px;
padding-right: 100px;
padding-bottom: 150px;
padding-left: 200px;
</style>
</head>
<body>
</body>
</html>
• The CSS height and width properties are used to set the height and width
of an element.
element.
o The height and width properties are used to set the height and width of an
element.
margins. It sets the height/width of the area inside the padding, border, and
The height and width properties may have the following values:
• auto - This is default. The browser calculates the height and width
Example
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
Setting max-width
▪ The max-width can be specified in length values, like px, cm, etc., or in
percent (%) of the containing block, or set to none (this is default. Means
▪ The problem with the <div> above occurs when the browser window is
smaller than the width of the element (500px). The browser then adds a
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
Outline
An outline is a line that is drawn around elements, OUTSIDE the borders, to make
outline-style
outline-color
outline-width
outline-offset
Outline Offset
The outline-offset property adds space between an outline and the edge/border of
margin: 30px;
outline-offset: 15px;
Text Color
body {
color: blue;
}
Text Alignment
1. text-align
2. text-align-last
3. direction
4. unicode-bidi
5. vertical-align
Text Alignment — text-align
o set the horizontal alignment of a text
o default: left
o Values: - left, right, center, justify
o “justify” — each line is stretched so that every line has equal width,
and the left and right margins are straight
Text Direction
The direction and unicode-bidi properties can be used to change the text
direction of an element
p{
direction: rtl;
unicode-bidi: bidi-override;
}
Vertical Alignment
sets the vertical alignment of an element
img.a {
vertical-align: baseline;
}
<p>An <img class="a" src="sqpurple.gif" width="9" height="9">
image with a default alignment.</p>
Text Decoration
• text-decoration-line
• text-decoration-color
• text-decoration-style
• text-decoration-thickness
• text-decoration
text-decoration-line
• adds a decoration line to text
• values: overline, line-through, underline
text-decoration-color
▪ set the color of the decoration line.
▪ values: colors name, RGB, HSL
text-decoration-style
Same as border style and same values
text-decoration-thickness
❖ can be auto or a value
The text-decoration: none; is used to remove the underline from links
a{
text-decoration: none;
}
Text Transformation
❖ specify uppercase and lowercase letters in a text
❖ values: uppercse, lowercase, capitalize
Text Spacing
Text Indentation
specify the indentation of the first line of a text
p{
text-indent: 50px;
}
Letter Spacing
specify the space between the characters in a text
h1 {
letter-spacing: 5px;
}
Line Height
used to specify the space between lines
p.extrabig {
line-height: 0.8;
}
Word Spacing
used to specify the space between the words in a text
p.one {
word-spacing: 10px;
}
White Space
specifies how white-space inside an element is handled.
p{
white-space: nowrap;
Text Shadow
adds shadow to text.
specify the horizontal shadow (2px) and the vertical shadow (2px)
h1 {
text-shadow: 2px 2px;
}
• Choosing the right font has a huge impact on how the readers experience
a website.
• The right font can create a strong identity for your brand.
• Using a font that is easy to read is important. The font adds value to your
text. It is also important to choose the correct color and text size for the
font.
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.
All the different font names belong to one of the generic font families.
Sans-serif Arial
Verdana
Helvetica
Fantasy Copperplate
Papyrus
Css links:
With CSS, links can be styled in many different ways.
Styling Links
Links can be styled with any CSS property (e.g. color, font-family,
background, etc.).
Example
a{
color: hotpink;
}
In addition, links can be styled differently depending on what state they are in.
The four links states are:
• a:link - a normal, unvisited link
• a:visited - a link the user has visited
• a:hover - a link when the user mouses over it
• a:active - a link the moment it is clicked
Example
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* selected link */
a:active {
color: blue;}
CSS List Styles
o The List in CSS determines how the contents or items are listed in a
certain fashion, i.e., they can be arranged either neatly or randomly,
which aids in creating a clean webpage.
o Because they are adaptable and simple to handle, they may be used
to organize large amounts of material.
o The list's default style is borderless.
2. Ordered List: The list elements in ordered lists are identified by numbers
and letters.
The following CSS list properties are available for usage in controlling the CSS
lists:
o List-style-image: The pictures that will serve as list item markers may be
``specified using this parameter.
Syntax
selector:pseudo-class
property: value;
They can be applied to elements based on their state, position in the DOM, or
user interaction without needing to add specific classes or IDs to the HTML.
1. :hover
3. :focus
• Applies when an element has received focus, either from the user
selecting it with the mouse or by navigating to it using the keyboard.
4. :visited
5. :first-child
6. :last-child
7. :nth-child(n)
• Applies to an element that is the nth child of its parent, where n can
be a number, a keyword (odd or even), or a formula (an+b).
9. :first-of-type
10.:last-of-type
11.:not(selector)
12.:empty
14.:checked
15.:target
They are written with double colons (::) but some older pseudo-elements are still
supported with a single colon (:).
1. ::before
2. ::after
4. ::first-line
5. ::selection
6. ::placeholder
CSS navigation refers to styling navigation bars and menus on a web page using
CSS.
Navigation bars are an essential part of web design, providing a way for users to
navigate through different sections of a website.
Types of Navigation
Basic Structure
A typical navigation bar consists of an unordered list (<ul>) with list items (<li>)
and links (<a>).
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
Horizontal Navigation Bar
To create a horizontal navigation bar, you need to style the <ul> and <li> elements
to display inline.
nav ul {
margin: 0;
padding: 0;
overflow: hidden;
nav ul li {
nav ul li a {
display: block;
text-align: center;
nav ul li a:hover {
For a vertical navigation bar, you can stack the list items vertically and adjust the
width.
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
nav ul li {
nav ul li a {
display: block;
color: white;
text-decoration: none;
nav ul li a:hover {
background-color: #111; }
Dropdown Menus
Dropdown menus can be created by nesting another unordered list inside a list
item.
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a>
<ul>
<li><a href="#seo">SEO</a></li>
</ul>
</li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
nav ul ul {
background-color: #333;
padding: 0;
margin: 0;
nav ul ul li {
nav ul ul li a {
<nav>
<ul id="nav-menu">
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<ahref="javascript:void(0);” class="icon"
onclick="toggleMenu()">☰</a>
</nav>
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
nav ul li {
float: left;
nav ul li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
nav ul li a:hover {
background-color: #111;
.icon {
nav ul li {
float: none;
nav ul li a {
display: block;
text-align: left;
.icon {
float: right;
font-size: 30px;
}
CSS Selector:
CSS selectors are patterns used to select and style the elements on a web page.
They form the basis of CSS, allowing you to apply styles to HTML elements
based on their type, class, ID, attributes, and more.
Basic Selectors
*{
margin: 0;
padding: 0;
p{
color: blue;
Class Selector
.example {
font-size: 20px;
}
ID Selector
Selects the element with a specific ID. IDs should be unique within a page.
#header {
background-color: grey;
Attribute Selector
a[target] {
color: red;
input[type="text"] {
Combinator Selectors
div p {
color: green;
div > p {
color: blue;
h1 + p {
font-size: 18px;
h1 ~ p {
color: grey;
Pseudo-Class Selectors
:hover
a:hover {
text-decoration: underline;
:focus
input:focus {
border-color: blue;
}
:nth-child(n)
tr:nth-child(even) {
background-color: #f2f2f2;
:first-child / :last-child
p:first-child {
font-weight: bold;
p:last-child {
font-style: italic;
:not(selector)
p:not(.intro) {
color: grey;
Pseudo-Element Selectors
::before
Inserts content before the content of an element.
p::before {
font-weight: bold;
color: red;
::after
p::after {
font-style: italic;
::first-letter
p::first-letter {
font-size: 2em;
color: blue;
::first-line
p::first-line {
font-weight: bold;
}
Grouping Selectors
h1, h2, h3 {
CSS3 User Interface: Box Sizing, Filters – Menu creation -Responsive CSS
1. Box Sizing
Definition: Controls how the total width and height of an element are calculated.
Property: box-sizing
Values:
content-box (default): The width and height include only the content, not
padding, border, or margin.
border-box: The width and height include the content, padding, and border, but
not the margin.
Usage:
*{
box-sizing: border-box;
}
This setting simplifies layout design by making the element’s width and height
calculations more intuitive.
2. Filters
Property: filter
Common Filters:
Usage:
.nav-links a:hover {
filter: brightness(85%);
This example darkens the menu links on hover for a subtle visual effect.
3. Responsive Design
Definition: Creating web pages that look good on all devices by using flexible
layouts, images, and CSS media queries.
Key Techniques:
Flexible Grid Layouts: Use percentages or flexible units (like em, rem, or
vw/vh) for widths and heights.
Media Queries: Apply different styles based on the device's characteristics (e.g.,
screen width, resolution).
html
CSS Flexbox and Grid: Modern layout models that provide powerful alignment
and distribution capabilities.
HTML Structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Responsive Menu</title>
</head>
<body>
<header>
<nav class="navbar">
<div class="logo">MySite</div>
<ul class="nav-links">
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
</nav>
</header>
<script src="script.js"></script>
</body>
</html>
CSS Styling:
/* styles.css */
*{
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
header {
background-color: #333;
color: #fff;
padding: 10px 0;
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 20px;
.logo {
font-size: 24px;
font-weight: bold;
.nav-links {
list-style: none;
display: flex;
}
.nav-links li {
margin: 0 10px;
.nav-links a {
color: #fff;
text-decoration: none;
.nav-links a:hover {
background-color: #575757;
border-radius: 4px;
filter: brightness(85%);
.menu-toggle {
display: none;
flex-direction: column;
cursor: pointer;
.menu-toggle .bar {
width: 25px;
height: 3px;
background-color: #fff;
margin: 4px 0;
.nav-links {
display: none;
flex-direction: column;
width: 100%;
position: absolute;
top: 60px;
left: 0;
background-color: #333;
.nav-links li {
text-align: center;
margin: 10px 0;
.menu-toggle {
display: flex;
}
.nav-links.active {
display: flex;
}
2D TRANSFORMATION IN CSS:
1. translate()
The translate() function moves an element from its current position. It can accept
two parameters: translateX and translateY.
Syntax:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 100px;
height: 100px;
background-color: red;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
2. rotate()
The rotate() function rotates an element around a fixed point (by default, the
center of the element).
Syntax:
transform: rotate(angle);
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
transform: rotate(45deg);
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
3. scale()
The scale() function changes the size of an element. It can accept two parameters:
scaleX and scaleY.
Syntax:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
.box {
width: 100px;
height: 100px;
background-color: green;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
4. skew()
Syntax:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
width: 100px;
height: 100px;
background-color: yellow;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
5. matrix()
The matrix() function combines all 2D transform functions into one. It is defined
by six values: matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(),
translateY()).
Syntax:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 100px;
height: 100px;
background-color: purple;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
3D TRANSFORMATION:
1. Perspective
The perspective property defines how far the object is placed from the view. It
affects the depth of the 3D transformation.
Syntax:
perspective: value;
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.container {
perspective: 1000px;
.box {
width: 100px;
height: 100px;
background-color: blue;
transform: rotateY(45deg);
transform-origin: center;
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>
2. transform-style
Syntax:
transform-style: preserve-3d;
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.parent {
perspective: 1000px;
.child {
width: 100px;
height: 100px;
background-color: green;
transform: rotateY(45deg);
transform-origin: center;
</style>
</head>
<body>
<div class="child"></div>
</div>
</body>
</html>
3. rotateX(), rotateY(), rotateZ()
Syntax:
transform: rotateX(angle);
transform: rotateY(angle);
transform: rotateZ(angle);
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 100px;
height: 100px;
background-color: red;
transform-origin: center;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
Syntax:
transform: scaleX(value);
transform: scaleY(value);
transform: scaleZ(value);
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 100px;
height: 100px;
background-color: yellow;
transform-origin: center;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
5. translate3d()
Syntax:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
.box {
width: 100px;
height: 100px;
background-color: purple;
transform-origin: center;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
6. matrix3d()
Syntax:
transform: matrix3d(a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4);
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
transform-origin: center;
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>