html {
background-color: white;
}
body {
width: 960px;
margin: 0 auto;
background-color: green;
}
html {
background-color: white;
}
body {
width: 80%;
margin: 0 auto;
background-color: green;
}
Two- or three-colum layout is very common!
There may be a layout for the page and separate layouts for the components on the page.
E.g. this is still just a page that has a three-column layout:
Other: floating, positioning, multicolumn text.
In RWD, we adapt the layout to the characteristics of the device/window.
E.g.
one-column layout for narrow devices/windows;
two-column layout when there is more room;
three-column layout when there is even more room.
A flexbox contains items, which are laid out in a one-dimensional way,
either in rows or in columns.
The items in the flexbox can 'flex',
grow or shrink.
Don't use flexbox when flow layout is sufficient!
On the container:
display: flex;
Parent properties (the flexbox itself) (the container) |
Child properties (the flex items) (the contents) |
---|---|
flex-direction |
order |
flex-wrap |
flex-grow |
justify-content |
flex-shrink |
align-items |
flex-basis |
align-content |
align-self |
To center text or an image horizontally within a parent element, use
text-align: center;
p {
text-align: center;
}
To center a single element horizontally within a parent element, set its left and right margin to auto
.
(Note that nothing will happen unless the element is narrower than the parent.)
p {
margin-left: auto;
margin-right: auto;
}
Common shorthand:
p {
margin: 0 auto;
}
This technique is often used to center the whole web page, e.g.:
html {
background-color: white;
}
body {
width: 80%;
margin: 0 auto;
background-color: green;
}
To center several elements elements horizontally within a parent element, use a flexbox.
main {
display: flex;
justify-content: center;
}
p {
flex-basis: 20%;
flex-grow: 0;
}
<main>
<section>
<h2>Heading1</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</section>
<section>
<h2>Heading2</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate
</p>
</section>
<section>
<h2>Heading3</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua.
</p>
</section>
</main>
main {
display: flex;
}
main > section {
border: 1px solid black;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}
Very easy if all images are the same size.
<section>
<img src="image001.jpg" alt="…" />
<img src="image002.jpg" alt="…" />
<img src="image003.jpg" alt="…" />
<img src="image004.jpg" alt="…" />
…
</section>
section {
display: flex;
flex-wrap: wrap;
}
But there are more decisions to make when images are not the same size.
<section>
<img src="image001.jpg" alt="…" />
<img src="image002.jpg" alt="…" />
<img src="image003.jpg" alt="…" />
<img src="image004.jpg" alt="…" />
…
</section>
section {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: center;
}
section > img {
margin-top: 1em;
}