CSS
CSS Basics
<p style="font-size: 21px;">Hello</p>body { color:black; } <-- This is a rule. Consists of a selector (body, or .class or #id) and property:value declarationsletter-spacing is so small, these are usually done in px, like 1-2px<div class="a-box"> Use lowercase, use hyphens (not underscores or camelCase), numbers only if NOT first characterp can be put in body, so it also applies to everything that aren't in p (like a)section instead of div when it is understood to be a layout element with an h1, h2, etc title* {box-sizing: border-box;} - this changes the default (content-box) and allows setting width/height to include the padding and borders of the elementbody {margin: 0;}: Background colors, etc will not extend to the edges of the screen because browsers have built-in margin to avoid text starting on the edges. Turn it off at the top of your css, always for every site.h1, h2, h3, p { margin-top: 0; }: Do this for text elements when starting to stay consistent (because the browser automatically gives margin-top for these on all pages)img {max-width: 100%; display: block;} is also a good thing to always use on every site. Images are default inline-block, which leads to weird spacing sometimes.line-height: 1.6; for body, from 1.4 to less than 2. Smaller for headers#eee or off-white for background, #444 or off-black for textfont-size: 18px; at least for bodymax-width: 650px; around that for main text containerThe Box Model
box-sizing is set to content-box by default. Change to border-box to have width/height setting include the padding and bordermargin-top and margin-bottom set equal to font size.margin-bottom: 25px and bottom element has margin-top: 50px, the margin space is actually 50px and not 75px.margin-top: 0px, and its first child has margin-top: 50px, those will merge and the parent will move with the child! To prevent margins touching each other (causing unpredictable behavior), use padding on parent element to separate its margin from its childrens'.margin: 10px 20px 100px 40px; ==> margin-top, margin-right, margin-bottom, margin-leftmargin: 10px; ==> all margins set to 10pxmargin: 10px 20px; ==> top&bottom are 10px, left&right are 20pxmargin: 0 20px 10px; ==> top is 0px, left&right are 20px, bottom is 10pxborder: 2px solid pink; Width, style, color (order doesn't matter?). For single side, border-left and so forth. Can also do border-bottom-color: red; to override a property of a single border side
(If you set a background-color for the content, it would only cover the padding and the content itself.)margin-left: auto will set all available space on the left side (meaning the element is pushed to the right edge of the parent)margin: 0 auto sets both left and right to auto, centering the element inside its parentCSS Advanced
floatdisplay:flex; on an element makes all its children become columns. So set it to a div wrapping all your columns.<span><div></div></span>), cannot set margin/padding on top/bottom (only left/right), and cannot set width/height. Use inline-block for inline with margin/paddingdisplay: inline-block: inline, but can set margin/padding. Often for buttons: use padding for the size, not width/height.<a class="btn btn-accent">
position: absolute; allows you to place content anywhere on the page. Useful for overlappingjustify-content: space-between can push children to left/right if using display: flex or inline-flextransform: translateY(-50%); - takes 50% of the height (y) of the div and moves the div up (negative) by that amount. Combine with top: 50%;, this is a way to vertically-align to parent div@keyframes animationName {} using js naming convention. In here, specify from 0-100% the steps (like opacity: 0, etc) using 0% {}. Then in the div you want the animation in when it loads, call animation: animationName 0.5s, with seconds lastSelectors
input[type="text"] - select inputs with a type attribute of "text".my-class p means all p that are somewhere inside .my-class.my-class > p means all p that are the direct child of .my-class. More specific..my-class ~ p means all p that generally follow a .my-class element, and they both have the same parent.my-class + p means all p that directly follows .my-class, and they both have the same parenta will apply the rule to every state. Usually you'd do this first, then target the individual ones below as neededa:link - only <a> with a href. Same as a[href]. Changes the default look. Only applies to NOT visited, NOT focus, etc.a:visited - a link that's been visited before. Often the same styling as a:linka:hover - cursor hovering over link. Often the same styling as a:focusa:active - when mouse is actively pressed down on it; usually a flasha:focus - using tab; what it looks like tabbed. Applies to text input etc, not just links#id:target - when used with a hash ID, target will match that ID whenever the current URL has that hash. e.g. at URL yoursite.com/#home, #home:target will match:root - root of the document; almost always the html element:first-child / :last-child - if the targeted element is the first/last child of its parent:nth-child() (:nth-last-child() is the same, but counts from the bottom)
:nth-child(3) - selects the 3rd child:nth-child(even) / :nth-child(odd) - selects even/odd children:nth-child(3n) - selects every 3rd child:nth-child(4n-1) - (4x0)–1 = -1 (no match). (4 x 1)–1 = 3rd Element. (4x2)–1 = 7th Element ...etc.:nth-of-type() - like above, but if there are children of different types. e.g. img:nth-of-type(odd):nth-last-of-type() - like above, but counts from the bottom instead of the top:first-of-type / :last-of-type - div img:first-of-type would select the first image inside the first div and the first image inside the second div, etc.:only-of-type - if the element is the only one of its own inside the parent:not() - e.g. div:not(.music) = all divs except those with a class of “music”. e.g. input:not([disabled]):empty() - elements with no text and no children: e.g. <p></p>::first-letter - selects the first letter of the text in the element::first-line – selects the first line of text in the element (responsive)::before / ::after - add content before/after the rest of the content inside the element
div::before { content: "before"; } will result in<div>
before<!-- Rest of stuff inside the div --></div>
Responsive Design
max-width and min-width: If your element is width 100% and you don't want it to get too big on big viewpoints, set a max-width to a fixed value like 620px. min-width lets something now get too small in small viewports.object-fit: for images, choose how you want it to fit a certain height/width. Most commonly, cover is used to crop the image to the right size instead of stretching it. Also object-position to change where the crop occurs@media media-type and (media-features) { ... } where only media-type or media-features is required (but both can be targeted as well)media-type includes screen, print (for when users choose to print your page), and speech
@media print { ... }media-features includes width, orientation, or specific features like hover (whether the user can hover with a cursor)
@media (min-width: 375px) and (max-width: 800px) { ... } would target all types of media with minimum width of 375px and maximum width of 800px@media screen and (orientation: portrait) { ... }
Background Images / Gradients

background-image: url(folder/filename.jpg); /* don't forget to use .. as necessary from the css folder */background-position: center; and background-size: cover;background-color on top, in case the image doesn't load, to ensure text is readable, etc.background-blend-mode: multiply;
background-image with a background-color or another background-imagebackground-images, use a comma to separate them on one linemultiply - increases darkness so neither has colors brighter than the otherscreen - increases lightness so neither has colors darker than the otheroverlay - increases contrast by making dark colors darker and light colors lighterbackground-image: linear-gradient(red, blue); (or just background as shorthand)
linear-gradient(to right, red, blue). Can also say to bottom right, etclinear-gradient(45deg, red, blue)linear-gradient(red 25%, blue 25%, blue 50%, red 50%, red 75%, blue 75%) ... this will make 1/4 red, 1/4 blue, 1/4 red, 1/4 blue.border: 5px solid; first, then border-image: linear-gradient(to left, red, blue) 1; 1 specifies the border-image-slice, which if you're using real images, tells the browser how to position the pieces. Since it's just a gradient, using 1 is okViewport Units
vw - viewport widthvh - viewport heightbody { height:100vh; } is often done when you want your body to be the full height of the screen. On default, body height is as tall as its contents (making the background property misleading)vmin - viewport minvmax - viewport max. The min/max compare the viewport's height & width and returns the min (smaller) or max (larger)Forms
<form action="/page.html" method="post"> </form>for and id for them:
<label for="fname">Name:</label><input id="fname" type="text" />aria-label can fulfil that function from an accessibility viewpointTransitions & Transform
transition: color 1s; - color 1 second transitionall, which is bad for performancehover - this will only make the transition happen when the element is being hovered. Off hover, it snaps back to normal. Put transitions on the normal elementtransform: scale(3); - makes the element look 3x its size; does not affect the layoutFlexbox
justify-content: used in the flexbox parent. Acts on the main axis: If the flex parent has the default flex-direction of row, it's the horizontal axis. If flex-direction is set to column, the main axis is the vertical axis.
space-between will take empty space and distribute it evenly between the column children
space-around will also distribute the space around the leftmost and rightmost columns as well. It gives equal space to both sides of each column, meaning the middle spaces will be double as wide as the edge spaces.space-evenly like space-around except each column has visually the same amount of space on left and rightflex-start aligns children to the left -- this is the defaultcenter aligns children to the horizontal centerflex-end aligns children to the rightmargin: auto where appropriate to align a child in the center of whichever directionalign-items: used in the flexbox parent. Acts on the cross axis: If the flex parent has the default flex-direction of row, it's the vertical axis. If flex-direction is set to column, the cross axis is the horizontal axis.
stretch is default. The column children will all stretch to match the height of the flexbox if a height is set, or the child with the tallest height (as this will cause the parent's height to be that height)flex-start will vertically align the children to the top, no longer stretching the children to the tallest one. Each child's height will match its contents.
center will vertically align the children to the center, which is a great way to vertically center elements
flex-end will vertically align to the bottom
baseline vertically aligns the children by the line where the biggest font sits on, so children with smaller text will shift down accordinglyalign-self on a child to have it individually align on the same axis of align-items
.flex-child:nth-of-type(3) { align-self: flex-end; } will target the 3rd flex-childflex-direction: set to row by default, meaning the parent is a row and the children are columns
flex-direction: column sets the parent to a column, which children being rowsjustify-content will now add horizontal space between children, and align-items will now horizontally align the children
flex-wrap: wrap; default is nowrap, which forces flex items onto one line (can cause scrolling, etc)
wrap allows flex items to wrap to multiple lines within the flexbox if there's not enough space to fit on one line, staying within the width and adding height (if flex-direction: row).wrap-reverse reverses align-items direction and the direction of new lines when wrapped
flex-flow: row wrap;: includes flex-direction and flex-wrapalign-content: only works when the flexbox has multiple lines of children (flex-wrap)
align-content can make the (multi-line) children keep their size and organize themselves, such as at at the flex-start (leaving empty space at the end of the parent)align-items is only concerned with aligning the items vertically within each rowalign-content will act as the align-items for all the children as one unit
space-between, space-around, and space-evenly are valid values as wellorder: 2;
nth-of-type() selector, which looks at HTML onlyflex-grow: 0; allows children to grow to fit available space on the main axis if parent's width (if flex-direction: row) or height (if flex-direction: column) is set. 0 by default. Setting 1, 2, 3, ... changes the relative sizes of the children's growth
flex-grow works on the main axis, justify-content won't work anymore if grow is onflex-shrink: 1; when the flexbox parent is too small to handle its children, they will start to shrink (even if their width/heights are set). You can control which children shrink more than others. Default is 1, setting to 0 will not allow that child to shrink.flex-basis: auto; works on the main axis, setting the ideal width (if flex-direction: row) or height (if flex-direction: column) of the child
auto, so it will match the child's size to its content. But if a width/height is set, it will use thatflex-basis is set to a value (like 300px), it will override any width/height and use thatflex-basis: 1; will cause the item to shrink rapidly to reach as small as possiblemin/max-height and min/max-width, because those take precedence. So be careful with thoseflex: 100px;, that is the same as setting flex-basis: 100px; but also with flex-grow: 1;flex: grow shrink basis; - shorthand for all three
grow is the only one you have to specify, the others are optionalbasis, it will be 0px, NOT autoflex: initial; is the same as the default, which is flex: 0 1 auto;flex: auto; is the same as flex: 1 1 auto;, which is VERY commonflex: none; is the same as flex: 0 0 auto;flex: 5; - grow: 5, shrink: 1, basis: 0pxflex: 30%; - grow: 1, basis: 30%flex: 3 4; - grow: 3, shrink: 4, basis: 0pxflex: 1 30px; - grow: 1, basis: 30pxalign-self individually aligns the child on the cross axis (same one as align-items)
margins
flex-direction: row) or top/bottom (if flex-direction: column), use margins
CSS Grid
display: grid; on the parentgrid-template-columns: 200px 200px 350px; - creates 3 columns of specified widthsgrid-template-rows: 50px auto; - creates a rows of 50px, and auto creates a row that will be as tall as its tallest child (it's also the default)
grid-template: 50px 200px / 200px 200px 350px; - starts with rows. Accomplishes the same thing as the above two lines
grid-column: 1 / -1; is a way to make a child stretch the whole width of the parent grid. Negatives are counted from the last row/column at -1, -2, -3...grid-column: span 2; means to have the content take up 2 cells. Can also specify starting location like grid-column: 3 / span 2; width: 100%; and height: 100%; will cause the content to match the size of its cell (and won't allow overflow)align-items: start; is concerned with the up/down-ness of the rows.
end, center, etcalign-self: baseline; can align the up/down-ness of an individual child in its rowjustify-content: end; is concerned with the left/right-ness of the columns.
justify-self: start; can align the left/right-ness of an individual child in its column
grid-gap: 0 2em; for touching row content but space between columnsgap and will be able to work with flexbox toogrid-template, the grid will automatically create implicit rows/columns for them to live inauto height and widthgrid-auto-rows: 100px; and grid-auto-columns: 100px;
Unassigned grid items appear below the rest in their own implicit row
Periods are unnamed cells intended to be empty (may have as many .. as you want for readability)
grid-area: 1 / 2 / -1 / 3; can be used as shorthand for grid-row-start / grid-column-start / grid-row-end / grid-column-endfr - fraction
grid-template-columns: 1fr 2fr; creates two columns that grow & shrink. 2fr will grow twice as fastgrid-template-columns: 3fr 2fr; would mean left column of size 3/5 and right column of size 2/5.fr cannot be used as the minimum size in a minmax()minmax(2em, 50%)
grid-template-columns: 100px minmax(200px, 500px); to define the second column as willing to shrink to 200px and willing to grow to 500pxmin-content & max-content
grid-template-columns: 1fr min-content; will have the second column's content shrink its width as much as it cangrid-template-columns: 1fr max-content; will have the second column's content expand its width (such as having text all on one line) as much as it can. Won't add whitespace, just sets width to its max contentrepeat()
grid-template-columns: repeat(3, 1fr); makes 3 columns of size 1frauto-fill vs auto-fit
repeat(auto-fill, minmax(200px, 1fr));
repeat(auto-fit, minmax(200px, 1fr));
Flexbox vs. Grid
minmax(), repeat(), and auto-fill2021 Lily Peng