Section 1: CSS Basics
CSS Syntax
The basic syntax for CSS is straightforward:
css Copy
selector {
property: value;
}
Selectors
CSS selectors are patterns used to select elements to style:
Element selector: p
Class selector: .classname
ID selector: #idname
Attribute selector: [attribute]
Descendant selector: div p
Child selector: div > p
Adjacent sibling selector: h1 + p
General sibling selector: h1 ~ p
Universal selector: *
Grouping selector: h1, p, .classname
Pseudo-class selector: a:hover
Pseudo-element selector: p::before
Attribute selector with value: input[type="text"]
css Copy
/* Element Selector: Targets all <p> elements */
p { color: pink }
/* Class Selector: Targets all elements with class "classname" */
.classname { color: pink }
/* ID Selector: Targets the element with ID "idname" */
#idname { color: pink }
/* Attribute Selector: Targets elements with the specified attribute */
[ attribute ] { color: pink }
/* Descendant Selector: Targets <p> elements inside <div> elements */
div p { color: pink }
/* Child Selector: Targets <p> elements that are direct children of <div> elements */
div > p { color: pink }
/* Adjacent Sibling Selector: Targets <p> elements immediately following an <h1> element */
h1 + p { color: pink }
/* General Sibling Selector: Targets <p> elements that follow <h1> elements (not necessarily immediately) */
h1 ~ p { color: pink }
/* Universal Selector: Targets all elements */
* { color: pink }
/* Grouping Selector: Targets all elements specified (comma-separated) */
h1 , p , .classname { color: pink }
/* Pseudo-class Selector: Targets elements in a specific state (e.g., :hover) */
a :hover { color: pink }
/* Pseudo-element Selector: Targets a specific part of an element (e.g., ::before) */
p ::before { color: pink }
/* Attribute Selector with Value: Targets elements with a specific attribute value */
input [ type = "text" ] { color: pink }
Use comments to annotate your CSS code:
css Copy
/* This is a CSS comment */
Section 2: Box Model
The box model is fundamental in CSS layout and design:
Width and Height
css Copy
width: auto | <length > | <percentage > | max-content | min-content | fit-content | fill-available
height: auto | <length > | <percentage > | max-content | min-content | fit-content | fill-available
min-width : <length > | <percentage > | auto | max-content | min-content | fit-content | fill-available
min-height : <length > | <percentage > | auto | max-content | min-content | fit-content | fill-available
max-width : <length > | <percentage > | none | max-content | min-content | fit-content | fill-available
max-height : <length > | <percentage > | none | max-content | min-content | fit-content | fill-available
Margin
css Copy
margin: <length > | <percentage > | auto
margin-top : <length > | <percentage > | auto
margin-right : <length > | <percentage > | auto
margin-bottom : <length > | <percentage > | auto
margin-left : <length > | <percentage > | auto
Padding
css Copy
padding: <length > | <percentage >
padding-top : <length > | <percentage >
padding-right : <length > | <percentage >
padding-bottom : <length > | <percentage >
padding-left : <length > | <percentage >
Border
css Copy
border: < border-width > < border-style > < border-color >
border-width : thin | medium | thick | <length >
border-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset
border-color : <color >
border-top : < border-width > < border-style > < border-color >
border-right : < border-width > < border-style > < border-color >
border-bottom : < border-width > < border-style > < border-color >
border-left : < border-width > < border-style > < border-color >
border-radius : <length > | <percentage >
border-image : <' border-image-source ' > || <' border-image-slice ' > [ / < 'border-image-width' > | / < 'border-image-width' >? / < 'border-image-outset' > ]? || <' border-image-repeat ' >
Box-Sizing
css Copy
box-sizing : content-box | border-box ;
Section 3: Typography
Control text appearance with these properties:
Font Properties
css Copy
font-family : < family-name > | < generic-family >
font-size : < absolute-size > | < relative-size > | <length > | <percentage >
font-weight : normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
font-style : normal | italic | oblique
font-variant : normal | small-caps
line-height : normal | <number > | <length > | <percentage >
Text Properties
css Copy
text-align : left | right | center | justify | justify-all
text-decoration : none | underline | overline | line-through
text-transform : none | capitalize | uppercase | lowercase | full-width
text-indent : <length > | <percentage >
letter-spacing : normal | <length >
word-spacing : normal | <length >
white-space : normal | nowrap | pre | pre-wrap | pre-line
word-break : normal | break-all | keep-all | break-word
word-wrap : normal | break-word
text-overflow : clip | ellipsis | <string >
Section 4: Colors and Backgrounds
Color
css Copy
color: <color > ;
Background
css Copy
background-color : <color >
background-image : url(<url >) | none
background-repeat : repeat | repeat-x | repeat-y | no-repeat
background-position : left | center | right | top | bottom | <percentage > | <length >
background-size : auto | cover | contain | <length > | <percentage >
background-attachment : scroll | fixed | local
background-clip : border-box | padding-box | content-box | text
background-origin : border-box | padding-box | content-box
Section 5: Layout
Display
css Copy
display: inline | block | inline-block | flex | inline-flex | grid | inline-grid
| none;
Position
css Copy
position: static | relative | absolute | fixed | sticky
top: <length > | <percentage > | auto
right: <length > | <percentage > | auto
bottom: <length > | <percentage > | auto
left: <length > | <percentage > | auto
z-index : auto | <integer >
Float
css Copy
float: left | right | none;
Clear
css Copy
clear: none | left | right | both;
Flexbox
css Copy
flex-direction : row | row-reverse | column | column-reverse
flex-wrap : nowrap | wrap | wrap-reverse
flex-flow : < flex-direction > || < flex-wrap >
flex: < flex-grow > < flex-shrink > < flex-basis >
flex-grow : <number >
flex-shrink : <number >
flex-basis : auto | <length > | <percentage >
align-items : stretch | flex-start | flex-end | center | baseline
align-self : auto | stretch | flex-start | flex-end | center | baseline
align-content : flex-start | flex-end | center | space-between | space-around | stretch
justify-content : flex-start | flex-end | center | space-between | space-around | space-evenly
Grid
css Copy
grid-template-columns : < track-size > ... | < line-name > < track-size > ...
grid-template-rows : < track-size > ... | < line-name > < track-size > ...
grid-template-areas : < grid-area-name > | . | none | ...
grid-column : <start > / < end >
grid-row : <start > / < end >
grid-column-gap : <length > | <percentage >
grid-row-gap : <length > | <percentage >
grid-gap : <length > | <percentage >
grid-auto-columns : < track-size > | auto
grid-auto-rows : < track-size > | auto
grid-auto-flow : row | column | dense
Section 6: Transitions
css Copy
transition: <property > <duration > < timing-function > <delay >
transition-property : <property > | all
transition-duration : < time >
transition-timing-function : ease | linear | ease-in | ease-out | ease-in-out | < cubic-bezier >
transition-delay : < time >
Section 7: Animations
css Copy
animation: <name > <duration > < timing-function > <delay > < iteration-count > <direction > < fill-mode > < play-state >
animation-name : <keyframes > | none
animation-duration : < time >
animation-timing-function : ease | linear | ease-in | ease-out | ease-in-out | < cubic-bezier >
animation-delay : < time >
animation-iteration-count : <number > | infinite
animation-direction : normal | reverse | alternate | alternate-reverse
animation-fill-mode : none | forwa | backward | both
animation-play-state : running | paused
Section 8: Responsive Design
Media Queries
css Copy
@media <media-feature> {
/* CSS rules */
}
@media (min-width: < length > ) {
/* CSS rules for devices with a minimum width */
}
@media (max-width: < length > ) {
/* CSS rules for devices with a maximum width */
}
@media (orientation: portrait | landscape ) {
/* CSS rules for portrait or landscape orientation */
}
Viewport Meta Tag
html Copy
< meta name = "viewport" content = "width=device-width, initial-scale=1" />
Section 9: Miscellaneous
Custom Properties (CSS Variables)
css Copy
:root {
--main-bg-color : #f0f0f0 ;
}
.element {
background-color: var ( --main-bg-color );
}
Clip Paths
css Copy
clip-path : <shape > | polygon(<points >) | circle(<radius > at <position >) |
ellipse(< radius-x > < radius-y > at <position >);
Object Fit
css Copy
object-fit : fill | contain | cover | none | scale-down ;
Transform
css Copy
transform: < transform-function > ...
transform-origin : <length > | <percentage >
Filter
css Copy
filter : none | < filter-function > .. .;
Outline
css Copy
outline: < outline-width > < outline-style > < outline-color >
outline-width : thin | medium | thick | <length >
outline-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset
outline-color : <color >
outline-offset : <length >
Resize
css Copy
resize: none | both | horizontal | vertical;
Visibility
css Copy
visibility: visible | hidden | collapse;
Clip
css Copy
clip: rect(<top > <right > <bottom > <left >);
Will-Change
css Copy
will-change : auto | <property > .. .;
Grid Template
css Copy
grid-template-columns : < track-size > ... | < line-name > < track-size > ...
grid-template-rows : < track-size > ... | < line-name > < track-size > ...
grid-template-areas : < grid-area-name > | . | none | ...
Conclusion
CSS is a powerful tool that empowers you to design and style your web pages with precision and creativity. From the basic building blocks of CSS syntax and selectors to the complexities of Flexbox and Grid layouts, mastering these properties and concepts will enable you to create visually appealing and responsive websites.