How to vertically center a <span> inside a div? [duplicate]
Asked Answered
S

5

283

The code:

<div 
  id="theMainDiv"
  style="
    border:solid 1px gray;
    cursor:text;
    width:400px;
    padding:0px;"
>
  <span 
    id="tag1_outer" 
    style="
      background:#e2e6f0;
      padding-right:4px;
      padding-left:4px;
      border:solid 1px #9daccc;
      font:normal 11px arial;
      color:#3c3c3c"
  >as</span>
</div>

As it renders now, the span is align the bottom-left corner of the div.

Starinsky answered 5/12, 2010 at 4:4 Comment(1)
Solution without lineheight and with flex is: id="theMainDiv" style=" border:solid 1px gray; cursor:text; width:400px; padding:0px; display:flex; align-content: middle;"Allard
R
217

See my article on understanding vertical alignment. There are multiple techniques to accomplish what you want at the end of the discussion.

(Super-short summary: either set the line-height of the child equal to the height of the container, or set positioning on the container and absolutely position the child at top:50% with margin-top:-YYYpx, YYY being half the known height of the child.)

Rackety answered 5/12, 2010 at 4:7 Comment(9)
@Blender Heh, though therein references a third option: display:table-cell; vertical-align:middle (along with a display:table-row parent). :) But no, you can be sure that I would never advocate using HTML table elements for layout.Rackety
what happens if you padding-top??? a fixed no based on height of the parent. i think your solution works even if the height is dynamic right.Appease
So if you want to do this without a fixed height, there's no way to do it?Tylertylosis
@Kyralessa No, if you don't have a fixed height for the content there exist a couple techniques not yet listed in my page. For example, see: css-tricks.com/centering-in-the-unknownRackety
@Rackety - you should add the last option in css-tricks.com/centering-in-the-unknown to your answer. It's so clean and ie8+!Anywise
@StevenLu Thanks for the motivation. 5 years ago I had not the experience with SO that I have now, and I too would downvote my answer as it is only a link to an external site. I will find time to put the various code answers into my answer. Hopefully "soon".Rackety
Yep, there are a lot of helpful articles out there by now that show many ways to get this job done. However, It's really a shame how complex every one of these approaches is. Flexbox offers a glimmer of hope, but is also incredibly complex.Benignity
"Setting the line-height equal to that of container.." doesn't seem to work when the text on line is using different font sizes. e.g. In method 2 example in your article, I changed the text "is" to <span style="font-size:72px;">is</span> and text was no longer appearing at center. This was exactly the case for which I was looking for a way to center and I chanced upon this question.Humo
What if YYY is unknown??Paternal
M
210

At your parent DIV add

display:table;

and at your child element add

 display:table-cell;
 vertical-align:middle;
Myrna answered 12/12, 2012 at 9:8 Comment(1)
This works, but is there an easy way to horizontally center the child element in the parent as well? Since the child's display isn't an inline-block anymore, the parent's "text-align: center;" css no longer works.Constantin
I
75

Quick answer for single line span

Make the child (in this case a span) the same line-height as the parent <div>'s height

<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; }



Or you can target it with a child selector

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

Background on my own use of this

I ran into this similar issue where I needed to vertically center items in a mobile menu. I made the div and spans inside the same line height. Note that this is for a meteor project and therefore not using inline css ;)

HTML

<div class="international">        
  <span class="intlFlag">
    {{flag}}        
  </span>

  <span class="intlCurrent">
    {{country}}
  </span>

  <span class="intlButton">
    <i class="fa fa-globe"></i>
  </span> 
</div>

CSS (option for multiple spans in a div)

.international {
  height: 42px;
}

.international > span {
  line-height: 42px;
}

In this case if I just had one span I could have added the CSS rule directly to that span.

CSS (option for one specific span)

.intlFlag { line-height: 42px; }

Here is how it displayed for me

enter image description here

Isocrates answered 31/1, 2014 at 0:17 Comment(3)
Upvote for high-quality child div example text.Thermotherapy
this only works if the text does not go over multiple linesSyntax
@Syntax yeah it's literally mentioned in the opening sentence.Isocrates
B
42

As in a similar question, use display: inline-block with a placeholder element to vertically center the span inside of a block element:

html, body, #container, #placeholder { height: 100%; }

#content, #placeholder { display:inline-block; vertical-align: middle; }
<!doctype html>
<html lang="en">
  <head>
  </head>

  <body>
    <div id="container">
      <span id="content">
        Content
      </span>
      <span id="placeholder"></span>
    </div>
  </body>
</html>

Vertical alignment is only applied to inline elements or table cells, so use it along with display:inline-block or display:table-cell with a display:table parent when vertically centering block elements.

References:

CSS Horizontal and Vertical Centering

Bisque answered 22/6, 2012 at 18:19 Comment(1)
The solution works as long as #content is not wrappedEadwina
S
1

To the parent div add a height say 50px. In the child span, add the line-height: 50px; Now the text in the span will be vertically center. This worked for me.

Seem answered 9/9, 2013 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.