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>
时间: 2024-08-11 01:23:40

CSS布局(上)的相关文章

上中下结构DIV CSS布局实例

实例布局之上中下结构DIV CSS布局 上中下结构为常见布局结构,一般普通(企业网站)网页我们可以大致分为上(头部).中(内容区).下(底部版权)组成.而这其实是由最简单上下结构CSS布局演变而成,只不过多了一个DIV层结构而且,本质布局方法技巧不变. 一.主要思维   -   TOP 不管多少个上下结构或单一结构,一般主体内容都是居中的,这个使用就需要使用css margin样式(让布局居中兼容各大浏览器),同时一般网页都会固定宽度,也就是要使用css width设置好每个DIV层宽度. 这里

DIVCSS5模块 上标题下简介列表DIV CSS布局

类似上下结构的上标题下简介列表DIV CSS布局实例模块 大标题+简单简介列表模块CSS布局,布局讲解.图文+代码介绍,在线演示,打包下载该模块完整源代码. 上标题下简介上下结构列表CSS布局效果截图 布局这样的模块,标题一般只占一行,标题文字过多自动换行的文字将自动隐藏处理,简介内容字数控制来显示最多两排,通常上海早泄治疗医院程序员会考虑调用最多多少个文字字数. 一.DIVCSS模块布局分析   -   TOP 从局部模块分析很容易找到规律,每列都是标题+简介方式布局,下边出现下边框线效果.找

找人上门官网的CSS布局:上中下三栏自适应高度CSS布局

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

前端设计之CSS布局:上中下三栏自适应高度CSS布局

网页代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Co

深度理解div+css布局嵌套盒子

1. 网页布局概述 网页布局的概念是把即将出现在网页中的所有元素进行定位,而CSS网页排版技术有别于传统的网页排版方法,它将页面首先在整体上使用<div>标记进行分块,然后对每个快进行CSS定位以及设置显示效果,最后在每个块中添加相应的内容.利用CSS排版方法更容易地控制页面每个元素的效果,更新也更容易,甚至页面的拓扑结构也可以通过修改相应的CSS属性来重新定位.  2. 盒子模型 盒子模型是CSS控制页面元素的一个重要概念,只有掌握了盒子模型,才能让CSS很好地控制页面上每一个元素,达到我们

现在主流网站为什么都用div+css布局而不是用table

由于刚刚接触前端,一直觉得table布局在代码上看起来比div+css更整洁,div+css布局的页面,一堆的<div><div><div>...</div></div></div>看起来都让人犯密集恐惧症,那么为什么现在的主流网站还都乐此不疲呢?为什么div+css反而成了一种主流布局方式呢?一直对此不解.这篇文章好像是解决了我的问题,先摘录过来,以便查阅. 以下内容摘自:http://www.divcss5.com/wenji/w

&lt;转载&gt;div+css布局教程之div+css常见布局结构定义

在使用div+css布局时,首先应该根据网页内容进行结构设计,仔细分析和规划你的页面结构,你可能得到类似这样的几块: 页面层容器.页面头部.标志和站点名称.站点导航(主菜单).主页面内容.子菜单.搜索框.页脚(版权和有关法律声明). 通常采用DIV元素来将这些结构定义出来,类似这样: <div id="Container"></div> 页面层容器 <div id="header"></div> 页面头部 <di

&lt;转载&gt;Div+Css布局教程(-)CSS必备知识

目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的高度(Height:45px;). Background:设置对象的背景颜色.背景图像. 1.背景颜色 background:#09F; 2.背景图像 background:url(file:///C|/Users/Administrator/Desktop/huipu.jpg) repeat-x;

css012 css布局简介

css012  css布局简介 一.    网页布局的类型 网页布局的类型 1.固定宽度 2.流式 3.相应式web设计 二.    如何进行css布局 1.强大的<div>标签 网页的html代码数量应该尽可能减到最少,尤其是div标签的嵌套,当使用div标签有助于网页布局设计时,就很好用.但是并不是div嵌套的越多越好. 2.html5的分区元素 <div>  用于分割区域,块级元素 <span> 行内分区元素 <article> 给网页或者网页的分区提