开发者

Make <h1> vertically center with CSS?

开发者 https://www.devze.com 2023-03-12 04:21 出处:网络
<header id=\"logo\"> <img src=\"logo.png\" /> <h1>Some text here</h1> </header>
<header id="logo">
    <img src="logo.png" />
    <h1>Some text here</h1>
</header>

I use

h1{display: inline;}

to make them in the same line, but the text is lower than the image, the images is 48x48px and the text size is 23px, I'd like to make the text vetically center to the image, how could I do that with CSS? Just need to support Chrome.

Thanks for your an开发者_开发问答swers, finally got it work: http://jsfiddle.net/tFdpW/


Like this? http://jsfiddle.net/xs4x6/

<header id="logo">
    <img src="http://dummyimage.com/48x48/f0f/fff" />
    <h1>Some text here</h1>
</header>

header img {
    vertical-align: top
}

h1 {
    display: inline;
    font-size: 23px;
    line-height: 48px
}


Try to add vertical-align:middle; to the image.

Example: http://jsfiddle.net/Uxs7w/


h1{display: inline;line-height:48px;}


@wong2; first of all. inline property doesn't support vertical margin, padding properties .So, there are other options.

first: remove display:inline & give float to it.

img, h1{float:left;}
h1{margintop:15px; margin-left:10px;}

second: give display:table to it.

#logo{display:table}
img, h1{display:table-cell}
h1{vertical-align:middle;}

thrid: if don't want to remove display:inline.

h1{display: inline;line-height:48px;}


if the "logo" header got the right height, just use the css vertical-align:middle property

http://www.w3schools.com/css/pr_pos_vertical-align.asp


Try to use

h1 {
display: table-cell;
vertical-align: middle;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消