大家好,欢迎来到IT知识分享网。
div居中方法(共9种)
<div style="width: 500px;height: 100px;line-height: 100px;border: 1px solid green;text-align:center;"> <span>行级元素</span> </div>
块级元素:为其自身设置margin:auto样式
<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-cell;此元素会作为一个表格单元格显示(类似 和 )
<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>
<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>
<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>
<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>
<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>
<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>
<div style="margin: 50vh auto 0;transform: translateY(-50%);line-height:100px width: 100px;height: 100px;border: 1px solid gold;text-align:center;"> 块级元素 </div>
<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>
<div style="display: grid; place-items: center;"> <div> 块级元素 </div> </div> // CSS // body { // display: grid; // place-items: center; // }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/136973.html