问题
Note: I've already read How to vertically center a <span> inside a div? but here it's slightly different (see below).
I'm trying to insert some big text inside a circle button, an image inside another one, and a title aligned vertically.
This perfectly works on Chrome (I've used various answers from Circle button css):
But strangely, it doesn't work on Firefox and also iPhone Safari (bad alignment):
How to solve such a vertical alignment problem?
.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>
Note: I've also tried with vertical-align:middle
as suggested in 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; }
but I think the problem here comes from the fact the child span has a different font size.
回答1:
As mentioned in @TemaniAfif's comments, the font itself can influence the height and alignment, as seen in this jsFiddle.
One solution would be to use JavaScript: How to vertically align all text in CSS?
Another one would be to generate the + sign with CSS only: 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>
来源:https://stackoverflow.com/questions/60253431/vertical-alignement-of-span-inside-a-div-when-the-font-size-is-big