Imagine a gift, inside is the gift, wrapped in foam all around (padding), and the giftbox outside of it (border) and then a wrapping paper on the giftbox (margin).
block
as it’s display, the browser will follow these rules to layout the element:
Certain elements have block
as their default display, such as: divs, headers, and paragraphs.
inline
as it’s display, the browser will follow these rules to layout the element:
Certain elements have inline
as their default display, such as: span tags, anchors, and images.
Standard Box Model vs Border-Box
Inline-Block
block
and inline
.
Padding
padding-top
, padding-right
, padding-bottom
, padding-left
(clockwise!)Border
border-width
, border-style
, border-color
.Margin
margin-top
, margin-right
, margin-bottom
, margin-left
.margin: 0 auto
to center an element.position
property allows us to set the position of elements on a page and is an integral part of creatnig a Web page layout.static
, relative
, absolute
, fixed
, sticky
.static
) is used with: top
, right
, bottom
, and left
to position an element on a page.Static Positioning
Relative Positioning
#pink-box {
background-color: #ff69b4;
bottom: 0;
left: -20px;
position: relative;
right: 0;
top: 0;
}
Absolute Positioning
.container {
background-color: #2b2d2f;
position: relative;
}
#pink-box {
position: absolute;
top: 60px;
}
.container {
background-color: #2b2d2f;
position: relative;
}
#pink-box {
position: absolute;
top: 60px;
}
#blue-box {
position: absolute;
}
.container {
background-color: #2b2d2f;
position: relative;
}
#pink-box {
background-color: #ff69b4;
bottom: 60px;
position: absolute;
}
.container {
background-color: #2b2d2f;
}
#pink-box {
background-color: #ff69b4;
bottom: 60px;
position: absolute;
}
Fixed Positioning
Sticky Positioning
Flexbox is a CSS module that provides a convenient way for us to display items inside a flexible container so that the layout is responsive.
Using Flexbox
Flexbox automatically resizes a container element to fit the viewport size without needing to use breakpoints.
Flexbox layout applies styles to the parent element, and it’s children.
.container {
display: flex; /*sets display to use flex*/
flex-wrap: wrap; /*bc flex tries to fit everything into one line, use wrap to have the elements wrap to the next line*/
flex-direction: row; /*lets us create either rows or columns*/
}
flex-flow
can be used to combine wrap and direction.justify-content
used to define the alignment of flex items along the main axis.align-items
used to define the alignment on the Y-axis.align-content
redistributes extra space on the cross axis.
By default, flex items will appear in the order they are added to the DOM, but we can use the order
property to change that.
flex-grow
: dictates amount of avail. space the item should take up.flex-shrink
: defines the ability for a flex item to shrink.flex-basis
: Default size of an element before the remaining space is distributed.flex
: shorthand for grow, shrink and basis.align-self
: Overrides default alignment in the container.Bootstrap vs CSS Grid
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
grid-template-areas:
"header header header"
"main . sidebar"
"footer footer footer";
grid-column-gap: 20px;
grid-row-gap: 30px;
justify-items: stretch;
align-items: stretch;
justify-content: stretch;
align-content: stretch;
}
.item-1 {
grid-area: header;
}
.item-2 {
grid-area: main;
}
.item-3 {
grid-area: sidebar;
}
.item-4 {
grid-area: footer;
}
repeat
, fractions.Grid Template Areas
gives us a handy way to map out and visualize areas of the grid layout.Grid Gaps
can be used to create ‘gutters’ between grid item.s
The way we have defined our grid with grid-templates
and areas
are condidered explicit.
We can also implicitly
define grids.
.grid-container {
display: grid;
grid-template-columns: 100px 100px 100px 100px;
grid-template-rows: 50px 50px 50px;
grid-auto-columns: 100px;
grid-auto-rows: 50px;
}
Spanning Columns & Rows
We can use the following properties to take up a specified num of cols and rows.
grid-column-start
grid-column-end
grid-row-start
grid-row-end
All four properties can take any of the following values: the line number, span #, span name, auto.
.item-1 {
grid-row-start: row2-start; /* Item starts at row line named "row2-start" */
grid-row-end: 5; /* Item ends at row line 5 */
grid-column-start: 1; /* Item starts at column line 1 */
grid-column-end: three; /* Item ends at column line named "three" */
}
.item-2 {
grid-row-start: 1; /* Item starts at row line 1 */
grid-row-end: span 2; /* Item spans two rows and ends at row line 3 */
grid-column-start: 3; /* Item starts at column line 3 */
grid-column-end: span col5-start; /* Item spans and ends at line named "col5-start" */
}
Grid Areas
Justify & Align Self
Justify Self is used to align self on the row.