当字体大小较大时,div内跨度的垂直对齐

问题描述 投票:1回答:1

注意:我已经读过How to vertically center a <span> inside a div?,但在这里稍有不同(请参阅下文)。


我正在尝试在圆形按钮内插入一些大文本,在另一个按钮内插入图像,以及垂直对齐的标题。

这在Chrome上非常有效(我使用了Circle button css的各种答案):

enter image description here

但是奇怪的是,它在Firefox和iPhone Safari(不正确的对齐方式)上均不起作用:

enter image description here

如何解决这样的垂直对齐问题?

.circlebtn { height: 5.4em; width: 5.4em; border-radius: 50%; background-color: black; color: white; text-align: center; cursor: pointer; border: 0; user-select: none; outline: 0; display: inline-block; margin: 1.9em 0 3em 1em; float: left; }
.circlebtn img { width: 3.75em; }
#a span { font-size: 4em; font-weight: bold; }
#c { margin: 1.3em 0 0 1em; float: left; font-size: 2em; }
<button id="a" class="circlebtn"><span>+</span></button>
<button id="b" class="circlebtn"><img src="https://via.placeholder.com/762x416" id="b"></button>
<h1 id="c">HELLO</h1>

注意:我也尝试过按照vertical-align:middle中的建议使用How to vertically center a <span> inside a div?

<div class="parent">
  <span class="child">Yes mom, I did my homework lol</span>
</div>
You should then add the CSS rules

.parent { height: 20px; }
.child { line-height: 20px; vertical-align: middle; }

但是我认为这里的问题来自子跨度具有不同的字体大小的事实。

html css button safari font-size
1个回答
1
投票

如@TemaniAfif的评论中所述,字体本身会影响高度和对齐方式,如this jsFiddle中所示。

一种解决方案是使用JavaScript:How to vertically align all text in CSS?

另一种方法是仅使用CSS生成+符号:Make plus symbol in CSS

.plus {
  width:4.5em;
  height:4.5em;
  
  background:
    linear-gradient(#fff,#fff),
    linear-gradient(#fff,#fff),
    #000;
  background-position:center;
  background-size: 50% 0.5em, 0.5em 50%;
  background-repeat:no-repeat;
}

.radius {
  border-radius:50%;
}
<div class="plus radius">
</div>
© www.soinside.com 2019 - 2024. All rights reserved.