CSS常用布局方式-两列布局、三列布局
CSS基础
2.几种布局方式
1)table布局
当年主流的布局方式,第一种是通过table tr td布局
示例:
<style type="text/css">
table{
width: 800px;
height: 300px;
border-collapse: collapse;
}
.left{
background-color: red; }
.right{
background-color: blue;
}
</style>
<body>
<table>
<tr>
<td class="left">左</td>
<td class="right">右</td>
</tr>
</table>
</body>
页面效果: 文字自动垂直居中,很方便 同样可以设置左右的width
第二种是类比表格的table class
示例:
<style type="text/css">
.table{
display: table;
width: 800px;
height: 300px;
/*border-collapse: collapse;*/ }
.tb_row{
display: table-row;
} .tb_cell{
display: table-cell;
vertical-align: middle;
} .left{
background-color: red;
}
.right{
background-color: blue;
}
table
</style>
<body>
<div class="table">
<div class="tb_row">
<div class="left tb_cell">左</div>
<div class="right tb_cell">右</div>
</div>
</div>
</body>
页面效果: 跟表格布局一样 
2)flexbox布局
- 两列布局
**关键:父级元素设置display:flex**
示例:
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.parent{
width: 800px;
height: 300px;
display: flex;
}
.left{
width: 300px;
height: 100%;
background: red;
}
.right{
flex: 1;
height: 100%;
background: blue;
} </style> <body>
<div class="parent">
<div class="left">左</div>
<div class="right">右</div>
</div>
</body>
页面效果:
- 三列布局
**关键:父级元素设置display:flex**
示例:
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.parent{
width: 800px;
height: 300px;
display: flex;
}
.left{
width: 300px;
height: 100%;
background: red;
}
.middle{
width: 200px;
height: 100%;
}
.right{
flex: 1;
height: 100%;
background: blue;
} </style>
<body>
<div class="parent">
<div class="left">左</div>
<div class="middle">中</div>
<div class="right">右</div>
</div>
</body>
页面效果:
3)float布局 (float+margin)
兼容性好 但是要注意清楚浮动(clear: both display:block)
- 两列布局——左侧定宽,右侧自适应
**关键:**
*左侧设置float:left
右侧:margin-left: leftWidth*
示例:
<style>
*{
margin: 0;
padding: 0;
} .parent{
width:800px;
height:200px;
}
.left{
width:200px;
height:100%;
float:left;
background-color:red;
}
.right{
height:100%;
margin-left:200px;
background-color:blue;
}
</style>
<body>
<div class="parent">
<div class="left">左</div>
<div class="left">右</div>
</div>
</body>
页面效果:
- 三列布局
**关键:**
*左侧设置float:left
右侧设置float:right
中间:margin-left: leftWidth;margin-right:rightWidth*
示例:
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.parent{
width: 800px;
height: 200px;
}
.left{
width: 200px;
height: 100%;
background-color: red;
float: left;
} .right{
float: right;
width: 200px;
background-color: blue;
height: 100%;
} .middle{
margin-left: 200px;
margin-right: 200px;
}
</style> <body>
<div class="parent">
<div class="left">左</div>
<div class="right">右</div>
<div class="middle">中</div> </div>
</body>
页面效果:
**注意:float特点:尽量靠上,尽量靠左(右),所以右侧浮动div要先写,中间div后写**
4)inline-block布局——不存在清除浮动问题 适用与定宽的布局
<style type="text/css">
.parent{
font-size: 0;
width: 800px;
height: 200px;
}
.left{
font-size: 14px;
width: 300px;
height: 200px;
display: inline-block;
background-color: red;
}
.right{
font-size: 14px;
width: 500px;
height: 200px;
display: inline-block;
background-color: blue;
}
</style>
<div class="parent">
<div class="left">左</div>
<div class="right">右</div>
</div>
页面效果:
注意: 想要父级元素的宽度等于两个子元素宽度之和,需要设置父级元素的 font-size:0 否则两个子元素不会再一行展示
同时,需要设置两个子元素的font-size: 14px; 否则子元素内的文字不会展示!
**想象成文字,注意间隙的处理!!!** 间隙存在于两个div间的空白: 
5)响应式布局
让前台页面可以再不同的设备,不同大小的屏幕上正常展示,一般都是指处理屏幕大小问题
首先设置viewport
利用rem 1rem=html.font-size
<meta name="viewport" content="width=device-width,initial-scale=1.0">
利用media query
@media (max-width: 640px){
.left{
display: none;
}
}
三.Q&A
1) position: absolute 和fixed 的区别:
前者是相对于最近的relative/absolute 元素
后者是相对于屏幕 (viewport)
2) display:inline-block 间隙的原因
原因: 空白字符间距
解决:清除空白字符或者消灭间距
—中间不留空白
<div>
</div></div>
</div>
—将空白字符注释
<div>
</div><!--
--><div></div>
—消灭间距
font-size: 0
3)如何清除浮动以及原因
浮动的元素不占用父元素的空间,有可能会超出父元素进而对其他元素产生影响,所以要清除父元素浮动
方法:
让盒子负责自己的布局:
overflow: hidden(auto)
在元素后面加上:
::after{clear: both}
4)如何适配移动端页面
- viewport
- rem/viewport/media query
- 设计上: 隐藏 折行 自适应
CSS常用布局方式-两列布局、三列布局的更多相关文章
- web标准(复习)--3 二列和三列布局
今天学习二列和三列布局,将涉及到以下内容和知识点 二列自适应宽度 二列固定宽度 二列固定宽度居中 xhtml的块级元素(div)和内联元素(span) float属性 三列自适应宽度 三列固定宽度 三 ...
- CSS两列及三列自适应布局方法整理
布局 自适应 两列 三列 在传统方法的基础上加入了Flex布局并阐述各方法的优缺点,希望对大家有所帮助.先上目录: 两列布局:左侧定宽,右侧自适应 方法一:利用float和负外边距 方法二:利用外边距 ...
- css 左右固定宽度,中间自适应的三列布局
float——浮动布局: 使用浮动,先渲染左右两个元素,分别让他们左右浮动,然后再渲染中间元素,设置它的margin左右边距分别为左右两个元素的宽度. <!DOCTYPE html> &l ...
- ccs之经典布局(二)(两栏,三栏布局)
接上篇ccs之经典布局(一)(水平垂直居中) 四.两列布局 单列宽度固定,另一列宽度是自适应. 1.float+overflow:auto; 固定端用float进行浮动,自适应的用overflow:a ...
- CSS布局:Float布局过程与老生常谈的三栏布局
原文见博客主站,欢迎大家去评论. 使用CSS布局网页,那是前端的基本功了,什么两栏布局,三栏布局,那也是前端面试的基本题了.一般来说,可以使用CSSposition属性进行布局,或者使用CSSfloa ...
- 转:CSS布局:Float布局过程与老生常谈的三栏布局
使用CSS布局网页,那是前端的基本功了,什么两栏布局,三栏布局,那也是前端面试的基本题了.一般来说,可以使用CSSposition属性进行布局,或者使用CSSfloat属性布局.前者适合布局首页,因为 ...
- Web标准:三、二列和三列布局
知识点: 1.二列自适应宽度 2.二列固定宽度 3.二列固定宽度居中 4.xhtml的块级元素(div)和内联元素(span) 5.float属性 6.三列自适应宽度 7.三列固定宽度 8.三列固定宽 ...
- css常用居中方式
一.水平居中 1.内联元素 父级元素加 text-align: center 即可 html <div class="container"> <a>内联元素 ...
- CSS 布局实例系列(三)如何实现一个左右宽度固定,中间自适应的三列布局——也聊聊双飞翼
今天聊聊一个经典的布局实例: 实现一个三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化 可能很多朋友已经笑了,这玩意儿通过双飞翼布局就能轻松实现.不过,还请容我在双飞 ...
随机推荐
- 微软在Build 2019大会上发布Fluid Framework协作平台
在今年年度开发者大会上,微软已经为开发人员宣布了一个新的Fluid Framework.该框架基本上是一个新的基于Web的平台,允许团队在自由流动的流程上工作.微软已经分享了一些新功能,可以帮助团队在 ...
- docker系列(三):docker容器
1 引言 在前面博文中,我们介绍了镜像.如果说镜像犹如面向对象中的类,本节要说的容器就是由类实例化出来的对象了,有了类才可以创建容器. 先从拉取一个官方提供的ubuntu最新镜像: $ docker ...
- 透过systemctl管理mysqld服务
1. 背景 CentOS 7.x 之前的版本,系统启动时,第一支呼叫的程序是 init ,然后 init 去唤起所有的系统所需要的服务,无论是本地服务还是网络服务.所有的服务启动脚本都放置于 /etc ...
- 第十章 Centos7-系统进程管理
第十章 Centos7-系统进程管理 本节所讲内容: 10.1 进程概述和ps查看进程工具 10.2 uptime查看系统负载-top动态管理进程 10.3 前后台进程切换- nice进程优先 ...
- adb <-> adbserver <-> JDWP
简单理一下adb,adbserver,jdwp 之间的关系.角色与相关协议 | 上位机 | USB/TCP | 下位机 | adb <-> adbserver ...
- index-css-添加类-移除类-toggleClass-attr
1=>index()会返回当前元素在所有的兄弟元素里面的索引值用法:$("a").click(function(){ console.log($(this).index()) ...
- luoguP3017Brownie Slicing
https://www.luogu.org/problem/P3017 题意 给你一个蛋糕,R行C列 ,每个点有巧克力碎屑(如下) 1 2 2 1 3 1 1 1 2 0 1 3 1 1 1 1 1 ...
- BASIC合集
握手包 给你握手包,flag是Flag_is_here这个AP的密码,自己看着办吧. 提交格式:flag{WIFI密码} 破解wifi密码 丢到kali,用aircrack-ng kali有一个包含常 ...
- (day55)七、查询优化、MTV和MCV、choices、AJAX、序列化
目录 一.ORM查询优化 (一)only与defer (1)only (2)defer (二)select_related与prefatch_related (1)select_related (2) ...
- 解决Python开发中,Pycharm中无法使用中文输入法问题
Pycharm是开发Python程序的利器,但有时会遇到无法输入中文的情况.表现为:在Ubuntu系统可以正常输入中文,却在Pycharm内写注释的时候,切换不出中文.下面演示如何解决此问题. 1.在 ...