【发布时间】:2023-03-08 07:58:01
【问题描述】:
#xxx {
position:relative;
display:flex;
height:32px;
width:150px;
background-color:black;
color:white;
font-family: Helvetica, Arial, sans-serif;
justify-content: center;
margin-left:10px;
}
#xxx > span {
align-self: center;
}
#xxx > img{
margin-right:auto;
}
<body>
<div id="xxx"><img src="xxx.png" height="32" width="32"/><span>hello</span></div>
</body>
我一直在尝试使以下内容正确对齐。我试图让图像左对齐并且跨度内的文本居中。起初我试图用绝对定位跨度并设置 left:0;但这搞砸了。然后我尝试了 margin:auto 但这搞砸了居中对齐的元素。
<!DOCTYPE HTML>
<html>
<style>
#xxx {
position:relative;
display:flex;
height:32px;
width:150px;
background-color:black;
color:white;
font-family: Helvetica, Arial, sans-serif;
justify-content: center;
margin-left:10px;
}
#xxx > span {
align-self: center;
}
#xxx > img{
margin-right:auto;
}
</style>
<body>
<div id="xxx"><img src="xxx.png" height="32" width="32"/><span>hello</span></div>
</body>
</html>
【问题讨论】:
-
paulie 我不喜欢这个问题的答案,你已将其标记为重复,我不清楚
-
感谢 link2pk,这是迄今为止最好的。你能提供一个答案吗?
-
哈哈谢谢 Michael_B 是的,你是对的 ;) 感谢您的回复,是的,我已经抵消了左边距以抵消绝对不占用空间。干杯伙伴。