Css Documentation Hashir
Css Documentation Hashir
What is CSS?
CSS stands for Cascading Style Sheets
CSS describes how HTML elements are to be displayed on screen, paper, or in other media
CSS saves a lot of work. It can control the layout of multiple web pages all at once
External stylesheets are stored in CSS files
1. Background Color
The background-color property in CSS sets the background color of an element.
Syntax:
element {
background-color: color;
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.highlight {
background-color: yellow;
</style>
</head>
<body>
</body>
</html>
In this example, the paragraph with the class highlight will have a yellow background.
2. Margin and Padding
Margins and padding are crucial in controlling the spacing around and within elements, respectively.
Margin: The space outside an element's border.
Padding: The space between an element's content and its border.
Syntax:
element {
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
background-color: lightblue;
margin: 20px;
padding: 15px;
</style>
</head>
<body>
</body>
</html>
In this example, the .box element has a margin of 20px and padding of 15px, creating space outside
and inside the element, respectively.
3. Box Model
The CSS box model describes the rectangular boxes generated for elements in the document tree and
consists of:
Content: The actual content of the element.
Padding: Clears an area around the content.
Border: A border that goes around the padding and content.
Margin: Clears an area outside the border.
Visual Representation:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 150px;
padding: 10px;
margin: 20px;
background-color: lightgreen;
}
</style>
</head>
<body>
</body>
</html>
In this example, the .box element has specific width, padding, border, and margin, illustrating the box
model components.
width: value;
height: value;
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.rectangle {
width: 200px;
height: 100px;
background-color: coral;
</style>
</head>
<body>
<div class="rectangle"></div>
</body>
</html>
In this example, the .rectangle element has a width of 200px and a height of 100px.
5. ID and Class
IDs and classes are used to select and style elements.
ID: Unique identifier for a single element. Defined with #idname.
Class: Can be applied to multiple elements. Defined with .classname.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#unique {
color: blue;
.common {
font-weight: bold;
</style>
</head>
<body>
</body>
</html>
In this example, the paragraph with the ID unique is styled with blue text, while paragraphs with the
class common are bold.
6. Z-Index
The z-index property determines the stacking order of positioned elements.
The z-index property specifies the stack order of an element (which element should be placed in front
of, or behind, the others).
An element can have a positive or negative stack order:
Default value is 0.
Syntax:
element {
z-index: number;
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box1 {
le : 50px;
top: 50px;
width: 100px;
height: 100px;
background-color: red;
z-index: 1;
.box2 {
le : 80px;
top: 80px;
width: 100px;
height: 100px;
background-color: blue;
z-index: 2;
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
In this example, .box2 overlaps .box1 because it has a higher z-index value.
7. Opacity
The opacity property sets the transparency level of an element.
Syntax:
element {
Example:
<!DOCTYPE html>
<head>
<style>
.transparent {
opacity: 0.5;
background-color: purple;
width: 100px;
height: 100px;
</style>
</head>
<body>
<div class="transparent"></div>
</body>
</html>
Certainly! Let's delve into each of the specified CSS topics, providing detailed explanations and
examples for each.
8. Position
The position property in CSS specifies how an element is positioned in the document. It accepts the
following values:
static: The default value. Elements are positioned according to the normal flow of the
document.
relative: The element is positioned relative to its normal position.
absolute: The element is positioned relative to its nearest positioned ancestor. If none exists,
it's positioned relative to the initial containing block.
fixed: The element is positioned relative to the viewport and doesn't move when scrolled.
sticky: The element toggles between relative and fixed positioning based on the user's scroll
position
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.container {
width: 300px;
height: 200px;
background-color: lightgray;
.box1 {
top: 20px;
le : 20px;
width: 100px;
height: 100px;
background-color: coral;
.box2 {
bo om: 10px;
right: 10px;
width: 100px;
height: 100px;
background-color: lightblue;
</style>
</head>
<body>
<div class="container">
<div class="box1">Absolute</div>
</div>
<div class="box2">Fixed</div>
</body>
</html>
In this example, .box1 is positioned absolutely within the .container, while .box2 is fixed relative to
the viewport.
9. Flexbox
he Flexible Box Layout (flexbox) is a layout model that allows responsive elements within a container
to be automatically arranged depending on screen size.t is particularly useful for creating complex
layouts and aligning items.
Key Properties:
display: flex; Defines a flex container.
flex-direction: Specifies the direction of the flex items (row, row-reverse, column, column-
reverse).
justify-content: Aligns items along the main axis (flex-start, flex-end, center, space-between,
space-around).
align-items: Aligns items along the cross axis (flex-start, flex-end, center, stretch).
flex-wrap: Specifies whether items should wrap (nowrap, wrap, wrap-reverse).
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.container {
display: flex;
align-items: center;
height: 200px;
background-color: lightgray;
.box {
width: 100px;
height: 100px;
background-color: coral;
</style>
</head>
<body>
<div class="container">
</div>
</body>
</html>
In this example, the .container is a flex container with three child .box elements. The boxes are spaced
evenly with justify-content: space-around and centered along the cross axis with align-items: center.
11. Grid
CSS Grid Layout is a two-dimensional layout system for the web, allowing developers to create
complex layouts with rows and columns.
Key Properties:
display: grid; Defines a grid container.
grid-template-columns: Specifies the number and size of columns.
grid-template-rows: Specifies the number and size of rows.
grid-gap: Sets the gap between rows and columns.
grid-column: Specifies how many columns an item spans.
grid-row: Specifies how many rows an item spans.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
background-color: dodgerblue;
padding: 10px;
background-color: #f1f1f1;
padding: 10px;
font-size: 30px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</body>
</html>
We can adjust the gap size by using one of the following properties:
column-gap
row-gap
gap
12. Columns
The CSS columns property is a shorthand for setting both the column-width and column-count
properties, allowing for the creation of multi-column layouts.his property enables content to flow into
multiple columns, similar to newspaper layouts.
Key Properties:
column-count: Specifies the number of columns an element should be divided into.
column-width: Specifies the ideal width of each column.
column-gap: Sets the gap between columns.
column-rule: A shorthand property for setting the width, style, and color of the line drawn
between columns.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.newspaper {
column-count: 3;
column-gap: 40px;
</style>
</head>
<body>
<div class="newspaper">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod ncidunt
ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci
ta on ullamcorper suscipit lobor s nisl ut aliquip ex ea commodo consequat. Duis autem vel eum
iriure dolor in hendrerit in vulputate velit esse moles e consequat, vel illum dolore eu feugiat nulla
facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit
augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend op on congue
nihil imperdiet doming id quod mazim placerat facer possim assum.
</div>
</body>
</html>
13. Overflow
The overflow property in CSS controls what happens to content that exceeds the boundaries of its
container.It determines whether the content should be clipped, display scrollbars, or be visible outside
the container.
Values:
The overflow property has the following values:
visible - Default. The overflow is not clipped. The content renders outside the element's box
hidden - The overflow is clipped, and the rest of the content will be invisible
scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content
auto - Similar to scroll, but it adds scrollbars only when necessary
Example:
div {
width: 200px;
height: 65px;
background-color: coral;
overflow: visible;
}
In this example, the .box class has a fixed width and height. If the content exceeds these dimensions,
scrollbars will appear due to the overflow: auto; property.
14. Pseudo-Classes
Pseudo-classes are keywords added to selectors that specify a special state of the selected elements.
They allow styling elements based on their state, position, or user interaction.
Common Pseudo-Classes:
:hover: Applies when the user designates an element (with a pointing device), but does not
activate it.
:active: Applies when an element is being activated by the user.
:focus: Applies when an element has received focus.
:nth-child(n): Matches elements based on their position in a group of siblings.
:first-child: Matches an element that is the first child of its parent.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
a:hover {
color: red;
p:first-child {
font-weight: bold;
</style>
</head>
<body>
</body>
</html>
In this example, the link changes color when hovered over, and the first paragraph is bold due to the
:first-child pseudo-class.
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
html {
font-size: 16px;
.px-size {
font-size: 24px;
.rem-size {
font-size: 1.5rem;
.vw-size {
font-size: 5vw;
</style>
</head>
<body>
</body>
</html>
16. Borders
Borders in CSS define the edges of an element. They can have different styles, widths, and colors.
Syntax:
selector {
Example:
div {
This example applies a solid black border of 2px width to a <div> element.
Border Properties:
border-width: Specifies the thickness of the border.
border-style: Defines the style (solid, dashed, dotted, etc.).
border-color: Sets the color of the border.
border-radius: Rounds the corners of an element.
17. Fonts
Fonts define the text appearance in CSS. They can be customized using different font families, sizes,
weights, and styles.
Syntax:
selector {
font-size: 16px;
font-weight: bold;
font-style: italic;
Example:
p{
font-size: 18px;
font-weight: 700;
font-style: italic;
}
This applies the "Times New Roman" font to paragraphs with a size of 18px, bold weight, and italic
style.
Font Properties:
font-family: Specifies the typeface.
font-size: Determines the text size.
font-weight: Controls the thickness of the text (e.g., normal, bold, bolder, lighter).
font-style: Defines the text style (e.g., normal, italic, oblique).
selector {
property: value;
Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightgreen;
body {
background-color: lightblue;
}
</style>
</head>
<body>
<p>Resize the browser window. When the width of this document is 600 pixels or less, the
background-color is "lightblue", otherwise it is "lightgreen".</p>
</body>
</html>
This changes the background color of the body to lightblue when the screen width is 600px or less.
Common Media Features:
max-width: Targets devices with widths up to the specified value.
min-width: Applies styles for screen sizes above a given width.
orientation: Adjusts styles based on device orientation (portrait or landscape).
resolution: Modifies styles based on screen resolution.
19. Animations
CSS animations bring motion to elements, enhancing user experience.
Syntax:
@keyframes anima on-name {
from {
property: value;
to {
property: value;
selector {
anima on: anima on-name dura on ming-func on delay itera on-count direc on;
Example:
@keyframes fadeIn {
from {
opacity: 0;
to {
opacity: 1;
div {
20. Layout
CSS layout techniques define how elements are arranged on the page.
Common Layout Techniques:
1. Flexbox:
.container {
display: flex;
align-items: center;
display: grid;
gap: 10px;
This creates a two-column grid with equal widths and a 10px gap.
3. Positioning:
.absolute-box {
top: 50px;
le : 100px;
This places an element at an absolute position relative to its nearest positioned ancestor.
Layout Properties:
display: Defines how an element is rendered (block, inline, flex, grid, etc.).
position: Specifies how an element is positioned (static, relative, absolute, fixed, sticky).
margin & padding: Control spacing around and inside elements.
width & height: Set the dimensions of elements.