Vertical alignement of span inside a div when the font-size is big

柔情痞子 提交于 2021-02-11 06:17:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!