shorthand for margin
– the order of the numbers is clockwise from the top
margin: [top] [right] [bottom] [left];
// alternatively
margin: [top/bottom] [right/left];
margin: 0 5px
results in 0
at top and bottom and 5px
at the right and leftby default, type elements (p
, h1
, etc.) have a top and bottom margin equal to their line height
block
elements – take up 100% width and 0 height by default; height grows based on content
inline
elements – flow with the content, don't create their own line
padding-top
will make it overflow with the content above it)width
and height
doesn't do anything for these elementsmargin
on inline elements doesn't really push the adjacent elements awayinline-block
elements – allow you to have the benefits of block elements (no overlap) while keeping the content flowing (not 100% wide)a
selector, a:hover
, a:visited
, a:active
(currently being clicked), a:focus
(currently selected e.g. via tab):focus
is when an element is able to accept input – the cursor in an input box or a link that has been tabbed to.:active
is when an element is being activated by a user – the time between when a user presses a mouse button and then releases it.when multiple selectors at the same level of specificity apply for an element, and they modify the same CSS properties, the last one always wins:
a:active {
color: red;
}
a:hover {
color: black;
}
/* when clicking on an <a> element, it is both hovered and active, and
because the last style wins, it will be red. */
applying the same styles to multiple selectors:
p, h1, h2 {
margin-top: 0px;
}
within a specific selector, the later property takes precedence:
h2 {
font-size: 32px;
color: #104c90;
margin-bottom: 10px;
margin: 0; /* this property overrides margin-bottom */
}
shorthand for border
property:
border: 1px solid #000;
order of specificity, from less specific to more specific: element (e.g. div
) → class (e.g. .container
) → id (e.g. #user-input
)
compound selectors: can specify a style for a selector that applies to descendant elements of some other selector
/*
select all <a> elements that are inside elements of class "section-two"
*/
.section-two a {
color: black;
}
<a>
element of a "section-one"
-class element, not just direct childrenyou can use compound selectors with pseudo-selectors:
/*
select all hovered, "btn"-class elements inside "section-one" elements
*/
.section-one .btn:hover {
background: white;
}
/*
select all hovered, "btn" elements inside hovered, "section-one" elements
*/
.section-one:hover .btn:hover {
background: white;
}
specificity and compound selectors:
.button a { }
has 11 points (10 for class + 1 for element), while p #item .class-one .class-two
has 121 points (100 for id, 20 for class, 1 for element)