Skip to content
DevNursery.com - New Web Developer Docs
GitHub

CSS Cheatsheet

Properties

CSS PropertyPurposeExample
colorSets the text colorcolor: #FF5733;
font-sizeSets the font sizefont-size: 16px;
font-familyDefines the font familyfont-family: Arial, sans-serif;
text-alignAligns text within an elementtext-align: center;
background-colorSets the background colorbackground-color: #F0F0F0;
marginSets the margin around an elementmargin: 10px;
paddingSets the padding inside an elementpadding: 20px;
borderDefines the border of an elementborder: 1px solid #000;
widthSets the width of an elementwidth: 300px;
heightSets the height of an elementheight: 200px;
displaySpecifies how an element is displayeddisplay: block;
positionControls the positioning of an elementposition: absolute;
z-indexSets the stacking order of elementsz-index: 1;
opacityAdjusts the transparency of an elementopacity: 0.7;
box-shadowAdds a shadow to an elementbox-shadow: 3px 3px 5px #888888;
text-decorationAdds or removes text decorationstext-decoration: underline;
text-transformControls text capitalizationtext-transform: uppercase;
line-heightSets the height of a line of textline-height: 1.5;
letter-spacingAdjusts the spacing between charactersletter-spacing: 2px;
text-shadowAdds a shadow to texttext-shadow: 2px 2px 4px #000000;
border-radiusRounds the corners of an elementborder-radius: 10px;
box-sizingControls box model calculationsbox-sizing: border-box;
floatFloats an element left or rightfloat: left;
clearClears floated elementsclear: both;
list-styleSets style for list itemslist-style: disc inside;
text-overflowSpecifies how overflowed text is showntext-overflow: ellipsis;
overflowControls how content overflowsoverflow: scroll;
cursorDefines the mouse cursorcursor: pointer;
background-imageSets a background imagebackground-image: url('image.jpg');
background-sizeSets the size of background imagesbackground-size: cover;
transitionCreates smooth transitionstransition: opacity 0.5s ease-in;
transformApplies 2D or 3D transformationstransform: rotate(45deg);
text-alignAligns text within an elementtext-align: right;
vertical-alignVertically aligns inline elementsvertical-align: middle;
outlineAdds an outline around an elementoutline: 2px dashed #00FF00;
box-decoration-breakControls how box decorations are split across multiple linesbox-decoration-break: clone;
font-weightSets the font weightfont-weight: bold;
font-styleSets the font stylefont-style: italic;
font-variantControls font variantsfont-variant: small-caps;
text-indentSets the indentation of the first line of texttext-indent: 20px;
line-heightSets the line heightline-height: 1.5;
list-style-typeDefines the list item marker stylelist-style-type: square;
list-style-positionSpecifies where the list item marker should be placedlist-style-position: inside;
border-collapseControls table border collapsing behaviorborder-collapse: collapse;
table-layoutDefines the algorithm for table layouttable-layout: fixed;
text-align-lastAligns the last line of text in a block elementtext-align-last: justify;
word-spacingAdjusts the spacing between wordsword-spacing: 2px;
animationDefines animations for elementsanimation: slide 2s ease infinite;
transitionCreates smooth transitionstransition: opacity 0.5s ease-in;
overscroll-behaviorControls the behavior of scrolling beyond the boundsoverscroll-behavior: auto;
transform-originSets the origin for transformationstransform-origin: center;
will-changeHints to the browser for optimizationwill-change: transform;
backdrop-filterApplies filter effects to the area behind an elementbackdrop-filter: blur(5px);
caret-colorSets the color of the text input caretcaret-color: blue;
box-reflectCreates reflection effects for elementsbox-reflect: below 0;

Selectors

CSS SelectorPurposeExample
elementSelects elements by their typep { color: blue; }
.classSelects elements by their class.btn { background: #FF5733; }
#idSelects elements by their id#header { font-size: 24px; }
*Selects all elements* { margin: 0; padding: 0; }
selector1, selector2Selects multiple elementsh1, h2 { font-family: 'Arial'; }
element.classSelects elements with a specific classdiv.error { color: red; }
element#idSelects an element with a specific idinput#username { border: 1px solid #ccc; }
element[attr]Selects elements with a specific attributea[href] { text-decoration: none; }
element[attr=value]Selects elements with a specific attribute and valueinput[type="text"] { width: 200px; }
element:first-childSelects the first child of its parentul li:first-child { font-weight: bold; }
element:last-childSelects the last child of its parentul li:last-child { margin-bottom: 10px; }
element:nth-child(n)Selects the nth child of its parentul li:nth-child(odd) { background-color: #f0f0f0; }
element:nth-last-child(n)Selects the nth child of its parent, counting from the lastul li:nth-last-child(3) { font-style: italic; }
element:visitedSelects visited linksa:visited { color: purple; }
element:hoverSelects elements when hovered overbutton:hover { background-color: #444; }
element:focusSelects elements when focusedinput:focus { border-color: #0078d4; }
element::beforeSelects an element’s before pseudo-elementp::before { content: ">>"; }
element::afterSelects an element’s after pseudo-elementblockquote::after { content: " - Anonymous"; }
element:not(selector)Selects elements that do not match the selectordiv:not(.special) { opacity: 0.8; }

Measurements

CSS MeasurementPurposeExample
pxPixels (1/96th of 1 inch)font-size: 16px;
emRelative to the font size of the parent elementmargin: 1em;
remRelative to the font size of the root element (html)font-size: 1.2rem;
vhRelative to 1% of the viewport heightheight: 50vh;
vwRelative to 1% of the viewport widthwidth: 30vw;
%Percentage relative to the parent elementwidth: 50%;
cmCentimeterswidth: 5cm;
mmMillimetersmargin: 10mm;
inInchesheight: 3in;
ptPoints (1/72 of an inch)font-size: 12pt;
pcPicas (1 pica = 12 points)line-height: 1.5pc;
exRelative to the x-height of the current fontwidth: 3ex;
chRelative to the width of the “0” (zero) characterpadding: 2ch;
vminRelative to the smaller of viewport width and heightwidth: 20vmin;
vmaxRelative to the larger of viewport width and heightheight: 15vmax;
frFraction unit for CSS Grid Layoutgrid-template-columns: 1fr 2fr;
degDegrees for rotation and anglestransform: rotate(45deg);
radRadians for rotation and anglestransform: rotate(0.7854rad);
gradGradians for rotation and anglestransform: rotate(50grad);