官术网_书友最值得收藏!

  • Mastering CSS
  • Rich Finelli
  • 403字
  • 2021-07-08 09:45:57

Floating columns

So, back to the order of our business here: floating the three columns on our home page. To do this, I want to give each div tag a class of column that we can style. So, in the HTML, let's go to each div tag in the secondary section and use the nice multiple cursor feature of Sublime Text to add class="column" to all three of them at once:

<section class="secondary-section"> 
<div class="wrapper">
<div class="column">...</div>
<div class="column">...</div>
<div class="column">...</div>
</div>
</section>

In my CSS I have made a big comment denoting this section of my CSS for these three columns, I encourage you to as well.

Under this comment, we'll target .column and apply float: left. The width will be 320px.

/**************** 
3 columns 
****************/ 
.column { 
  float: left; 
  width: 320px; 
} 

Ideally, whenever you float elements, try to add a width. If all three columns are 320px , that's going to add up to exactly 960 pixels and fit the width of that wrapper perfectly. If we were to use a number that added up to more than 960 pixels, then not all of the three div tags would fit in that space. One would wrap to the bottom so they wouldn't have all the three div tags on one row. It's important that the width of all the floated div tags is never more than the parent div tag. So save this and refresh the site:

It looks like all the three columns are floated next to each other. That worked out pretty well, except we don't have any margin between columns. So let's go back to our code and let's give it a margin-left property of 30px. Save this and refresh the browser:

.column { 
  float: left; 
  width: 320px; 
  margin-left: 30px; 
} 

Following is the output of preceding code block:

We get a margin of 30px, but we also get our third column drifted down to the bottom because it can't fit in the width allowed.

Let's fix this by reducing the width of the columns to 300px each:

.column { 
  float: left; 
  width: 300px; 
  margin-left: 30px; 
} 

Now if you look at the browser, you will also see that we don't need a margin-left on the first column. We don't need a left margin next to empty space:

Let's get rid of this margin left on the first column. We can do this by targeting that individual .column property using a pseudo class called first child.

主站蜘蛛池模板: 古蔺县| 邯郸县| 夏邑县| 大石桥市| 华安县| 措勤县| 芜湖县| 马边| 布拖县| 河曲县| 台东县| 深圳市| 台湾省| 长乐市| 噶尔县| 尼勒克县| 荥阳市| 扎鲁特旗| 邵阳市| 芦山县| 奉贤区| 莒南县| 察雅县| 英吉沙县| 廊坊市| 沧源| 札达县| 施甸县| 渝中区| 饶平县| 合江县| 牡丹江市| 玉林市| 灌南县| 若羌县| 泾阳县| 莱西市| 云浮市| 三明市| 永寿县| 岱山县|