Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a document written in HTML or XML. CSS describes how elements should be rendered on screen, on paper, in speech, or on other media.
A CSS rule consists of a selector and a declaration block:
selector {
property: value;
}
Example:
p {
color: red;
}
Selectors are used to target the HTML elements you want to style.
*
)element
)#id
).class
)[attribute]
):pseudo-class
)::pseudo-element
)selector1, selector2
)Colors can be defined using names, HEX values, RGB, RGBA, HSL, and HSLA.
color: red; /* Name */
color: #ff0000; /* HEX */
color: rgb(255, 0, 0); /* RGB */
color: rgba(255, 0, 0, 0.5); /* RGBA */
color: hsl(0, 100%, 50%); /* HSL */
color: hsla(0, 100%, 50%, 0.5); /* HSLA */
background-color
background-image
background-repeat
background-attachment
background-position
background-size
background
body {
background-color: lightblue;
background-image: url('background.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: cover;
}
border
border-width
border-style
border-color
border-radius
div {
border: 2px solid black;
border-radius: 10px;
}
Margins are used to create space around elements, outside of any defined borders.
margin-top
margin-right
margin-bottom
margin-left
margin
div {
margin: 20px;
}
Padding is used to create space around an element’s content, inside of any defined borders.
padding-top
padding-right
padding-bottom
padding-left
padding
div {
padding: 20px;
}
height
width
max-height
max-width
min-height
min-width
div {
height: 100px;
width: 100px;
}
The CSS box model describes the rectangular boxes that are generated for elements in the document tree and laid out according to the visual formatting model.
content
padding
border
margin
div {
width: 300px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}
color
text-align
text-decoration
text-transform
text-indent
letter-spacing
line-height
text-shadow
word-spacing
p {
color: blue;
text-align: center;
text-decoration: underline;
text-transform: uppercase;
letter-spacing: 2px;
line-height: 1.5;
}
font-family
font-style
font-size
font-weight
font-variant
font
p {
font-family: Arial, sans-serif;
font-size: 16px;
font-weight: bold;
font-style: italic;
}
Icons can be added using various icon libraries like Font Awesome.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<i class="fas fa-camera"></i>
a:link
a:visited
a:hover
a:active
a:link {
color: blue;
}
a:visited {
color: purple;
}
a:hover {
color: red;
}
a:active {
color: orange;
}
list-style-type
list-style-position
list-style-image
list-style
ul {
list-style-type: square;
list-style-position: inside;
}
border-collapse
border-spacing
caption-side
empty-cells
table-layout
table {
border-collapse: collapse;
}
td, th {
border: 1px solid black;
padding: 8px;
}
none
block
inline
inline-block
flex
grid
div {
display: block;
}
static
relative
absolute
fixed
sticky
div {
position: absolute;
top: 50px;
left: 100px;
}
The z-index
property specifies the stack order of an element.
div {
position: absolute;
z-index: 1;
}
visible
hidden
scroll
auto
div {
overflow: scroll;
}
left
right
none
left
right
both
none
div {
float: left;
}
div.clear {
clear: both;
}
Flexbox is a layout model that provides a more efficient way to lay out, align, and distribute space among items in a container.
display: flex;
flex-direction
justify-content
align-items
align-content
flex-wrap
flex-grow
flex-shrink
flex-basis
flex
order
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
CSS Grid Layout is a two-dimensional layout system for the web.
display: grid;
grid-template-columns
grid-template-rows
grid-gap
grid-column
grid-row
grid-area
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
}
Media queries are used to apply different styles for different devices or screen sizes.
@media screen and (max-width: 600px) {
body
{
background-color: lightblue;
}
}
CSS transitions allow you to change property values smoothly (over a given duration).
transition-property
transition-duration
transition-timing-function
transition-delay
transition
div {
transition: width 2s;
}
div:hover {
width: 200px;
}
CSS animations allow you to animate transitions from one CSS style configuration to another.
@keyframes
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
div {
animation-name: example;
animation-duration: 4s;
}
CSS transforms allow you to rotate, scale, skew, or translate an element.
transform
transform-origin
div {
transform: rotate(45deg);
}
Pseudo-classes are used to define the special state of an element.
a:hover {
color: red;
}
Pseudo-elements are used to style specified parts of an element.
p::first-line {
color: blue;
}
CSS variables are entities defined by CSS authors that contain specific values to be reused throughout a document.
:root {
--main-color: blue;
}
p {
color: var(--main-color);
}
CSS has several built-in functions, such as calc()
, var()
, rgb()
, hsl()
, and others.
div {
width: calc(100% - 50px);
}
Responsive design makes web pages render well on a variety of devices and window or screen sizes.
img {
max-width: 100%;
height: auto;
}
@media screen and (max-width: 600px) {
.container {
flex-direction: column;
}
}
This tutorial provides a comprehensive overview of CSS, from basic concepts to advanced techniques. By mastering these topics, you’ll be able to create visually appealing and responsive web pages.
https://youtu.be/ESnrn1kAD4E?si=Z2gX5J0jf7CiBQbI