当 font-size 较大时 div 内跨度的垂直对齐
Vertical alignement of span inside a div when the font-size is big
注意:我已经读过 How to vertically center a <span> inside a div? 但这里略有不同(见下文)。
我正在尝试在一个圆形按钮内插入一些大文本,在另一个按钮内插入一个图像,以及一个垂直对齐的标题。
这在 Chrome 上非常有效(我使用了 的各种答案):
但奇怪的是,它在 Firefox 和 iPhone Safari(错误对齐)上不起作用:
如何解决这样的垂直对齐问题?
.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>
注意:我也按照 How to vertically center a <span> inside a div?:
中的建议尝试使用 vertical-align:middle
<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; }
但我认为这里的问题来自 child 跨度具有不同的字体大小。
如@TemaniAfif 的评论所述,字体本身会影响高度和对齐方式,如 this jsFiddle 中所示。
一种解决方案是使用 JavaScript:
另一种方法是仅使用 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>
注意:我已经读过 How to vertically center a <span> inside a div? 但这里略有不同(见下文)。
我正在尝试在一个圆形按钮内插入一些大文本,在另一个按钮内插入一个图像,以及一个垂直对齐的标题。
这在 Chrome 上非常有效(我使用了
但奇怪的是,它在 Firefox 和 iPhone Safari(错误对齐)上不起作用:
如何解决这样的垂直对齐问题?
.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>
注意:我也按照 How to vertically center a <span> inside a div?:
中的建议尝试使用vertical-align:middle
<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; }
但我认为这里的问题来自 child 跨度具有不同的字体大小。
如@TemaniAfif 的评论所述,字体本身会影响高度和对齐方式,如 this jsFiddle 中所示。
一种解决方案是使用 JavaScript:
另一种方法是仅使用 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>