Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

gary peart
gary peart
6,496 Points

Flexbox - 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
Steven Parker
221,323 Points

Looks pretty good! :+1:

Two comments:

  1. container_mainbody doesn't need a flex property because it's a flex container but not a flex item.
  2. the flex property for ul and the "sections" can be simply flex: 1, since flex: 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
gary peart
6,496 Points

Brilliant!!! 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.