CSS Cheat Sheet: A Comprehensive Guide for Beginners and Professionals
A cheat sheet to save beginners a lot of time by providing you with a quick reference guide for common CSS properties and shorthands
Basic Syntax and Structure
CSS stands for Cascading Style Sheets and is used to style and layout HTML documents.
CSS rules are made up of selectors and declarations.
A selector identifies the elements to be styled, and a declaration consists of a property and a value.
Declarations are separated by a colon (:) and surrounded by curly braces {}.
Common Properties
font-family
: Specifies the font to be used for the textfont-size
: Specifies the size of the fontcolor
: Specifies the color of the textbackground-color
: Specifies the background color of an elementwidth
andheight
: Specify the width and height of an elementmargin
: Specifies the space around an elementpadding
: Specifies the space inside an elementborder
: Specifies a border around an elementdisplay
: Specifies the type of box an element should be displayed as (e.g. block, inline, inline-block)
Selectors
Element selectors: Select elements based on their tag name (e.g.
p
,div
,span
)Class selectors: Select elements based on their class attribute (e.g.
.warning
,.error
)ID selectors: Select elements based on their id attribute (e.g.
#header
,#footer
)Attribute selectors: Select elements based on their attributes (e.g.
a[href]
,input[type=text]
)
Cascade and Inheritance
CSS has a "cascade" of rules, where rules with higher specificity take precedence over rules with lower specificity
CSS also has "inheritance," where properties are inherited from parent elements to child elements
Box Model
Every element in an HTML document is treated as a box, with properties for content, padding, border, and margin
The width and height of an element refer to the content area, and the padding, border, and margin are added to the outside of the content area
Positioning
CSS provides several positioning schemes, including static, relative, absolute, and fixed
Static positioning is the default and positions an element according to the normal flow of the document
Relative positioning offsets an element from its normal position
Absolute positioning removes an element from the normal flow of the document and positions it relative to its nearest positioned ancestor (if any) or the initial containing block
Fixed positioning positions an element relative to the browser window and it stays fixed even when the page is scrolled
Floats
CSS allows elements to be floated to the left or right of the page
Floated elements are removed from the normal flow of the document and other elements will flow around them
Clearfix is a common technique used to contain floated elements
Grid Systems
CSS grid systems are used to create a structured layout using rows and columns
There are several popular grid systems available, including Bootstrap, Foundation, and Semantic UI
Responsive Design
CSS media queries are used to create responsive designs that adapt to different screen sizes and devices
Media queries can be used to apply different styles based on the width, height, resolution, and other features of the user's device
CSS3 Resources
There are various resources online to learn more about CSS. Some of the best resources include: