gpt4 book ai didi

html - 如何使用CSS水平居中div和span元素

转载 作者:行者123 更新时间:2023-11-28 17:27:15 25 4
gpt4 key购买 nike

这是 JS Fiddle 的链接:JS Fiddle

我试图让我的边框和图标水平位于页面的中心。使用背景色 #f30

但是一切似乎都停留在左侧。我在这里尝试了几篇文章,但它们似乎没有用,还检查了 Google 以获取更多信息,但似乎没有任何解决办法。

这是供您检查的 HTML:

<div id ="heading_layout" class="section-1">
<span class="d"><span class="icon"></span></span>
</div>

这是供您检查的 CSS:

#heading_layout {
margin-top: 30px;
width: 100%;
text-align: center;
background: #f30;
}

.section-1 span.d:before {
float: left;
content: '';
width: 10%;
height: 1px;
border-bottom: 2px dashed #8000ae;
}

.section-1 span.d:after {
position: relative;
right: 0px;
float: left;
top: 100%;
content: '';
width: 10%;
height: 1px;
border-bottom: 2px dashed #8000ae;
}

span.icon {
position: relative;
margin-left: 15px;
margin-right: 15px;
margin-top: -14px;
height: 30px;
width: 30px;
border-radius: 50%;
float: left;
background:
#8000ae url('')
3px 3px no-repeat;
}

这是 JS Fiddle 的链接:JS Fiddle

谢谢。

最佳答案

我重新编辑了你的 CSS:

#heading_layout {
margin-top: 30px;
width: 100%;
text-align: center;
background: #f30;
}

.section-1 span.d:before {
display: inline-block;
content: '';
width: 10%;
height: 1px;
border-bottom: 2px dashed #8000ae;
margin-bottom: 12px;
}

.section-1 span.d:after {
position: relative;
right: 0px;
display: inline-block;
top: 100%;
content: '';
width: 10%;
height: 1px;
border-bottom: 2px dashed #8000ae;
margin-bottom: 12px;
}

span.icon {
position: relative;
margin-left: 15px;
margin-right: 15px;
height: 30px;
width: 30px;
border-radius: 50%;
display: inline-block;
background:
#8000ae url('')
3px 3px no-repeat;
margin-top: 5px;
}

我做了什么:

  1. 将“float: left”替换为“display: inline-block”——现在文本对齐实际上影响了它们。
  2. 删除了不必要的负边距

就这些。希望我说清楚了。

关于html - 如何使用CSS水平居中div和span元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26929664/

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