开发者

Center image above list items

I am working on a project and having trouble with my UL>LI s开发者_运维技巧tyle.

i am trying to put images all over the list and i want when i hover over the link the image color change so i use 2 images

1 orange & 1 blue

but i am unable to centrise the images over top navigation links.

Center image above list items

the codes are :

HTML:

<div id="TopMenu">
    <ul style="display:">
        <li class="HeaderLiveChat" style="display:none"></li>
        <li class="First" style="display:">
            <a href="/account.php">My Account</a>
        </li>
        <li style="display:">
            <a href="/orderstatus.php">Order Status</a>
        </li>
        <li style="display:">
            <a href="/wishlist.php">Wish Lists</a>
        </li>
        <li>
            <a href="/giftcertificates.php">Gift Certificates</a>
        </li>
        <li class="CartLink" style="display:">
            <a href="/cart.php">
        </li>
        <li style="display:">
        <div>
            <a onclick="" href="/login.php">Sign in</a>
            or
            <a onclick="" href="/login.php?action=create_account">Create an account</a>
        </div>
        </li>
    </ul>
    <br class="Clear">
</div>

CSS:

/* Top Navigational Menu */
#TopMenu {
    position: absolute;
    right: 10px;
    top: 70px;
    font-size: 10px;
    text-align: right;
}

#TopMenu ul, #TopMenu li {
    list-style: none;
    padding: 0;
    margin: 0;
}

#TopMenu ul li {
    display: inline;
    list-style-image: none;
    list-style-position: outside;
    list-style-type: none;
    margin: 0px;
    padding: 0px 4px 0 6px;
}

#TopMenu ul li.First {
    border-left: 0;
}

#TopMenu ul li.home {
    background:url('http://cdn1.iconfinder.com/data/icons/munich/16x16/home.png') no-repeat;
    height:16px; width:16px;
}

#TopMenu a {
    color: #333;
}

#TopMenu a:hover, #TopMenu a:visited {
    color: #333;
}

#TopMenu li div {
    display: inline;
}

Please kindly help me to solve this

NOTE:

I want changed icon over each link, the above image is just an example image

Thank you


Made two changes

  1. Added a top padding of 25px to li for the image

    #TopMenu ul li {
    display: inline;
    list-style-image: none;
    list-style-position: outside;
    list-style-type: none;
    margin: 0px;
    padding: 25px 4px 0 6px;
    }
    
  2. changed the logos background-position:center; ( Please note that I have used short hand notation )

    #TopMenu ul li.home {
    background:url('http://cdn1.iconfinder.com/data/icons/munich/16x16/home.png') no-repeat center;
    height:16px; width:16px;
    }
    #TopMenu ul li.home:hover {
    background:url('http://cdn1.iconfinder.com/data/icons/cologne/16x16/home.png') no-repeat center;
    }
    

Working Demo : http://jsfiddle.net/naveen/HuTge/1/

P.S: Please note that I am using #TopMenu and markdown is not working properly.


You need to add css for your anchors:

a {
  padding-top: 20px;
  background: url('http://cdn1.iconfinder.com/data/icons/munich/16x16/home.png') no-repeat 50% 0px;
  height: 16px;
  width: 16px;
}

The 50% centers the background. Of course, you should use a more specific selector for all your anchors, instead of simply a.

To change the image on mouse over, you need to:

a:hover {
 background-image: url('http://icons.iconarchive.com/icons/3xhumed/mega-games-pack-32/16/Team-Fortress-2-new-16-icon.png');
}

see demo here


HTML:

<ul class="navigation">
    <li>
        <a id="nav-myaccount" href="/account.php">My Account</a>
    </li>
    <li>
        <a id="nav-orderstatus" href="/orderstatus.php">Order Status</a>
    </li>
</ul>

CSS:

ul.navigation {
    list-style: none;
    margin: 0;
    padding: 0;
}

ul.navigation li {
    display: inline;
    margin: 0px;
    padding: 0px 4px 0 6px;
}

ul.navigation li a#nav-myaccount {
    background-image: url(nav-myaccount.png);
    background-repeat: no-repeat;
    background-position: center top;
}

ul.navigation li a#nav-myaccount:hover {
    background-image: url(nav-myaccounthover.png);
    background-repeat: no-repeat;
    background-position: center top;
    color: #333;
}

ul.navigation li a#nav-orderstatus {
    background-image: url(nav-orderstatus.png);
    background-repeat: no-repeat;
    background-position: center top;
}

ul.navigation li a#nav-orderstatus:hover {
    background-image: url(nav-orderstatushover.png);
    background-repeat: no-repeat;
    background-position: center top;
    color: #333;
}

Note that instead of using different pictures for every navigation item, you should put them all in the same picture and use background-position to change the position of the background, that way when you hover your mouse on navigation item, client doesn't have to load the hover image and there won't be a split-second moment when the image blinks because the hover image isn't loaded yet.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜