开发者

how to align text under image in css menu

开发者 https://www.devze.com 2023-03-10 17:45 出处:网络
I want to align menu text at the bottom of image how to i achieve it? Expected output: ImageImageImageImage

I want to align menu text at the bottom of image how to i achieve it?

Expected output:

Image    Image    Image    Image
[menutext]    [menutext][menutext]    [menutext]

Actual output :

Image[menutext]      Image[menutext]      Image[menutext]      Image[menutext]  

my Css Code:

#vilaniHeader
{
    margin: 0;
    padding: 0;
    height: 80px;
    background-color: Black;
}

#vilaniHeader h1
{
    padding-left: 15%;
    font: Arial;
    font-size: 30px;
    color: #ffffff;
    font-weight: bold;
    float: left;
}
#vilaniHeader #menu
{
    color: #ffffff;
    font: Arial;
    font-size: 18px;
    font-weight: bold;
    padding-top: 30px;
    padding-left: 30%;
}

#vilaniHeader #menu ul
{
    list-style: none;
    margin: 0;
    padding: 0;
    padding-right: 300px;
    padding-bottom: 300px;

}
#vilaniHeader #menu li
{
    display: inline;
    margin: 0 15px 0 15px;
    float: none;
    text-align:center;

}

#vilaniHeader #menu a
{
    text-decoration: none;
    color: #ffffff;
}
#vilaniHeader #menu .menuHome
{
    color: red;
    clear:both;
    padding-top:50px;
    background-image:url:("Styles/menuHome.png") ;
    vertical-align:text-top;
}

开发者_C百科and My HTML code

<div id="vilaniHeader">
                <h1>
                   Comany name
                </h1>
                <div id="menu">
                 <ul>
                 <li class="menuHome"><img src="Styles/menuHome.png" />Home</li>
                 <li><a href="About.aspx">Car</li>
                 <li><a href="About.aspx">Mobile</li>
                 <li><a href="About.aspx">OldThings</li>
                 <li><a href="About.aspx">Matrimoni</li>
                 </ul>
                </div>
            </div>

I want menu text should be align at the bottom of the image plese help me to do that.


I came up with this solution building upon the answer here from tejash. My answer validates and is search engine friendly.

  • I prefered to use links within a div but I imagine this will work with an ul
  • I use a background image that does not show if CSS is disabled
  • I use a span set displayed as block because a div inside an a tag does not validate
  • I use a class to place the image but use ids if you want different pics for each link
  • Change the width + heights to suit your needs

HTML

<div id="nav">
    <a href="#"><span class="image"></span><span>About Us</span></a>
    <a href="#"><span class="image"></span><span>Investors</span></a>
</div>

CSS

#nav a {
    display:block; 
    float: left;
    width:100px; 
}
.image {
    display:block;
    background: url("myimage.jpg") no-repeat scroll center center transparent;
    height:40px;
    width:100px; 
}


Make the img a block element so it takes the full width / line-breaks afterwards.

#menu li { display:block; }

That’s all.


I would suggest add some wrapper on text and make image and wrapper both display:block; You can use span tag as an wrapper for text.

HTML

<ul>
<li><a><img src="Styles/menuHome.png" /><span>Home</span></a></li>
</ul>

CSS

li img, li span { display:block; }


If you want your text to overlay your image, but at the bottom, you should try to play around with the line-height property. That will cause your text to move down, so it will be in the center of it's line.


I have two solutions for you. style1 works for items with text smaller than the image. style2 works for items with text wider than the image. Easiest is to make sure that the images are always wider or smaller than the text, so that you need only one style.

CSS:

#menu {
    list-style:none
}
#menu li {
    float:left;
    text-align:center
}
#menu .style1 img, #menu .style2 span {
    overflow:hidden
}
#menu .style1 span, #menu .style2 img {
    display:block
}

HTML:

<div id="vilaniHeader">
    <h1>Comany name</h1>
    <ul id="menu">
        <li class="style1"><img src="Styles/menuHome.png" width="10" alt="" /> <span>Home</span></li>
        <li class="style2"><img src="Styles/menuHome.png" width="100" alt="" /> <span>Car</span></li>
    </ul>
</div>

I'm not a span-fan but it seems like you can't do it without here.


BTW, why don't you just add a br?

CSS:

#menu {
    list-style:none
}
#menu li {
    float:left;
    text-align:center
}

HTML:

<div id="vilaniHeader">
    <h1>Comany name</h1>
    <ul id="menu">
        <li><img src="Styles/menuHome.png" width="10" alt="" /><br />Home</li>
        <li><img src="Styles/menuHome.png" width="100" alt="" /><br />Car</li>
    </ul>
</div>

I guess that's the most easy and reliable solution.


You can do this way, obviously replacing the image sample I used. For the link to work, you can use a jQuery click event on LI, so it searches for the link inside the clicked LI and then opens the desired link.

http://jsfiddle.net/WcePK/

HTML

<ul>
    <li class="menuHome"><img src="Styles/menuHome.png" />Home</li>
    <li style="background-image: url('http://jsfiddle.net/img/logo.png')"><a href="About.aspx">Car</a></li>
    <li style="background-image: url('http://jsfiddle.net/img/logo.png')"><a href="About.aspx">Mobile</a></li>
    <li style="background-image: url('http://jsfiddle.net/img/logo.png')"><a href="About.aspx">OldThings</a></li>
    <li style="background-image: url('http://jsfiddle.net/img/logo.png')"><a href="About.aspx">Matrimoni</a></li>
</ul>

CSS

LI {
      float: left;
      margin: 5px;
      padding: 50px 10px 10px;
      min-width: 100px;
      background-repeat: no-repeat;
      background-position: center 10px;
      background-color: #366D93;
      text-align: center;
      cursor: pointer
}
0

精彩评论

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