2

I want my text to be vertically middle in <p> instead of align to the bottom.

My code:

<p style="color:red;text-align:left;">
  <img src="https://i.ibb.co/zFNxsT4/1.png" width="25"> &nbsp;&nbsp;
  <b>Where Should I go ? </b>
</p>

And the result:

enter image description here

i have tried vertical-align:middle but it doesn't have any changes.
Any other ways that it could make the text vertically align in middle?

hatched
  • 795
  • 2
  • 9
  • 34

6 Answers6

1

The only (and the best cross-browser) way as I know is to use an inline-block helper with height: 100% and vertical-align: middle on elements.

1

I would use flexbox

I changed your markup a little bit, hope it's alright. It uses <strong> instead of <b> and I gave the image a height aswell.

.flex {
  display: flex;
}

.flex strong {
  align-self: center;
}
<p class="flex">
  <img src="https://i.ibb.co/zFNxsT4/1.png" width="25" height="25"> &nbsp;&nbsp;
  <strong>Where Should I go ? </strong>
</p>

Fiddle demo

Marc Hjorth
  • 1,797
  • 2
  • 18
  • 24
1

Set vertical-align: middle on the children of your <p> element:

p {
  color: red;
  text-align: left;
}

img { 
  width: 25px;
}

p > * {
  vertical-align: middle;
}
<p>
  <img src="https://i.ibb.co/zFNxsT4/1.png"> &nbsp;&nbsp;
  <b>Where Should I go ?</b>
</p>
jo_va
  • 13,504
  • 3
  • 23
  • 47
1

You could use flexbox to do this. Also, it would be semantically more correct to write your code like this:

.wrapper {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
}

.image {
  flex: 0 0 auto;
  width: 25px;
  margin: 0 10px 0 0;
}
<div class="wrapper">
  <img class="image" src="https://i.ibb.co/zFNxsT4/1.png">
  <p style="color:red;">Where Should I go ? </p>
</div>
DavidWorldpeace
  • 519
  • 2
  • 7
  • 22
1

There is absolutly no need for tables or any other stuff.

You may use vertical-align: middle. But you need to set the images vertical-align aswell. Otherwise a vertical-align is not specified.

Here is a working version:

HTML

<p style="color:red; text-align:left;">
  <img src="https://i.ibb.co/zFNxsT4/1.png" width="25">
  <b>Where Should I go ? </b>
</p>

CSS

p > img, p > b {
    vertical-align: middle;
}

FIDDLE https://jsfiddle.net/e2y1cL07/

Pro tip:

If I want to row elements side-by-side and automatically want them to move to the parents width, I apply display: inline-block to each children of my parent.

0

Try to add flex to your p it will set content inside in the middle:

display: flex;
align-items: center;
Zirek
  • 513
  • 3
  • 11
  • 20