Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align symbols and text

Tags:

css

I have the word STARS followed by 5 stars (★)

The problem I have is that the stars are smaller than the word and I want them to vertically align, but can't figure out how.

Here's what I'm trying: Fiddle here too

html, body {
  font-size: 100%; /* for our beloved IE */
  font-size: 12px;
  line-height: 1.2em;
  color: #333;
}
h3 {
  font-size: 2em;
  line-height:1em;
  font-weight: 100;
}
.stars {
  opacity: 0.6;
  font-size: 0.7em;
  vertical-align: middle; /* <-- I thought this would do it */
}
<h3>STARS <span class="stars">&#9733;&#9733;&#9733;&#9733;&#9733;</span></h3>
like image 876
StudioTime Avatar asked Apr 21 '15 21:04

StudioTime


People also ask

How do I vertically align text and icons?

If you want to make a text appear vertically aligned next to a Font Awesome icon, you can use the CSS vertical-align property set to “middle” and also, specify the line-height property. Change the size of the icon with the font-size property.

How do you vertically align text?

Center the text vertically between the top and bottom margins. Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center.

How do you vertically align an element?

The vertical-align property can be used in two contexts: To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an image in a line of text. To vertically align the content of a cell in a table.


2 Answers

The easiest thing to do is just put a position: relative on there and bump them up a bit.

This is something you should not do often (messing with positions can make your page flow messy fast if you don't know what you're doing), but it works for little situations like yours:

html, body {
  font-size: 100%; /* for our beloved IE */
  font-size: 12px;
  line-height: 1.2em;
  color: #333;
}
h3 {
  font-size: 2em;
  line-height:1em;
  font-weight: 100;
}
.stars {
  opacity: 0.6;
  font-size: 0.7em;
  position: relative;
  bottom: .1em; /* em will make it work at any font size, thanks Xufox*/
}
<h3>STARS <span class="stars">&#9733;&#9733;&#9733;&#9733;&#9733;</span></h3>

Adjust to your preference. I only put a .1em bump up because I think that looks best at various font sizes, but you fiddle around with it till you like it.


As for why the vertical-align didn't work, it's because that property only acts on inline elements, see Rick Hitchcock's answer for more detail on that. The h3 isn't an inline block so there's nothing to align the span with!

While you could change the displays around to make this work properly it's not really what you want for your situation. The position is better suited to aligning icons (like font-awesome icons) with the text next to them.

like image 183
OneHoopyFrood Avatar answered Oct 14 '22 01:10

OneHoopyFrood


You can solve the problem by making the h3 a table row and .stars a table cell.

According to the table height algorithm, when you use vertical-align: middle on a table cell:

The center of the cell is aligned with the center of the rows it spans.

Add these styles:

h3 {
  display: table-row;
}

.stars {
  display: table-cell;
}

Snippet:

html, body {
  font-size: 100%; /* for our beloved IE */
  font-size: 12px;
  line-height: 1.2em;
  color: #333;
}

h3 {
  font-size: 2em;
  line-height:1em;
  font-weight: 100;
  display: table-row;
}

.stars {
  opacity: 0.6;
  font-size: 0.7em;
  vertical-align: middle;
  display: table-cell;
}
<h3>STARS <span class="stars">&#9733;&#9733;&#9733;&#9733;&#9733;</span></h3>
like image 26
Rick Hitchcock Avatar answered Oct 14 '22 01:10

Rick Hitchcock