1. CSS Syntax β
- CSS is used to style HTML elements using selectors and declarations.css
selector { property: value; }
2. Ways to Apply CSS β
Inline CSS:
styleattribute in HTML tags.html<p style="color: red;">This is red text.</p>Internal CSS:
<style>tag in the<head>section of HTML.html<style> p { color: blue; } </style>External CSS: A separate
.cssfile linked with<link>tag.html<link rel="stylesheet" href="styles.css" />
3. Common CSS Selectors β
Element Selector: Targets all instances of an element.
cssp { color: green; }Class Selector: Targets elements with a specific class (
.).css.class-name { font-size: 16px; }ID Selector: Targets an element with a unique ID (
#).css#unique-id { background-color: yellow; }Universal Selector: Selects all elements (
*).css* { margin: 0; padding: 0; }
4. CSS Properties β
Text & Font β
color: Text color.font-size: Size of text.font-family: Font type.font-weight: Boldness (normal,bold,bolder,lighter).text-align: Align text (left,right,center,justify).line-height: Space between lines.text-transform: Uppercase, lowercase, capitalize text.text-decoration: Underline, overline, line-through.
Box Model β
width,height: Set size of elements.margin: Space outside an element.padding: Space inside an element (around content).border: Border around an element.box-sizing: Includes padding and border in elementβs total width/height.cssdiv { width: 100px; padding: 10px; box-sizing: border-box; }
Background β
background-color: Element background color.background-image: Image as background.background-repeat: Repeat image (repeat,no-repeat,repeat-x,repeat-y).background-size: Size of background image (cover,contain).
Positioning β
position: Element positioning (static,relative,absolute,fixed,sticky).top,right,bottom,left: Position relative to its container.z-index: Stacking order for overlapping elements.
Display & Visibility β
display: How an element is displayed (block,inline,inline-block,none).visibility: Hides element without removing space (visible,hidden).
Flexbox β
display: flex: Turns container into a flex container.justify-content: Align items horizontally (flex-start,center,space-between).align-items: Align items vertically (flex-start,center,stretch).flex-direction: Set direction of flex items (row,column).css.container { display: flex; justify-content: center; align-items: center; }
CSS Grid β
display: grid: Turns container into a grid container.grid-template-columns,grid-template-rows: Define column/row sizes.grid-gap: Spacing between grid items.css.grid-container { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
5. Media Queries β
- Media queries allow for responsive design by applying styles based on screen size or device type.
css
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}- Common Breakpoints:
- Mobile:
@media (max-width: 600px) - Tablet:
@media (max-width: 768px) - Desktop:
@media (min-width: 1024px)
- Mobile:
6. Pseudo-Classes and Pseudo-Elements β
Pseudo-Classes β
- Target elements based on state (hover, focus, etc.).css
a:hover { color: red; }
Pseudo-Elements β
- Target specific parts of an element (first letter, first line, etc.).css
p::first-letter { font-size: 2em; color: red; }
7. CSS Units β
- Absolute Units:
px,cm,mm(fixed sizes). - Relative Units:
%,em,rem,vh,vw(relative to parent or viewport).
8. Animations & Transitions β
Transitions β
- Smoothly animate changes between styles.css
div { transition: background-color 0.3s ease; }
Keyframe Animations β
- Define complex animations with keyframes.css
@keyframes move { 0% { left: 0px; } 100% { left: 100px; } } div { animation: move 2s infinite; }
9. CSS Properties for Layout β
float: Floats elements left or right.clear: Prevents elements from wrapping around floated elements.overflow: Handles overflow (visible,hidden,scroll,auto).
10. CSS Variables β
- Store reusable values in CSS.css
:root { --main-color: #3498db; } div { color: var(--main-color); }
