Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Align.docx58.8 KB Align - Solution.docx
58.9 KB
Quick reference
Align
There are many ways to align and center objects.
When to use
Any time you want to align something, or center something, there are many ways to do it.
Instructions
We've already looked at text-align for aligning text. There are many ways to align classes and images.
One way to align an image:
img {
display: block;
margin: auto;
}
Another way to align text is with the position: absolute;
img {
position: absolute;
right: 100px;
top: 300px:
}
Another way to align text inside a class is to set a line height equal to the height of the class and then use text-align: center
.box {
line-height: 300px;
text-align: center;
height: 300px;
border: 2px solid red;
}
Hints & tips
- There are many ways to align and center things.
- Position: absolute; allows you to be precise.
- display: block, and margin: auto; is a fun center hack.
Lesson notes are only available for subscribers.