CSS布局(上)
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>
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布局(上)的更多相关文章
- 从零开始学习html(十二)CSS布局模型——上
一.css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了. 布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之 ...
- css常见的各种布局上(两列布局)
常见的布局上(两列布局) 1.常见的两列布局 1.1左边固定,右边自适应,左边宽度已知,右边默认占满整行,使用left 左浮动,右边不浮动,设置margin-left:左侧宽度 <style&g ...
- 界面设计技法之css布局
css布局之于页面就如同ECMAScript之于JS一般,细想一番,html就如同语文,css就如同数学,js呢,就是物理,有些扯远,这里就先不展开了. 回到主题,从最开始的css到如今的sass(l ...
- CSS布局 - 三栏布局
CSS布局技术可谓是前端技术中最基础的技术,就是因为基础,所以我认为要更加熟练,深入的去掌握,去梳理. 一. 传统 ---> 浮动实现的三栏布局 采用浮动实现的三栏布局有以下特点及注意事项: · ...
- DIV+CSS布局中主要CSS属性介绍
Float: Float属性是DIV+CSS布局中最基本也是最常用的属性,用于实现多列功能,我们知道<div>标签默认一行只能显示一个,而使用Float属性可以实现一行显示多个div的功能 ...
- CSS 布局
近日开发中,总感觉页面布局方面力不从心.以前也曾学过这方面的内容,但是不够系统,因此我打算整理一下. 在web 页面中一般有 table 和 css+div 两种布局方式. 其中css+div 又分为 ...
- CSS布局经典—圣杯布局与双飞翼布局
在我之前的博客网页整体布局完全剖析-剖完你不进来看一下么?中总结单列.两列.三列固宽与变宽布局,我还以为已经囊括了所有经典的网页布局方法了呢,当然除了CSS3的弹性盒模型没有涉及到,现在看来确实是自己 ...
- html学习第三天—— 第12章——css布局模型
清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上,又不同于我们常说的 ...
- CSS布局基础——BFC
what's BFC? 第一次看到这个名词,我是拒绝的,css什么时候还有这个东西?于是迫不及待的google了一下,才发现原来它无时无刻不在我们的css当中,只不过它并不是一个属性,不需要我们平常使 ...
随机推荐
- idea中maven报错:无效的目标发行版: 1.8
1.project.pom中修改版本 <maven.compiler.source>1.7</maven.compiler.source><maven.compiler. ...
- Mac OS环境下配置Myeclipse2015的经验
反复测试装了多次,现在把成功安装的方法陈列如下: 1. 相关的资源: (1)下载 myeclipse-2015-stable-2.0-offline-installer-macosx.dmg 链接:h ...
- 6.用CXF编写基于Spring的WebService
首先是服务器端: //实体类 public class Weather { private String region;//区域编码 private String regionName;//区域名称 ...
- 【转】Eclipse打JAR包,插件FatJar安装与使用
原文地址:http://blog.csdn.net/jikeyzhang/article/details/4731968 下载RUL: 下载fatJar插件,解压缩后是一个.../plugins/(n ...
- 使用IronPython给.Net程序加点料
开发的时候,经常被策划频繁变动的方案而苦恼.这时候就想要加入点动态语言来辅助一下. 在考虑用动态语言之前也曾想过使用动态加载dll的方式,实现基础接口来调用.在卸载的时候遇到了问题,虽可以通过应用程序 ...
- 《JAVA与模式》之抽象工厂模式
场景问题 举个生活中常见的例子——组装电脑,我们在组装电脑的时候,通常需要选择一系列的配件,比如CPU.硬盘.内存.主板.电源.机箱等.为讨论使用简单点,只考虑选择CPU和主板的问题. 事实上,在选择 ...
- 用JQ仿造礼德财富网的图片查看器
现在就职于一家P2P平台,自然也会关注同行其它网站的前端技术,今天要仿造的是礼德内页的一个图片查看器效果.不过说白了,无论人人贷也好礼德财富也好,很多地方的前端都做的不尽如人意,比如忽略细节.缺乏交互 ...
- RCP:解决Navigator快捷键不生效的问题
自己扩展CNF之后,导航栏的删除.复制.黏贴等快捷键失效了,在网上搜索了半天,结果最终不如自己看源码. 本篇文章的主要目的不止于解决快捷键失效,更在于如何处理类似的问题,如何利用debug快速定位.这 ...
- 算法:POJ1008 Maya Calendar
此题非常水,不做说明. package practice; import java.io.BufferedInputStream; import java.util.Scanner; /** * @a ...
- 一个App完成入门篇(五)- 完成新闻页面
本节教程将介绍如何用DeviceOne简单而高效的完成一个新闻页面. 导入项目 数据模板分离MVVM模型 自定义事件 展示新闻 九宫格展示 将要学习的demo效果图如下所示 1. 导入完整项目 本节示 ...