CSS Cheatsheet
2/18/2019
As web developers, we often use pure css or css-based technologies (sass/less/css-in-js). However, often we forget some of css's more intricate rules and gotchas. Below is a comprehensive css cheatsheet that you can view every so often as a refresher:
Selectors
Selector types:
-
Element:
elementName {...rules}(i.e. "p" or "ul") - Class:
.className {...rules} - Id:
#idName {...rules} -
Other attributes:
[attributeName="attributeValue"] {...rules}
Specificity (from most to least)
!importanttag- Inline style
- Id (1,0,0)
- Class (0,1,0)
- Element (0,0,1)
Chained selectors:
-
element element- any descendent (ie.container h1) -
element > element- any direct descendent (ieul.coolList > li) -
.class.class- any element with all specified classed (ieul.coolList.highlight) -
selector, selector- both selectors (ie.coolThing, .highlight) -
selector + selector- adjacent following sibling -
selector ~ selector- general following sibling
Pseudo classes
-
links:
-
a:link- targets hyperlinks that have not yet been visited -
a:visited- targets hyperlinks that have been visited a:hover- targets hyperlinks that are hovered overa:active- targets hyperlinks that are clicked on
-
forms
:focus- targets input that has focus-
:disabled/:enabled- targets input that has been disabled/enabled -
:checked- targets checkbox that has been checked -
:invalid/:valid- targets input that has an invalid/valid state -
:required/optional- targets input that is required/optional
-
positional:
-
:first-child - first child in relation to parent
(ie
li:first-child) -
:last-child - last child in relation to parent (ie
li:last-child) -
:nth-child(2) - child at index in relation to
parent (ie
li:nth-child(2)) -
:nth-child(2n) - all child elements at even
position (ie
li:nth-child(2n)) -
:nth-child(2n) - all child elements at odd position
(ie
li:nth-child(2n)) - :before {content:"before"} - adds content before element
- :after {content:"after"} - adds content after element
-
:first-child - first child in relation to parent
(ie
-
- For the same specificity, the last rule in the source wins.
Applied Inheritance
initialsets the value to the browser's default.-
inheritsets the value to whatever is for parent element.
The Box Model
- All elements can be considered "boxes". The layers of the box are (from outer to inner): margin, border, padding, content
- By default, padding, border and width/height affect the total width/height of an element
-
box-sizingattribute-
border-box: padding and margin fall within width, so total width is always same as width -
content-box: padding and margin fall within width, so total width is sum of all 3, (content-box is the default)
-
Units
-
em: based on elementfont-size. If font-size is 30px, 1em is 30px -
rem: based on defaultfont-size. -
%(forfont-size): based on percentage of default font-size
Colors
- hex,rgb and hsl
Backgrounds
-
background-size:-
cover: scales the background image as much as possible without skewing -
background-attachment: can change positioning of background image
-
Hiding/showing elements
opacity:0;- hidden but still takes up spacevisibility:hidden;- hidden but still takes up spacedisplay:none;- hidden and removed from layout
Positioning Elements
-
position:relative;: can move element from original position (i.e.top: 20px; -
position:absolute;: element is taken out of the flow of the page. Elements positioned absolutely will be relative to first parent element withposition:relative; -
position:fixedalways relative to the document, and maintains position while scrolling
Media Queries:
-
@media(max-width: 740px){..rules}: applies rules based on viewport constraints
display property
-
inline- lays out horizontally, width/height defined by content only, no margin/padding top/bottom (but can do margin/padding left/right) -
inline-block- lays out horizontally, width/height can be defined, can add margin/padding top/bottom -
block- lays out vertically (unless floated), width/height/margin/padding can be defined for all sides -
flex- behaves asblock, but applies flex layout to children -
inline-flex- behaves asinline-block, but applies flex layout to children
Flexbox
-
Applying
- Use
display:flex
- Use
-
flex-direction-
row- lays children out horizontally, left to right, (this is the default) -
row-reverse- lays children out horizontally, right to left column- lays children out vertically, top to bottomcolumn-reverse- lays children out bottom to top
-
-
order- Defaults to 0
-
For
flex-direction:columnthe higher the order the lower it will appear, if order value is the same, then html position will take effect -
For
flex-direction:rowthe higher the order the farther right it will appear, if order value is the same, then html position will take effect
-
justify-content- aligns the content in the alongflex-direction-
flex-start- aligns the content to the start (top forflex-direction:columnand left forflex-direction:row, (this is the default) -
flex-start- aligns the content to the start (top forflex-direction:columnand left forflex-direction:row, (this is the default) -
flex-end- aligns the content to the end (bottom forflex-direction:columnand right forflex-direction:row -
center- aligns the content to the "center" (middle forflex-direction:columnand center forflex-direction:row -
space-between- aligns first element to start, last element to end and all other children spaced out evenly space-around- spaces out all elements evenly
-
-
align-items- aligns the content in the perpendicular toflex-direction-
center,start,end- same as injustify-content stretch- expands elements to fit container-
baseline- lines up elements to the first line of text in first element
-
-
align-self- can be applied to an individual child element (same values asalign-items) -
flex-basis- controls the size of an element along theflex-direction(width forflex-direction:rowand height forflex-direction:column) -
flex-growandflex-shrink- controls how the element takes up extra space
-
it is relative, so for 3 elements, width
flex-growvalues 1, 2 and 3 respectively, element 1 will take up 1/3 space, element 2 2/3 of space and element 3 gets 3/6 of the space - talk about default
-
flex-grow:0 - means element won't grow beyond its
flex-basis
-
flex: flex-grow flex-shrink flex-basis- this is the shorthand -
flex-wrap-
nowrap- elements don't wrap, even if overflowing the container -
wrap- elements do wrap, if overflowing the container -
wrap-reverse- elements do wrap in direction of bottom to top, if overflowing the container
-
Transitions
-
transition-property: which properties to animate -
transition-duration: how long the animation lasts -
transition-timing-function: how the intermediate states are calculated -
transition-delay: to start the animation after a certain amount of time -
transition: transition-property transition-duration transition-timing-function transition-delay- the shorthand