Text
Padding: the space in between the border and the content. Padding is added on to the total width of the box.
Margin: moves the "entire box" to the left, right, up or down.
Border: a line that surrounds the the element. Borders are added on to the total width of the box.
You can affect all 4 sides of the box individually
padding: 5px; - gives all 4 sides the same padding
padding: 3px 4px 5px 6px; - shorthand version
margin: 5px; - gives all 4 sides the same margins
margin: 3px 4px 5px 6px; - shorthand version
To added rounded corners:
You can add a photo for a border:
Demo
BOX SIZING
By default, overflow is set to visible which allows content to "break" outside the box when it has a specific height.
OVERFLOW
DISPLAY
BOX SHADOW
Demo
p{
width: 400px;
float: left
}
img{
float: right;
}
The paragraph will appear on the left and the image will appear on the right.
INLINE ELEMENTS
BLOCK ELEMENTS
Clears floated elements and returns it to it's natural state
clear:(left, right, both, none, inherit)
containing elements will not "enclose" floated elements by default.
floating the containing element is one fix
setting the overflow:auto is another way to fix this
using the after selector is also a way to fix this
p{
position: relative;
top:20px;
left: 50px;
}
p{
position: absolute;
top:20px;
left: 50px;
}
.blue{
position: relative;
top:20px;
left: 50px;
z-index: 5;
}
.red{
position: relative;
top:20px;
left: 50px;
z-index: 3;
}
Blue will go in front of red since it's a higher number
Used to layout components such as navigation, photo galleries, listings, etc..
Not used for overall page layout (CSS Grid)
make all items the same height
horizontal and vertical centering
change the order in which items are displayed
Advantages
Flexbox needs a container with the display set to "flex" in order to work
<div id="container">
<div class="box box1">One</div>
<div class="box box2">Two</div>
<div class="box box3">Three</div>
</div>
#container{
display: flex;
}
flex-direction: (row, column, row-reverse, column reverse)
flex-wrap: (nowrap, wrap, wrap-reverse)
flex-flow: (flex-direction, flex-wrap)
justify-content: (flex-start, flex-end, center, space-between, space-around)
align-self: (flex-start, flex-end, baseline)
align-content: (flex-start, flex-end, center, space-around, space-between, stretch)
New way to layout pages
Not supported by all browsers (but can use a backup!)
turn the element into a grid container by using the display properties
set up columns and rows (think excel!)
assign each grid item to an area on the grid
Process
Grid line: dividing lines
Grid cell: the box inside the grid lines
Grid area: rectangular area made up of one or more adjacent cells
Grid track: The space in between 2 grid lines (grid column or grid row)
<div id="layout">
<div id="one">three</div>
<div id="two">two</div>
<div id="three">three</div>
</div>
#layout{
display: grid;
}
#layout{
display: grid;
grid-template-rows: 100px 400px 100px;
grid-template-columns: 200px 500px 200px;
}
grid-template-rows, grid-template-columns: (none, optional sizes and or names)
#layout{
display: grid;
grid-template-rows: [header-start] 100px [content-start] 400px [footer-start] 100px;
grid-template-columns: 200px 500px 200px;
}
you can also use fractional units - fr. Take up whatever space is left.
DEMO CSS GRID