首页 技术 正文
技术 2022年11月16日
0 收藏 778 点赞 4,195 浏览 1502 个字

水平居中:
内联元素:text-align:center;相对于父级居中显示
块级元素:margin:0 auto;但是需要同时width,否则无法看到效果
多个块级元素居中:在此想要探讨一下display:inline-block;和display:flex;
方法1:在父级上设置text-align:center;在元素上设置:display:inline-block;同时为了使文字向左显示,设置了text-align:left;在浏览器中的显示为,子元素高度自适应,底部对齐。
方法2:在子元素上设置display:center;justify-content:center;子元素的高度一致,与元素中内容最多的高度相同。
二种方法在谷歌,火狐,360均兼容,但是第2种方法在IE10中不兼容,之前的IE版本可想也是不兼容的。当然在此也要处理由于浮动导致离层,父级背景颜色加不上去的问题。

 <main class="inline-block-center">
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
</main>
 <main class="flex-center">
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
</main>
 body {
background: #f06d06;
font-size: 80%;
}
main {
background: white;
margin: 20px 0;
padding: 10px;
}
main div {
background: black;
color: white;
padding: 15px;
max-width: 125px;
margin: 5px;
}
.inline-block-center {
text-align: center;
}
.inline-block-center div {
display: inline-block;
text-align: left;
}
.flex-center {
display: flex;
justify-content: center;
}

垂直居中:

方法1:设置padding-top和padding-bottom的值相等
方法2:设置height和line-height的值相等
方法3:固定高度可以有一种处理方式position:absolute;top:50%;height:100px;margin-top:-50px;如果高度不固定,也可以使用js动态获取高度来。
还有css3的一些属性,考虑到兼容性问题不在叙述。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,959
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,112
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,778