「译」CSS之展示与布局 Display and Positioning

译自:codecademy

Flow of HTML

浏览器会从左到右边,从上到下,渲染没有CSS的HTML elements,也被称为 flow of elements in HTML.

CSS也有可以有关改变浏览器排布elements的properties。其明确了一个element在页面中应当位于何处,它是否应当与other elements共处一行,及其他相关属性。

本篇介绍了以下5个properties:

position    display    z-index    float    clear

Position

block-level elements

Block-level elements 对整行宽度创造了一个block,它防止 other elements 与其出现在同一水平空间内。

值得注意的是,block-level elements只占其自己所在那一行的空间,并不覆盖其他elements,它的默认位置是在浏览器左边。通过设置position property, 你可以改变 the default position of an element。它包含以下values:

static

static - the default value (it does not need to be specified)

relative

这个value使得你可以相对于(relative) 它的 default static position 来  position an element。

.box-bottom { 

 position: relative;

}

尽管上述代码指导浏览器去expect a relative positioning of the div,但是它没有明确那个div应该放在哪。(也就是具体放在哪?)

.box-bottom { 

 position: relative;

top: 20px;

left: 50px;

}

在上例中<div>被2个 offset(边距) properties 设置了位置,它一共有4个属性:

top - moves the element down.

bottom - moves the element up.

left - moves the element right.

right - moves the element left.

上例中,相对于其 default static position(下图虚线框处),the <div> 向下移动了20px,向左移动了50px。

注意,如果 position property 被设为static (default),offset properties 将不再生效。

Position: Absolute

当一个 element 的 position 被设为 absolute 时,all other elements 将会忽略它,就像它不在页面上一样。而该 element 将会根据它最近的 positioned parent element (设置了position属性,且其value不是static)来定位。(若没有,则根据<body>定位)

.box-bottom { 

     background-color: DeepSkyBlue;

     position: absolute; 

     top: 20px; 

     left: 50px;

}

在上例中, the .box-bottom <div> 会从左上角向下、向右移动。若不设置 offset properties , the top box 将完全被 by the bottom box 覆盖。请看下方图片:


absolute

Position: Fixed

When an element’s position is set to absolute,其位置会随着滚轮而滚动。若需固定某个元素的位置,则需要将其 position 的值设为 fixed 。(常用于导航栏)

.box-bottom { 

 background-color: DeepSkyBlue;

     position: fixed; 

     top: 20px; 

     left: 50px;

}

fixed

Z-Index

当页面上的boxes设置了不同的position时,它们可能会互相覆盖,导致其content难以阅读。

.box-top {

    background-color: Aquamarine;

}

.box-bottom {

    background-color: DeepSkyBlue;

    position: absolute;

    top: 20px;

    left: 50px;

}

上例中,当页面滚动时,.box-bottom<div> 会忽略 .box-top <div> ,并将其覆盖。

当elements相互覆盖时,The z-index property 控制 an element 在页面上应该多靠后或多靠前(PS图层)。

The z-index property 只能为integer value(整数值)。

上例中,我们之所以设置了the .box-top position to relative ,是因为z-index属性对 static elements 不会生效The z-index of 2 使得 .box-top element 移到前面一层。因为它比 .box-bottomz-index, 1 更大。

如上图, 你会看到 the top box 已经移到了 the bottom box 的前面。

Display

每个HTML elements 都有 a default display value ,其决定(dictate)了该 element 是否与 other elements 共用一个 horizontal space。

Some elements 填满了整行宽度,无论其 content size是多少。Other elements 则视 their content 所需,来占用相应的 horizontal space ,他们可以横向紧挨 other elements。

In this lesson, we’ll cover three values for the display property: inlineblock, and inline-block.

Inline Display

某些 tags, 如 <em>, <strong>, and <a>,被称为 inline elements,其 display 属性的默认值为 inline。

Inline elements 有一个紧紧包裹着 their content 的 box,它仅占据所需空间来展示,不需要在其他elements后面另起一行。这些 element 的 height and width 无法被CSS文档指定。例如:


上述代码将显示为:To learn more about inline elements, click MDN documentation.

Display property 可以使得任何(any)element 变为 inline element,包括那些默认值不是 inline 的 elements,如: paragraphs, divs, and headings.


The CSS in the example above will change the display of all<h1>elements toinline. The browser will render<h1>elements on the same line as other inline elements immediately before or after them (if there are any).

上图中,所有<h1>elements 将变为 inline elements,浏览器将在一行渲染所有<h1>elements 和 other inline elements ,如果它们是紧挨着的话( immediately before or after them )。

Block Display

Block Display 在默认会占满整行宽度,称为 block-levele elements.同时,它们的width属性可以被设置。除非有其他方面的(otherwise)的指定,否则其 height 是适应(accommodate)其content的。如heading elements(<h1>through<h6>),<p>,<div>and<footer>。

即使内容不够,也将独占一行

Inline-Block Display

Display属性的第三个value是 inline-block。Inline-block display 兼有 inline and block elements 的特点。Inline-block elements 可以在一行内相互紧靠。同时我们也能通过width and height properties,来设定它的尺寸(dimension)。Images are the best example of default inline-block elements.

举例来说,下图CSS中的 <div>s 将会以指定的尺寸展示在一行。

上例中,3个包含文本内容的矩形的divs将会展示在一行(如果提供足够的横向宽度的话),且每个的尺寸都是200px*300px。即使内部文本不需要这么大的空间,也是如此。

Display: Flex

这个display属性的值——flex,可以用来水平排列elements。

下例中,有一个class值为parent的div:

为了使div中的子元素横向排列,在CSS中可以使用:

第一行表示其子元素横向排列;

第二行确保没有child element 排出页面外;

第三行使子元素在页面水平居中。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容