Web Tech Unit II
Web Tech Unit II
Unit-2
Web Technology-(BCS502)
CSS Introduction
• 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.
• CSS can control the layout of multiple web pages all at once.
1. CSS1 (Cascading Style Sheets Level1) - The initial version of CSS, released in December 1996. CSS1
provided basic styling capabilities for HTML documents, including properties for text, colors, backgrounds,
margins, and borders.
2. CSS2 (Cascading Style Sheets Level2) - Released in May 1998, CSS2 introduced new features such as
positioning, z-index, media types, and more advanced selectors like attribute selectors and child selectors.
3. CSS2.1 - The version 2.1, published as a W3C Recommendation in June 2011, clarified and refined CSS2,
addressing inconsistencies and ambiguities in the specification. CSS2.1 focused on improving
interoperability among web browsers. Note: Each version of CSS builds upon
the previous ones, adding new
features and refining existing
4. CSS3 (Cascading Style Sheets Level 3) - CSS3 is a collection of modules that extend the capabilities of capabilities to meet the evolving
CSS. It introduces numerous new features and enhancements, including advanced selectors, multiple needs of web developers and
designers. CSS is referred as just CSS
column layouts, animations, transformations, gradients, shadows, and more.
now, without a version number.
5. CSS4 (Cascading Style Sheets Level 4) - CSS4 is an ongoing effort to extend CSS3 with new features and
enhancements.
How it works:
4 Choose a design you like from the Design Ideas task pane.
How it works:
4 Choose a design you like from the Design Ideas task pane.
•CSS Saves Time: You can write CSS once and then reuse same sheet in multiple HTML pages.
•Pages Load Faster: If you are using CSS, you do not need to write HTML tag or attributes
every time. Just write one CSS rule of a tag and apply it to all the occurrences of that tag.
•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 get
a far better look to your HTML page.
•Multiple Device Compatibility: For the same HTML document, different versions of a website
can be presented for different screen widths
•Global Web Standards: Now most of the HTML attributes are being deprecated and it is being
recommended to use CSS.
• The selector targets the HTML element/elements that you want to style.
• The declaration block contains one or more declarations enclosed in curly braces {}.
• Each declaration consists of a property and a value separated by a colon :. Declarations are separated by
semicolons ;.
1. Inline CSS:
2. Internal CSS:
3. External CSS:
Responsive design - CSS offers features like media queries that enable developers to create
responsive layouts that adapt to different screen sizes and devices, ensuring a consistent user
experience.
Flexibility and Control - CSS provides precise control over the presentation of HTML
elements, allowing developers to customize layout, typography, colors, and other visual
properties.
Consistency and Reusability - Developers can ensure consistency across the entire website,
by defining styles in a central CSS file. Styles can be reused across multiple pages, reducing
redundancy and making updates easier.
Search Engine Optimization (SEO) - CSS can be used to structure content in a way that
improves search engine visibility.
Ease of Maintenance - Centralized CSS files make it easier to maintain and update styles
across a website. Changes can be applied globally, ensuring uniformity and reducing the risk
of inconsistencies.
Faster Page Loading - External CSS files can be cached by browsers, resulting in faster page
loading times for subsequent visits to a website. This caching mechanism reduces server load
and bandwidth consumption.
CSS works by associating rules with HTML elements. A CSS rule contains
two main parts:
a selector which specifies the HTML element(s) to style.
a declaration block which contains one or more declarations
separated by semicolons.
Each declaration includes a property name and a value, specifying the
aspect of the element's presentation to control.
<html> div {
<head> border: 5px inset gold;
<title>CSS Tutorial</title> background-color: black;
<style> width: 300px;
h1 { text-align: center;
color: #36CFFF; }
} </style>
</head>
p{ <body>
font-size: 1.5em; <div>
color: white; <h1>Hello World!</h1>
} <p>This is a sample CSS code.</p>
</div>
</body>
</html>
CSS works by selecting HTML elements and applying properties to style them. Properties
define what aspect of the element you want to change.
1. Background Styling
body {
background-color: #f0f0f0; /* light gray background */
background-image: url('[Link]'); /* set background image */
background-repeat: no-repeat; /* place the background image once */
background-size: cover; /* image covers entire area */
}
2. Text Formatting
h1 {
color: #333; /* text color */
text-align: center; /* center text */
text-transform: uppercase; /* make uppercase */
letter-spacing: 2px; /* space between letters */
line-height: 1.5; /* space between lines */
}
3. Controlling Fonts
p{
font-family: 'Arial', sans-serif; /* font family */
font-size: 16px; /* font size */
font-weight: bold; /* bold text */
font-style: italic; /* italic text */
font-variant: small-caps; /* small caps */
}
Creating Style Sheet Inline, Internal, External CSS <link rel="stylesheet" href="[Link]">
CSS Properties Attributes like color, margin, padding color: red; margin: 10px;
Text Formatting Color, alignment, spacing, case text-align: center; letter-spacing: 1px;
Controlling Fonts Font type, size, weight, style font-family: Arial; font-weight: bold;
The element or elements that are selected by the selector are referred to as subject of the selector.
Syntax
Pseudo-classes are always denoted by a single colon (:) followed by the pseudo-class name:
selector:pseudo-class-name {
CSS properties
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 23
CSS Pseudo-classes
<h2>Using :focus</h2>
</body>
</html>
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 29
Working with Lists and Tables
CSS provides various properties to style HTML lists (<ul>, <ol>, <dl>).
list-style-type: Controls the appearance of the list item marker (e.g., disc, circle, square, decimal,
list-style-position: Determines whether the marker is placed inside or outside the list item
content box.
single declaration.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 30
Working with Lists and Tables
Example:
ul {
list-style-type: square;
background-color: #f0f0f0;
padding: 15px;
}
li {
margin-bottom: 5px;
color: #333;
}
<style>
table { <table>
width: 100%; /* Full width */ <thead>
border-collapse: collapse; /* Removes double borders */
}
<tr><th>Header 1</th><th>Header 2</th></tr>
</thead>
th, td { <tbody>
border: 1px solid #ddd;
padding: 8px;
<tr><td>Row 1 Cell 1</td><td>Row 1 Cell 2</td></tr>
text-align: left; <tr><td>Row 2 Cell 1</td><td>Row 2 Cell 2</td></tr>
} </tbody>
th {
</table>
background-color: #f4f4f4;
font-weight: bold;
}
tr:nth-child(even) { Output
background-color: #fafafa; /* Zebra striping */ Screen >>
}
tr:hover {
background-color: #f1f1f1; /* Highlight row on hover */
}
</style>
<style>
table { <table>
width: 100%; /* Full width */ <thead>
border-collapse: collapse; /* Removes double borders */
}
<tr><th>Header 1</th><th>Header 2</th></tr>
</thead>
th, td { <tbody>
border: 1px solid #ddd;
padding: 8px;
<tr><td>Row 1 Cell 1</td><td>Row 1 Cell 2</td></tr>
text-align: left; <tr><td>Row 2 Cell 1</td><td>Row 2 Cell 2</td></tr>
} </tbody>
th {
</table>
background-color: #f4f4f4;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #fafafa; /* Zebra striping */
}
tr:hover {
background-color: #f1f1f1; /* Highlight row on hover */
}
</style>
• border-collapse: Controls whether table cell borders are rendered separately (separate)
or collapsed into a single border (collapse).
• border-spacing: Sets the distance between adjacent cell borders when border-collapse
is separate.
• width / height: Defines the dimensions of the table and its cells.
• text-align / vertical-align: Controls the alignment of content within table cells.
• padding: Adds space between the cell content and its border.
• background-color: Sets the background color for table elements (table, rows, cells).
• caption-side: Positions the table <caption> element (top or bottom).
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 34
Working with Lists and Tables
table {
Table Styling Properties: Example width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
th {
background-color: #eee;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #f9f9f9;
Faculty Name: Ms. Ramandeep Kaur} | College: GCET | Department: CSE 35
Working with Lists and Tables
List Item (li) margin, font-size, color, ::before for custom bullets
To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
<body> <style>
#para1 {
<p id="para1">Hello World!</p> text-align: center;
<p>This paragraph is not affected by the style.</p> color: red;
}
</body> </style>
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 40
The CSS id Selector Example:
<body> .center {
text-align: center;
<h1 class="center">Red and center-aligned heading</h1> color: red;
<p class="center">Red and center-aligned }
paragraph.</p>
</body> Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 42
The CSS id Selector Example:
Responsive Design:
Knowing how the box model works helps make elements adjust properly when screen sizes
change (like on mobile devices).
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 46
The CSS Box Model : Why is the Box Model Needed?
Browser Rendering:
Browsers use the box model to render elements. Without it, the size and spacing would be
unpredictable.
**The box model is needed to define, calculate, and control the size and spacing
of elements on a web page for a clean, predictable layout.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 47
The CSS Box Model : Types
content-box (default):
The width and height CSS properties apply only to the content area. Padding and border
add extra size outside the content.
So, total size = content + padding + border + margin.
box-sizing: content-box;
• The width and height apply only to the content.
• Padding and border add to the total size.
Example:
border-box:
The width and height include content, padding, and border, making it easier to size
elements exactly. Margin is still added outside. This model is often preferred for layout
control because it simplifies calculations.
box-sizing: border-box;
• The width and height include content + padding + border.
• Easier to control element size because total width remains as defined.
Example:
1. Content Area
• The content area is the central part of the CSS box
model, containing the main content (e.g., text,
images, videos, or elements like <p> or <span>).
• It can be styled with CSS properties like height and
width.
• The content edge refers to the four edges of the
content area
Left content edge
Right content edge
Top content edge
Bottom contentFaculty
edgeName: Ms. Ramandeep Kaur | College: GCET | Department: CSE 51
The CSS Box Model : Key Components
2. Padding Area
• The padding area is the space between the content and the
border of an element.
• It includes the areas highlighted in light green and skin color in
the example.
• The distance between the content edge and the border is the
padding.
• The border marks the end of the padding area.
• The padding area contributes to the element's total dimensions.
• Padding can be adjusted using CSS properties.
• It works similarly with box-sizing: content-box and box-sizing:
border-box, but with slight calculation differences.
3. Border Area
• The area that marks the end of an element is called as
the border it is the outer fencing for the element.
• The default border properties are provided in CSS to
control the thickness of this outer fencing.
• The border area also add 's up to the complete height
and width of the element.
• The more the border width the more will be the height or
width of the element.
• In the above image the area marked with skin color is
called the border area.
4. Margin Area
• The area outside the border of an element is called
the margin area.
• Basically this area depends on the parent of the
element.
• The distance between the border of the parent
element and the border of the child element is
called as the margin.
• CSS has provides certain margin properties to get
control over this scenario.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 54
Box Sizing Property in CSS
There are two type's of box-sizing properties in CSS :
1. Content-Box(default property)
When the user set's the value of the box-sizing
property for an element as content-box or even if
user do not set's it ,it remains by default as content-
box and in the actual height and width of the
element the dimensions of the content area as well
This code will create a box model
as the padding area is added to constitute the final with a border line width of 0.4px
always and border-area of 1.6px
dimensions of the element.
and padding area as 20px width on
both sides of the content area.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 55
Box Sizing Property in CSS
There are two type's of box-sizing properties in CSS :
Padding
• Padding adds extra space inside the element, around
the content.
• Padding Left: 20px
• Padding Right: 20px
• Total padding width: 20px + 20px = 40px
Border
• The border, being solid, has a width, but it is calculated
differently from the padding.
• Line Width of Border: 0.4px (the width of the line itself)
• Area of Border: 1.6px (the actual space the border
occupies visually)
• Border width for both sides: 1.6px (left) + 1.6px (right) =
3.2px
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 57
Box Sizing Property in CSS
Total Width
The reason the total width is increased unexpectedly is because box-sizing: content-box applies
the width to the content area only .The padding and border are added outside the content area,
leading to an increase in the overall width and height of the element.
2. Border-Box
<p>The CSS box model is essentially a box that wraps around every HTML element.</p>
<p>It consists of (from innermost part to outermost part): content, padding, border, and margin.</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> Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 64
</html>
The CSS Box Model
Explanation of the different parts (from
innermost part to outermost part):
>>> In order to set the width and height of an element correctly in all browsers, you need to
know how the box model works.
**This <div> element will have a total width of 350px and a total height of 80px:
>>> When you set the width and height properties of an element with CSS, you just set the
width and height of the content area. To calculate the total width and height of an element,
you must also include the padding and borders.
Content
• The actual content of the element, like text, images, etc.
• Size controlled by width and height properties.
Padding
• Space inside the element, between the content and the border.
• Controlled by padding properties (e.g., padding-top, padding-right).
Border
• The border that wraps around the padding and content.
• Controlled by border-width, border-style, border-color.
Margin
• The space outside the border, separating the element from other elements.
• Controlled by margin properties.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 69
The CSS Box Model
Total width =
Total height =
content-box (default):
>>>>The width and height apply to the
content only (padding and border added
outside).
border-box: .box {
width: 200px;
>>>>The width and height include padding: 10px;
content + padding + border, so the total border: 5px solid black;
margin: 20px;
size is fixed. box-sizing: border-box; /* width includes padding and
border */
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 71
The CSS Box Model
Example:
div {
width: 200px;
height: 100px;
}
Property Description
Description: Space between the content and the border. It adds space inside
the element.
Example:
div {
padding: 20px;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 74
The CSS Box Model
Property Description
Padding Properties
Style Description
none No border is drawn. The element has no visible border at all.
solid A single solid line. The most common and simple border style.
dashed A series of short dashes, like a broken line.
dotted A series of dots, spaced evenly around the border.
double Two solid lines drawn parallel to each other with some space between.
groove Looks like a carved or engraved groove; it gives a 3D effect by using
light and shadow.
ridge Opposite of groove; looks like a raised ridge, also a 3D effect with
highlights and shadows.
inset Makes the element appear embedded or sunken into the page (3D inset
effect).
outset Makes the element appear raised or coming out of the page (3D outset
effect). Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 76
The CSS Box Model
Example:
div {
border: 2px solid black;
}
Property Description
border Shorthand for width, style, color
border-width Width of the border
border-style Style of the border (solid, dotted, etc)
border-color Color of the border
border-top Shorthand for top border
border-right Shorthand for right border
border-bottom Shorthand for bottom border
border-left Shorthand for left border
border-radius Rounds the corners
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 78
The CSS Box Model
Description: Space outside the border. It separates the element from other
elements.
Example:
div {
margin: 15px;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 79
The CSS Box Model
Property Description
margin Shorthand for all sides
margin-top Margin on the top
margin-right Margin on the right
margin-bottom Margin on the bottom
margin-left Margin on the left
1. Grouping in CSS
h1, h2, p {
color: blue;
}
2. Dimensions in CSS
div {
width: 100px;
height: 200px;
}
div {
display: flex;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 84
Activity Session
What to add in your profile card: Here are the minimum requirement.
Section Description Example
A heading with the
Name Student Name
person's name
A sentence or two Engineering student who loves
Short Bio
about the person coding and web development.
A list of 3 technical or HTML, CSS, XML, Javascript,
Skills List
soft skills [Link]
A small table with email Email: student@[Link],
Contact Info (Table)
and phone Phone: +91-1234567890
4. Positioning
Example:
.box {
position: absolute;
top: 50px;
left: 100px;
}
5. Floating
Example:
img {
float: right;
margin: 10px;
}
6. Align
Example:
.container {
display: flex;
justify-content: center;
align-items: center;
}
7. Pseudo-classes
Example:
a:hover {
color: red;
text-decoration: underline;
}
Teach horizontal & vertical menus using ul, li, and display.
9. Image Sprites
Example:
.icon {
background: url("[Link]") no-repeat;
width: 50px;
height: 50px;
}
.icon-home { background-position: 0 0; }
.icon-user { background-position: -50px 0; }
.icon-mail { background-position: -100px 0; }
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 92
CSS Advanced
10. Attribute Selectors
Types:
Example:
input[type="text"] {
border: 1px solid gray;
}
a[href^="https"] {
color: green;
} Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 93
CSS Advanced
Example:
div {
background-color: rgba(255, 0, 0, 0.5);
}
Layouts:
1. Grouping
Example:
h1, h2, p {
color: darkblue;
font-family: Arial, sans-serif;
}
h3 {
font-family: Arial, sans-serif;
font-weight: bold;
color: darkblue;
margin: 10px;
} Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 97
CSS Advanced : Activity Session - Solution
Q : How can you simplify while still keeping logic correct?
2. Make only the <p> that comes directly after the <h2> italic.
3. Select all <p> elements that are siblings after .student-list and make them red.
4. Add a new <p> somewhere else in the HTML and ask which selectors will style it.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 101
Activity Session :
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 102
CSS Advanced
2. Dimension
Example:
div {
width: 300px;
height: 200px;
max-width: 100%;
min-height: 150px;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 103
CSS Advanced : Activity Session
Answer the Questions >>
div {
width: 400px;
1. What is the width and height of <div>? height: 200px;
background-color: lightblue;
2. If the text in <p> is very short, what will its height be? }
img {
width: 100%;
max-width: 200px;
height: auto;
border: 2px solid black;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 104
CSS Advanced : Activity Session - Solution
Answer the Questions >>
div {
width: 400px;
1. What is the width and height of <div>? height: 200px;
background-color: lightblue;
(Answer: 400×200px) }
2. If the text in <p> is very short, what will its height be? p{
width: 300px;
min-height: 100px;
(Answer: 100px, because of min-height). max-height: 150px;
background-color: lightgreen;
3. If the text is very long, will <p> grow endlessly? }
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 106
CSS Advanced : Grouping Selectors
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 107
CSS Advanced
3. Display
Example:
span {
display: block;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 108
CSS Advanced: Display Property
CSS Display Property defines how an element is displayed in the document flow. Common
values:
• block – Element takes full width, starts on a new line (e.g., <div>, <p>).
• inline – Element only takes up as much width as needed, flows inline (e.g., <span>, <a>).
• inline-block – Behaves like inline but allows setting width and height.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 109
CSS Advanced: Activity Session
<!DOCTYPE html> .block { display: block; width: 150px; }
<html lang="en"> .inline { display: inline; width: 150px; }
<head> .inline-block { display: inline-block; width: 150px; }
<style> .none { display: none; }
.box { </style>
border: 2px solid black; </head>
margin: 5px; <body>
padding: 10px;
background: lightyellow; <div class="box block">Block</div>
} <div class="box inline">Inline</div>
<div class="box inline-block">Inline-block</div>
<div class="box none">None (Hidden)</div>
</body>
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 110
</html>
CSS Advanced: Activity Session
<!DOCTYPE html> <div class="box block">Block</div>
<html lang="en"> <div class="box inline">Inline</div>
<head> <div class="box inline-block">Inline-block</div>
<style> <div class="box none">None (Hidden)</div>
.box {
border: 2px solid black; </body>
margin: 5px; </html>
padding: 10px;
background: lightyellow;
}
.block { display: block; width: 150px; }
.inline { display: inline; width: 150px; }
.inline-block { display: inline-block; width: 150px; }
.none { display: none; }
</style>
</head>
<body>
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 111
CSS Advanced : Activity Session
<!DOCTYPE html> <div class="box block">Block</div>
<html lang="en"> <div class="box inline">Inline</div>
<head> <div class="box inline-block">Inline-block</div>
<style> <div class="box none">None (Hidden)</div>
.box {
border: 2px solid black; </body> 1. What happens when you change an element from inline to block
margin: 5px; </html> display?
padding: 10px;
background: lightyellow;
} 2. Can you set the width of an inline element? Why or why not?
.block { display: block; width: 150px; }
.inline { display: inline; width: 150px; }
.inline-block { display: inline-block; width: 3. When should you use inline-block instead of inline or block?
150px; }
.none { display: none; }
4. What does display: none do to an element on the page?
</style>
</head>
<body> 5. How do block and inline elements affect the flow of content?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 112
CSS Advanced
What happens when you change inline to block?
— Starts new line
2. Can you set the width of an inline element? Why or why not?
No, because inline elements only take up as much space as their content needs and ignore width and height
properties.
Use inline-block when you want the element to flow inline with others but still be able to set its width and
height.
It completely hides the element and removes it from the layout, so it takes up no space.
Block elements start on a new line and stack vertically; inline elements flow within the same line
horizontally.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 114
Activity Session : CSS Selectors and Box Model
<div class="card">
<h2>Title</h2> 1. What color will the heading be?
<p>This is a paragraph.</p>
</div> 2. What about the paragraph?
p{
color: green;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 115
Activity Session : CSS Selectors and Box Model
Snippet 2: Class, ID, and Grouping Selectors
<p id="intro"
class="highlight">Welcome!</p>
<p class="highlight">Hello!</p>
#intro { Questions:
font-size: 20px;
} What styles will apply to the first paragraph?
#intro, .highlight {
color: red;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 116
Activity Session : CSS Selectors and Box Model
Snippet 3: Attribute and Pseudo-class Selectors
<a href="[Link]
target="_blank">Visit</a>
<a href="[Link] Again</a>
a[target="_blank"] {
color: orange; Questions:
}
Which link turns orange?
a:hover {
text-decoration: underline; When does underline appear?
}
What if we remove the target attribute?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 117
Activity Session : CSS Selectors and Box Model
Snippet 4: Calculating Box Size
<div class="box">Content</div>
.box {
width: 200px;
padding: 10px;
border: 5px solid black; Questions:
margin: 20px;
} What's the total width and height of the element?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 118
Activity Session : CSS Selectors and Box Model
Snippet 5: Box-Sizing Demo
<div class="content-box">A</div>
<div class="border-box">B</div>
div {
width: 150px;
padding: 20px;
border: 5px solid green; Questions:
margin-bottom: 10px;
} Which box will be larger visually?
.border-box {
box-sizing: border-box;
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 119
background-color: lightgreen;
}
Activity Session : CSS Selectors and Box Model
Snippet 6: Margin Collapsing
<div class="outer">
<div class="inner">Text</div>
</div>
.outer {
margin-top: 30px; Questions:
background: #eee;
} Will the total space above .inner be 50px?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 120
div:nth-child(1) {
position: static; /* Default */
CSS Advanced : Activity Session background-color: lightblue;
width: 100px;
Answer the Questions >> height: 50px;
margin: 5px;
}
1. describe where each box will appear:
div:nth-child(2) {
position: relative;
• Box A top: 20px;
• Box B left: 30px;
• Box C background-color: lightgreen;
width: 100px;
2. Which positioning keeps element in normal flow? height: 50px;
}
3. Which positioning moves element but keeps space
reserved? div:nth-child(3) {
4. Which positioning removes element from normal position: absolute;
flow? top: 0;
right: 0;
background-color: lightcoral;
width: 100px;
height: 50px;
}
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 121
div:nth-child(1) {
position: static; /* Default */
CSS Advanced : Activity Session - Solution background-color: lightblue;
width: 100px;
Answer the Questions >> height: 50px;
margin: 5px;
}
1. describe where each box will appear:
div:nth-child(2) {
• Box A → normal flow position: relative;
• Box B → moves 20px down, 30px right, but space still top: 20px;
reserved left: 30px;
background-color: lightgreen;
• Box C → positioned absolutely in top-right of container
width: 100px;
height: 50px;
2. Which positioning keeps element in normal flow? }
→ static
div:nth-child(3) {
position: absolute;
[Link] positioning moves element but keeps space
top: 0;
reserved? right: 0;
→ relative background-color: lightcoral;
width: 100px;
[Link] positioning removes element from normal flow? height: 50px;
→ absolute }
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 122
CSS Advanced : Activity Session
Answer the Questions >> /* Header and Footer */ /* Content */
div:nth-child(1), div:nth-child(4) { div:nth-child(3) {
display: block; display: block;
Draw the layout on paper: width: 100%; width: 400px;
height: 50px; height: 200px;
• Header and Footer → full background-color: lightblue; background-color: lightcoral;
width margin: 5px 0; position: relative;
} top: -200px; /* moves up over
sidebar */
• Sidebar → left side
/* Sidebar */ left: 160px; /* moves right next
div:nth-child(2) { to sidebar */
• Content → next to sidebar, display: block; }
slightly overlapping (because width: 150px;
of relative top/left) height: 200px; /* Buttons */
background-color: lightgreen; span {
• Buttons → side by side below position: relative; display: inline-block;
content top: 0; width: 100px;
left: 0; height: 30px;
} background-color: yellow;
margin: 5px;
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE
} 123
CSS Advanced : Positioning
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 124
CSS Advanced : Types of Positioning in CSS
CSS positioning is used to control the exact placement and layering of HTML elements on a web page.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 125
CSS Advanced : Types of Positioning in CSS
1. static (Default)
The default positioning; elements appear in the normal flow of the document.
div {
position: static;
}
Characteristics:
• Cannot be moved with top, left, right, bottom.
• Used when no special positioning is needed.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 126
CSS Advanced : Types of Positioning in CSS
2. relative
The default positioning; elements appear in the normal flow of the document.
div {
position: relative;
top: 20px;
left: 10px;
}
Characteristics:
• Element keeps its space in the document.
• Useful for creating reference points for absolutely positioned children.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 127
CSS Advanced : Types of Positioning in CSS
3. absolute
Removes the element from the document flow and positions it relative to the
nearest ancestor that is positioned (i.e., not static).
.container {
position: relative;
}
.box {
position: absolute;
top: 0; Characteristics:
left: 0; • Can overlap other elements.
}
• Useful for dropdowns, tooltips, or popups.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 128
CSS Advanced : Types of Positioning in CSS
4. fixed
button {
position: fixed;
bottom: 10px;
right: 10px;
}
Characteristics:
• Always visible even when scrolling.
• Used for sticky headers, floating buttons, etc.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 129
CSS Advanced : Types of Positioning in CSS
5. sticky
Starts as relative, but becomes fixed once it reaches a certain scroll position.
header {
position: sticky;
top: 0;
}
Characteristics:
• Useful for headers or table columns that stay visible during scroll.
• Requires a scrollable container or page.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 130
CSS Advanced : Types of Positioning in CSS
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 131
CSS Advanced : List of Properties for CSS Positioning
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 132
CSS Advanced : What is float in CSS?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 133
CSS Advanced : What is float in CSS?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 134
CSS Advanced : Why do we use float?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 135
CSS Advanced : Values of float
Value Description
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 136
CSS Advanced : How does float behave?
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 137
CSS Advanced : Example of float
<style>
.photo {
float: left;
width: 150px;
margin-right: 15px; /* space between image and text */
}
</style>
<div>
<img src="[Link]" alt="Sample" class="photo" />
<p>
This paragraph will wrap around the image floated to the left.
Notice how the text flows nicely beside the image instead of below it.
</p>
</div>
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 138
CSS Advanced : Example of float
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 139
CSS Advanced : Common Points related to Float in CSS
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 140
CSS Advanced : Common Use Cases::: Float in CSS
Text wrap around images: Float images left or right to let text flow
around them.
Simple two-column layouts: Float two divs left and right to create side-
by-side columns.
Navigation bars: Float menu items to the left or right for horizontal
menus.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 141
CSS Advanced : Limitations of float::: Float in CSS
Float was not originally intended for full page layouts, so it can
be tricky.
Modern CSS layout tools like Flexbox and Grid are more
powerful and easier to control.
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 142
CSS Advanced : Limitations of float::: Float in CSS
• Floating moves an element left or right, allowing text and inline elements
Faculty Name: Ms. Ramandeep Kaur | College: GCET | Department: CSE 143
Thankyou