Complete Web Development
Complete Web Development
Web
Development
- SATISH KONDURU
• What is HTML?
• HTML stands for Hyper Text Markup
Language.
• It is the standard language for
creating webpages.
Introduction • Describes the structure of a
to HTML webpage using elements and
tags.
• History
• Invented by Tim Berners-Lee in
1991.
• Current version: HTML5.
Why Learn HTML?
<body>
</html>
HTML Lists
<em> (Emphasis)
Purpose:
• Adds emphasis to text, typically displayed in italic.
Semantic Meaning:
• The content is important and should be emphasized when read.
<p>You should <em>always</em> back up your files.</p>
Elements
<b> (Bold)
• Purpose: Makes text bold purely for stylistic reasons, without semantic emphasis.
• Semantic Meaning: None (used for stylistic purposes only).
<i> (Italic)
• Purpose: Makes text italic purely for stylistic reasons, without semantic emphasis.
• Semantic Meaning: None (used for stylistic purposes only).
and
Subscript Tag: <sub>
•Lowers text below baseline
Subscript •Common uses:
•Chemical formulas (H₂O)
Ex:
<p>E = mc<sup>2</sup></p>
<p>Water formula: H<sub>2</sub>O</p>
Inline vs. Block Elements in HTML
href(Hypertext Reference):
Specifies the destination URL
1. Basic Syntax of the link.
Link Text:
The clickable text displayed
to users.
a) External Links
Links to an external website.
<a href="[Link]
Google</a>
b) Internal Links
Links to another page on the same website.
<a href="/[Link]">About Us</a>
c) Anchor Links (Same Page)
<input type="text"
name="email"
placeholder="Enter your
email">
• You can make a text input field
mandatory by adding the required
attribute. This ensures that the field
must be filled out before the form can
be submitted.
• <input type="text" name="email"
id="email" required placeholder="Enter
Required your email">
Field • Validation:
• When the form is submitted, browsers
automatically check that the required
field is not empty.
• The user will be prompted with a
validation message if the field is left
empty.
Try this
<form action="">
<label for=“username">Username:</label>
<input
type="text"
name=“username"
id=“username"
required
placeholder="Enter your user
name"
/>
<button type="submit">Submit</button>
</form>
Input with Pattern
Matching
• The pattern attribute allows you to specify a
regular expression to match the input value.
This is commonly used for fields like phone
numbers or zip codes.
• <input type="text"
name="zipcode"
pattern="\d{5}"
title="Enter a 5-digit zip code">
• You can limit the number of characters
a user can enter into a text input by
Maximum using the maxlength and minlength
attributes.
and
Minimum <input type="text" name="username"
maxlength="20" minlength="5"
Length placeholder="Username (5-20
characters)">
• The autofocus attribute automatically
focuses the text input field when the
page loads. This can be helpful in forms
Text Input where you want the user to start typing
right away.
with
Autofocus <input type="text" name="username"
id="username" autofocus
placeholder="Enter your username">
• You can disable or make an input field
read-only.
Disabled • Disabled Input: The input is uneditable
and does not get submitted with the
and Read- form.
<form>
<label>
1. Checkboxes <input type="checkbox"
(<input type="checkbox">) name="subscribe" value="newsletter">
Subscribe to newsletter
</label>
</form>
<form>
<label>
<p>HTML is a <mark>markup</mark>
language used to structure content
on the web.</p>
Summary
<header>: Contains the website's heading and navigation.
<main>: Contains the main content of the page, including articles and sections.
Internal styles are typically used when you want to apply CSS
rules to a single document but don't want to affect other
pages.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document with Internal Styles</title>
<style>
/* CSS styles go here */
body {
background-color: #f4f4f4;
font-family: Arial, sans-serif;
}
h1 {
color: blue;
text-align: center;
}
p{
color: gray;
}
</style>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is a paragraph with internal styles.</p>
</body>
</html>
External Styles: The Best
Way to Write Styles
• External styles involve writing CSS in a
separate file and then linking it to an
HTML document using the <link> tag.
• This approach is widely considered the
best practice for web development,
especially for larger websites or web
applications.
• External styles allow for better
separation of concerns, reusability,
maintainability, and improved
performance.
How to Link <head>
<link rel="stylesheet" href="[Link]">
an External </head>
CSS File
CSS (Cascading Style Sheets) is used to
describe the presentation (layout, colors,
fonts, spacing, etc.) of a web document
written in HTML.
The "anatomy" of CSS refers to the
components that make up a CSS rule
and how they work together to apply
Anatomy of styles to HTML elements.
Syntax:[attribute] { styles }
Example:
<input type="text">
<input type="password">
<input placeholder="Enter text here">
input {
border: 5px solid
blue;
}
2. Attribute Value Selector
Description: Selects elements where the specified attribute
has an exact value.
Syntax: [attribute="value"] { styles }
Ex:
<input type="text">
<input type="password">
<input type="submit">
[href$=".pdf"] {
color: red;
}
5. Attribute Value Contains Selector (*=)
Description: Selects elements where the attribute value
contains a specific substring.
Syntax: [attribute*="value"] { styles }
<a href="[Link] Blog</a>
<a href="[Link] Blog</a>
[href *= "blog"] {
font-weight: bold;
}
6. Attribute Value (Whitespace-Separated)
Selector (~=)
Description: Selects elements where the attribute value
contains a specific word (space-separated).
Syntax: [attribute~="value"] { styles }
<div class="box large red"></div>
<div class="box small green"></div>
[class ~= "large"] {
border: 3px solid black;
}
7. Attribute Value (Dash-Separated) Selector (|=)
Description: Selects elements where the attribute value is an exact
match or starts with a specific value followed by a dash (-).
Syntax: [attribute|="value"] { styles }
<div lang="en"></div>
<div lang="en-us"></div>
<div lang="fr"></div>
[lang|="en"] {
font-family: Arial, sans-serif;
}
Result: Both <div lang="en"> and <div lang="en-us"> are styled because
their lang attribute is either an exact match en or starts with "en-"
Working with
background-color
• CSS provides the background-color
property to set the background
color of an element. It is a
fundamental styling property widely
used to enhance the appearance
and readability of a webpage.
Color Values
Named Colors: HSL Colors:
hue, saturation, lightness
div { background-color: red; } div { background-color: hsl(16, 100%, 60%); /* Same as
#ff5733 */; }
HEX Colors:
HSLA Colors:
div { background-color: #ff5733; } hue, saturation, lightness
div { background-color: hsla(16, 100%, 60%, 0.5)/* Same as
RGB Colors: #ff5733 */; }
RGBA Colors:
h1 {
font-family: Arial, Helvetica,
sans-serif;
}
Specifying
Fonts
p{
font-family: "Times New Roman", Georgia, serif;
}
Web Fonts
<link
href="[Link]
o:wght@400;700&display=swap" rel="stylesheet">
Fallback Fonts
body {
font-family: 'Open Sans', 'Helvetica Neue', sans-serif;
}
Setting Default Font
body {
font-family: 'Arial', sans-serif;
}
Best Practices
selector {
font-size: value;
}
Values
Absolute Units:
Fixed sizes that do not scale with the
viewport or parent element.
Common absolute units:
px, pt, cm, mm, in.
p{
font-size: 16px; /* 16 pixels */
}
Values
Relative Units:
Scale relative to the parent element or root font size.
Common relative units:
em, rem, %, vw, vh
h1 {
font-size: 2em; /* 2 times the parent font size */
}
p{
font-size: 100%; /* Same as parent element font size
*/
}
div {
font-size: 10vw; /* 10% of the viewport width */
}
selector {
font-weight: value;
}
Values
Using Numeric Values
Ranges from 100 (thin) to 900 (extra-
bold).
400 is normal weight, and 700 is bold.
h1 {
font-weight: 700; /* Bold text */
}
Values
Using Keywords
normal : Equivalent to 400 (default
text weight).
bold: Equivalent to 700 (bold text).
Bolder: Bolder than the parent
element’s font weight.
Lighter: Lighter than the parent
element’s font weight.
3. Font-Style
• The font-style property defines
whether the text is normal, italicized,
or oblique.
selector {
font-style: value;
}
3. Font-Style
• The font-style property defines
whether the text is normal, italicized,
or oblique.
selector {
font-style: value;
}
Values
a. Normal
Default text style.
p{
font-style: normal;
}
Values
b. Italic:
Renders text in an italic style, if the font
supports it.
p{
font-style: italic;
}
Values
c. Oblique
Slants the text to the right, even if the font doesn’t
have an italic version.
p{
font-style: oblique;
}
Text Alignment in CSS
• Text alignment determines the
horizontal positioning of text content
within its container.
• The text-align property in CSS is used
to control how text is aligned
relative to its container or parent
element.
The text-align Property
• The text-align property aligns text
horizontally within an element.
selector {
text-align: value;
}
Values of text-align
Property
• Left/Start:
Aligns text to the left of the container.
Default alignment for languages
written left-to-right (e.g., English,
Spanish).
p{
text-align: left/start;
}
Values of text-align
Property
• Right/End:
Aligns text to the right of the container.
Commonly used for alignment in right-to-
left languages (e.g., Arabic, Hebrew).
p{
text-align: right/end;
}
Values of text-align
Property
• Center:
Centers the text horizontally within
the container.
p{
text-align: center;
}
Values of text-align
Property
• Justify:
Aligns text so that both the left and
right edges are flush with the
container (like in newspapers or
books).
Adds spacing between words to
achieve this effect.
p{
text-align: justify;
}
Values of text-align
Property
div {
text-align: center;
}
p{
text-align: inherit; /* Will align center,
like the parent div */
}
Line-Height in CSS
• The line-height property in CSS controls
the vertical spacing between lines of
text.
• It is a key factor in improving the
readability and aesthetics of text by
adjusting how densely or loosely lines
are spaced within a block of text.
Values of Line-Height
in CSS
• Normal:
Default value.
The browser calculates the line height
based on the font size (usually 1.2 times
the font size).
p{
line-height: normal; /* Default spacing */
}
Values of Line-Height
in CSS
• Number (Unitless)
A multiplier of the element’s font size.
Example: A line-height of 1.5 means
the line height is 1.5 times the font size.
p{
font-size: 16px;
line-height: 1.5; /* 1.5 x 16px = 24px */
}
Values of Line-Height
in CSS
• Length (With Units)
Specifies an exact value using units
like px, em, rem
The spacing between lines is fixed,
regardless of the font size.
p{
line-height: 24px;
}
Values of Line-Height
in CSS
• Percentage:
p{
line-height: 150%;
}
Letter-Spacing in
CSS
• The letter-spacing property in CSS
controls the horizontal spacing
between characters in text, also
known as tracking.
• Adjusting letter spacing can enhance
readability, create stylistic effects, or
ensure better alignment in your
design.
p{
letter-spacing: 2px; /* Adds 2 pixels of
space between letters */
}
Word-Spacing in
CSS
• The word-spacing property in CSS
controls the horizontal spacing
between words in text.
• Adjusting word spacing can enhance
readability, create design effects, or
accommodate specific layout
requirements.
p{
word-spacing: 5px; /* Adds 5 pixels of
space between words */
}
Text Shadows
in CSS
• Text shadows in CSS allow you to add shadow
effects to text, giving it depth, dimension, or
stylistic flair.
Syntax:
text-shadow: offset-x offset-y blur-radius color;
•offset-x: Horizontal shadow position.
Positive values move the shadow to the
right.
Negative values move the shadow to the left.
•offset-y: Vertical shadow position.
text-shadow: Positive values move the shadow downward.
h2 {
text-shadow:
2px 2px 4px gray,
-2px -2px 3px lightblue;
}
text-transform
The text-transform property controls
the capitalization or transformation of
text.
Values:
• None (default)
• Uppercase
• Lowercase
• Capitalize
• inherit
text-decoration
• The text-decoration property
specifies the decoration added to
text, such as underlining, overlining,
strikethrough, or a combination of
these.
• It is the combination of 3 properties
• text-decoration-line
• text-decoration-color
• text-decoration-style
text-decoration-line
• Specifies the type of decoration.
• Values:
• None(No Decoration)
• Underline
• Overline
• Line-through
• blink
text-decoration-color
• Specifies the color of the text
decoration.
• Can use any CSS color value (ex:
red, #f00, rgb(255,0,0)
text-decoration-style
• Defines the style of the text
decoration.
• Values:
• Solid (default)
• Double
• Dotted
• Dashed
• wavy
p{
text-decoration: underline red solid;
}
Shorthand p{
Examples text-decoration: underline overline
dashed blue;
}
Grouping Selectors
• Grouping selectors in CSS allows you
to apply the same styles to multiple
elements by combining their
selectors. This reduces redundancy
and makes your code more efficient
and easier to maintain.
AB{ B is the
Here, A is the
descendant
/* styles */ ancestor
element (direct
} element.
or indirect).
Key Points:
• Matches all descendants (at any
depth) of A that match the selector
B
• This is not limited to immediate
children but applies to all nested
levels.
Example
<div class="container">
<div class="child">
<p class="descendant">This is a paragraph.</p>
</div>
<span class="descendant">This is a span.</span>
</div>
.container .descendant {
color: blue;
}
• The child combinator
selects elements that are
2. Child direct children of a
specified element. It does
Combinator ( > ) not select deeper
descendants, only the
immediate child elements.
A>B{
/* styles */
}
Syntax: A is the parent element.
B is the direct child of A
• Matches only the immediate children of A
that match B.
Key Points: • Does not apply to deeper nested
descendants.
Example
<div class="container">
<div class="child">
.container > .child {
<p>This is a paragraph.</p> color: green;
</div>
<h1> }
This is a Heading.
<div class="child">
<p>This is a paragraph.</p> Difference from Descendant:
</div>
If we use .container .child , it
</h1>
</div>
would also apply to .child
</div> elements nested at any level
inside .container
Adjacent Sibling
Combinator ( + )
The adjacent sibling combinator
selects an element that is
immediately preceded by another
specified element (they share the
same parent).
Syntax
A+B
{
/* styles */
}
A~B{
/* styles */
}
A is the reference sibling.
B is any sibling of A that appears after it.
Example
<ul> li:first-child {
<li>Item 1</li> font-weight: bold;
<li>Item 2</li> }
<li>Item 3</li>
</ul>
Last Child Selector
(:last-child)
The :last-child pseudo-class targets
the last child of its parent.
A:last-child
{
/* styles */
}
Example
<ul> li:last-child {
<li>Item 1</li> font-weight: bold;
<li>Item 2</li> }
<li>Item 3</li>
</ul>
nth-Child Selector
(:nth-child())
A:nth-child(n) {
/* styles */
}
N can be
<ul> li:nth-child(odd) {
<li>Item 1</li> background-color:
lightgray;
<li>Item 2</li>
}
<li>Item 3</li>
<li>Item 4</li> li:nth-child(2) {
</ul> color: red;
}
• The :nth-last-child() pseudo-class is
nth-Last- similar to :nth-child() but counts
elements from the end.
Child
Selector - A:nth-last-child(n) {
:nth-last- /* styles */
child() }
Example
<ul> li:nth-last-child(1) {
<li>Item 1</li> color: purple;
<li>Item 2</li> Font-weight: bold;
<li>Item 3</li> }
</ul>
This rule applies to the last <li> element Item 3 because it is the 1st child from the end.
nth-of-Type Selector(:nth-of-type())
<div> p:nth-of-type(2) {
<p>Paragraph 1</p> color: blue;
<span>Span 1</span> }
<p>Paragraph 2</p>
</div>
This rule applies to the last < p > element Paragraph 2 because it is the 2nd Para of type
Compound Selectors
in CSS
• Compound selectors are
combinations of simple selectors
(such as element type, class, ID, or
pseudo-classes) that are used to
target specific elements in CSS.
• These selectors allow you to apply
styles to elements based on multiple
conditions, offering greater
specificity and flexibility.
Types of
Compound
Selectors
• Targets an element based on its type
and class.
Combining
/* Selects elements with both "card" and
Multiple "featured" classes */
Classes .[Link] {
border: 2px solid gold;
}
The Box Model
CSS Box Model
• The CSS Box Model is a fundamental concept in
web design and layout. It describes how
elements on a web page are represented as
rectangular boxes, consisting of the following
components:
1. Content
The actual content of the box, such as text,
images, or other elements.
2. Padding
The space between the content and the box's
border. Padding is transparent and can expand
the size of the box.
3. Border
The edge surrounding the padding. Borders can
have different styles, colors, and widths.
4. Margin
The outermost layer of the box, creating space
between the box and adjacent elements.
Components
of the Box • Content Area
Contains the content inside the box.
Model
Components of
the Box Model
Its size is determined by the width and height properties.
• Padding Area
Adds space between the content and the border.
Set using the padding property (e.g., padding: 10px;).
• Border Area
Encases the padding and content.
Set using the border property (e.g., border: 1px solid
black;).
• Margin Area
Creates space between the element and neighboring
elements.
Set using the margin property (e.g., margin: 20px;).
Example
.box {
<div class="box"> width: 200px; /* Content width */
This is a box. height: 100px; /* Content height */
</div> padding: 20px; /* Space inside the border */
border: 5px solid black; /* Border thickness */
margin: 30px; /* Space outside the border */
background-color: #789;
}
Explanation
• Width & Height: Content
width is 200px, and height is
100px.
• Padding: Adds 20px of space
around the content,
increasing the box's total size.
• Border: Adds 5px thickness
around the padding.
• Margin: Adds 30px space
between the box and
neighboring elements.
Width:
200px (content) + 20px (padding-left) + 20px (padding-right) + 5px
(border-left) + 5px (border-right) = 250px
Height:
100px (content) + 20px (padding-top) + 20px (padding-bottom) + 5px
(border-top) + 5px (border-bottom) = 150px
Box-Sizing Property
.content-box { .border-box {
width: 200px; width: 200px;
height: 100px; height: 100px;
padding: 20px; padding: 20px;
border: 5px solid black; border: 5px solid black;
background-color: lightcoral;
background-color: lightgreen;
box-sizing: border-box;
box-sizing: content-box;
display: inline-block;
display: inline-block;
}
}
Working with
Borders in the CSS
Box Model
• Borders in the CSS Box Model are
the layer between the content (plus
padding) and the margin. Borders
can significantly affect the
appearance and size of an
element. By customizing borders,
you can enhance your webpage
design.
Key Border Properties in CSS
border:
A shorthand property to define the width, style, and color of
a border.
border: 2px solid blue;
Key Border Properties in CSS
border-width:
Sets the thickness of the border. Can use values in px, em,
or keywords like thin, medium, thick
CSS ridge
inset
outset
none (default)
border-style: dashed;
border-color:
Key Border Specifies the color of the border using
color names, hex codes, RGB, or HSL
Properties in values.
Individual Borders:
border-radius: 10px; /*
Rounded corners */
border-radius: 50%; /* Circle
for square elements */
The total size of an element is calculated
as…
Content-Box:
Total Width = width + padding-left + padding-right + border-left + border-right
Total Height = height + padding-top + padding-bottom + border-top + border-bottom
Border-Box:
Total Width = width (includes border and padding)
Total Height = height (includes border and padding)
Individual Border Sides
Example <html lang="en">
<head>
Basic Border:
<style>
<html lang="en">
.individual-border {
<head>
width: 200px;
<style>
.basic-border { height: 100px;
width: 150px; border-top: 5px solid red;
height: 100px; border-right: 3px dashed blue;
border: 3px solid black; border-bottom: 8px dotted green;
padding: 10px; border-left: 4px double black;
margin: 20px; padding: 10px;
background-color: lightblue; margin: 15px;
}
background-color: lightyellow;
</style>
}
</head>
</style>
<body>
</head>
<div class="basic-border">Basic Border</div>
<body>
</body>
</html> <div class="individual-border">Different Borders</div>
</body>
</html>
Example Circle Using Border-Radius
<html lang="en">
Border-Radius for Rounded <head>
Corners: <style>
<html lang="en">
<head>
.circle-border {
<style> width: 100px;
.rounded-border { height: 100px;
width: 150px;
border: 4px solid orange;
height: 150px;
border: 4px solid purple; border-radius: 50%; /* Circle */
border-radius: 15px; /* Rounded corners */ background-color: lightgreen;
padding: 20px;
}
background-color: pink;
}
</style>
</style> </head>
</head> <body>
<body>
<div class="circle-border"></div>
<div class="rounded-border">Rounded
Corners</div> </body>
</body>
</html>
</html>
Adding Padding to Elements in
CSS
• Padding is the space between
the content of an element and
its border. It helps create space
inside an element, enhancing
readability and layout design.
• Padding does not affect the
background color or border but
can influence the total size of
the element, depending on the
box-sizing property
Padding (Shorthand):
Sets padding for all four sides of an element in one
declaration.
padding: 10px;
applies 10px padding to the top, right, bottom, and
Key left.
Properties for
Padding Individual Padding Properties:
padding-top: Specifies padding at the top.
padding-right: Specifies padding on the right.
padding-bottom: Specifies padding at the bottom.
padding-left: Specifies padding on the left.
Two Values
padding: 10px 20px;
/* 10px (top/bottom), 20px (left/right) */
Four Values:
padding: 10px 20px 30px 40px;
/* 10px (top), 20px (right), 30px (bottom), 40px (left) */
Working with
Margins in CSS
• Margins are the outer space
around an element that
separates it from other elements.
• Unlike padding, which is inside the
element's border, margins define
the space outside the element.
• Margins can collapse (under
certain conditions) and allow you
to control spacing between
elements effectively.
margin (Shorthand)
Sets the margin for all four sides (top,
right, bottom, left) in one declaration.
Example:
margin: 10px;
applies 10px margin to all sides.
margin: 0 auto;
/* Center the element horizontally */
What is the display
Property?
• The display property specifies how an
element is displayed in the document
(i.e., its layout behavior).
• It determines whether an element is
• block-level,
• inline-level, element {
• flex,
display: value;
• grid, or
• none, }
• among others.
Display Property
Values
• Block:
• The element takes up the entire
width of its parent container.
• Starts on a new line.
• Examples: <div>, <p>, <h1> by
default.
Display Property
Values
• Inline:
The element only takes as much width
as its content.
Stays on the same line as other inline
elements.
Examples: <span>, <a>, <strong> by
default.
Display Property
Values
• inline-block:
Combines characteristics of inline
and block:
It stays in the same line.
You can set its width, height,
padding, and margin.
Display Property
Values
• none:
The element is completely removed
from the document (no space is
taken).
Margin
Collapsing?
• Margin collapsing is a
behavior in CSS where the
vertical margins of
adjacent or nested
elements combine into a
single margin instead of
adding up.
• This only applies to vertical
margins (top and bottom),
not horizontal margins (left
and right).
When Does
Margin Collapsing
Occur?
• When two block-level elements are adjacent (placed one
after the other), their vertical margins collapse into the larger
of the two.
1. Adjacent • <div style="margin-bottom: 20px; background: lightblue;">Div 1</div>
Siblings • <div style="margin-top: 30px; background: lightgreen;">Div 2</div>
Resulting Margin: 30px (the larger of 20px and 30px).
• When a child element's top margin meets its parent's
top margin, or a child element's bottom margin meets
its parent's bottom margin, the margins collapse into
2. Parent and one.
<div style="margin-top: 20px; background: lightcoral;">
Child Elements <p style="margin-top: 30px;">Child Paragraph</p>
</div>
Resulting Margin: 30px (the larger of 20px and 30px).
• If a block-level element has no content,
padding, or border, its top and bottom
margins collapse into one.
<div style="margin: 20px 0; background:
3. Empty Block lightyellow;"></div>
Elements
Resulting Margin: 20px (instead of 20px + 20px).
How to
Prevent
Margin
Collapsing?
1. Add Padding or Border
• max-width:
Ensures the element is not larger than the specified width.
If the content or container expands, it will stop expanding
once it reaches the max-width.
Example
<div style="min-width: 200px; background:
lightblue;">
This box will never be smaller than 200px.
</div>
Pseudo- p:first-child {
Classes font-weight: bold;
}
:last-child
Targets the last child of its parent.
Structural
Pseudo- p:last-child {
Classes color: green;
}
:nth-child(n)
Pseudo-
Classes li:nth-child(2) {
background-color: yellow;
}
:nth-of-type(n)
Pseudo-
Classes p:nth-of-type(odd) {
color: blue;
}
UI Element States
USED FOR STYLING USER
INTERFACE ELEMENTS.
:checked
UI Element
States button:enabled {
cursor: pointer;
}
Pseudo-Classes
for Links
a:link {
color: blue;
}
Pseudo-Classes for
Links
:visited
a:visited {
color: purple;
}
Negation Pseudo-
Class
Negation
Pseudo-Class
:not(selector)
div:not(.highlight) {
background-color:
lightgray;
}
Pseudo-
Elements in
CSS
• CSS pseudo-elements are used to style
specific parts of an element.
Pseudo- • Unlike pseudo-classes, pseudo-
elements let you create and style
Elements elements that don’t exist in the DOM,
such as adding decorative content or
targeting the first line of text.
What is a Pseudo-
Element?
• A pseudo-element is a keyword that
allows you to style a specific part of
an element.
• It is used to add or style additional
elements without modifying the
HTML structure.
• Pseudo-elements start with a double
colon :: (modern syntax) or a single
colon : (for backward compatibility).
::before
Pseudo-
Elements h1::after {
content: " ";
color: gold;
}
::first-line
Pseudo-
Elements p::first-letter {
font-size: 3em;
font-weight: bold;
color: crimson;
}
Specificity in CSS
Specificity in CSS
Specificity Hierarchy:
1. Inline Styles
2. IDs
3. Classes, Attributes, and Pseudo-Classes
4. Type Selectors and Pseudo-Elements
5. Universal Selector
Examples of Specificity
Selector Specificity Value (a, b, c, d) Explanation
style="color: red;" (1, 0, 0, 0) Inline styles have the highest specificity.
#header (0, 1, 0, 0) One ID selector.
.button (0, 0, 1, 0) One class selector.
One type selector (input) + one attribute
input[type="text"] (0, 0, 2, 0) selector ([type="text"]).
div p (0, 0, 0, 2) Two type selectors (div, p).
div + p (0, 0, 0, 2) Two type selectors (div, p).
One type selector (h1) + one pseudo-
h1::before (0, 0, 0, 2) element (::before).
One ID selector (#header) + one class
#header .button:hover (0, 1, 2, 0) selector (.button) + one pseudo-class
(:hover).
One ID selector (#header) + two type
#header p strong (0, 1, 0, 2) selectors (p, strong).
Avoiding
Specificity Issues
1. Use Consistent Naming
Avoid overly specific selectors that are hard to
override.
2. Use Class Selectors
Classes are reusable and have moderate
specificity.
3. Avoid Inline Styles
Inline styles make maintenance harder.
4. Minimize !important
Only use !important as a last resort.
5. Reset or Normalize CSS
Start with a reset to avoid unexpected
inherited styles.
Specificity Cheatsheet
Common button {
Use case font-size: 1rem; /* 16px */
padding: 1em; /* Relative to button
font size (16px) */
}
vw and vh are relative
length units in CSS based on
the viewport size
Full-Screen Elements:
Perfect for creating full-page sections or components that span the entire
screen width or height.
Viewport-Dependent Layouts:
Ideal for designing layouts that need to adapt to different screen
dimensions, especially in modern web and mobile designs.
When to Use:
2. Device-Independent Design:
Avoid cm, mm, in, pt, and pc as
they depend on the physical screen
resolution and aren't practical for web
design.
Relative Units
Examples: div {
%, em, rem, vw, vh, vmin, vmax.
width: 80%;
/* 80% of the parent
% (Percent) container */
Relative to the size of the parent }
element for widths, heights, margins, or
paddings.
Use for fluid layouts that adjust based
on the parent container.
em:
Relative to the font size of the parent element.
Use for component-specific scaling, such as padding,
margins, or typography adjustments that depend on the
component’s font size.
button {
font-size: 1em; /* Equal to parent font size */
padding: 0.5em 1em; /* Scales with font size */
}
rem:
Relative to the root element’s font size.
Use for global consistency, especially for font sizes,
spacing, and layout dimensions.
html {
font-size: 10px; /* 1rem = 10px */
}
h1 {
font-size: 2rem; /* 20px */
}
Working with Floats in
CSS
The float property is an older CSS
technique primarily used for
positioning elements within a
container.
Floats are still relevant for certain use
cases, but they have largely been
replaced by Flexbox and CSS Grid for
modern layouts.
What is the float
Property?
The float property is used to position an
element to the left or right within its
containing block, allowing inline elements
(like text) to wrap around it.
Values of float:
left: The element floats to the left of its
container.
right: The element floats to the right of its
container.
none (default): The element does not float.
inherit: Inherits the float property from its
parent.
Common Use Cases
for Floats
• Text Wrapping Around Images:
Floats are commonly used to wrap
text around an image.
<div>
<img src="[Link]" alt="Example"
style="float: left; margin-right: 10px;">
<p>This is some text that wraps
around the floated image.</p>
</div>
Two-Column Layouts
(Legacy Method):
Floats were often used to create side-
by-side layouts before Flexbox and
Grid became standard.
<div>
<div style="float: left; width:
50%;">Left Column</div>
<div style="float: right; width:
50%;">Right Column</div>
</div>
• When an element is floated, it is
The Problem removed from the normal document
flow, which can cause the parent
with Floats: container to collapse if it contains only
floated elements.
Working With Background Images in CSS
• To add a background image to an
element, you use the background-image
property in CSS
selector {
background-image: url('image-path');
Basic Syntax }
for Ex:
div {
Background width: 500px;
Images height: 300px;
background-image: url("[Link]");
background-size: cover; /*or contain*/
}
Defines the starting position of the
background image. The default is 0% 0%,
which means the top-left corner of the
element.
body {
background-image: url('[Link]');
background-repeat: no-repeat/repeat;
background-position: center/left/right;
background-size: cover/contain;
background-attachment: fixed/scroll;
}
Linear Gradients in
CSS
• Linear gradients in CSS are a way to
create smooth color transitions
along a straight line.
• They are a part of the background-
image property and allow you to
blend multiple colors in various
directions.
background-image:
linear-gradient(direction,
color-stop1, color-stop2, ...);
Specifying
Directions
Using Keywords
to top: Gradient goes from bottom to top.
to bottom: Gradient goes from top to
bottom (default).
to left: Gradient goes from right to left.
to right: Gradient goes from left to right.
div {
background: linear-gradient(to right, red,
blue);
}
You can specify angles in degrees (0deg
to 360deg).
0deg: Gradient starts from the top.
90deg: Gradient starts from the left.
180deg: Gradient starts from the bottom.
Using Angles
div {
background: linear-gradient(45deg,
red, yellow, blue);
}
Using Color Stops
body{
background: linear-gradient(to right,
red 0%, yellow 50%, blue 100%);
}
• The repeating-linear-gradient function
creates patterns by repeating the
gradient.
Repeating div {
Linear background: repeating-linear-
gradient(45deg, red, yellow 20px, blue
Gradients 40px);
}
Transparent
Gradients
• You can use transparent as a color
to create gradients that fade into
transparency.
div {
background: linear-gradient(to
bottom, rgba(255, 0, 0, 1), rgba(255,
0, 0, 0));
}
Multi-Color
Gradients
• You can add more than two colors
to a gradient.
div {
background: linear-gradient(to right,
red, yellow, green, blue);
}
Gradient with Text
CSS Filter
Syntax selector {
filter: effect1(value) effect2(value);
}
• Creates a blurring effect. The
higher the value, the more blur is
applied.
blur() img {
filter: blur(10px);
}
• Adjusts the brightness of an
element.
img {
filter: brightness(0.8);
}
• Adjusts the contrast of an
element.
• 1 is the default.
• Values above 1 increase
contrast, and values below 1
contrast() decrease it.
img {
filter: contrast(1.2);
}
• Converts the element to
grayscale.
• 0 is full color (default).
• 1 is fully grayscale.
grayscale()
img {
filter: grayscale(1);
}
Other filters
invert(): opacity()
Inverts the colors of an element. Adjusts the transparency of an
0 is the default (no inversion). element..
1 is fully inverted. 1 is fully opaque
0 is fully opaque.
saturate(): sepia():
Controls the intensity of colors in
Applies a sepia (brownish) tone
the element.
1 is the default (original saturation). to the element.
Values above 1 increase 0 is the default (no effect)
saturation, and values below 1 decrease it. 1 applies a full sepia effect.
Positioning in CSS
Positioning in • CSS positioning allows you to control
the placement of elements in a web
CSS page. It is one of the core concepts in
web design and development.
CSS Position
Property
The position property specifies how
an element is positioned in the
document. Its values include:
static (default)
relative
absolute
fixed
sticky
• Static
Values of Default value for all elements.
Position Elements are positioned in the normal
div {
position: relative;
top: 20px; /* Moves the element 20px down */
left: 10px; /* Moves the element 10px to the
right */
}
Values of Position Property
• Absolute
Positions the element relative to its
nearest positioned ancestor.
If no ancestor has a position other
than static, the element is positioned
relative to the <html> element.
div {
position: absolute;
top: 50px;
left: 100px;
}
Values of Position Property
• Fixed
Positions the element relative to the
viewport.
The element does not move when
scrolling.
div {
position: fixed;
bottom: 10px;
right: 10px;
}
Values of Position Property
• Sticky
A hybrid between relative and
fixed.
The element is treated as relative
until a certain scroll position is
reached, after which it becomes
fixed.
div {
position: sticky;
top: 0; /* Sticks to the top of the
container */
}
Used with relative, absolute, fixed, and
sticky positioning:
selector {
transition: property duration timing-function delay;
}
Parameters
Parameter Description
property The CSS property to animate (e.g., width,
opacity).
• Display
• Visibility
• position
• Defines how long the transition will
take.
.box {
transition- transition-duration: 2s;
duration }
Units: s (seconds), ms (milliseconds)
Default: 0s (instant change)
Timing Function Description
Starts slow, speeds up, then
ease (default)
slows down.
linear Constant speed throughout.
transition- ease-in-out
Starts slow, speeds up, then
slows down.
timing-function cubic-bezier(n,n,n,n)
Custom speed curve using
Bezier curves.
• Delays the start of the transition.
transition- .box {
transition-delay: 1s;
delay }
• You can animate multiple properties
by separating them with commas.
Using .box {
Multiple transition: background-color 1s ease,
width 0.5s linear;
Transitions }
.box {
transition: all 0.5s ease;
}
Using all for .box:hover {
Global background-color: red;
transform in }
Ex:
CSS? .box {
transform: rotate(45deg);
}
2D Transforms
• 2D transformations operate on the X
and Y axes.
1. translate(x, y)
Moves an element without
affecting other elements.
.box {
transform: translate(50px, 100px);
/* Move right 50px, down 100px */
}
2D Transforms
• 2D transformations operate on the X
and Y axes.
1. rotate(angle)
Rotates an element clockwise
or counterclockwise.
.box {
transform: rotate(45deg); /* Rotate
45 degrees */
}
2D Transforms
• 2D transformations operate on the X
and Y axes.
1. scale(x, y)
Resizes an element along X and
Y axes.
.box {
transform: scale(1.5, 2);
/* 1.5x wider, 2x taller */
}
2D Transforms
• 2D transformations operate on the X
and Y axes.
1. skew(x, y)
Tilts an element along the X
and Y axes.
.box {
transform: skew(30deg, 10deg);
}
Combining Multiple
Transforms
• You can apply multiple transformations
together
.box {
transform: translate(50px, 50px) rotate(45deg)
scale(1.5);
}
• Defines the point around which
transformations occur.
.box {
Creating a }
Flexbox By default:
Layout The main axis is horizontal (left to right).
Items are arranged in a row.
flex-direction
.item {
flex: 1;
/* Equivalent to flex-grow: 1; flex-shrink: 1; flex-basis: 0; */
flex: 2 1 100px; /* Custom values */
}
align-self
• @media keyword
• A media type (optional)
• One or more media features (conditions)
• CSS rules inside {}
Feature Description
Applies if the aspect ratio is greater than or
min-aspect-ratio: x/y
equal to x/y.
Applies if the aspect ratio is less than or equal
max-aspect-ratio: x/y
to x/y.
Web Design }
font-size: 16px;
(RWD) }
Example
/* Mobile (Up to 600px) */
@media (max-width: 600px) {
body {
font-size: 14px;
}
}
Mobile-First vs. Desktop-First Approach
• Mobile-First (Recommended)
Start with mobile styles first and add styles for larger screens using min-width.
/* Mobile styles */
body {
font-size: 14px;
}
/* Tablet */
@media (min-width: 600px) {
body {
font-size: 16px;
}
}
/* Desktop */
@media (min-width: 1024px) {
body {
font-size: 18px;
}
}
Mobile-First vs. Desktop-First Approach
Desktop-First
• Start with desktop styles and use max-width for smaller screens.
• /* Mobile styles */
body {
font-size: 14px;
}
/* Tablet */
@media (min-width: 600px) {
body {
font-size: 16px;
}
}
/* Desktop */
@media (min-width: 1024px) {
body {
font-size: 18px;
}
}
CSS Grid
CSS Grid Layout
.container {
display: grid;
}
Defining Rows and Columns
<div class="item">3</div> }
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 100px);
gap: 10px;
}
.item1 {
background: red;
grid-column: 1 / 3;
grid-row: 1 / 2;
}
Auto Placement &
grid-auto-flow
.container {
display: grid;
grid-template-columns: repeat(3,
1fr);
grid-auto-rows: minmax(100px,
auto);
grid-auto-flow: row;
}
This means new rows are
automatically added as needed.
Instead of manually positioning items, you can use grid area names.
.container {
display: grid;
grid-template-areas:
"header header header"
"sidebar content content"
"footer footer footer";
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto;
}
Areas }
grid-area: header;
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
.footer {
grid-area: footer;
}
• CSS Grid makes it easy to create
responsive layouts.
Using minmax()
.container {