css怎么让两个table并排-尊龙游戏旗舰厅官网
水平居中
水平居中可能是css布局中最常用到的布局,这里介绍几种水平居中的方式
1、使用inline-block 和 text-align实现
.parent{text-align: center;} .child{display: inline-block;}优点:兼容性好;
不足:需要同时设置子元素和父元素
2、使用margin:0 auto来实现
.child{width:200px;margin:0 auto;}优点:兼容性好
缺点: 需要指定宽度
3、使用table实现
.child{display:table;margin:0auto;}优点:只需要对自身进行设置
不足:ie6,7需要调整结构
4、使用绝对定位实现
.parent{position: relative;} .chlid{position: absolute;}.parent{position:relative;} /*或者实用margin-left的负值为盒子宽度的一半也可以实现,不过这样就必须知道盒子的宽度,但兼容性好*/ .child{position:absolute;left:50%;transform:translate(-50%);}不足:兼容性差,ie9及以上可用
5、使用flex布局实现
/*第一种方法*/ .parent{display:flex;justify-content:center;} /*第二种方法*/ .parent{display:flex;} .child{margin:0 auto;}缺点:兼容性差,如果进行大面积的布局可能会影响效率
垂直居中
1、使用vertical-aligin(在使用vertical-align的时候,由于对齐的基线是用行高的基线作为标记,故需要设置line-height或设置display:table-cell;)
/*第一种方法*/ .parent{display:table-cell;vertical-align:middle;height:20px;} /*第二种方法*/ .parent{display:inline-block;vertical-align:middle;line-height:20px;}2、绝对定位
.parent{position:relative;} .child{positon:absolute;top:50%;transform:translate(0,-50%);}3、flex
.parent{display:flex;align-items:center;}另外有几个水平垂直全部剧中的方式
1、利用vertical-align,text-align,inline-block实现
.parent{display:table-cell;vertical-align:middle;text-align:center;} .child{display:inline-block;}2、利用绝对定位实现
.parent{position:relative;} .child{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}3、利用flex实现
.parent{display:flex;justify-content:center;align-items:center;}多列布局
左右定宽
适用于定宽的一侧为导航,自适应的一侧为内容的布局
1、利用float margin实现
.left{float:left;width:100px;} .right{margin-left;margin-left:100px;}2、利用float margin(fix)实现
3、使用float overflow实现
.left{width:100px;float:left;} .right{overflow:hidden;}4、overflow:hidden,触发bfc模式,浮动无法影响,隔离其他元素,ie6不支持,左侧left设置margin-left当作left与right之间的边距,右侧利用overflow:hidden 进行形成bfc模式
如果我们需要将两列设置为等高,可以用下述方法将“背景”设置为等高,其实并不是内容的等高
.left{width:100px;float:left;} .right{overflow:hidden;} .parent{overflow:hidden;} .left,.right{padding-bottom:9999px;margin-bottom:-9999px;}5、使用table实现
.parent{display:table;table-layout:fixed;width:100%;} .left{width:100px;} .right,.left{display:table-cell;}6、实用flex实现
.parent{display:flex;} .left{width:100px;} .right{flex:1;}利用右侧容器的flex:1,均分了剩余的宽度,也实现了同样的效果。而align-items 默认值为stretch,故二者高度相等
右列定宽,左列自适应
1、实用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;}2、使用table实现
.parent{display:table;table-layout:fixed;width:100%;} .left{display:table-cell;} .right{width:100px;display:table-cell;}3、实用flex实现
.parent{display:flex;} .left{flex:1;} .right{width:100px;}两列定宽,一列自适应
基本html结构为父容器为parent,自容器为left,center,right.其中,left,center定宽,right自适应
1、利用float margin实现
.left,.center{float:left:width:200px;} .right{margin-left:400px;}2、利用float overflow实现
.left,.center{float:left:width:200px;} .right{overflow:hidden;}3、利用table实现
.parent{display:table;table-layout:fixed;width:100%;} .left,.center,.right{display:table-cell;} .left,.center{width:200px;}4、利用flex实现
.parent{display:flex;} .left,.center{width:100px;} .right{flex:1}两侧定宽,中栏自适应
1、利用float margin实现
.left{width:100px;float:left;} .center{float:left;width:100%;margin-right:-200px;} .right{width:100px;float:right;}2、利用table实现
.parent{width:100%;display:table;table-layout:fixed} .left,.center,.right{display:table-cell;} .left{width:100px;} .right{width:100px;}3、利用flex实现
.parent{display:flex;} .left{width:100px;} .center{flex:1;} .right{width:100px;}一列不定宽,一列自适应
1、利用float overflow实现
.left{float:left;} .right{overflow:hidden;}2、利用table实现
.parent{display:table;table-layout:fixed;width:100%;} .left{width:0.1%;} .left,.right{display:table-cell;}3、利用flex实现
.parent{display:flex;} .right{flex:1;}多列等分布局
多列等分布局常出现在内容中,多数为功能的,同阶级内容的并排显示等。
html结构如下
1、实用float实现
.parent{margin-left:-20px}/*假设列之间的间距为20px*/ .column{float:left;width:25%;padding-left:20px;box-sizing:border-box;}2、利用table实现
.parent-fix{margin-left:-20px;} .parent{display:table;table-layout:fixed;width:100%;} .column{display:table-cell;padding-left:20px;}3、利用flex实现
.parent{display:flex;} .column{flex:1;} .column .column{margin-left:20px;}九宫格布局
1、使用table实现
2、实用flex实现
总结
以上是尊龙游戏旗舰厅官网为你收集整理的css怎么让两个table并排_关于css布局的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: