
要使用 CSS 在所有浏览器中垂直居中 div 元素,请使用 Flexbox。 CSS3提供了另一种布局模式Flexible Box,俗称Flexbox。使用此模式,您可以轻松地为复杂的应用程序和网页创建布局。与浮动不同,Flexbox 布局可以完全控制框的方向、对齐方式、顺序和大小。
标签是 HTML 中元素的容器。我们可以在 div 中放置任何类型的内容。首先添加元素,然后使用 CSS 设置它们的样式。我们现在只能将 div 垂直居中,但可以水平居中。
为了使 div 垂直居中,我们将使用 flex 属性,align-items 属性。该值设置为 center 以使 div 居中。在下面的示例中,我们使用具有中心值的align-items属性将其中一个div设置为居中对齐 –
demo2 { display: flex; align-items: center; height: 60%;}
然后将内容设置在同一个 div demo2 中以居中对齐 –
立即学习“前端免费学习笔记(深入)”;
@@##@@Archery is a bow and arrow game where each player is supposed to shoot arrows from a bow and hit a fixed target.
示例
现在让我们看看使用 CSS 为所有浏览器垂直居中 div 元素的示例 –
Center Div Elements body, html { height: 100%; } .demo2 { display: flex; align-items: center; height: 60%; } .demo1, .demo2 { border: 2px solid skyblue; background-color: blue; color: white; }Archery Tutorial
@@##@@Archery is a bow and arrow game where each player is supposed to shoot arrows from a bow and hit a fixed target.
Let us now center the div elements:
@@##@@Archery is a bow and arrow game where each player is supposed to shoot arrows from a bow and hit a fixed target.



以上就是如何使用CSS在所有浏览器中垂直居中一个div元素?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1551704.html
微信扫一扫
支付宝扫一扫