炒冷饭11

布局搭框架###

浮动 vs 负margin
对于相邻的两个浮动元素,如果因为空间不够导致第二个浮动元素下移,可以通过给第二个浮动元素设置 margin-left: 负值 来让第二个元素上移,其中 负值 大于等于元素上移后和第一个元素重合的临界值

单列布局

ee7cb56f-35bb-45b3-ba25-b1b66a001d.jpg

一栏布局

<style>
.layout{
width: 960px;
margin: 0 auto;
} #header{
height: 60px;
background: red;
} #content{
height: 400px;
background: blue;
} #footer{
height: 50px;
background: yellow;
}
</style>
<div id="header" class="layout">头部</div>
<div id="content" class="layout">内容</div>
<div id="footer" class="layout">尾部</div>

通栏布局优化

<style>
.layout{
width: 960px;
margin: 0 auto;
}
body{
min-width: 960px;
}#header{
height: 60px;
background: red;
}#content{
height: 400px;
background: blue;
}#footer{
height: 50px;
background: yellow;
}
</style>
<div id="header">
<div class="layout">头部</div>
</div>
<div id="content" class="layout">内容</div>
<div id="footer">
<div class="layout">尾部</div>
</div>

内部元素水平居中

.parent{text-align:center;}
.child{display: inline-block;}

双列布局(一列固定宽度,另外一列自适应宽度)

74f2ada5-53.jpg

左固定,右自适应——注意清父容器的浮动 注意margin-left的用法

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>两列</title>
<style>
#content:after{
content: '';
display: block;
clear: both;
}
.aside{
width: 200px;
height: 500px;
background: yellow;
float: left;
}
.main{
margin-left: 210px;
height: 400px;
background: red;
}#footer{
background: #ccc;
}
</style>
</head>
<body>
<div id="content">
<div class="aside">aside</div>
<div class="main">content</div>
</div>
<div id="footer">footer</div>
</body>
</html>

三列布局

1959.jpg

两侧两列固定宽度,中间列自适应宽度

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>三列</title>
<style> #content:after{
content: '';
display: block;
clear: both;
}
.menu{
width: 100px;
height: 500px;
background: pink;
float: left;
}
.aside{
width: 200px;
height: 500px;
background: yellow;
float: right;
}
.main{
margin-left: 110px; /为什么要加margin-left/
margin-right: 210px;
height: 400px;
background: red;
}#footer{
background: #ccc;
}</style>
</head>
<body>
<div id="content">

<div class="menu">aside</div>
<div class="aside">aside</div>
<div class="main">content</div>
</div>
<div id="footer">footer</div>
</body>
</html>

通用九宫格

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<style>
ul,li{
margin: 0;
padding: 0;
list-style: none;
}
.ct{
overflow:hidden;
width: 640px;
border:dashed 1px orange;
margin: 0 auto;
}

.ct .item{
float: left;
width:200px;
height:200px;
margin-right: 20px;
margin-top: 10px;
background: red;
}
.ct>ul{
margin-right: -20px;
}</style>
<div class="ct">
<ul>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
<li class="item">5</li>
<li class="item">6</li>
<li class="item">7</li>
<li class="item">8</li>
</ul>
</div>
</body>
</html>

圣杯布局:缺点浏览器拉小会出现问题
注意写法 margin-left:-100%
.aside{
width: 100px; /1/
height: 300px; /1/
background: red; /1/
margin-left: -100%; /4/
position: relative; /6/
left: -100px; /6/

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>圣杯布局</title>
<style> #content:after{
content: ''; /8/
display: block; /8/
clear: both; /8/
} #content{
padding-left: 100px; /5/
padding-right: 150px; /5/
}
.aside, .main, .extra{
float: left; /2/
}
.aside{

  width: 100px;        /*1*/
  height: 300px;       /*1*/
  background: red;     /*1*/  
  margin-left: -100%;  /*4*/
  position: relative;  /*6*/
  left: -100px;        /*6*/
}
.extra{
  width: 150px;        /*1*/
  height: 300px;       /*1*/
  background: yellow;  /*1*/
  margin-left: -150px; /*5*/
  position: relative;  /*7*/
  left: 150px;         /*7*/  
}
.main{
  height: 350px;       /*1*/
  background: blue;    /*1*/   
  width: 100%;         /*3*/
}  

</style>
</head>
<body>
<div id="content">
<div class="main">main</div>
<div class="aside">aside</div>
<div class="extra">extra</div>
</div>
</body>
</html>

双飞翼布局

<style> #content:after{
content: ''; /8/
display: block; /8/
clear: both; /8/
}#content{
}
.aside, .main, .extra{
float: left; /2/
}
.aside{
width: 100px; /1/
height: 300px; /1/
background: red; /1/
margin-left: -100%; /4/
}
.extra{
width: 150px; /1/
height: 300px; /1/
background: yellow; /1/
margin-left: -150px; /5/
}
.main{
/* background: blue; / /第1步添加,第7步注释掉/
/
height: 350px; / /第1步添加,第7步注释掉/
width: 100%; /
3/
}
.wrap{
margin-left: 100px; /
6/
margin-right: 150px; /
6/
background: blue; /
7/
height: 350px; /
7*/
}
</style>
<div id="content">
<div class="main">
<div class="wrap">main</div>
</div>
<div class="aside">aside</div>
<div class="extra">extra</div>
</div>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 13,292评论 0 8
  • 本文是针对刚学编程的小白,都是一些基础知识,如果想了解更多深层一点的东西,欢迎移步本人博客!! 博客地址 点击跳转...
    西巴撸阅读 3,576评论 0 0
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 5,891评论 0 1
  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 8,397评论 0 11
  • 美团一面 1.手写插入排序. 我当时写的 书本上的写法: 2.你觉得哪些方面是你擅长的? 回答:集合,多线程 3....
    wangcanfeng阅读 3,975评论 0 2