1、CSS布局之浮动

1.1、float之图文混排

float的意思就是元素漂浮在上层。

可以直接通过设置float属性实现图文混排,代码如下:

<div style="width:200px;height:200px;border: 1px solid gray;">
<img src="" alt="" style="width:100px;height:100px;float:right;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor.
</div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor.

1.2、float的副作用

当为元素设置float只有,它将会漂浮起来。那么它之后的元素就会忽略它,而进行定位。所以会导致元素重叠:

<div style="width:50px;height:50px;border:1px solid red;float:left">
我是悬浮元素
</div>
<div style="width:100px;height:100px;border:1px solid blue;">
我是标准div元素内容
</div>
我是悬浮元素
我是标准div元素内容

这个时候,就需要清除浮动(clear:both),另外,还可以通过clear:left|right来分别清除左右浮动:

<div style="width:50px;height:50px;border:1px solid red;float:left">
我是悬浮元素
</div>
<div style="width:100px;height:100px;border:1px solid blue;clear:both;">
我是标准div元素内容
</div>
我是悬浮元素
我是标准div元素内容

1.3、奇怪的浮动效果

在内容的浮动元素高度大于外部容器时,效果如下:

<div style="width:100px;border:1px solid blue;">
<div style="width:50px;height:150px;border:1px solid red;float:right">
我是悬浮元素
</div>
我是标准div元素内容
</div>
我是悬浮元素

我是标准div元素内容

 
**如何修复?**

可以通过clearfix样式来修复:

<style>
.clearfix {
overflow: auto;
zoom: 1;/*针对IE需要额外关照*/
}
</style>
<div class="clearfix" style="width:100px;border:1px solid blue;">
<div style="width:50px;height:150px;border:1px solid red;float:right">
我是悬浮元素
</div>
我是标准div元素内容
</div>

针对IE需要额外关照/
}
-->

我是悬浮元素

我是标准div元素内容

2、CSS布局之百分比宽度

百分比宽度可以非常容易的实现动态布局,但是当窗口变得很窄的时候,元素的展示可能会错乱。所以需要选择最合适的布局方式。

另外,不能使用min-width来修复这个问题,因为如果是左右结构,对左边的元素设定min-width,右边的元素是不会遵守的,可能会引起重叠。

3、CSS布局之媒体查询(Media Query)

“响应式设计(Responsive Design)”是一种让网站针对不同的浏览器和设备“响应”不同显示效果的策略,这样可以让网站在任何情况下显示的很棒!

如果要兼容移动端,请加上如下类似meta

<meta name="viewport" content="width=device-width, maximum-scale=2, minimum-scale=0.5, user-scalable=no">

因为众所周知,手机端的屏幕分辨率相当多,所以可以强制指定屏幕的width是等于设备宽度的,忽略分辨率。 maximum-scale 是指最大缩放比例。

4、CSS布局之column

CSS columns是较新的标准,不支持IE9及以下和Opera Mini,所以需要使用浏览器前缀。Column可以很轻松的实现文字的多列布局,示例如下:

<div style="width:150px;-webkit-column-count:3;-webkit-column-gap: 1em;">
CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局
</div>
CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局CSS布局

5、CSS布局之flexbox

flexbox布局模式被用来重新定义CSS中的布局方式。

<style>
.container{
width: 80%;
border: 1px solid red;
height:50px;
display: flex;
}
</style>
<div class="container">
<div style="width:100px;"></div>
<div style="flex:3;background:lightgray;">1</div>
<div style="flex:5;background:lightyellow;">2</div>
</div>

 
1
2

分析一下以上代码,只需要在容器上设置display:flex,那么内部元素的如果设置了flex样式,那么就会按照flex进行计算,然后实现flex布局。

flex布局,还能实现简单的垂直居中布局。

<div class="container" style="align-items: center; justify-content: center;">
我是垂直居中的元素
</div>
我是垂直居中的元素

其中,align-items设置水平居中,justify-content设置了垂直居中。

*: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. bzoj1003 物流运输
  2. django--app(六)
  3. transition、animation在macbook air上图片动画边缘抖动
  4. CSS2系列:外边距合并问题(margincollapse)
  5. es6新特性:
  6. 冒泡排序的python代码实现
  7. 7、创建ROS msg和srv
  8. urllib,request 设置代理
  9. Xshell连接ubuntu server端的vim(256色彩配置)
  10. [Luogu P1354]房间最短路问题
  11. 【原创】XAF 常见错误以及对应解决方法
  12. leetcode 第4题 Median of Two Sorted Arrays
  13. SpringBoot2.0 最简单的 idea 快速创建项目
  14. idea 更换svn地址
  15. poj 1080 基因组(LCS)
  16. linux ssh root登陆出现错误:Permission denied, please try again
  17. Linux系统服务之inetd
  18. 网页登入验证码的实现(java&amp;html)
  19. 解决Unity协程无法同步返回的问题
  20. thuwc2019总结

热门文章

  1. Create Oracle Enterprise Manager repository data after restore a database from another server
  2. VS2008 工程只生成dll不生成lib的解决方案
  3. MVC框架三大模块
  4. JAVA学习博客---2015-8
  5. toolControls添加工具项
  6. sublime Text 2 制表符
  7. .NET Mvc Razor也可以这样玩!
  8. C#中的线程二(Cotrol.BeginInvoke和Control.Invoke)
  9. Java IO8:IO简单总结
  10. 谈谈对BPM的理解