user843337
user843337

Reputation:

CSS Vertical Alignment of Div

I have a heading, then below that I display a selection of items next to each other. On the same line as the selection of items I display a single picture.

The issue I'm having is that I want the single image (X) to be aligned so that the bottom of it is in line with the bottom of the selection of items (A, B & C). Eg.

H1 Title             ----------
                     |        |
                     |        |
                     |    X   |
-----  -----  -----  |        |
| A |  | B |  | C |  |        |
-----  -----  -----  ----------

The issue I'm having is that it appears like so:

H1 Title

-----  -----  -----  ----------
| A |  | B |  | C |  |        |
-----  -----  -----  |        |
                     |    X   |
                     |        |
                     |        |
                     ----------

Here is the HTML Im using:

<h1>H1 Title</h1>

<div id="items">
<ul>
    <li>A</li>
    <li>B</li>
    <li>C</li>
</ul>
</div>

<div id="single_image">
        <img src="myImage.png" />
</div>

The CSS I'm using is:

#items { 
    float:left; 
}

#items ul { 
    list-style:none; margin:0; padding:0; 
}

#items ul li { 
    display:inline; float:left; 
    margin-bottom:20px; font-size:22px; 
}

#single_image { }

#single_image img { 
    float:right; height:130px; 
    width:inherit; margin-right:40px; 
}

Please could someone help me out? I can't work out the issue. I tried using the vertical align attribute on the img in the CSS, however it didn't seem to make any difference.

Upvotes: 0

Views: 263

Answers (2)

SeptimusFossett
SeptimusFossett

Reputation: 56

If the image is a fixed size, a quick fix for this problem would simply be to use a negative margin-top, so that the image is its own height above - so bottom is actually where the top is now.

E.g. If the image is 130px height, do:

#single_image img { margin-top: -130px; }

Upvotes: 1

MikeTedeschi
MikeTedeschi

Reputation: 583

One potential solution is to wrap them in a container and absolutely position #items to the bottom of the container. Here is a sample:

http://jsfiddle.net/5csWH/

<div class="container">
    <h1>H1 Title</h1>
    <div id="items">
        <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
        </ul>
    </div>
    <div id="single_image">
        <img src="myImage.png" />
    </div>
    <div class="clearfix"></div>
</div>

CSS:

#items          { float:left; }
#items ul       { list-style:none; margin:0; padding:0; position: absolute; bottom: 0; }
#items ul li    { float:left; margin-right:20px; font-size:22px; width: 50px; background: #DDD; }

#single_image       { }
#single_image img   { float:right; height:130px; width:inherit; background: #DDD; }

.container { width: 50%; margin: 0 auto; background: #EEE; position: relative; }
.clearfix { clear: both; }​

Upvotes: 0

Related Questions