布局、响应式布局

http://segmentfault.com/a/1190000003931851

响应式布局:http://www.jianshu.com/p/b6ef62709f15      详情查看:D:\个人笔记\响应式布局

多列布局

左列定宽,右列自适应该布局方式非常常见,适用于定宽的一侧常为导航,自适应的一侧为内容的布局

<body>
    <div class="left">left</div>
    <div class="right">right</div>
</body>
<style type="text/css">
        .left{
            width: 100px;
            height: 100px;
            background: yellow;
            float: left;
        }
        .right{
            height: 100px;
            background: green;
            margin-left: 100px;
        }
</style>               注:IE6会有3px的bug

使用float+overflow实现

.left{width:100px;float:left;}
.right{overflow:hidden;}

触发bfc模式,浮动无法影响,隔离其他元素,IE6不支持,左侧left设置margin-left当作left与right之间的边距,右侧利用overflow:hidden 进行形成bfc模式

如果我们需要将两列设置为等高,可以用下述方法将“背景”设置为等高,其实并不是内容的等高

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .left{
            width: 100px;
            height: 100px;
            background: yellow;
            float: left;
        }
        .right{
            height: 100px;
            background: green;
            overflow: hidden;
        }
        .parent{overflow:hidden;}
        .left,.right{padding-bottom:9999px;margin-bottom:-9999px;}
</style> 
</head>
<body>
<div class="parent">
    <div class="left">left</div>
    <div class="right">right</div>
</div>
</body>

使用table实现

.parent{display:table;table-layout:fixed;width:100%;}
.left{width:100px;}.right,.left{display:table-cell;}

利用右侧容器的flex:1,均分了剩余的宽度,也实现了同样的效果。而align-items 默认值为stretch,故二者高度相等

 

右列定宽,左列自适应

实用float+margin实现

 

.parent{background:red;height:100px;margin:0 auto;}
.left{background:green;margin-right:-100px;width:100%;float:left;}
.right{float:right;width:100px;background:blue;}

使用table实现

.parent{display:table;table-layout:fixed;width:100%;}
.left{display:table-cell;}
.right{width:100px;display:table-cell;}

实用flex实现

.parent{display:flex;}
.left{flex:1;}
.right{width:100px;}

两列定宽,一列自适应

基本html结构为父容器为parent,自容器为left,center,right.其中,left,center定宽,right自适应

利用float+margin实现

.left,.center{float:left:width:200px;}
.right{margin-left:400px;}

利用float+overflow实现

.left,.center{float:left:width:200px;}
.right{overflow:hidden;}

利用table实现

.parent{display:table;table-layout:fixed;width:100%;}
.left,.center,.right{display:table-cell;}
.left,.center{width:200px;}

利用flex实现

.parent{display:flex;}
.left,.center{width:100px;}
.right{flex:1}

两侧定宽,中栏自适应

利用float+margin实现

 

.left{width:100px;float:left;}
.center{float:left;width:100%;margin-right:-200px;}
.right{width:100px;float:right;}

利用table实现

.parent{width:100%;display:table;table-layout:fixed}
.left,.center,.right{display:table-cell;}
.left{width:100px;}.right{width:100px;}

利用flex实现

.parent{display:flex;}
.left,.center{width:100px;}
.right{flex:1}

一列不定宽,一列自适应

利用float+overflow实现

 

.left{float:left;}
.right{overflow:hidden;}

利用flex实现

.parent{display:flex;}
.right{flex:1;}

多列等分布局

多列等分布局常出现在内容中,多数为功能的,同阶级内容的并排显示等。

<div class="parent"><div class="column">1</div><div class="column">1</div><div class="column">1</div><div class="column">1</div>
</div>

实用float实现

.parent{margin-left:-20px}/*假设列之间的间距为20px*/
.column{float:left;width:25%;padding-left:20px;box-sizing:border-box;}

利用table实现

.parent{margin-left:-20px;}
.parent{display:table;table-layout:fixed;width:100%;}
.column{display:table-cell;padding-left:20px;}

利用flex实现

.parent{display:flex;}
.column{flex:1;}
.column+.column{margin-left:20px;}

九宫格布局

使用table实现
<div class="parent">
<div class="row"><div class="item"></div><div class="item"></div><div class="item"></div></div>
<div class="row"><div class="item"></div><div class="item"></div><div class="item"></div></div>
<div class="row"><div class="item"></div><div class="item"></div><div class="item"></div></div>
</div>
parent{display:table;table-layout:fixed;width:100%;}
.row{display:table-row;}
.item{display:table-cell;width:33.3%;height:200px;}

实用flex实现

.parent{display:flex;flex-direction:column;}
.row{height:100px;display:flex;}
.item{width:100px;background:red;}

全局布局

利用绝对定位实现

<div class="parent">
<div class="top">top</div>
<div class="left">left</div>
<div class="right">right</div>
<div class="bottom">bottom</div>
</div>

html,body,parent{height:100%;overflow:hidden;}
.top{position:absolute:top:0;left:0;right:0;height:0px;}
.left{position:absolute;top:100px;left:0;bottom:50px;width:200px;}
.right{position:absolute;overflow:auto;left:200px;right:0;top:100px;bottom:50px;}
.bottom{position:absolute;left:0;right:0;bottom:0;height:50px;}

利用flex实现

<div class="parent">
<div class="top">top</div>
<div class="middle"><div class="left">left</div>
<div class="right">right</div>
</div>
<div class="bottom">bottom</div>
</div>

响应式布局

meta标签的实用设置布局宽度等于设备宽度,布局viewport等于度量viewport

<meta name="viewport" content="width=device-width,initial-scale=1">


媒体查询

HTML 4和CSS 2目前支持为不同的媒体类型设定专有的样式表, 比如, 一个页面在屏幕上显示时使用无衬线字体, 而在打印时则使用衬线字体, screen 和 print 是两种已定义的媒体类型, 媒体查询让样式表有更强的针对性, 扩展了媒体类型的功能;媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成, 媒体查询中可用于检测的媒体特性有width、height和color(等), 使用媒体查询, 可以在不改变页面内容的情况下, 为特定的一些输出设备定制显示效果。

 

语法

@media screen and (max-width:960px){....}
<link rel="stylesheet" media="screen and (max-width:960px)" href='xxx.css' />
posted @ 2016-10-03 21:08  chenxj  阅读(95)  评论(0)    收藏  举报