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

Floating elements

Floating boxes are the most used in modern layouts. The theory of floating boxes was used especially in grid systems and inline lists in CSS frameworks. For example, class and mixin inline lists (in the Zurb Foundation framework) are based on floats.

Possibilities of floating elements

Elements can be floated to the left and right side. Of course, there is a method to reset floats too. The possible values are as follows:

float: left; // will float element to left
float: right; // will float element to right
float: none; // will reset float 

Most known floating problems

When you are using floating elements, you can have some issues. The most known problems with floated elements are as follows:

  • Too big elements (because of width, margin left/right, padding left/right, and badly counted width, which is based on the box model)
  • Not cleared floats

All of these problems provide specific effects, which you can easily recognize and then fix.

Elements which are too big can be recognized when they are not in one line, as they should be. What you should check first is if the box-sizing: border-box is applied, and then check width, padding, and margin.

You can easily recognize floats that are not cleared when the floating structure of some elements from next container are floated. This means that you have no clear fix in your floating container.

Defining clear fix/class/mixins

When I started developing HTML and CSS code, there was a method to clear the floats with .cb or .clear classes, both of which were defined as follows:

.clearboth, .cb {
    clear: both
}

This element was added in a container right after all the floated elements. This is important to remember about clearing the floats because containers that contains floating elements won't inherit the height of the highest floating element (which will have a height equal to 0),for example:

<div class="container">
    <div class="float">
        … content ...
    </div>
    <div class="float">
        … content ...
    </div>
    <div class="clearboth"></div>
</div>

CSS looks like the following:

.float {
    width: 100px;
    height: 100px;
    float: left;
}

.clearboth {
    clear: both
}

Nowadays, there is a better and faster way to clear floats. You can do this with the clear fix element, which can be defined as follows:

.clearfix:after {
    content: "";
    visibility: hidden;
    display: block;
    height: 0;
    clear: both;
}

And you can use it in HTML code:

<div class="container clearfix">
    <div class="float">
        ... content ...
    </div>
    <div class="float">
        ... content ...
    </div>
</div>

</div>

The main reason to switch on clear fix is that you save one tag (with the clearboth class). Recommended usage is based on the clear fix mixin, which you can define in SASS as follows:

=clear fix
&:after
    content: ""
    visibility: hidden
    display: block
    height: 0
    clear: both

Therefore, every time you need to clear floating in some container, you need to invoke it. For example, let us take the previous code:

<div class="container">
<div class="float">
        … content ...
</div>
<div class="float">
        … content ...
</div>
</div>

The container can be described as follows:

.container
  +clear fix

Example of using floating elements

The most known usage of float elements is grids. A grid is mainly used to structure the data displayed on a web page. In this chapter, let's check just a short draft of a grid. In the upcoming chapters, we will focus on automatization of creating the grid with mixins.

Let us create some HTML code:

<div class="row">
    <div class="column_1of2">
        Lorem
    </div>
    <div class="column_1of2">
        Lorem
    </div>

</div>
<div class="row">
    <div class="column_1of3">
        Lorem
    </div>
    <div class="column_1of3">
        Lorem
    </div>
    <div class="column_1of3">
        Lorem
    </div>

</div>

<div class="row">
    <div class="column_1of4">
        Lorem
    </div>
    <div class="column_1of4">
        Lorem
    </div>
    <div class="column_1of4">
        Lorem
    </div>
    <div class="column_1of4">
        Lorem
    </div>
</div>

And also create some SASS code:

*
  box-sizing: border-box

=clear fix
&:after
    content: ""
    visibility: hidden
    display: block
    height: 0
    clear: both

.row
  +clear fix

.column_1of2
  background: orange
  width: 50%
  float: left

&:nth-child(2n)
    background: red

.column_1of3
  background: orange
  width: (100% / 3)
  float: left

&:nth-child(2n)
    background: red

.column_1of4
  background: orange
  width: 25%
  float: left

&:nth-child(2n)
    background: red

The final effect is as follows:

Example of using floating elements

As you can see, we created a structure of a basic grid. In places where HTML code is placed Lorem here is a full lorem ipsum to illustrate the grid system.

主站蜘蛛池模板: 卫辉市| 绩溪县| 河间市| 江阴市| 山阴县| 临桂县| 五莲县| 宜兰市| 湘乡市| 平武县| 平潭县| 西贡区| 苏州市| 拜泉县| 利川市| 吐鲁番市| 五大连池市| 高尔夫| 东宁县| 鄱阳县| 韶关市| 赫章县| 剑河县| 揭西县| 怀宁县| 罗源县| 南开区| 开化县| 农安县| 英吉沙县| 大新县| 得荣县| 云和县| 托里县| 栾川县| 余江县| 天津市| 绵阳市| 东乌珠穆沁旗| 乐业县| 广灵县|