Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically center images within an LI element?

I am using jQuery mobile and I am trying to center some image icons within a list. The problem I am having is that the images are not vertically centered within a list item.

Can someone kindly point me in the right direction since I am not a CSS expert by far. I know I can get them using tables but I do not want to do that. Thanks.

Oh and I am using EJS in the code below. Please see the screenshot:

Screenshot of list

Here is my code:

<li data-icon="false">
            <a href="#">
                <img src="images/img_trans.gif" class='largePlatform platform_<%= releases[i].platform_abbr %>_large' width='30' height='30' style="position:absolute; top:25%; left:10px"/>
                <h2 style="position:absolute; top:25%; left:50px"><%= releases[i].platform_abbr %></h2>
                
                 <div data-role="controlgroup" data-type="horizontal" style="float:right" >
                 
                    <% if (purchaseText != "") { %>
                 
                        <img src="images/game_detail/<%= releases[i].purchase_button_icon %>-purchase.png" width="35" height="35" onclick="window.open('<%= releases[i].purchase_button_url %>');" alt="<%= purchaseText %>" style="position:relative; top:10px;"/>
                    
                    <% } %>
                    
                    <div data-role="button" data-icon="reminder" data-theme="<%= buttonTheme %>" onclick="<%= buttonAction %>(<%= releases[i].id %>)">
                   <%= buttonText %>
               </div>
                </div>
                    
            </a>
        </li>
like image 308
jini Avatar asked Feb 29 '12 13:02

jini


People also ask

How do I center an image in Li tag?

You can just remove img {display: block;} , by default it is inline level, they will get centered with text-align: center; set on the container, which you already did.

How do you vertically align the middle Li?

Inorder to make vertical-align: middle; work, you need to use display: table; for your ul element and display: table-cell; for li elements and than you can use vertical-align: middle; for li elements.

How do you center an image in an element?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.


1 Answers

Live Example:

http://jsfiddle.net/B6Z9N/

HTML

<li>
    <img src="http://dummyimage.com/20x20/000/000000.png" />
</li>​

CSS

li {
    border: 1px dotted black; /* Just to illustrate height */

    height: 100px;
    line-height: 100px;
    vertical-align: middle;
}​

Found this article: http://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/

like image 146
jared_flack Avatar answered Oct 13 '22 02:10

jared_flack