IMPORTANT CSS PROPERTIES
Basics
width
: Sets the width of an element's content area.height
: Sets the height of an element's content area.margin
: Specifies the margin space around an element. It clears an area outside the border.padding
: Specifies the padding space inside an element. It clears an area around the content.border
: Specifies the width, style, and color of an element's border.box-sizing
: Defines how the total width and height of an element are calculated, including padding and border.
Layout and Box Model:
display
: Specifies how an element is displayed (e.g.,block
,inline
,inline-block
,flex
,grid
).position
: Determines the positioning method used for an element (e.g.,static
,relative
,absolute
,fixed
).float
: Specifies whether an element should float to the left or right of its container.clear
: Specifies which sides of an element other floating elements are not allowed.
Typography:
font-family
: Specifies the font(s) used for text content.font-size
: Sets the size of the font.font-weight
: Sets the thickness of the font.line-height
: Sets the height of a line of text.text-align
: Specifies the horizontal alignment of text content.text-decoration
: Adds visual emphasis to text content (e.g.,underline
,line-through
,none
).
Color and Background:
color
: Sets the text color.background-color
: Sets the background color of an element.background-image
: Specifies one or more background images for an element.
Borders and Box Model:
border
: Specifies the width, style, and color of an element's border.border-radius
: Sets the radius of the element's corners.margin
: Specifies the margin space around an element.padding
: Specifies the padding space inside an element.
Flexbox and Grid:
flex
: Combines flex-grow, flex-shrink, and flex-basis properties to set the flexibility of a flex item.justify-content
: Aligns flex items along the main axis of the flex container.align-items
: Aligns flex items along the cross axis of the flex container.grid-template-columns
,grid-template-rows
: Defines the columns and rows of a grid layout.grid-column
,grid-row
: Specifies the grid lines on which an item will be placed.
Miscellaneous:
z-index
: Specifies the stack order of an element.opacity
: Sets the opacity level for an element.transition
: Defines transition effects for an element.box-shadow
: Adds shadow effects to an element's box.
Application
Layout and Box Model:
- DISPLAY
display: block;
display: inline;
display: inline-block;
display: flex;
display: grid;
display: block;
:Elements with
display: block;
take up the full width available, by default, and create a new line before and after the element.Common block-level elements include
<div>
,<p>
,<header>
,<footer>
,<section>
, etc.Block-level elements can have width, height, margin, padding, and border properties applied to them.
display: inline;
:Elements with
display: inline;
do not start on a new line. They flow with surrounding content and only take up as much width as necessary.Common inline elements include
<span>
,<a>
,<strong>
,<em>
, etc.Inline elements cannot have width, height, margin-top, margin-bottom, and padding-top, padding-bottom applied to them.
display: inline-block;
:Combines features of both
inline
andblock
. It allows the element to have a specified width and height, and it also respects top and bottom margins and padding.Elements with
display: inline-block;
are positioned like inline elements but behave like block elements concerning their content.Useful for creating elements like navigation menus or grid items that need to be displayed in a line while retaining block-like properties.
display: flex;
:Implements a flexible box model layout. It allows elements within a container to be dynamically arranged horizontally or vertically.
Child elements of a flex container become flex items and can be aligned and distributed in various ways using properties like
justify-content
,align-items
, andflex-direction
.Enables powerful layouts such as responsive navigation bars, flexible card layouts, and evenly spaced item distribution.
display: grid;
:Introduces a two-dimensional grid-based layout system. It divides a page into columns and rows, allowing content to be placed in cells.
Provides precise control over layout, alignment, and spacing of elements within the grid.
Properties like
grid-template-columns
,grid-template-rows
,grid-column
,grid-row
, etc., are used to define and position grid items.Offers versatility and simplicity in creating complex layouts, such as magazine-style designs or data tables.
POSITION
position: static; position: relative; position: absolute; position: fixed; position: static;
position: static;
This is the default position value. Elements with
position: static;
are positioned according to the normal flow of the document.Top, right, bottom, left, and z-index properties do not affect static elements.
position: relative;
Elements with
position: relative;
are positioned relative to their normal position in the document flow.When an element is given
position: relative;
, you can move it using the top, right, bottom, and left properties.The original space occupied by the element is preserved even after it's moved, which may affect the layout of surrounding elements.
position: absolute;
Elements with
position: absolute;
are positioned relative to the nearest positioned ancestor (an ancestor element with a position value other than static) or to the initial containing block if no positioned ancestor is found.They are removed from the normal document flow, meaning they don't affect the position of surrounding elements.
Use top, right, bottom, and left properties to specify the exact position of the element relative to its containing block.
If no ancestor has a position other than static, an absolutely positioned element is positioned relative to the initial containing block (usually the viewport).
position: fixed;
Elements with
position: fixed;
are positioned relative to the viewport, meaning they always stay in the same place even when the page is scrolled.Fixed elements are removed from the normal document flow, similar to absolutely positioned elements, and don't affect the layout of surrounding elements.
Like absolutely positioned elements, use top, right, bottom, and left properties to specify the exact position of the fixed element relative to the viewport.
position: sticky;
Elements with
position: sticky;
behave like a relatively positioned element within its container until it reaches a specified point (defined by thetop
,right
,bottom
, orleft
properties), at which point it becomes "stuck" and remains fixed at that position as the user scrolls.Sticky positioning is often used for headers or navigation bars to keep them visible at the top of the viewport as the user scrolls down the page.
FLOAT
float: left;
float: right;
float: none;
float: left;
When an element is floated to the left, it moves to the leftmost edge of its containing element and content flows around it on the right side.
Other block-level elements will wrap around the floated element's right side.
float: right;
When an element is floated to the right, it moves to the rightmost edge of its containing element, and content flows around it on the left side.
Other block-level elements will wrap around the floated element's left side.
float: none;
- This is the default value. Elements with
float: none;
do not float. They remain in the normal flow of the document, and other elements are not affected by them.
- This is the default value. Elements with
CLEAR
clear: none; clear: left; clear: right; clear: both; clear: inline-start; clear: inline-end;
clear
Property:
Syntax:
clear: none | left | right | both | inline-start | inline-end;
Values:
none
: The element is not moved below any floated elements. This is the default value.left
: The element is moved below preceding floated elements that are floated to the left.right
: The element is moved below preceding floated elements that are floated to the right.both
: The element is moved below both left and right floated elements.inline-start
: The element is moved below preceding floated elements, based on the inline direction of the document (e.g., left for LTR languages, right for RTL languages).inline-end
: The element is moved below preceding floated elements, opposite to the inline direction of the document.