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 CSS Basics (2014) Basic Layout Floats

Bartlomiej Zabielski
PLUS
Bartlomiej Zabielski
Courses Plus Student 10,363 Points

floats with Guil Hernandez

In the video Floats by Guil Hernandez where does he calculate the width:45.5% for the two images .resports and .tips.

I tried to use dev tools but can't figure it out

To float two items side by side as he does in this video, you must take into consideration the margins and any borders applied to the child elements of a parent container. Then the rest is a bit of Math. For example:

Suppose we created a div container element with a class name of "wrapper" and two child div elements. The parent container element has a width size of 400px. And the two child elements with class name child inside have a size of 180px in width, with 5px margin on the left and right sides and a 1px border. It will look like this in CSS:

.wrapper{
max-width: 400px;
min-height: 12em;
}

.child{
width: 180px;                                     /*180px / 400px = 0.45 * 100 = 45% */
min-height: 10em;
margin-left: 4px;                             /* 4px /400px = 0.01 * 100 = 1% */
margin-right: 2px;                         /*2px /400px  = 0.005 * 100 = 0.5%*/
border: 1px solid black;               /*1px / 400px = 0.0025 * 100 = 0.25%*/
float: left;
}

By applying a margin to both child elements on their left and right sides, we have a total of 20px margin, because 5+5+5+5 = 20. And a 1px border for each child element = 1 + 1 = 2px in total, and therefore, the margins along with the border added together gives us 22px in total. The child elements now have total width of 180px + 180px + 22px = 382px, and the remaining space left in the parent container is 400 - 382 = 18px. Thus, you will be able to float these child elements side by side since they fit into the parent container.

He used percentages, so you can alternatively convert the child elements width, margin and border property values to a percentage by doing what has been shown above in the code commented out.

To learn more, please visit: https://developer.mozilla.org/en-US/docs/Web/CSS/float

3 Answers

Bartlomiej Zabielski
PLUS
Bartlomiej Zabielski
Courses Plus Student 10,363 Points

hey I get that we need to make room for the margins i just dont see where he got his figure of 46.5% seems like a strange number cant find where it comes from will check again ><

Boban Talevski
Boban Talevski
24,793 Points

You need to have two of these columns in 100% width, meaning each should be set to 50%. But since we need to consider margins, borders etc. we need to go a bit lower than 50%, which is why I think he chose 46.5%. Could've been 45%, 47%, or any number close to 50 which makes sense and doesn't break the layout.