Web unit 3
Web unit 3
Introduction
Cascading Style Sheets (CSS) is a style sheet language used to describe the look and formatting of a
document written in a markup language like HTML. While HTML defines the structure and content
of a web page, CSS is responsible for its visual presentation. It allows web developers to separate
content from design, thus enhancing productivity, maintainability, and reusability of web documents.
CSS was developed by the World Wide Web Consortium (W3C) and has become a fundamental part
of modern web development. With CSS, designers can apply consistent styles to multiple web pages,
create responsive layouts, and improve accessibility.
Syntax of CSS
CSS uses a specific syntax to define style rules. Each rule consists of a selector and a declaration
block.
selector {
property: value;
property: value;
}
Example:
h1 {
color: blue;
font-size: 24px;
}
• Selector: Specifies the HTML element to be styled (e.g., h1, p, div).
• Property: A style attribute (e.g., color, font-size).
• Value: The value assigned to the property (e.g., blue, 24px).
Advantages of CSS
• Separation of structure and style
• Improved website performance
• Better accessibility and device compatibility
• Scalable for large web projects
• Enables animations and transitions
Introduction
CSS (Cascading Style Sheets) is a style sheet language used for describing the visual presentation of
HTML elements. It controls layout, colors, fonts, spacing, and overall design. To apply CSS to HTML
documents, rules must be added in specific locations, known as methods of CSS application. These
methods determine how styles are linked or embedded into a web page.
The three main methods for adding CSS rules are:
1. Inline CSS
2. Internal CSS
3. External CSS
Additionally, CSS can also be imported using the @import rule and media queries can apply CSS
conditionally. Understanding where to add CSS rules is fundamental for designing maintainable and
efficient web pages.
1. Inline CSS
Definition
Inline CSS involves adding CSS rules directly into the HTML element using the style attribute. It is
used to apply a unique style to a single element on the page.
Syntax
<tagname style="property: value;">Content</tagname>
Example
<p style="color: red; font-size: 18px;">This is a paragraph with inline styling.</p>
Use Cases
• Quick testing or temporary styling.
• Overriding other CSS rules for a specific element.
• Emails or embedded HTML where external files are restricted.
Advantages
• Easy to apply.
• Does not require an external or internal stylesheet.
• Style is immediately visible.
Disadvantages
• Not reusable.
• Leads to messy code.
• Difficult to maintain and debug.
• Reduces separation between content and design.
2. Internal CSS
Definition
Internal CSS is written within the <style> tag inside the <head> section of the HTML document. It
applies styles to the entire page.
Syntax
<head>
<style>
selector {
property: value;
}
</style>
</head>
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
text-align: center;
}
p{
font-size: 16px;
}
</style>
</head>
<body>
<h1>Welcome</h1>
<p>This is styled using internal CSS.</p>
</body>
</html>
Use Cases
• When styling is only required for a single page.
• For small projects or testing without external files.
• Rapid prototyping or personal websites.
Advantages
• Styles are centralized in one section of the document.
• Easier to maintain than inline CSS.
• Better separation than inline styles.
Disadvantages
• Styles not reusable across multiple pages.
• Increases page size.
• Difficult to manage for large projects.
3. External CSS
Definition
External CSS involves writing CSS rules in a separate .css file and linking it to an HTML document
using the <link> element in the <head>.
Syntax
HTML file:
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
style.css:
body {
background-color: #f9f9f9;
}
h1 {
color: blue;
}
Use Cases
• Large websites requiring a consistent look.
• Projects where styles need to be reused.
• Team collaboration and version control.
Advantages
• Clean and organized code.
• Styles can be reused across multiple pages.
• Better site performance with browser caching.
• Enhances collaboration and modularity.
Disadvantages
• Requires additional HTTP request.
• Changes are not immediately visible if CSS file fails to load.
• Not suitable for very small or single-page sites.
Reusability No No Yes
Practical Example
Let’s say you are building a website with a header, navigation menu, and a content section.
Using Inline CSS:
<h1 style="color: blue;">My Website</h1>
Using Internal CSS:
<style>
nav {
background-color: #333;
color: white;
}
</style>
Using External CSS (style.css):
header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
}
HTML Linking External CSS:
<link rel="stylesheet" href="style.css">
CSS Properties
Introduction
CSS (Cascading Style Sheets) is a language used to describe the presentation of HTML documents.
CSS properties are predefined keywords used to define how HTML elements should be displayed in a
web browser. Each property is paired with a value, and this combination forms a CSS rule.
The general syntax is:
selector {
property: value;
}
Example:
p{
color: blue;
font-size: 16px;
}
CSS properties control everything from text color, background, borders, margins, padding,
positioning, and more. Understanding various categories of CSS properties is essential for creating
visually appealing and well-structured web pages.
1. Text Properties
These properties are used to style text content inside HTML elements.
Important Text Properties
Property Description
color Sets the text color
Example
h1 {
font-family: Arial;
font-size: 28px;
color: navy;
text-align: center;
}
2. Background Properties
Background properties allow control over an element’s background such as color, image, position, and
repetition.
Key Background Properties
Property Description
Property Description
Example
div {
width: 300px;
height: 200px;
padding: 20px;
margin: 30px;
border: 2px solid black;
box-sizing: border-box;
}
4. Border Properties
CSS border properties define the width, style, and color of an element’s border.
Border Sub-Properties
Property Description
Example
img {
border: 5px dashed red;
border-radius: 15px;
}
Property Description
Example
div {
display: none; /* element not shown */
}
span {
display: inline;
}
7. Positioning Properties
CSS provides several positioning schemes for arranging elements.
Position Values
Value Description
static Default, normal flow
Supporting Properties
• top, right, bottom, left
• z-index
Example
#menu {
position: fixed;
top: 0;
width: 100%;
background-color: black;
color: white;
}
8. Flexbox Properties
Flexbox layout simplifies alignment and distribution of space among items in a container.
Key Flexbox Properties
Property Description
Example
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
9. Grid Properties
CSS Grid Layout allows you to design web pages using a two-dimensional grid.
Important Grid Properties
Property Description
Example
.grid {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 10px;
}
Property Description
Property Description
Example
button {
transition: background-color 0.5s ease-in-out;
}
button:hover {
background-color: lightgreen;
}
Introduction
Text is one of the primary components of any website. Controlling its appearance is essential for
ensuring readability, accessibility, and visual appeal. CSS (Cascading Style Sheets) provides a wide
range of text properties that help designers and developers manipulate the presentation of textual
content within HTML documents.
By applying CSS, you can modify:
• Font styles and sizes
• Text alignment
• Spacing between characters and lines
• Text transformation (uppercase/lowercase)
• Decoration such as underlining or striking through
Proper control of text improves user experience and supports branding consistency.
1. Font Properties
Font properties are used to define how text appears in terms of typeface, style, and size.
a. font-family
Defines the typeface used for text. Multiple font options are listed for fallback purposes.
p{
font-family: 'Arial', 'Helvetica', sans-serif;
}
b. font-size
Specifies the size of the text. It can be set in px, em, rem, %, etc.
h1 {
font-size: 36px;
}
c. font-style
Used to apply italic styling.
em {
font-style: italic;
}
d. font-weight
Controls the thickness of the text.
strong {
font-weight: bold;
}
e. font-variant
Used for small caps style.
span {
font-variant: small-caps;
}
f. Shorthand Property
You can combine all font properties using font.
p{
font: italic bold 16px/1.5 'Georgia', serif;
}
4. Text Spacing
Spacing properties allow you to adjust the space between characters and lines for better readability.
a. letter-spacing
Controls space between characters.
p{
letter-spacing: 2px;
}
b. word-spacing
Controls space between words.
p{
word-spacing: 10px;
}
c. line-height
Sets vertical spacing between lines.
p{
line-height: 1.8;
}
p::first-line {
font-weight: bold;
}
They are useful in:
• Drop caps
• Highlighting the first line
• Custom quotes (::before and ::after)
9. Responsive Typography
In modern web design, controlling text responsively is crucial.
Relative Units
• em – relative to parent font size
• rem – relative to root font size
• % – percentage of parent
Media Queries for Text
@media screen and (max-width: 600px) {
body {
font-size: 14px;
}
}
Introduction
Text formatting is one of the most essential aspects of web design and development. With CSS
(Cascading Style Sheets), you can apply advanced formatting to text elements in HTML to improve
visual appeal, readability, and user engagement. CSS text formatting controls everything from font
style and weight to alignment, decoration, transformation, spacing, and more.
Understanding how to format text using CSS ensures that websites are both aesthetically pleasing
and functionally responsive across various devices and screen sizes.
1. Font Styling
CSS offers several properties to format the appearance of fonts.
a. font-family
Specifies the typeface used. Always include fallback fonts.
p{
font-family: "Arial", "Helvetica", sans-serif;
}
b. font-size
Determines the size of the text. Units can be px, em, rem, %, etc.
p{
font-size: 18px;
}
c. font-style
Used to italicize text.
em {
font-style: italic;
}
d. font-weight
Specifies the boldness of text.
strong {
font-weight: bold;
}
e. font-variant
Displays text in small capital letters.
p{
font-variant: small-caps;
}
f. Shorthand property
p{
font: italic bold 16px/1.5 "Times New Roman", serif;
}
3. Text Decoration
Text decoration is used for visual effects like underlining, overlining, and strikethrough.
a. text-decoration
a{
text-decoration: none;
}
Values:
• none
• underline
• overline
• line-through
b. text-shadow
Applies shadow to text.
h2 {
text-shadow: 2px 2px 5px grey;
}
4. Text Transformation
Text transformation allows developers to alter the case of text.
a. text-transform
h1 {
text-transform: uppercase;
}
Values:
• uppercase
• lowercase
• capitalize
• none
6. White-Space Control
White space formatting ensures how spaces and line breaks behave.
a. white-space
pre {
white-space: pre;
}
Values:
• normal (default)
• nowrap
• pre
• pre-wrap
• pre-line
b. overflow-wrap
Controls how long words are broken into new lines.
div {
overflow-wrap: break-word;
}
p::first-line {
font-weight: bold;
}
b. ::before and ::after
Used to insert content before/after an element.
blockquote::before {
content: "“";
font-size: 2em;
}
Introduction
Pseudo-classes in CSS are special keywords that allow you to target elements based on their state or
position within the document. When it comes to text, pseudo-classes can be used to enhance the way
text behaves on different user interactions, such as hovering over a link or focusing on an input field.
These pseudo-classes provide a dynamic way to style text elements based on user interaction,
document structure, or specific states. By using them effectively, web developers can create more
interactive and visually appealing websites.
a:visited {
color: purple;
}
a:hover {
color: red;
text-decoration: underline;
}
a:active {
color: green;
}
b. Focus States
For accessibility, it’s important to style elements that are focused, especially form elements. This
ensures that keyboard users can easily identify which element is active.
input:focus, textarea:focus {
border-color: blue;
outline: none;
}
c. Highlighting Special Text
You can highlight specific text based on its position, such as using :first-child to make the first
paragraph stand out or :nth-of-type for more complex styling.
p:first-child {
font-weight: bold;
}
p:nth-of-type(3) {
color: red;
}
a:focus:active {
color: green;
}
These selectors allow for fine-grained control over text styling, responding to multiple conditions at
once.
a:hover {
color: orange;
}
}
This rule ensures that text formatting, like font size and color, adjusts when viewed on smaller
screens, enhancing user experience.
CSS Selectors
CSS (Cascading Style Sheets) selectors are the part of CSS rules used to target HTML elements and
apply styles to them. Selectors are essential in defining how styles apply to elements across a
webpage. Understanding the different types of selectors enables web developers to write more
organized, efficient, and specific CSS code.
This answer explains the following key selectors:
• Universal Selector
• Type Selector
• Class Selector
• ID Selector
• Grouping Selector
• Child Selector
• Descendant Selector
• Sibling Selector
• Adjacent Selector
• Attribute Selector
• Pseudo-Class Selector
4. ID Selector (#)
ID selectors are used to style a single unique element. IDs must be unique within the HTML
document.
Syntax:
#header {
background-color: gray;
}
HTML:
<div id="header">This is the header</div>
Explanation:
• Targets only the element with id="header".
• Typically used for layout sections like navigation, footer, etc.
5. Grouping Selector
Grouping selectors allow you to apply the same style to multiple elements using a single rule,
separated by commas.
Syntax:
h1, h2, h3 {
font-family: Georgia, serif;
}
Explanation:
• Applies the same font to all three heading types.
• Reduces redundancy in code.
Use Case:
a, button, input[type="submit"] {
cursor: pointer;
}
Lengths in CSS are used to define how large or small elements appear. They are most commonly
applied to properties like:
• width
• height
• font-size
• margin, padding
• border-width
• line-height
• letter-spacing
• top, left, right, bottom (positioning)
Example:
div {
width: 200px;
padding: 10px;
font-size: 16pt;
}
CSS lengths are categorized into:
• Absolute units
• Relative units
Example:
p{
font-size: 14pt;
line-height: 1.5in;
}
Example:
html {
font-size: 16px;
}
h1 {
font-size: 2rem; /* = 32px */
}
p{
font-size: 1.5em; /* relative to parent */
}
4. em vs rem
Example:
html {
font-size: 16px;
}
section {
font-size: 20px;
}
section p {
font-size: 1em; /* 20px */
}
section p span {
font-size: 2em; /* 40px, relative to p */
}
Using rem provides predictability, while em allows local scaling.
Unit Description
vmin Smaller of vw or vh
vmax Larger of vw or vh
Example:
div {
width: 80vw;
height: 60vh;
}
Useful for creating full-screen layouts, responsive headings, and flexible content boxes.
.box {
width: 50%;
}
If .container is 800px wide, then .box becomes 400px.
Important: Percentages behave differently depending on the property:
• width, height: Relative to parent box
• padding, margin: Relative to parent’s width
• font-size: Not percentage based
h1 {
font-size: 4vw;
}
This mix ensures consistent design across desktop, tablet, and mobile screens.
9. Practical Examples
Responsive Box:
.container {
width: 90%;
padding: 2em;
font-size: 1.2rem;
}
Fullscreen Section:
.fullscreen {
height: 100vh;
width: 100vw;
}
Typography:
h2 {
font-size: 2.5rem;
}
p{
font-size: 1em;
line-height: 1.5em;
}
CSS (Cascading Style Sheets) uses a box model concept to wrap every HTML element in a
rectangular box. This model describes how the element occupies space on a webpage and helps
developers control layout, spacing, borders, and positioning.
Understanding the CSS Box Model is essential for building well-structured, responsive, and visually
appealing websites.
4. Box-Sizing Property
The box-sizing property changes how width and height are calculated.
Values:
Value Description
content-box Default. Width includes only content. Padding and border are added.
Example:
div {
width: 300px;
padding: 20px;
border: 10px solid black;
box-sizing: border-box;
}
Here, the total width remains 300px, unlike in the default content-box.
5. Practical Example
HTML:
<div class="box">Hello World</div>
CSS:
.box {
width: 200px;
padding: 20px;
border: 2px solid red;
margin: 10px;
}
Result:
• Content width = 200px
• Padding = 40px total (20 left + 20 right)
• Border = 4px total
• Margin = 20px total
• Total width = 264px
Without setting box-sizing: border-box, the element visually expands beyond the set width.
6. Collapsing Margins
Margin collapsing occurs when two vertical margins meet. Instead of adding, the browser uses only
the larger one.
Example:
div1 {
margin-bottom: 20px;
}
div2 {
margin-top: 30px;
}
The total space between them is 30px, not 50px.
9. Best Practices
• Use box-sizing: border-box; globally to simplify width/height calculation:
*{
box-sizing: border-box;
}
• Always define padding and margin thoughtfully for mobile responsiveness.
• Use consistent units (px, rem, %) to maintain uniformity.