CSS常用布局实现方法
CSS 布局对我来说,既熟悉又陌生。我既能实现它,又没有很好的了解它。所以想总结一下,梳理一下 CSS 中常用的一列,两列,三列布局等的实现方法。本文小白,仅供参考。但也要了解下浮动,定位等。
一、一列布局
1. 居中定宽
这算是最简单且最容易实现的布局了吧。列出最核心的 CSS 代码:
body{text-align: center;font-size: 2em;}
.head,.main,.footer{width: 960px;margin: 0 auto;}
.main{background-color: #666666;height: 600px;}
.footer{background-color: #999999;height: 200px;}
其中,最主要的还是 margin 属性,当为元素设置了宽度,margin:0 auto 就能自动让元素居中。
2. 自适应
这个也非常简单,只需要将上述 CSS 代码中元素的 width 属性设置为百分比,这样就能让浏览器自动计算元素的宽度。
二、两列布局
1. 定宽
这个应该也没什么难度,只需设置好相应的宽度就好了。这里贴出代码:
body{text-align: center;font-size: 2em;}
.main{width: 960px;height: 900px;margin: 0 auto;}
.left{width: 300px;height: 900px;background-color: #eee;float: left}
.right{width: 660px;height: 900px;background-color: #999;float: right;}
这里涉及到了 float 属性,也就是常说的浮动了。一个向左浮动,一个向右浮动,恰好能实现两列布局。‘
2. 自适应
将 width 属性的值替换成百分比,就是这么简单。
body{text-align: center;font-size: 2em;}
.main{width: 80%;height: 900px;margin: 0 auto;}
.left{width: 30%;height: 900px;background-color: #eee;float: left}
.right{width: 70%;height: 900px;background-color: #999;float: right;}
注意:父元素也要设置成百分比。
三、三列布局
1. 左右定宽
body{text-align: center;font-size: 2em;margin: 0;padding: 0}
.main{height: 900px;background-color: #ddd;margin: 0 240px;}
.left{width: 240px;height: 900px;background-color: #eee;position: absolute;top: 0;left: 0}
.right{width: 240px;height: 900px;background-color: #999;position:absolute;top: 0;right: 0}
这里最主要的是用到了绝对定位,并且让中间的 margin 左右为两边的宽度。
2. 自适应
body{text-align: center;font-size: 2em;margin: 0;padding: 0}
.main{height: 900px;background-color: #ddd;margin: 0 20%;}
.left{width: 20%;height: 900px;background-color: #eee;position: absolute;top: 0;left: 0}
.right{width: 20%;height: 900px;background-color: #999;position:absolute;top: 0;right: 0}
同理,其换成百分比的形式。
四、混合布局
最后来个前面的大综合。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>混合布局自适应</title>
<link rel="stylesheet" href="">
<style type="text/css">
body{text-align: center;font-size: 2em;margin: 0;padding: 0}
.head,.main,.footer{width: 80%; margin:0 auto;}
.head{background-color: #ccc; height: 100px}
.footer{background-color: #9cc; height: 100px}
.main{position: relative;}
.left{width: 20%;height: 900px; background-color: #eee;position: absolute;top: 0;left: 0; overflow: hidden;}
.middle{height: 900px; background-color: #fcc; margin: 0 20%; overflow: hidden;}
.right{width: 20%; height: 900px; background-color: #eee;position: absolute; top: 0; right: 0;overflow: hidden;}
</style>
</head>
<body>
<div class="head">head</div>
<div class="main">
<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div> </body>
</html>
CSS常用布局实现方法的更多相关文章
- css 常用布局
「前端那些事儿」③ CSS 布局方案 我们在日常开发中经常遇到布局问题,下面罗列几种常用的css布局方案 话不多说,上代码! 居中布局 以下居中布局均以不定宽为前提,定宽情况包含其中 1.水平居中 a ...
- CSS常用布局整理(二)
1-2-1单列变宽布局 side列定宽300px,content列变宽,尺寸是100%-300px.核心的问题就是浮动列的宽度应该等于“100% - 300px”,而CSS显然不支持这种带有减法运算的 ...
- css常用布局
1.一列布局 html: <div class="header"></div> <div class="body">< ...
- CSS常用布局方式-两列布局、三列布局
CSS基础 2.几种布局方式1)table布局 当年主流的布局方式,第一种是通过table tr td布局 示例: <style type="text/css"> ta ...
- CSS常用布局整理
固定宽度布局 1-2-1布局(浮动) <html xmlns="http://www.w3.org/1999/xhtml"> <head> <titl ...
- css常用的属性方法 上篇
自己是从java后台自学转前端的,所以平时一些简单的css+html就不写了,列出的都是新手常用的一些属性,会持续更新,大神勿喷,留给新手做个参考! 尤其是跟我一样自学前端的. 背景关联 ba ...
- html+css 常用布局
1.中间固定宽度,两侧自适应 1.1 flex布局 <!DOCTYPE html><html lang="en"> <head> <met ...
- CSS常用布局技巧 实例
末尾用省略号! white-space: nowrap; overflow: hidden; text-overflow: ellipsis; ######################## 两个i ...
- CSS常用布局学习笔记
水平居中-行内元素 如果是文字和图片这类的行内元素,则在其父级元素上加上样式:text-align:center; 水平居中-定宽块元素 div{ width:100px; margin:0 auto ...
随机推荐
- uvalive 4513 Stammering Aliens
题意:给你一个串,问期中至少出现m次的最长子串及其起始位置的坐标. 思路:hash+LCP+二分答案 #include<cstdio> #include<cstring> #i ...
- Bzoj 4591: [Shoi2015]超能粒子炮·改 数论,Lucas定理,排列组合
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 178 Solved: 70[Submit][Stat ...
- 关于win10安装VisualSVN遇到的一个问题及解决办法
问题:在win10系统中安装VisaulSVN遇到问题,错误提示:There is problem with this Windows Installer package. A DLL require ...
- elecworks 报表----按线类型的电线清单
按线类型的电线清单中:列Wire number指的是线标注,不是电位标注 section:截面积
- hdoj 2817 A sequence of numbers【快速幂】
A sequence of numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdoj 2037 今年暑假不AC
今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- _int、NSInteger、NSUInteger、NSNumber的区别和联系
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
- js使用技巧大全
1.防止重新构建 var constructedHtml = ""; for(var i = 0,len = arr.length;i < len;i++){ constru ...
- 使用ApplicationLoader中出现报错:The IPA is invalid. It does not inlude a Payload directory
问题处理方法: 1.将achieve的.app后缀的软件包放在一个payload的文件夹中 2.压缩该文件夹,改变.zip后缀为.ipa 3.使用applicationLoader上传该文件
- 详解 MySQL 中的 explain
来源:persister 链接:http://www.blogjava.net/persister/archive/2008/10/27/236813.html 在 explain的帮助下,您就知道什 ...