gpt4 book ai didi

css - 你应该如何使用 CSS 水平居中元素?

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:45 24 4
gpt4 key购买 nike

到目前为止我看到的每个使用 CSS 将元素水平居中的示例都说要将 margin-leftmargin-right 设置为 auto。我在 CSS 方面不是很有经验,所以我想知道是否有理由设置这两个属性,而不是仅仅将一个或另一个设置为 50%?会不会有副作用?

最佳答案

您在问题中添加了 css3 标签。现代浏览器 ( http://caniuse.com/#feat=flexbox ) 中的一种选择是使用 Flexbox。添加 display: flex; justify-content: center 到父级。这将使它的 child 居中。

CSS Tricks 有一个很好的 Flexbox 指南:http://css-tricks.com/snippets/css/a-guide-to-flexbox/

关于css - 你应该如何使用 CSS 水平居中元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28178637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com