Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align icon vertically to the center of the first line of text

Tags:

Here are the eyes with align-items: center property. They are OK for one-line text and FAIL for multiline text:

<img>

Here are the eyes with align-items: baseline (or flex-start). They are better for multiline text but not ideal for all of them because I want to align the eye to the center of first line of the text:

enter image description here

What I’m trying to achieve is this:

enter image description here

See how the eye image is centered at the first line of the text?

Is it possible to do it elegantly with flexbox properties, without using padding or margin?

(This is a simplified example. In the real problem I don’t want to introduce padding because it will affect other items.)

Here is jsfiddle to play with: http://jsfiddle.net/sqp1wdap/

like image 505
oluckyman Avatar asked Aug 20 '15 12:08

oluckyman


People also ask

How do you center an icon vertically?

Use line-height for vertical centering with a fixed height To vertically center a single line of text or an icon within its container, we can use the line-height property. This property controls the height of a line in a run of text and adds an equal amount of space above and below the line-box of inline elements.

What is vertical-align text top?

The vertical-align attribute is for inline elements only. It will have no effect on block level elements, like a div. Also text-top only moves the text to the top of the current font size. If you would like to vertically align an inline element to the top just use this.


1 Answers

I found the solution: http://jsfiddle.net/sqp1wdap/3/

  1. Align both Eye and Text to flex-start
  2. Make line-height for text same as Eye height

Here is the edited code:

.LegendItem_Eye {   width: $slotWidth;   display: flex;   justify-content: center;   align-items: flex-start; // ← edit (1)   background: #eee; } .LegendItem_Text {   padding: 0 3px;   flex: 1;   align-self: flex-start; // ← edit (1)   background: #eaa;   line-height: $fontSize; // ← edit (2) } 

And here is how it looks like:
enter image description here

like image 110
oluckyman Avatar answered Sep 22 '22 12:09

oluckyman