Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically align an image inside a <h1> tag

Tags:

html

css

HTML

<h1>abc abc<span><img class="goldT" src="btns/nav02.png"></span></h1>  

css

h1{
    margin:15px 0 15px 20px;
    font-size:1.1rem;
    font-weight:bold;
    background:#e1e1e1;
    padding-left:10px;
    color: #444444;
}
h1 span{
    float:right;
    margin-right:14px;
}
h1 .goldT{
    width:140px;
    vertical-align:middle;
}  

How can I keep img goldT vertically centered with header text abc.

Here is the FIDDLE

like image 822
qadenza Avatar asked Nov 28 '13 07:11

qadenza


People also ask

How do you vertically align h1 tags?

Just use padding top and bottom, it will automatically center the content vertically.

How do I vertically align an image in HTML?

To center an image vertically, you can wrap it in a block element like a div and use a combination of the CSS position property, the left and top properties, and the transform property.

How do I vertically align text in a picture?

We need to create a parent element that contain both image and text. After declaring the parent element as flexbox using display: flex; we can align the items to the center using align-items: center;. Example: This example uses flexbox to vertically align text next to an image using CSS.


3 Answers

Try:

HTML:

<h1>
    <span class="text">abc abc</span><span class="span">
        <img class="goldT" src="https://www.google.com/images/srpr/logo11w.png">
    </span>
</h1>

CSS:

.span, .text {
    vertical-align:middle;
    display:table-cell;
}
h1 {
    display:table;
    width:100%;
}

Fiddle here.

like image 147
codingrose Avatar answered Oct 25 '22 12:10

codingrose


A much simpler solution is as follows:

<h1><span>abc abc</span><img class="goldT" src="btns/nav02.png"></h1>

On the h1 set a line-height and on the image and span set a vertical-align:middle;. Worked for me.

like image 23
Ukuser32 Avatar answered Oct 25 '22 12:10

Ukuser32


Try this here we can make image vertically center

Try this http://jsfiddle.net/SxxWV/10/

HTML

<div class="main">
<div class="box"><img  src="https://www.google.com/images/srpr/logo11w.png" />
</div>   
</div>

CSS

.main{ height:500px; border:1px red solid;position:relative}
.box{width:40px; height:40px; background:transparent; }

/* for centering */
img{width:100px}
.box { display: inline-block; width:100px }
.main{ text-align: center; }
.main:after{ content: ""; display: inline-block; height: 100%; vertical-align: middle; }
like image 28
Dinesh Kanivu Avatar answered Oct 25 '22 13:10

Dinesh Kanivu