Adrian
Adrian

Reputation: 2656

Center h1 over image in a div

How to center (vertically,horizontally) properly over an image in a ?

<div class="category-info">
    <div class="image">
            <h1>Title</h1>
            <img src="image.jpg" />
        </div>
    </div>

CSS

.category-info {
    text-align: center;
    height: 200px;
    width: 770px;
    overflow: auto;
    margin-bottom: 20px;
}

The image is 770px width and 200px height. I don't what to do next with . I tried several things, without success.

test image

Upvotes: 3

Views: 17211

Answers (4)

Milche Patern
Milche Patern

Reputation: 20492

html

<h1 style="background-image:url(your php source.img)">Title</h1>

css :

h1 {
    height: 200px;
    width: 770px;
    margin-bottom: 20px;
    text-align:center;
    vertical-align:middle;
    line-height:200px;
    background:transparent no-repeat scroll 50% 50%;
   }

and nothing else

Upvotes: 0

tedski
tedski

Reputation: 2311

I threw a position relative on the .image class and set the width and height on the image element (that way it doesn't resize when it loads). I changed the table back to the h1 and added your line-height of 200px. That is the only downside, you'll still have to manually set the line-height of the h1.

HTML:

<div class="category-info">
    <div class="image">
         <h1>Title</h1>
        <img src="http://placekitten.com/770/200" />
    </div>
</div>

CSS:

.category-info {
    text-align: center;
    margin-bottom: 20px;
}

.image{
    position:relative;
}

.image img{
    width:770px;
    height:200px;
}

.image h1{
    position:absolute;
    width:100%;
    color:white;
    line-height:200px;
    margin:0;
}

JSFiddle: http://jsfiddle.net/5wGwL/2/

Upvotes: 1

Mooseman
Mooseman

Reputation: 18891

Here you go: http://jsfiddle.net/QjLuP/4/

The CSS:

.image{
    position: relative;
    background: green; /* IE */
}
.image h1{
    z-index: 2;
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    font-size: 20px;
    width: 100%;
    height: 26px;
    padding: 0;
    margin: 0;
    margin-top: -13px; /* 1/2 height */
    text-align: center;
    background: red;
    background: rgba(170, 0, 0, 0.8); /* CSS3 */
}
.image img{
    z-index: 1;
    width: 100%;
    height: 100%;
    position: absolute;
    top:0;
    right:0;
    bottom:0;
    left:0;
    background:green
}

Upvotes: 5

Kevin Boucher
Kevin Boucher

Reputation: 16705

Have you tried this?

h1 { text-align:center; }

Upvotes: 0

Related Questions