CSS layout

WBOY
WBOYOriginal
2016-08-18 08:57:571610browse

Before explaining CSS layout, we need to know some knowledge in advance. In CSS, label elements in html are generally divided into three different types:

block elements, inline elements (also called inline elements) and inline block elements.

Commonly used block elements are:

dc6dce4a544fdca2df29d5ac0ea9906b, e388a4556c0f65e1904146cc1a846bee, 4a249f0d628e2318394fd9b75b4636b1...4e9ee319e0fa4abc21ff286eeb145ecc, c34106e0b4e09414b63b2ea253ff83d6, ff6d136ddc5fdfeffaf53ff6ee95f185, 5c69336ffbc20d23018e48b396cdd57a, f5d188ed2c074f8b944552db028f98a1, 208700f394e4cf40a7aa505373e0130b, < ;blockquote> 、ff9c23ada1bcecdd1a0fb5d5a0f18437

Commonly used inline elements are:

3499910bf9dac5ae3c52d5ede7383485, 45a2772a6b6107b401db3c9b82c049c2, 0c6dc11e160d3b678d68754cc175188a, 5a8028ccc7a7e27417bff9f05adf5932, 907fae80ddef53131f3292ee4f81644b, 8e99a69fbe029cd4e2b854e244eab143, 2e1cf0710519d5598b1f0f14c36ba674, 1244aa79a84dea840d8e55c52dc97869, b7f90f73cad438258bf67e62f79b2113, a34afbe0485df7c4c781fbf9013f75c0

Commonly used inline block elementsare:

a1f02c36ba31691bcfe87b2722de723b, d5fd7aea971a85678ba271703566ebfd

Element classification--block-level elements

What are block-level elements? In HTML, dc6dce4a544fdca2df29d5ac0ea9906b, e388a4556c0f65e1904146cc1a846bee, 4a249f0d628e2318394fd9b75b4636b1, ff9c23ada1bcecdd1a0fb5d5a0f18437, ff6d136ddc5fdfeffaf53ff6ee95f185 and 25edfb22a4f469ecb59f1190150159c6 are block-level elements. Setting display:block is to display the element as a block-level element. The following code converts the inline element a into a block element, so that the a element has the characteristics of block element.

a{display:block;}

Block-level element features:

1. Each block-level element starts on a new line, and subsequent elements also start on a new line. (It’s so domineering, one block-level element occupies one row)

2. The height, width, line height, top and bottom margins of the element can be set.

3. When not set, the width of an element is 100% of its parent container (the same as the width of the parent element), unless a width is set.

Element classification--inline elements

In HTML, 45a2772a6b6107b401db3c9b82c049c2, 3499910bf9dac5ae3c52d5ede7383485, 2e1cf0710519d5598b1f0f14c36ba674, 8e99a69fbe029cd4e2b854e244eab143 and 907fae80ddef53131f3292ee4f81644b are typical inline elements (inline elements) (inline) elements. Of course, block elements can also be set to inline elements through code display:inline. The following code converts the block element div into an inline element, so that the div element has the characteristics of an inline element.

 div{
     display:inline;
 }

......

<div>我要变成内联元素</div>

Inline element features:

1, and other elements are on the same line;

2. The height, width and top and bottom margins of the element cannot be set ;

3. The width of an element is the width of the text or image it contains and cannot be changed.

Element classification--inline block elements

Inline block element (inline-block) has the characteristics of both inline elements and block elements. The code display:inline-block sets the element as an inline block element. (New in css2.1), a1f02c36ba31691bcfe87b2722de723b and d5fd7aea971a85678ba271703566ebfd tags are such inline block tags.

inline-block element features:

1, and other elements are on the same line;

2. The height, width, line height, top and bottom margins of the element can be set.

Box model

css layout model

After understanding the basic concepts and box model types of CSS box models, we can delve into the basic model of web page layout. The layout model, like the box model, is the most basic and core concept of CSS. But the layout model is based on the box model and is different from what we often call CSS layout styles or CSS layout templates. If the layout model is the foundation, then the CSS layout template is the end, the external expression.
CSS contains 3 basic layout models, summarized in English as: Flow, Layer and Float.
In web pages, elements have three layout models:
1. Flow model (Flow)
2. Floating model (Float)
3. Layer model (Layer)

流动模型

先来说一说流动模型,流动(Flow)是默认的网页布局模式。也就是说网页在默认状态下的 HTML 网页元素都是根据流动模型来分布网页内容的。

流动布局模型具有2个比较典型的特征:

第一点,块状元素都会在所处的包含元素内自上而下按顺序垂直延伸分布,因为在默认状态下,块状元素的宽度都为100%。实际上,块状元素都会以行的形式占据位置。如右侧代码编辑器中三个块状元素标签(div,h1,p)宽度显示为100%。

 

第二点,在流动模型下,内联元素都会在所处的包含元素内从左到右水平分布显示。(内联元素可不像块状元素这么霸道独占一行)

 

右侧代码编辑器中内联元素标签a、span、em、strong都是内联元素。

浮动模型

块状元素这么霸道都是独占一行,如果现在我们想让两个块状元素并排显示,怎么办呢?不要着急,设置元素浮动就可以实现这一愿望。

任何元素在默认情况下是不能浮动的,但可以用 CSS 定义为浮动,如 div、p、table、img 等元素都可以被定义为浮动。如下代码可以实现两个 div 元素一行显示。

<span style="color: #000000;">div{
    width:200px;
    height:200px;
    border:2px red solid;
    float:left;
}
</span><span style="color: #0000ff;">2beeb2847e934999dc8b5f8f0e43d3704d765cbf8ce485a2a526f75a4eba1cce</span>
<span style="color: #0000ff;">90c1e677996bfc13f574f9f3045f90494d765cbf8ce485a2a526f75a4eba1cce</span>

 

层模型--绝对定位

如果想为元素设置层模型中的绝对定位,需要设置position:absolute(表示绝对定位),这条语句的作用将元素从文档流中拖出来,然后使用left、right、top、bottom属性相对于其最接近的一个具有定位属性的父包含块进行绝对定位。如果不存在这样的包含块,则相对于body元素,即相对于浏览器窗口

如下面代码可以实现div元素相对于浏览器窗口向右移动100px,向下移动50px。

<span style="color: #000000;">div{
    width:200px;
    height:200px;
    border:2px red solid;
    position:absolute;
    left:100px;
    top:50px;
}
</span><span style="color: #0000ff;">2beeb2847e934999dc8b5f8f0e43d3704d765cbf8ce485a2a526f75a4eba1cce</span>

层模型--相对定位

如果想为元素设置层模型中的相对定位,需要设置position:relative(表示相对定位),它通过left、right、top、bottom属性确定元素在正常文档流中的偏移位置。相对定位完成的过程是首先按static(float)方式生成一个元素(并且元素像层一样浮动了起来),然后相对于以前的位置移动,移动的方向和幅度由left、right、top、bottom属性确定,偏移前的位置保留不动。

如下代码实现相对于以前位置向下移动50px,向右移动100px;

 

<span style="color: #000000;">#div1{
    width:200px;
    height:200px;
    border:2px red solid;
    position:relative;
    left:100px;
    top:50px;
}

</span><span style="color: #0000ff;">2beeb2847e934999dc8b5f8f0e43d3704d765cbf8ce485a2a526f75a4eba1cce</span>

层模型--固定定位

fixed:表示固定定位,与absolute定位类型类似,但它的相对移动的坐标是视图(屏幕内的网页窗口)本身。由于视图本身是固定的,它不会随浏览器窗口的滚动条滚动而变化,除非你在屏幕中移动浏览器窗口的屏幕位置,或改变浏览器窗口的显示大小,因此固定定位的元素会始终位于浏览器窗口内视图的某个位置,不会受文档流动影响,这与background-attachment:fixed;属性功能相同。以下代码可以实现相对于浏览器视图向右移动100px,向下移动50px。并且拖动滚动条时位置固定不变。

<span style="color: #000000;">#div1{
    width:200px;
    height:200px;
    border:2px red solid;
    position:fixed;
    left:100px;
    top:50px;
}
</span><span style="color: #0000ff;">fed9cad356cec09afa3e2a7bd2513208</span>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。<span style="color: #0000ff;">aea4995305aa2743a243ce011e9fcbb5</span>

隐性改变display类型

有一个有趣的现象就是当为元素(不论之前是什么类型元素,display:none 除外)设置以下 2 个句之一:

 1. position : absolute 

 2. float : left 或 float:right 

简单来说,只要html代码中出现以上两句之一,元素的display显示类型就会自动变为以 display:inline-block(块状元素)的方式显示,当然就可以设置元素的 width 和 height 了,且默认宽度不占满父元素。

如下面的代码,小伙伴们都知道 a 标签是 行内元素 ,所以设置它的 width 是 没有效果的,但是设置为 position:absolute 以后,就可以了。

<span style="color: #0000ff;">b04f026226cff5a2aa6e2a4016f5fa27</span>
    <span style="color: #0000ff;">40cdaec49a935fbbe7039cc0c07207d9</span>进入课程请单击这里<span style="color: #0000ff;">ef2198faed6b9ce9f38821054836d50e</span>
<span style="color: #0000ff;">4d765cbf8ce485a2a526f75a4eba1cce</span><span style="color: #000000;">
css代码

</span><span style="color: #0000ff;">3b21e6c8c45975c3ab5015aaf5f6e09a</span><span style="background-color: #f5f5f5; color: #800000;">
.container a</span><span style="background-color: #f5f5f5; color: #000000;">{</span><span style="background-color: #f5f5f5; color: #ff0000;">
    position</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">absolute</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;">
    width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">200px</span><span style="background-color: #f5f5f5; color: #000000;">;</span><span style="background-color: #f5f5f5; color: #ff0000;">
    background</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;">#ccc</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #0000ff;">6d2d69a16dc4083b65da95a7bbc232fc</span>

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:CSS3---2D transformationNext article:CSS3---2D transformation