Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialgary peart
6,496 PointsFlexbox - Is this example I've done a valid layout?
Hi,
I'm trying not to confuse myself with Flexbox too much. I've done the example below today in the hope I've got the basic layout principles correct.
Is the example below a valid Flexbox layout?
Many thanks,
Gary.
<!DOCTYPE html>
<html>
<head>
<title>Flexbox Layout</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="header">
<h1 class="name">The Best Site</h1>
<ul>
<li><a href="">Google</a></li>
<li><a href="">Facebook</a></li>
<li><a href="">Twitter</a></li>
<li><a href="">Amazon</a></li>
<li><a href="">Google</a></li>
<li><a href="">Facebook</a></li>
<li><a href="">Twitter</a></li>
<li><a href="">Amazon</a></li>
</ul>
</div>
<div class="container_mainbody">
<div class="section1">
<h1>Section 1</h1>
<p>Some type of content goes here.</p>
</div>
<div class="section2">
<h1>Section 2</h1>
<p>Some type of content goes here.</p>
</div>
<div class="section3">
<h1>Section 3</h1>
<p>Some type of content goes here.</p>
</div>
</div>
<div class="footer">
<p>The footer goes at the bottom of the page.</p>
</div>
</body>
</html>
*{
margin: 0;
padding: 0;
font-family: Arial;
list-style: none;
text-decoration: none;
box-sizing: border-box;
}
.header {
border: solid 1px red;
width: 100%;
display: flex;
flex-flow: row wrap;
align-items: flex-start;
}
ul {
display: flex;
flex-flow: row wrap;
align-self: center;
border: 1px dashed green;
justify-content: center;
flex: 1 1 auto;
}
li {
border: 1px solid white;
}
ul li a{
max-width: 100%;
padding:0 20px;
}
.container_mainbody {
display: flex;
flex-flow: row wrap;
flex: 1 1 100%;
border: 1px solid black;
}
.section1,
.section2,
.section3 {
display: flex;
flex-flow: row wrap;
flex: 1 1 33.33%;
border: 1px solid black;
}
.section1 h1,
.section2 h1,
.section3 h1 {
flex: 0 0 100%;
}
.footer {
display: flex;
display: row wrap;
border: 2px solid red;
}
2 Answers
Steven Parker
232,162 PointsLooks pretty good!
Two comments:
- container_mainbody doesn't need a flex property because it's a flex container but not a flex item.
- the flex property for ul and the "sections" can be simply
flex: 1
, sinceflex: 1 1 auto
is assumed, and for 3 sections all with flex-grow of 1, each will get 1/3 of the available space anyway.
gary peart
6,496 PointsBrilliant!!! Thanks for the feedback Steven, greatly appreciated.
It took me a couple of days in order to understand the idea of Flexbox. It was only when I thought of a HTML <table> layout structure that the Flexbox idea really clicked.