Heim > Artikel > Web-Frontend > Lernerfahrung mit wichtigen Float-Eigenschaften von CSS
Werfen wir einen Blick auf das wichtige CSS-Attribut – float.
Der folgende Inhalt ist in folgende Unterabschnitte unterteilt:
1: Float-Attribut
2: Eigenschaften des Float-Attributs
2.1: Fließender Textumbrucheffekt
2.2: Das Vaterelement von float ist stark kollabiert
3: Methode zum Löschen von Floats
3.1: HTML-Methode
3.2: CSS-Pseudoelementmethode
4: Despace-Float
5: Float-Elemente blockieren
6: Float-Fluid-Layout
6.1: Einseitige Fixierung
6.2: Einseitige Fixierung mit unterschiedlichen DOM- und Displaypositionen
6.3: Einseitige Fixierung des DOM an der gleichen Position wie das Display
6.4: Intelligentes Layout
1: Float-Attribut
Float bedeutet, wie der Name schon sagt, Schweben. Aber in CSS wird es als Float verstanden. float hat vier Attribute, nämlich
<span style="color: #008080;">1</span> <span style="color: #0000ff;">float</span><span style="color: #000000;">:none; </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">float</span><span style="color: #000000;">:left; </span><span style="color: #008080;">3</span> <span style="color: #0000ff;">float</span><span style="color: #000000;">:right; </span><span style="color: #008080;">4</span> <span style="color: #0000ff;">float</span>:inherit;
Die beiden am häufigsten verwendeten Attributwerte sind linksschwebend und rechtsschwebend. Im folgenden Teilen wird nur Left Floating als Beispiel verwendet. Das Prinzip anderer schwebender Attributwerte ist das gleiche wie das des linken Gleitens.
2: Eigenschaften des Float-Attributs
2.1: Textumbrucheffekt von Float
Die ursprüngliche Absicht von Floating ist der Textumbrucheffekt.
Sehen Sie sich den folgenden Code und die folgende Demo an.
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="content"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">3</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">4</span> <span style="color: #000000;">Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World! </span><span style="color: #008080;">5</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">6</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;"> 4</span> <span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid black</span>; <span style="color: #008080;"> 5</span> } <span style="color: #008080;"> 6</span> <span style="color: #800000;">.container .content </span>{ <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;"> 8</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 150px</span>; <span style="color: #008080;"> 9</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 150px</span>; <span style="color: #008080;">10</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightpink</span>; <span style="color: #008080;">11</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 5px</span>; <span style="color: #008080;">12</span> }
content-Element ist auf „Left Floating“ eingestellt, sodass das div-Element vom Dokumentfluss getrennt wird und der Text darum herum angezeigt wird.
2.2: Das Vaterelement von Float ist stark kollabiert
Nehmen Sie das div-Element als Beispiel. Die Höhe des div-Elements wird automatisch um den Inhalt erweitert. Mit anderen Worten: Die Höhe ist so hoch wie der Inhalt. Wenn jedoch das Float-Attribut für das interne Element festgelegt ist, verringert sich die Höhe des übergeordneten Elements. Der Code und die Beispiele lauten wie folgt.
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">3</span> <span style="color: #000000;">Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World! </span><span style="color: #008080;">4</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">5</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
Wie unten gezeigt, CSS und Demo nach dem Zusammenklappen.
<span style="color: #008080;">1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;">2</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;">3</span> <span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid black</span>; <span style="color: #008080;">4</span> } <span style="color: #008080;">5</span> <span style="color: #800000;">.container p </span>{ <span style="color: #008080;">6</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;">7</span> }
Sie können die Änderungen sehen, die vor und nach dem Festlegen des Unterelements auf Float vorgenommen wurden.
3: Methode zum Löschen von Floats
Dann stellt sich die Frage: Wenn die internen Elemente schweben sollen, wie kann dann vermieden werden, dass die Höhe des übergeordneten Elements zusammenbricht?
Einige Schüler werden sicherlich denken: Wäre es nicht ausreichend, die Höhe direkt am übergeordneten Element festzulegen? Dies ist in der Praxis nicht möglich. Denn wenn die Höhe des übergeordneten Elements festgelegt ist und Sie später Inhalte hinzufügen möchten, müssen Sie den CSS-Code ändern, was sehr mühsam ist.
Es gibt zwei Möglichkeiten, das Problem des Höheneinbruchs des übergeordneten Elements zu lösen.
3.1: Fügen Sie am unteren Rand des übergeordneten Elements ein leeres div hinzu und fügen Sie dann das Attribut „clear: Both“ hinzu.
Der Code lautet wie folgt.
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">3</span> <span style="color: #000000;"> Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World! </span><span style="color: #008080;">4</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">5</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="clearfix"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">6</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid black</span>; <span style="color: #008080;"> 4</span> } <span style="color: #008080;"> 5</span> <span style="color: #800000;">.container p </span>{ <span style="color: #008080;"> 6</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;"> 7</span> } <span style="color: #008080;"> 8</span> <span style="color: #800000;">.container .clearfix </span>{ <span style="color: #008080;"> 9</span> <span style="color: #ff0000;"> overflow</span>:<span style="color: #0000ff;"> hidden</span>; <span style="color: #008080;">10</span> <span style="color: #ff0000;"> *zoom</span>:<span style="color: #0000ff;"> 1</span>; <span style="color: #008080;">11</span> }
3.2: Das übergeordnete Element legt die Pseudoklasse danach fest.
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">3</span> <span style="color: #000000;"> Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World! </span><span style="color: #008080;">4</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span> <span style="color: #008080;">5</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid black</span>; <span style="color: #008080;"> 4</span> <span style="color: #ff0000;"> *zoom</span>:<span style="color: #0000ff;"> 1</span>; <span style="color: #008080;"> 5</span> } <span style="color: #008080;"> 6</span> <span style="color: #800000;">.container p </span>{ <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;"> 8</span> } <span style="color: #008080;"> 9</span> <span style="color: #800000;">.container:after </span>{ <span style="color: #008080;">10</span> <span style="color: #ff0000;"> content</span>:<span style="color: #0000ff;"> ""</span>; <span style="color: #008080;">11</span> <span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> table</span>; <span style="color: #008080;">12</span> <span style="color: #ff0000;"> clear</span>:<span style="color: #0000ff;"> both</span>; <span style="color: #008080;">13</span> }
4:float元素去空格化
是什么意思呢?在平时的编码中,为了要符合HTML编码规范,都会为html标签添加缩进,达到美观的效果。可是缩进时就会产生空格,也就是 。当给元素设置左浮动时,元素本身左浮动,剩余的空格会被挤到最后,也就是上文所说的文字环绕效果。但是,要记住一点, 和回车敲下的空格的效果略有不同。
5:float元素块状化
在为内联元素设置浮动属性之后,display属性由inline变成block。并且可以为内联元素设置宽高。使用float加width属性可以实现一些简单的固定宽度的布局效果。
6:float流体布局
6.1:单侧固定,右侧自适应的布局。
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="left"</span><span style="color: #0000ff;">></span>左浮动+固定宽度<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">3</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="right"</span><span style="color: #0000ff;">></span>右边自适应宽度+margin-left<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">4</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container</span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> max-width</span>:<span style="color: #0000ff;">90%</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;">0 auto</span>; <span style="color: #008080;"> 4</span> } <span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span> <span style="color: #800000;">.left</span>{ <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;">left</span>; <span style="color: #008080;"> 8</span> <span style="color: #ff0000;"> text-align</span>:<span style="color: #0000ff;">center</span>; <span style="color: #008080;"> 9</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightpink</span>; <span style="color: #008080;">10</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 200px</span>; <span style="color: #008080;">11</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;">300px</span>; <span style="color: #008080;">12</span> } <span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #800000;">.right</span>{ <span style="color: #008080;">15</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightyellow</span>; <span style="color: #008080;">16</span> <span style="color: #ff0000;"> margin-left</span>:<span style="color: #0000ff;">200px</span>; <span style="color: #008080;">17</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;">300px</span>; <span style="color: #008080;">18</span> <span style="color: #ff0000;"> padding-left</span>:<span style="color: #0000ff;">10px</span>; <span style="color: #008080;">19</span> }
6.2:DOM与显示位置不同的单侧固定
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="right"</span><span style="color: #0000ff;">></span>右浮动+固定宽度<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">3</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="left"</span><span style="color: #0000ff;">></span>左边自适应宽度+margin-right<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">4</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> max-width</span>:<span style="color: #0000ff;"> 90%</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>; <span style="color: #008080;"> 4</span> } <span style="color: #008080;"> 5</span> <span style="color: #800000;">.container .right </span>{ <span style="color: #008080;"> 6</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> right</span>; <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 200px</span>; <span style="color: #008080;"> 8</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 200px</span>; <span style="color: #008080;"> 9</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightpink</span>; <span style="color: #008080;">10</span> } <span style="color: #008080;">11</span> <span style="color: #800000;">.container .left </span>{ <span style="color: #008080;">12</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightyellow</span>; <span style="color: #008080;">13</span> <span style="color: #ff0000;"> margin-right</span>:<span style="color: #0000ff;"> 200px</span>; <span style="color: #008080;">14</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;">15</span> <span style="color: #ff0000;"> padding-left</span>:<span style="color: #0000ff;"> 10px</span>; <span style="color: #008080;">16</span> }
也就是说,html元素与显示在页面上的元素位置不相同。
6.3:DOM与显示位置相同的单侧固定
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="left-content"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">3</span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> 左浮动+width100% </span><span style="color: #008000;">--></span> <span style="color: #008080;">4</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="left"</span><span style="color: #0000ff;">></span>margin-right<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">5</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">6</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="right"</span><span style="color: #0000ff;">></span>左浮动+固定宽度+margin-left负值<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">7</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> max-width</span>:<span style="color: #0000ff;"> 90%</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>; <span style="color: #008080;"> 4</span> } <span style="color: #008080;"> 5</span> <span style="color: #800000;">.container .right </span>{ <span style="color: #008080;"> 6</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 200px</span>; <span style="color: #008080;"> 8</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 200px</span>; <span style="color: #008080;"> 9</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightpink</span>; <span style="color: #008080;">10</span> <span style="color: #ff0000;"> margin-left</span>:<span style="color: #0000ff;"> -200px</span>; <span style="color: #008080;">11</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;">12</span> } <span style="color: #008080;">13</span> <span style="color: #800000;">.container .left </span>{ <span style="color: #008080;">14</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightyellow</span>; <span style="color: #008080;">15</span> <span style="color: #ff0000;"> margin-right</span>:<span style="color: #0000ff;"> 200px</span>; <span style="color: #008080;">16</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;">17</span> <span style="color: #ff0000;"> text-align</span>:<span style="color: #0000ff;"> center</span>; <span style="color: #008080;">18</span> }
6.4:智能布局
所谓智能布局,就是两栏都不需要设置宽度,宽度随内容自适应。
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="left"</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 3</span> <span style="color: #000000;"> float+margin-right+自适应宽度 </span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="right"</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 6</span> <span style="color: #000000;"> display:table-cell ---IE8+; </span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> display:inline-block ---IE7+; </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> 自适应宽度 </span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008080;">10</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> max-width</span>:<span style="color: #0000ff;"> 90%</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>; <span style="color: #008080;"> 4</span> } <span style="color: #008080;"> 5</span> <span style="color: #800000;">.container .left </span>{ <span style="color: #008080;"> 6</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;"> 8</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightpink</span>; <span style="color: #008080;"> 9</span> } <span style="color: #008080;">10</span> <span style="color: #800000;">.container .right </span>{ <span style="color: #008080;">11</span> <span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> table-cell</span>; <span style="color: #008080;">12</span> <span style="color: #ff0000;"> *display</span>:<span style="color: #0000ff;"> inline-block</span>; <span style="color: #008080;">13</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;">14</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightyellow</span>; <span style="color: #008080;">15</span> }
<span style="color: #008080;"> 1</span> <span style="color: #800000;">.container </span>{ <span style="color: #008080;"> 2</span> <span style="color: #ff0000;"> max-width</span>:<span style="color: #0000ff;"> 90%</span>; <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>; <span style="color: #008080;"> 4</span> } <span style="color: #008080;"> 5</span> <span style="color: #800000;">.container .left </span>{ <span style="color: #008080;"> 6</span> <span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;"> 8</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightpink</span>; <span style="color: #008080;"> 9</span> } <span style="color: #008080;">10</span> <span style="color: #800000;">.container .right </span>{ <span style="color: #008080;">11</span> <span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> table-cell</span>; <span style="color: #008080;">12</span> <span style="color: #ff0000;"> *display</span>:<span style="color: #0000ff;"> inline-block</span>; <span style="color: #008080;">13</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>; <span style="color: #008080;">14</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> lightyellow</span>; <span style="color: #008080;">15</span> }
总结以下:
1:当一个元素设置float属性时,会使元素块状化。
2:float属性的创造初衷就是为了文字环绕效果而生的。
3:float元素会使父元素高度塌陷。
4:float元素会除去换行带来的空格。
5:使用float可以实现两栏自适应的布局。
更多深入了解CSS,可以关注一下张鑫旭大牛