Intro Setup | Selectors | Pseudo Elements
CSS Selectors
h1 {} - select a certain element
#id {} - select an id
.class {} - select classes
* {} - universal selector (select all elements)
h2, p {} - use comma to select multiple elements
CSS Combinators
div p {} - descendant select, select an element within an element
div > p {} - child selector, selects all p elements that are children of div
div + p {} - adjacent sibling, selects an element directly after that element
div ~ p {} - general sibling, selects all elements that are next siblings
CSS Attribute Selectors
[attribute name] - to select elements with a certain attribute, a[target]
[attribute="value"] - to select elements with a certain attribute and value, a[target="_blank"]
[attribute~="value"] - to select elements with an attribute value with a certain value
[attribute^="value"] - whose value starts with a specified value
[attribute$="value"] - who value ends with a specified value
[attribute*="value"] - who value contains a specified value
To select a certain element or elements to be styled using CSS.
<link href="example.css" rel="stylesheet" /> - added inside head element link to external source
<link href="" rel="stylesheet" media="screen and (max-width: 600px)" /> - optional media query
<div style="color: white"> White Text </div> - style attribute
<style>
display: block; - style information element added inside the head element
</style>
Different ways to add CSS to HTML.
These Achor Pseudo Classes Must Be In Order for them to Work
a:link {styling} - unvisited link
a:visited {styling} - visited link
a:hover {styling} - mouse over link
a:active {styling} - selected link, clicked
a.highlight:hover {styling} - pseudo-classes can be combined with classes
More Pseudo Classes
:checked - input:checked - selects every checked input element
:disabled - input:disabled - selects every disabled input element
:empty - p:empty - selects every p element that has no children
:enabled - input:enabled - selects every enabled input element
:first-child - p:first-child - selects every p element that is the first child of it’s parent
:first-of-type - p:first-of-type - selects every p element that is the first p element of its parent
:focus - input:focus - selects every input that is being focused on
:focus-within - matches if the element or any of its descendants are being focused on
:hover - a:hover, selects links that mouse is being hovered over
:in-range - input:in-range, selects every input that within a specified range
:invalid - input:invalid, selects all input with an invalid value
:lang(languge) - p:lang(it), selects every p element with lang attribute value starting with “it”
:last-child - p:last-child, selects every p element that is the last child of it’s parent
:last-of-type - p:last-of-type, selects every p element that is the last p in its container
:not(selector) - :not(p) - select every non p element
:nth-child(n) - p:nth-child(even) - selects every even numbered p child, n - can be even or odd
:nth-last-child(n) - counting from the last child
:nth-last-of-type(n) - p:nth-last-of-type(2), selects every p element that is the second of parent
:nth-of-type(n) - p:nth-of-type - selects every p that is the second p of its container
:only-of-type - p:only-of-type - selects every p that is the only p of its parent
:only-child - p:only-child - selects every p that is the only child of its parent
:optional - input:optional - selects every input that doesn’t have required attribute
:out-of-range - input:out-of-range - selects inputs with a value outside a specified range
:read-only - input:read-only - selects inputs with a readonly attribute
:read-write - input:read-write - selects inputs with no readonly attribute
:required - input:required - selects inputs with a required attribute
:root - Selects the document’s root element
:target - #news:target - select the current active #news element (Clicked URL containing that a)
:valid - select all inputs with a valid attribute
A pseudo-class is a keyword added to a selector, used to add styles to certain elements under certain conditions.
::after - p::after, insert “content” after every p element needs a content: ; property
::before - p::before, insert “content” before every p element needs content: ; property
::first-letter - p::first-letter, selects the first letter of every p element
::first-line - p::first-line, selects the first line of every p element
::marker - ::marker, selects the markers of list items
::selection - p::selection, selects the portion of an element selected by a user
::placeholder - input::placeholder, selects placeholder text of inputs
Pseudo elements lets you style a specific part of selected elements.
:root {--variable-name: red; }
element { color: var(--variable-name); }
Reusable style values to be used throughout stylesheets. Easily change these values.
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
Used to import styles from other sources.
Colors
color: rgb(0, 0, 255);
color: rgb(0%, 0%, 100%);
color: rgb(0, 0, 255 / 0.5)
color: rgba(0, 0, 255, 0.5)
Select colors using red, green, and blue light mixtures. Alpha is opacity. Written in different ways.
color: hsl(360 50% 50%);
color: hsl(240deg 30% 60% / .7);
color: hsl(240 50% 50% / 25%);
color: hsla(240 50% 50% 0.5);
Select color using it's hue, lightness, and saturation. Alpha is optional transparency.
#RGB // The three-value syntax
#RGBA // The four-value syntax
#RRGGBB // The six-value syntax
#RRGGBBAA // The eight-value syntax
Select colors using hex codes.
color: transparent;
color: currentcolor;
color: inherit;
transparent - have no color, transparent
currentcolor - current color being used in element
inherit - to inherit the color from the parent element
Borders | Outline
border-style: style;
dotted
dashed
solid
double
groove
ridge
inset
outset
none
hidden
Other Properties:
border-width: ;
border-color: ;
Specifies what type of border to display.
border-top-style: ;
border-right-style: ;
border-bottom-style: ;
border-left-style: ;
border-top-width: ;
border-top-color: ;
To style a specific border side.
border-radius: 50%;
border-top-left-radius: ;
border-top-right-radius: ;
border-bottom-right-radius: ;
border-bottom-left-radius: ;
To curve borders of an element.
outline-style:
outline-color:
outline-width:
Outline properties, uses the same values as border.
border-image: url(image.jpg) 30 stretch;
border-image-source: url('image.jpg'); // to use an image as a border
border-image-slice: 30; // divides the image
border-image-repeat: stretch; // defines how images should fit into their dimensions
border-image-outset: 15px; // sets the distance between border and border-box
To use an image for setting borders.
Display
display: block;
display: inline;
display: inline-block;
display: none;
Changes an element's rendering box.
display: flex;
display: inline-flex;
display: grid;
display: inline-grid;
Makes an element into a mutable container to hold and arrange child elements.
visibility: visible;
visibility: hidden;
visibility: collapse;
Shows or hides an element.
Box Model | Positioning
To Specify Type of Positioning
position: static; //default, goes with normal flow of page
position: relative; // according to it's normal position
position: fixed; // according to the viewport
position: absolute; // according to nearest parent element, parent must have position: relative;
position: sticky; // when position is reached via scroll will "stick" to it
Position Using These Properties
top: ;
right: ;
bottom: ;
left: ;
To change the position of an element.
float: none;
float: left;
float: right;
Places an element on it's left or right side of it's container.
Shorthand Methods:
padding: 10px; // all 4 sides
padding: top right bottom left;
padding: top/bottom | left/right
padding: top | left/right | bottom
Other Methods:
padding-top: ;
padding-right: ;
padding-bottom: ;
padding-left: ;
The space between content and border.
clear: none;
clear: left;
clear: right;
clear: both;
Sets whether an element should be moved below a floated element.
height: 100%;
max-height: 100%;
To set an element's height. Not recommended to set an height unless needed.
Shorthand Methods:
margin: 10px; // all 4 sides
margin: top right bottom left;
margin: top/bottom | left/right
margin: top | left/right | bottom
Other Methods:
margin-top: ;
margin-right: ;
margin-bottom: ;
margin-left: ;
To add space around an element's border.
box-sizing: border-box;
To account for any border and padding in the values you specify for an element's width and height.
Grid
display: grid;
display: inline-grid;
To make a grid container. Block or inline level. All children inside container become grid-items.
grid-auto-rows: size;
grid-auto-columns: size;
To implicity (automatically) add new rows or columns;
grid-template-areas: 'myArea myArea';
grid-area: myArea;
Use naming conventions to set grids and place items.
justify-items: ; // positions grid items left to right
Values:
start - aligns grid items to the left side of the grid area
end - aligns grid items to the right side of the grid area
center - aligns grid items to the center of the grid area
stretch - stretches all items to fill the grid area
align-items: ; // positions grid items from top to bottom
Values:
start - aligns items to the top side of the grid area
end - aligns items to the bottom side of the grid area
center - aligns items to the center of the grid area
stretch - stretches all items to fit the grid area
Position how grid items should fit inside it's grid area.
grid-template-columns: ;
grid-template-rows: ;
grid-template: grid-template-rows / grid-template-columns;
Values:
fr // defines a fraction of a grid
repeat(amount, size) // a function to create columns and rows
repeat(amount, size size) // to add two columns or rows at a time
minmax(size, size) // first size always used, second depending on grid size
auto-fill // fits as many possible columns as possible on a row
auto-fit // fits whatever columns there are into the space, stretching the columns
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); // responsive grid
Specify the number of columns and rows.
grid-auto-flow: row; // new items should fill rows
grid-auto-flow: column; // new items should fill columns
grid-auto-flow: row dense; // dense fills holes earlier if smaller items are added
grid-auto-flow: column dense;
Information
align-content: ;
Values:
start
end
center
space-between
space-around
place-self: align-self / justify-self;
To align the grid rows inside the container.
grid-column-start: ;
grid-column-end: ;
grid-column: 1/-1; // use all columns or rows
grid-area: grid-row-start / grid-column-start / grid-row-end / grid-column-end ;
Define which columns to place a grid item.
grid-row-start: ;
grid-row-end: ;
grid-row: ;
grid-area: grid-row-start / grid-column-start / grid-row-end / grid-column-end ;
Define which rows to place a grid item.
justify-content: ;
Values:
center
start
end
space-between
space-around
space-evenly
stretch
place-self: align-self / justify-self;
To align the grid inside the container.
Flex
align-content: ;
Values:
start
end
center
space-between
space-around
Used to align the flex lines.
align-self: ;
Values:
stretch
center
start
end
Align a flex item independent from other items.
flex-direction: column;
flex-direction: column-reverse;
flex-direction: row;
flex-direction: row-reverse;
Which direction to stack the items.
justify-content: ;
Values:
center
flex-start
flex-end
space-around
space-between
Used to align items on the main-axis.
flex-grow: ; // specifies how much an item will grow relative to other items
flex-shrink: ; // specifies how much an item will shrink relative to other items
flex-basis: ; // initial length of a flex item
flex: grow shrink basis; // shorthand
Set how flex items will grow or shrink to fit in a container.
flex-wrap: wrap;
flex-wrap: nowrap;
flex-wrap: wrap-reverse;
Specifies whether items should wrap or not. ( Break into new multiple lines ).
align-items: ;
Values:
center
flex-start
flex-end
stretch
baseline
Used to align items on the cross-axis.
Typography
text-shadow: x-shadow y-shadow blur color;
text-shadow: 2px 2px 5px red;
To add shadow effects on text.
word-break: ;
Values:
normal
break-all
keep-all
Set whether line breaks should appear where text overflows.
family-font: ;
Font Types:
serif // have small strokes at the end of each letter
sans-serif // clean lines no strokes or lines
monospace // all letters have a fixed width
cursive // imitate human hand writing
fantasy // decorative/playful fonts
To change the font's characters.
text-decoration: ; //shorthand
Values:
underline
overline
line-through
none
Other Properties:
text-decoration-color: any color
text-decoration-style: solid, double, dotted, dashed, wavy
text-decoration-thickness: length value
Sets the appearance of decorative lines on text.
text-transform: ;
Values:
capitalize
uppercase
lowercase
none
Specify capitalization on text.
white-space: ;
Values:
normal
nowrap // makes text into one line, doesn't break
pre
pre-wrap
pre-line
break-spaces
Sets how white space should be handled in text.
Images
object-fit: ;
Values:
fill
contain
cover
none
scale-down
Specify how an image or video should fit into it's container.
object-position: ;
object-position: 50% 50%;
object-position: right top;
object-position: left bottom;
Specify how an image should be positioned inside it's container.
mask-image: url();
mask-repeat: no-repeat;
Use a transparent png image (mask image) to use as a mask layer over another image.
Filter
filter: none | blur(1) | brightness(1) | contrast(1) | drop-shadow(x y blur color) | grayscale(0)
| hue-rotate(90deg) | invert(75%) | opacity(50%) | saturate(1) | sepia(1) | url();
Filter applies effects on an element or image.
Scroll
::-webkit-scrollbar { }
::-webkit-scrollbar-track { }
::-webkit-scrollbar-thumb { }
::-webkit-scrollbar-thumb:hover { }
div::-webkit-scrollbar { }
Create custom scrollbars for page or elements. Can use different styling properties.
scroll-snap-align: ; start end center // how you want snapped item positioned
scroll-snap-type: ; x mandatory, y proximity, both mandatory // used on container to set snap control
scroll-snap-stop: ; normal always // always - scroll container always stops at possible snap positions
Set snap positions on scrolls.
overflow: ; // both
overflow-x: ; // horizontal only
overflow-y: ; // vertical only
Values:
visible
hidden
clip
scroll
auto
When content doesn't fit, make it scrollable.
Lists | Markers
list-style-type: square circle disc "string value";
To change the shape of markers of a list.
list-style-position: outside inside;
Set the position of the ::marker relative to it's list item.
Tables
caption-side: top;
Values:
top
bottom
block-start
block-end
inline-start
inline-end
To change position of the caption element on a table.
table-layout: auto;
width: 150px;
Sets an algorithm to layout table cells, rows, and columns.
Math Functions | Units
px // pixels
cm // centimeters
mm // millimeters
in // inches
pt // points
pc // picas
Fixed lengths.
rem // relative to the root element (use for fontsize, padding, margin)
% // relative to the parent element (use for widths, heights, padding, margin, positioning)
em // relative to the parent element
vw // relative to 1% of the viewport width
vh // relative to 1% of the viewport height
vmin // relative to 1% of the viewport's smaller dimension
vmax // relative to 1% of the viewport's bigger dimension
Relative to another length. Better for responsiveness.
width: clamp(min, val, max);
Takes three values. A minimum value, a preferred value, and the maximum value allowed.
Backgrounds
background-repeat: no-repeat;
Values:
repeat-x // repeated horizontally
repeat-y // repeated vertically
no-repeat // not repeated
repeat // repeated throughout
space // repeated with whitespace and nonclipped
round // stretched as size increases
Sets how background images are repeated.
background-origin: border-box;
Values:
border-box // positioned relative to border box
padding-box // positioned relative to padding box
content-box // positioned relative to content box
Sets the backgrounds origin.
background-position: top;
Values:
top left // top center // top right
center left //
right
center
x y - offsets
Sets initial position of background images.
background-clip: border-box;
Values:
border-box // extends to outside edge of border
padding-box // extends to padding edge
content-box // clipped to content box
Use image for text
background-clip: text;
-webkit-background-clip: text;
color: transparent;
Set the color of a background.
background-image: url();
background-image: linear-gradient();
Set an image as a background.
background-size: contain;
Values:
contain // large as possible without stretching or cropping
cover // fills the container
unit unit // use px or rem set width and height
To change the size of a background image.
Gradients | Shadows
background: linear-gradient(direction, color-1, color-2, ...);
Creates an image using a mixture of colors.
filter: drop-shadow(x-offset y-offset blur color);
Applies a drop-shadow to html element.
Animations
@keyframes name {
(0%) from { styles }
(100%) to { styles }
}
Another Example:
@keyframes identifier {
0% {
top: 0;
left: 0;
}
30% {
top: 50px;
}
68%,
72% {
left: 50px;
}
100% {
top: 100px;
left: 100%;
}
}
To change styles from one to another. Use from and to, or percentages to define styles at different time stamps.
animation-iteration-count: infinite // 2;
Sets how many times an animation should be played before stopping.
animation-timing-function: linear;
Values:
linear // animates at even speed
ease // speeds up in middle slows down at end
ease-in // slow start
ease-out // slow end
ease-in-out // slow start and end
steps(4, jump-start) // displays in stops
cubic-bezier(p1, p2, p3, p4) // uses cubic-bezier-curve
Sets how an animation progresses through the duration of each cycle.
animation-direction: normal;
Values:
normal // plays forward
reverse // plays backwards
alternate // reverses direction after cycle
alternate-reverse // same but backwards
To change the direction of an animation.
animation-fill-mode: none;
Values:
none //
forwards //
backwards //
both //
Sets how an animation applies styles before and before it animates.
animation-play-state: paused;
Values:
paused // animation currently paused
running // animation currently running
Sets whether an animation is paused or running.
Transforms | Transitions
transform: rotate3d(X, Y, Z, angle);
Rotates an element around a fixed axis in a 3d space.
transform: matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY());
Shorthand for all 2d transform properties.
transition: ;
Properties:
transition-property: ; // select which properties to add a transition on
Values:
all // select all effects
css property // select a certain property (ex: width, color...)
transition-duration: 2s; // set duration of transition
transition-timing-function: ease; // set a velocity for a transition
transition-delay: 2s; // set a delay before a transition
Shorthand for transition properties. To set a duration to different effects.
transform-style: preserve-3d;
Sets whether children of an element are positioned in the 3D space or are flattened in the plane of the element.
transform: matrix3d(
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16);
Defines a 3D transformation as a 4x4 homogeneous matrix
perspective: 30rem;
To give an element some perspective. Distance from the user and z=0 plane.
Columns
column-count: 2;
Values:
unit
auto // determined by column-width
Breaks content into a number of columns.
column-width: 5rem;
columns: column-count column-width; //shorthand
Specifies a suggested width for columns.
break-inside: avoid-column;
Values:
auto
avoid
avoid-page
avoid-column
avoid-region
Sets how page, column, or region breaks should behave inside a generated box. Example shown in video at 2:50.
Media Queries
@media only mediatype and (expressions) {
CSS-Code;
}
mediatypes:
all // selects all types
print // used for printers
screen // used for screens
speech // used for screenreaders
Expressions:
(max-width: ) // from width and below
(min-width: ) // from width and above
(orientation: landscape) // landscape mode
(orientation: portrait) // portrait mode
Example:
@media only screen and (max-width: 480px) {
.box {
flex-direction: column;
}
}
To apply different styles to different screen sizes.
/* Mobile */
@media screen and (min-width: 0px) {
}
/*Tablet Portrait*/
@media screen and (min-width: 480px) {
}
/*Tablet Landscape*/
@media screen and (min-width:768px) {
}
/* Laptop */
@media screen and (min-width: 1024px){
}
/* Desktop */
@media screen and (min-width: 1440px){
}
/* 4k */
@media screen and (min-width: 2560px){
:root {font-size: 1.4rem;}
}
Simple media query setup targeting all devices. Preferably, use for mobile first approach.
<link rel="stylesheet" media="screen and (min-device-width: 800px)" href="800.css" />
To set different stylesheets at different screen sizes.
UI
resize: ;
Values:
both
horizontal
vertical
none
Determines whether an element is resizable or not.
cursor: ;
Values:
url() // add custom cursor
auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
To set the mouse cursor when over an element.
pointer-events: none;
The element is never the target of pointer events. But pointer events will trigger events on parent element.