CSS布局(上)

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

1、CSS布局之display

1.1、dispaly

dispaly是CSS中最重要的用于控制布局的属性,每个元素都有一个默认的display,大多数元素的默认值通常是block(块级元素)或inline(行内元素)。

另一个常用的display是none。一些特殊元素的默认值就是它,如script、link等。

1.2 display:none 与 visibility:hidden

display设置为none,是不会保存元素本该显示的空间,但是visibility:hidden会保留。

<div style="width: 100px; height: 100px; border: 1px solid red;float:left;">
<span style="display:none;">ABCD</span>EFG
</div>
<div style="width: 100px; height: 100px; border: 1px solid red;float:left;">
<span style="visibility:hidden;">ABCD</span>EFG
</div>
ABCDEFG
ABCDEFG
 

1.3、更多的display值

比较常用的有list-item,inline-block,table,table-cell,flex等。

全部列表如下:

none inline block contents list-item inline-block inline-table 

table table-cell table-column table-column-group table-footer-group table-header-group table-row table-row-group 

flex inline-flex grid inline-grid 

ruby ruby-base ruby-text ruby-base-container ruby-text-container

run-in

/* Global values */
display: inherit;
display: initial;
display: unset;

1.4 可改写的display属性

虽然每个元素都有默认的display,但是我们可以随时随地的重写它,比如将li元素修改为inline-block,制作水平菜单。

2、元素居中

2.1、水平居中

通过设置margin为auto可以实现水平居中,前提是元素必须得有宽度

<div style="width:400px;margin:0 auto;height:10px;border:1px solid red;"></div>
 

2.2、垂直居中

因为table的cell可以设置垂直居中,所以玩么可以模拟这样的效果

<div style="width: 400px;height: 200px;border: 1px solid red;display: table-cell; vertical-align: middle;">
<div style="width:100px; height:100px;background: blue;"></div>
</div>
 

2.3、绝对居中

知道水平居中和垂直居中,那么绝对居中就比较容易实现了。组合一下:

<div style="width: 200px;height: 200px;border: 1px solid red;display: table-cell; vertical-align: middle;">
<div style="width:100px; height:100px;background: blue;margin:0 auto;"></div>
</div>
 

还有没有更好的方式呢?如下:

通过设置position:absolute,然后top、bottom、left、right值为0,margin:auto;实现绝对居中。 如果要相对容器居中,设置容器的position为relative。

<div style="width: 200px;height: 200px;border: 1px solid red; position:relative;">
<div style="width:100px; height:100px;background: blue;margin:auto;position:absolute;top:0;left:0;bottom:0; right: 0;"></div>
</div>
 

3、盒子模型

盒子模型(box-sizing)有两种典型值,分别为content-box,border-box。

3.1、content-box

此时,设置在元素上的宽度为内容宽度,那么元素所占用的宽度为:width + border * 2 + padding * 2 + margin * 2。宽度同理

3.2、border-box

此时,设置在元素上的宽度为包含border的宽度,那么占用总宽度为width + margin * 2。内容宽度为width - padding * 2 - border * 2。

3.3 示例

<div style="width:100px; margin: 10px; padding: 15px; border: 5px solid blue; box-sizing:content-box"></div>
<div style="width:100px; margin: 10px; padding: 15px; border: 5px solid blue; box-sizing:border-box"></div>
 
 

3.4、浏览器兼容性

为了保证浏览器兼容性,需要加上特定浏览器前缀。

4、元素定位

如果要实现更多复杂的布局,那么就需要了解下position了。

4.1、position:static

static是position属性的默认值,position:static的元素不会被特殊定位。

4.2、position:relative

在相对定位(relative)的元素上设置top、right、bottom、left会使其偏离正常位置,其他元素不会调整位置来弥补它偏离后剩下的空隙。

<div style="border:1px solid red; width: 400px; height: 200px;">
<div style="background: blue; width:100px; height: 100px;"></div>
ABCDE
</div>
<div style="border:1px solid red; width: 400px; height: 200px;">
<div style="background: blue; width:100px; height: 100px;position:relative; left: 100px;top:50px;"></div>
ABCDE
</div>
 

ABCDE

 

ABCDE

4.3、position:fixed

固定定位(fixed)元素会相对于视窗来定位,所以就算页面滚动,它还是会留在相同位置。示例请看左下角。

<div style="width: 100px;height:100px; position:fixed; bottom: 0; right: 0;
border: 1px solid red;">固定定位</div>
固定定位

4.4、position:absolute

绝对定位元素(absolute)与fixed类似,但是它不是相对视窗,而是相对最近的positioned(position值不是static的元素都是positioned元素)祖先元素,如果没有这样的祖先元素,那么它相对于文档的body元素,并且会随着页面滚动而滚动。

<div style="border:1px solid red; width: 400px; height: 200px;position:relative;">
<div style="background: blue; width:100px; height: 100px;position:absolute;top: 25px;right:25px;"></div>
</div>
 

CSS布局(上)的更多相关文章

  1. 从零开始学习html(十二)CSS布局模型——上

    一.css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了. 布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之 ...

  2. css常见的各种布局上(两列布局)

    常见的布局上(两列布局) 1.常见的两列布局 1.1左边固定,右边自适应,左边宽度已知,右边默认占满整行,使用left 左浮动,右边不浮动,设置margin-left:左侧宽度 <style&g ...

  3. 界面设计技法之css布局

    css布局之于页面就如同ECMAScript之于JS一般,细想一番,html就如同语文,css就如同数学,js呢,就是物理,有些扯远,这里就先不展开了. 回到主题,从最开始的css到如今的sass(l ...

  4. CSS布局 - 三栏布局

    CSS布局技术可谓是前端技术中最基础的技术,就是因为基础,所以我认为要更加熟练,深入的去掌握,去梳理. 一. 传统 ---> 浮动实现的三栏布局 采用浮动实现的三栏布局有以下特点及注意事项: · ...

  5. DIV+CSS布局中主要CSS属性介绍

    Float: Float属性是DIV+CSS布局中最基本也是最常用的属性,用于实现多列功能,我们知道<div>标签默认一行只能显示一个,而使用Float属性可以实现一行显示多个div的功能 ...

  6. CSS 布局

    近日开发中,总感觉页面布局方面力不从心.以前也曾学过这方面的内容,但是不够系统,因此我打算整理一下. 在web 页面中一般有 table 和 css+div 两种布局方式. 其中css+div 又分为 ...

  7. CSS布局经典—圣杯布局与双飞翼布局

    在我之前的博客网页整体布局完全剖析-剖完你不进来看一下么?中总结单列.两列.三列固宽与变宽布局,我还以为已经囊括了所有经典的网页布局方法了呢,当然除了CSS3的弹性盒模型没有涉及到,现在看来确实是自己 ...

  8. html学习第三天—— 第12章——css布局模型

    清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上,又不同于我们常说的 ...

  9. CSS布局基础——BFC

    what's BFC? 第一次看到这个名词,我是拒绝的,css什么时候还有这个东西?于是迫不及待的google了一下,才发现原来它无时无刻不在我们的css当中,只不过它并不是一个属性,不需要我们平常使 ...

随机推荐

  1. 使用HtmlAgilityPack解析Html(非常好用)

    /// <summary> /// 设计成一个exe,解决WebBrowser控件内存泄漏的问题. /// </summary> public partial class Ma ...

  2. js中的变量小例子

    s中的变量function foo(){ n=99;}alert(n);//undefined,因为没有调用该函数 function foo(){ n=99;}foo();alert(n);//99, ...

  3. [XAF] How to set List View Columns Title Customization align center?

    https://www.devexpress.com/Support/Center/Question/Details/T423138

  4. webdriver 获取alert 提示no alert is active

    http://hi.baidu.com/janice515/item/bce536bb136e8441bb0e120f 摘上面: 一般正常情况下会报错,如 no alert is  active  目 ...

  5. JavaScript + HTML 虚拟键盘效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. linux redmine 搭建

    redmine搭建过程参考:http://www.tuicool.com/articles/InMbym 注意事项: 配置文件必须以2个空格开始 启动(指定了端口,和绑定的IP): ruby bin/ ...

  7. .net core 中环境变量的配置

    配置文件: Properties目录下的launchSettings.json IISExpress配置 "ASPNET_ENV": "EnvironmentOne&qu ...

  8. 哪些JavaScript IDE最好用?

    阅读本文之前,分享大家一张图片,看图会发现JavaScript开发需求最高,占比达到42.84%,因此掌握JavaScript语言好工作就不愁啦,工欲善其事必先利其器,那么选择IDE来开发是至关重要的 ...

  9. ASP.NET Web API 应用教程(一) ——数据流使用

    相信已经有很多文章来介绍ASP.Net Web API 技术,本系列文章主要介绍如何使用数据流,HTTPS,以及可扩展的Web API 方面的技术,系列文章主要有三篇内容. 主要内容如下: I  数据 ...

  10. Java序列化格式详解

    RPC的世界,由于涉及到进程间网络远程通信,不可避免的需要将信息序列化后在网络间传送,序列化有两大流派: 文本和二进制. 文本序列化 序列化的实现有很多方式,在异构系统中最常用的就是定义成人类可读的文 ...