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

  • 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.

主站蜘蛛池模板: 花莲县| 彝良县| 文成县| 海伦市| 边坝县| 奉贤区| 罗平县| 阿拉善右旗| 永泰县| 光泽县| 贵州省| 临漳县| 五大连池市| 宁明县| 达州市| 镇巴县| 兰坪| 静乐县| 龙游县| 霍山县| 上犹县| 大庆市| 杭锦旗| 游戏| 开化县| 河东区| 左贡县| 平舆县| 马鞍山市| 射阳县| 集贤县| 云林县| 九寨沟县| 长宁区| 聂拉木县| 松滋市| 新沂市| 什邡市| 肇东市| 昔阳县| 娄底市|