Position
The position property determines how an element is positioned in the document. Options include static (default), relative, absolute, fixed, and sticky. This property allows elements to be precisely placed relative to their normal position, parent, or the viewport.
position:relative - Setting the top, right,bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position
div.relative {
position: relative;
left: 30px;
} /* The element will appear left indented by 30px */position: absolute - the element is positioned relative to the nearest positioned ancestor(i.e. the nearest ancestor with position set torelative, absolute, or sticky)
position: absoluteelements are removed from the normal flow of the document and can overlap.
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
} /* The element will be below the top by 80px and right align relative to its ancestor element */position: fixed - the element is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled.
position: fixedelements are removed from the normal flow of the document and can overlap.
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
} /* The element is fixed to the bottom and right and moves along with page scrolling */position: sticky - the element is a combination of relative and fixed positioning. It behaves like a relatively positioned element until the user scroll to a certain point, at which point it becomes fixed.
position: stickyelements are NOT removed from the normal flow of the document, unlikeposition: fixed
div.sticky {
position: sticky;
top: 50px;
right: 0;
width: 300px;
} /* Once the user scroll down enough, the element will maintain a fixed position of below the top by 50px while sticking to the right edge */Notes
- % for
top|left|right|bottomare relative to parent's dimensions
Examples: www.w3schools.com/css/css_positioning.asp
Z-Index
z-index: 1000;- Define the draw order/ stack order of the element
- Higher number means the element is on top/ in front
- z-index accepts negative numbers
Display
The display property affects how the element flow in the layout. Common values includeinline, block, flex, inline-block, gridand none.
- The property
visibility: hiddenis also useful. column-count: xshould NOT be used withflexandgrid
More Examples: www.w3schools.com/css/css_display_visibility.asp
display: block|inline|inline-block|flex|grid|none;
visibility: hidden;
column-count: x;Width & Height
width: length|%;
height: length|%;
min-width: length|%;
max-width: length|%;
min-height: length|%;
max-height: length|%;- % value is relative to the parent container
- Setting
width: 100%;andmax-width: 1440px;on the container is very useful for responsive layout min-width: useful for preventing the content-container from getting too narrow relative to other containers
Spacing
white-space: normal|nowrap|pre: specifies how white-space and text wrapping inside elements are handledmargin: length: sets the space outside the element's borderpadding: length: sets the space between the content and border of the elementgap: length: shorthand forrow-gapandcolumn-gap; sets the space between the children elements- Only used for multi-column,
flex, andgridelements - In multi-column,
gapdefines the gaps between columns - In
flex,gapdefines the space between flex items and flex lines - In
grid,gapdefines the gaps between rows and columns box-sizing: border-box: includes the border when calculating an element's total width and height- Extremely useful for setting up layout and makes it easier to calculate width and height
- With
box-sizing: border-box, the width of 2 columns element can be set to 50% - Without
box-sizing: border-box, the width of 2 columns element has to be 48% to account for the padding and border
* {
box-sizing: border-box;
}
margin: length;
padding: length;
gap: length;Scrolling
overflow-x: visible|hidden|scroll|auto;
overflow-y: visible|hidden|scroll|auto;visible(default): the content is not clipped and it may be rendered outside the content boxhidden: the content is clipped, but no scrolling mechanism is given
Background
/* background property is shorthand form */
background: none;
background-color: color|transparent;
background-image: url("image.jpg")|none;
background-position: left top|right|center|...|center bottom|x% y%;
background-size: auto|cover|contain|width height;
background-clip: border-box|padding-box|content-box;
background-attachment: scroll|fixed|local;
background-repeat: repeat|repeat-x|repeat-y|no-repeat;
background-origin: padding-box|border-box|content-box:background-clip: specifies how far the background should extend within elementborder-box: extends behind the border
background-attachment: sets whether the background image will scrolls with the pagebackground-origin: specifies the starting point of the background imageborder-box: within the border area of the box
Examples: www.w3schools.com/cssref/css3_pr_background.php
Border
border-radius: 1-4 length|%;
box-shadow: none|h-offset v-offset blurRadius spreadRadius color;/* border property is a shorthand property, but not for border-radius */
border: border-width border-style border-color;
border-width: medium|thin|thick|length;
border-style: none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset;
border-color: color|transparent;Font
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
font-weight: bold|normal|lighter|bolder|900;
font-style: italic|normal|oblique|oblique 40deg;bolder and lighter values are relative to the parent's.
Examples: developer.mozilla.org/en-US/docs/Web/CSS/font-weight
Text
/* Text color */
color: red;
/* Text spacing */
white-space: nowrap|pre|normal;text-align: left|right|center|justify;
text-transform: none|capitalize|uppercase|lowercase;
text-shadow: h-shadow v-shadow blurRadius color|none;/* text-decoration is the shorthand property */
text-decoration: text-decoration-line text-decoration-color text-decoration-style text-decoration-thickness;
text-decoration-color: color;
text-decoration-line: none|underline|overline|line-through;
text-decoration-style: solid|double|dotted|dashed|wavy;
text-decoration-thickness: auto|from-font|length/percentage;Notes for Wrapping Text
white-space: pre|pre-wrap|pre-linepreserve white-space and act like the HTML tag<pre>pre: preserve all spaces and line breaks without wrapping text similar to <pre> blockpre-wrap: preserves spaces and line breaks, but also wraps textpre-line: preserves line breaks, but collapses extra spaces and wraps textoverflow-wrap: normal|anywhere|break-worddetermines how browser should break up long text that doesn't contain spaces
Inherit & Initial Values
The inherit and initial values can be used on almost any properties.
inherit: the property will inherit its value from its parent elementinitial: return the property to its default value
background-color: inherit;
color: initial;Transition
/* transition property is the shorthand form */
transition: property duration timing-function delay;
transition-property: none|all|property;
transition-duration: time;
transition-timing-function: linear|ease|ease-in|ease-out|ease-in-out|step-start|step-end|steps(int,start|end)|cubic-bezier(n,n,n,n);
transition-delay: time;transition-property: specifies the name of the CSS property the transition effect is for (default is all)transition-duration: determines how many seconds (s) or milliseconds (ms) the effect takes to completetransition-timing-function: sets the speed curve of the transition effect, allow changes in speed over its durationease(default): slow start, then fast, and finally end slowlylinear: same speed from start to finishease-in: slow startease-out: slow endtransition-delay: determines how many seconds (s) or milliseconds (ms) before the transition starts
// Useful transition values
.nav-menu {
transition: transform 0.3s ease;
OR
transition: transform 0.3s ease, opacity 0.3s ease;
}Examples:
transition: 0.3s easeis useful for menu transitions (0.3s duration and ease timing-function)- Do NOT use
display: nonealong withtransitionbecause it prevents the animation from running - Instead, use
opacity: 0to hide the element andopacity: 1to show it
More Examples: www.w3schools.com/css/css3_transitions.asp
Transform
transform: none|transform-functions;Transform Functions:
translateX(x): move across x-axistranslateX(-50%): move element to the left by 50% of its own width, NOT its parenttranslateY(Y): move across y-axistranslate(x,y): move element across x- and y-axistranslate3d(x,y,z): allow 3D translationscale(x): scale in the x- and y-directions by the same amountscale(1.5): make element 1.5x largerscale(150%): make element 1.5x largerscale(x,y): scale in the x- and y-directionsrotate(angle): rotate element by angle
For more transform functions: www.w3schools.com/cssref/css3_pr_transform.php
Examples:
.card-image:hover {
transition: transform 0.3s ease;
transform: scale(1.03);
}scale(x)can be used for more interactive image gallery
.leftNavMenu {
display: none;
transition: transform 0.3s ease;
transform: translateX(-100%);
}
.leftNavMenu.open {
display: fixed;
top: 0;
transform: translateX(0);
}translateX(-100%)hides the menu to the left by 100% of its width (whole menu)translateX(0)moves the menu from -100% to 0, the left edge of the viewport
.centerElementHorizontally {
position: fixed;
left: 50%;
transform: translateX(-50%);
}left: 50%moves the left edge of the element to the right by 50% of its parent's widthtranslateX(-50%)moves the element to the left by 50% of its width; effectively centering it horizontally