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 trial

CSS

Frank D
Frank D
14,042 Points

Two column layout challenge: Why not simply using primary {float:left} and secondary {float:right} writing less code?

I have tested the method shown by Guil in this video and it works great! Bless him for the great explanation. But, why not simply floating the two columns to the left and right respectively? That way you wouldn't need the display: inline-block property as well as the margin-right: -4px and the vertical-align: top;?

I just can't figure out why not simply writing less code by floating them. What would be the advantages of using the Guil method to adjust the columns?

Thanks, Frank

5 Answers

Joe Consterdine
Joe Consterdine
13,965 Points

Hi Frank,

I personally hardly ever use inline-block to place my elements side by side. It's a good alternative to have though.

Why would you use inline-block?

Well for one using inline-block always you use the 'vertical-align' property. You can then position your elements at the top, middle and bottom of a container with one selector.

If you were to try and position a div vertically centered normally you'd have to use css3 selectors like transform to make that happen.

Also lets say you have a span element which is inline be default, what if you needed padding or margin on that element? You can't because it's inline. However, if you make it 'display: inline-block' now you can adding padding and margin.

I personally prefer floating, but there may be occasions you'd use inline-block.

Frank D
Frank D
14,042 Points

Hi Joe, thanks for your response!

I really love your idea of using the inline-block for top, middle and bottom vertical alignments. I never though about that before as I have always be using the floating properties to align layouts on my projects.

Thank you for sharing your reflections.

However, I have found in the next videos (my bad) Guil suggestions to this topic and they are very well explained: Basically they go like this:

• Use inline-block to lay out navigation items side by side or create a simple two-column layout • Use inline-block when you need control over center and vertical alignment • Use floats to flow content along the left or right side of an element • Floats do not create default horizontal white space between elements • Be aware of collapsing containers

jason chan
jason chan
31,009 Points
@media (min-width: 769px) {
.col {
  width:50%;
  }
  .primary{
  float: left;
  }
  .secondary {
  float:right;
}
}

That's how i did it.

Jacob Miranda
Jacob Miranda
19,392 Points

I don't believe the float property was created to be used as a layout technique. It serves a different purpose. Although it works for creating layouts, I don't like to use it because you have to work around it collapsing columns if you don't use a clearfix. It can behave kind of weird.

I prefer using the "display: inline-block" rule because you have more control over how things will layout, aside from the browser rendering the space IF you leave a space between two tags closing and opening.

Everyone will have their own way of doing things, so until there is a full-proof method (fingers crossed for flexbox), I would say anyone is welcome to using their own method, but it would be best to understand how each one works and why before making your decision.

Anthony c
Anthony c
20,907 Points

I think it has less to do with this method being the best method, and more to do with teaching the underlying concepts. These classes aren't about best practices (although sometimes they are, and many times tips on best practices are mentioned) but it's about giving you the building blocks. So when you learn about flex box you can see how it is superior to the other methods. Instead of just knowing how to use flexbox period.

I also used float as my solution to solve the problem, it came more natural to me. And gave me good results.