Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, you'll learn to display the columns side by side in medium and large screens, using floats.
Column layout tips
To add a left gutter to all but the first column, you can use this:
.col + .col {
padding-left: 1em;
}
To remove the right gutter from the column closest to the right edge of the page, use :last-child
.col:last-child {
padding-right: 0;
}
Bringing it all together
This snippet floats all columns left and applies a right gutter. It applies a left gutter to all but the first column, so the first column will be flush with the left margin of the page. Then it removes the right gutter from last column, so that it's flush with the right margin of the page.
.col {
float: left;
padding-right: 1em;
}
.col + .col {
padding-left: 1em;
}
.col:last-child {
padding-right: 0;
}
You can also apply a gutter using just a left padding or margin value, like this:
.col {
float: left;
}
.col + .col {
padding-left: 1.5em;
}
-
0:00
In this video, I am going to display columns side by side, in medium and
-
0:04
large screens using floats.
-
0:06
In my style sheet I'll go inside the media query, and I'm going to float the columns
-
0:11
left by grouping the call class with the name selector here.
-
0:16
So right after the name class I'll add a comma and then add the class col.
-
0:22
So now both the name and columns will float left.
-
0:27
And at this break point I wanna display only two columns side by side.
-
0:32
If I place all three columns on the same line when the screen is 769 pixel wide,
-
0:37
the columns will appear too narrow on the screen,
-
0:40
making the content difficult to read.
-
0:43
So in this break point, I'm only going to give the primary and
-
0:46
secondary columns a width.
-
0:48
So right below the name and col rule, I'll create a new
-
0:53
rule that targets primary and the class secondary.
-
1:01
Then I'm going to set their width to 50%.
-
1:05
I'm not giving the tertiary column a width yet.
-
1:08
That column will appear below the primary and secondary columns in this break point.
-
1:12
So the tertiary column's width remains that base width of 100% and
-
1:17
we'll see that in the browser in just a few.
-
1:20
Next, right below the top media query I'm going to define a second media
-
1:26
query to write the column and container styles for a wider three column layout.
-
1:32
So I'll create a new media query by typing @media.
-
1:37
And this break point will be at a min-width of 1025 pixels.
-
1:46
So when the view port or device is 1025 pixels or wider.
-
1:51
The styles written here will be applied to the layout.
-
1:54
In this new media query, I'll create a new rule
-
1:57
that targets container and then I'll set its width to 80%.
-
2:02
So it's a little narrower than the 90% width given to
-
2:07
container in the 769 breakpoint.
-
2:13
Right below the width declaration I'm going to define a max
-
2:19
width of 1150 pixels so
-
2:21
that the layout doesn't get too wide when the screen is 1025 pixels or wider.
-
2:26
So in this break point all three columns will be laid out side by side on the same
-
2:30
line and I want the primary column to be slightly wider than the secondary and
-
2:36
tertiary columns.
-
2:38
So I'm going to target the primary column and give it a width of 40%.
-
2:47
Then right below, I'll target the secondary and tertiary columns.
-
2:58
And I'm going to set their width value to 30%.
-
3:00
So the primary column is 40% while secondary and tertiary are 30%.
-
3:07
So together that adds up to a total width of 100%.
-
3:11
So the float inside
-
3:15
the main header and column containers cause the container's height to collapse.
-
3:21
And we know this because the layout and the header and columns, looks broken.
-
3:25
So we no longer see the main header background and
-
3:28
the footer content is actually wrapping around the right column.
-
3:33
And the footer is also displaying near the middle of the page.
-
3:36
Fortunately you know the fix for this.
-
3:38
So the CSS already has a clear fix rule.
-
3:42
So back in index.html
-
3:45
I’m going to apply the clearfix class to the collapsing containers.
-
3:49
First I will add the clearfix class to the container inside main-header.
-
3:54
So right after the container class, I’ll type clearfix.
-
3:59
Then I’ll scroll down to the column container and add the class clearfix.
-
4:07
And now the header and columns look great.
-
4:09
So that's it.
-
4:10
My best city guide layout is complete.
-
4:22
Great work so far.
-
4:24
Up next, you'll learn how to place elements, like navigation menus and
-
4:28
icons anywhere on the page, using relative, absolute, and fixed positioning.
You need to sign up for Treehouse in order to download course files.
Sign up