服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - 编程技术 - html+css实现div居中的8种方法

html+css实现div居中的8种方法

2023-05-30 11:24小白慢慢努力中 编程技术

本文主要介绍了html+css实现div居中的8种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

水平居中

1.行级元素:为该行级元素的父元素设置text-align:center配合line-height样式

?
1
2
3
<div style="width: 500px;height: 100px;line-height: 100px;border: 1px solid green;text-align:center;">
    <span>行级元素</span>
</div>

2.块级元素:为其自身设置margin:auto样式

?
1
2
3
4
5
<div style="width: 500px;height: 100px;border: 1px solid red;">
    <div style="line-height: 100px;text-align: center;margin:auto;width: 100px;height: 100px;border: 1px solid gold">
        块级元素
    </div>
</div>

垂直居中

方法一:

display:table;此元素会作为块级表格来显示(类似 <table>),表格前后带有换行符.

display:table-cell;此元素会作为一个表格单元格显示(类似 <td> 和 <th>)

?
1
2
3
<div style="display: table;width: 500px;height: 200px;border: 1px solid red;">
    <div style="display: table-cell;vertical-align: middle;text-align:center;">块级元素</div>
</div>

方法二:

利用flex布局使内部块级元素水平,垂直居中(display:flex;justify-content: center; align-items:center;)

?
1
2
3
<div style="display:flex;justify-content: center; align-items:center; width: 500px;height: 200px;border: 1px solid green;text-align:center;line-height:100px ">
    <div style="width: 100px;height: 100px;border: 1px solid gold">块级元素</div>
</div>

方法三:

利用定位实现,父元素:position:relative; ,子元素:position: absolute;top:50%;left:50%;transform:translate(-50%,-50%);

?
1
2
3
4
5
<div style="position:relative; width: 500px;height: 200px;border: 1px solid red;">
    <div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px">
        块级元素
    </div>
</div>

方法四:

绝对定位, left:50%,top: 50% + margin-left:-(自身宽度的一半),margin-top:-(自身高度的一半)
缺点:要自己计算容器的宽高,万一容器的宽高改变还要修改css样式

?
1
2
3
4
5
6
<div style="position:relative; width: 500px;height: 200px;border: 1px solid red;">
    <div style="position:absolute;top:50%;left:50%;margin-left: -50px;margin-top: -50px;
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px">
        块级元素
    </div>
</div>

方法五:

绝对定位,left: 0,right: 0, top: 0, bottom: 0 + margin:auto

?
1
2
3
4
5
6
<div style="position:relative; width: 500px;height: 200px;border: 1px solid red;">
    <div style="position:absolute;top: 0;left: 0;right: 0;bottom: 0;margin: auto;
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px">
        块级元素
    </div>
</div>

方法六:
固定定位position:fixed;并设置一个较大的z-index层叠属性值

?
1
2
3
4
<div style="position:fixed;top: 50%;left: 50%;margin-left: -50px;margin-top: -50px;z-index: 999;
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px">
    块级元素
</div>

方法七:
要把元素相对于视口进行居中,那么相当于父元素的高度就是视口的高度,视口的高度可以用vh来获取:

?
1
2
3
4
<div style="margin: 50vh auto 0;transform: translateY(-50%);line-height:100px
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;">
    块级元素
</div>

方法八:

Flex加margin:auto

?
1
2
3
4
5
<div style="display: flex;width: 500px;height: 200px;border: 1px solid red;">
    <div style="margin: auto;width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px">
        块级元素
    </div>
</div>

到此这篇关于html+css实现div居中的8种方法的文章就介绍到这了,更多相关html+css实现div居中内容请搜索服务器之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持服务器之家!

延伸 · 阅读

精彩推荐