Module- 2
Module- 2
Module- 2
▪CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript.
▪CSS is designed to enable the separation of presentation and content, including layout, colors,
and fonts. This separation can improve content accessibility, provide more flexibility and control in
the specification of presentation characteristics, enable multiple web pages to share formatting
by specifying the relevant CSS in a separate CSS file, and reduce complexity and repetition in the
structural content.
What is CSS?
Definition: CSS (Cascading Style Sheets) is a style sheet language used for describing the
presentation of a document written in HTML or XML. Including the design, layout and variations in
display for different devices and screen sizes.
Advantages:
◦ Centralized style for a single document.
External Style
These styles are kept separately and called or linked from the required page. This is the syntax followed
for linking a external style sheet.
Advantages: Reusable, keeps HTML clean.
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<p>This text is styled using external CSS.</p>
</body>
CSS Selectors
A CSS SELECTOR SELECTS THE HTML ELEMENT(S) YOU
WANT TO STYLE
CSS Selectors
CSS Selectors are essential for targeting HTML elements to apply styles. Each selector type
helps in applying styles based on different criteria.
Example:
p { color: blue; }
This will turn the text color of all <p> elements to blue.
Class Selector
Description: Targets elements with a specific class attribute. Multiple elements can share the
same class.
Example:
.example(classname)
{
font-size: 14px;
}
This applies a font size of 14px to all elements with the class example.
ID Selector
Description: Targets a single element with a specific id attribute. IDs must be unique within a
page.
Example:
This will set the background color of the element with the ID unique to yellow.
2. Attribute Selectors
Description: Targets elements based on the presence or value of an attribute.
Usage: Useful for applying styles based on attributes of elements, like input types or data attributes.
Examples:
Presence:
[type] { border: 1px solid black; }
Usage: Useful for styling elements dynamically based on user interactions or their position.
Examples:
Usage: Useful for styling particular parts of an element like the first line or first letter.
Examples:
Usage: Useful for styling elements based on their location relative to other elements.
Examples:
Descendant Selector:
div p { color: green; }
Selects all <p> elements that are descendants of a <div>.
Child Selector:
ul > li { margin: 5px; }
Selects direct child <li> elements of a <ul>.
Group Selectors
Description: Groups multiple selectors to apply the same style to different elements.
Example:
Applies the same font-family to all <h1>, <h2>, and <h3> elements.
These selectors allow for flexible and precise styling of HTML elements, enhancing the
presentation and user experience of web pages.
CSS fonts
APPEARANCE OF TEXT ON A WEBPAGE
CSS fonts
CSS fonts are used to define and control the appearance of text on a webpage through the use of
various typefaces and styles.
They dictate how text should be rendered, including the typeface (font family), size, weight, style,
and spacing.
CSS fonts help ensure that text is presented consistently across different devices and browsers by
specifying exact font characteristics.
CSS fonts
Here’s a brief overview:
Typeface: The specific design of the characters, including different families like serif, sans-serif,
monospace, cursive, and fantasy.
Font Family: A collection of typefaces that share common design features, specified in CSS to
apply to text elements.
Web Fonts: Fonts that are loaded and used directly from the web, allowing for custom fonts that
aren't installed on the user's system. Examples include Google Fonts and Adobe Fonts.
System Fonts: The default fonts available on the user's operating system, used when no specific
web font is defined.
CSS Font Properties Detailed Overview
1. font-family
Purpose: Specifies the typeface for text.
Syntax: font-family: 'Font Name', Generic Family;
Description: Allows multiple fonts to be listed as a fallback mechanism. The browser uses the first
available font.
Generic Families: Include serif, sans-serif, monospace, cursive, fantasy, and system-ui.
Example:
p{
font-family: 'Arial', sans-serif;
}
CSS Font Properties Detailed Overview
2. font-size
Purpose: Defines the size of the font.
Syntax: font-size: value;
Values:
Absolute Sizes: px, pt, in, cm, mm, em (relative to the parent element), rem (relative to the root element).
Relative Sizes: small, medium, large, x-large, xx-large.
Example:
h1 {
font-size: 24px;
}
CSS Font Properties Detailed Overview
3. font-style
Example:
em {
font-style: italic;
}
CSS Font Properties Detailed Overview
4. font-weight
Values:
Numeric Values: 100, 200, 300, 400, 500, 600, 700, 800, 900.
Example:
strong {
font-weight: bold;
}
CSS Font Properties Detailed Overview
5. font-variant
Purpose: Adjusts font features such as small-caps.
Syntax: font-variant: value;
Values:
normal: Default setting.
small-caps: Converts lowercase letters to small capital letters.
Example:
h2 {
font-variant: small-caps;
}
CSS Font Properties Detailed Overview
6. font-stretch
Values:
Example:
.narrow-text {
font-stretch: condensed;
}
CSS Font Properties Detailed Overview
7. font (Shorthand Property)
Purpose: A shorthand property for setting multiple font properties in a single declaration.
Syntax: font: [font-style] [font-variant] [font-weight] [font-size] [line-height] [font-family];
Example:
.header {
font: italic small-caps bold 20px/1.5 'Verdana', sans-serif;
}
Each element is represented as a rectangular box, and the model includes several layers.
CSS Box Model
Default Browser Behavior:
◦ Browsers assign default position values to HTML elements.
◦ This default behavior can lead to unexpected layouts and limited design flexibility.
Practical Examples:
◦ Background Color: When you apply a background color to an element, it fills the entire box, not just the
content area.
◦ Text Alignment: Text alignment is relative to the element's box, including its padding and borders.
CSS Box Model
Concept of the Box Model: Every element on a webpage is treated as being within a box.
Changing properties like background color affects the entire box, not just the content.
Understanding the Box Model helps in managing layout, spacing, and alignment more effectively.
Description: The innermost area where the content of the element (such as text or an image) is
displayed.
Example:
.box {
width: 200px;
height: 100px;
}
CSS Box Model
2. Padding
Description: The space between the content area and the border. Padding adds space inside
the element around the content.
Properties: Can be set for all sides (padding), or individually (padding-top, padding-right, padding-
bottom, padding-left).
Example:
.box {
padding: 20px;
}
CSS Box Model
3. Border
Description: The line surrounding the padding and content area. It can be styled in various
ways (e.g., solid, dashed, dotted).
Example:
.box {
border: 2px solid black;
}
CSS Box Model
4. Margin
Description: The outermost space that separates the element from its surrounding elements.
Margins create space outside the border.
Properties: Can be set for all sides (margin), or individually (margin-top, margin-right, margin-left,
margin-bottom)
Example:
.box {
margin: 30px;
}
CSS Box Model
4. Box Sizing Property
Description: Determines how the total width and height of an element are calculated.
Properties: Can be set for all sides (margin), or individually (margin-top, margin-right, margin-left, margin-bottom)
Values:
content-box: The default value. Width and height apply only to the content area. Padding, border, and margin are added outside this area.
border-box: The width and height include the content, padding, and border. The total size of the element is set by these values, making it
easier to manage dimensions.
Example:
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}