开发者

Adding a margin to a single list item in a horizontal list

Is it possible to add a margin to the top of a certain item in a horizontal unordered list?

I've made a cloud-themed navigation bar, but I don't want all the clouds to be all in one straight line, so I'm trying to make it so they aren't all in one line.

None of these answers have worked for me, it doesn't change the list items at all, so I've added some of the code here:

HTML:

<div id="clouds">
    <ul>
        <li id="home"><a href="#"><img src="buttons/home.png"></a></li>
        <li id="info"><a href="#"><img src="buttons/Info.png"></a></li>
        <li id="designs"><a href="#"><img src="buttons/Designs.png"></a></li>
        <li id="contact"><a href="#"><img src="buttons/Contact.png"></a></li>
    </ul>
</div>

CSS:

body{
    background-image: -webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0, rgb(94,132,19)),
        color-stop(0.6, rgb(124,180,34)),
        color-stop(0.6, rgb(252,253,255)),
        color-stop(1, rgb(117,178,209))
    );
    background-image: -moz-linear-gradient(
        center bottom,
        rgb(94,132,19) 0%,
        rgb(124,180,34) 60%,
        rgb(252,253,255) 60%,
        rgb(117,178,209) 100%
    );
    min-height: 500px;    
    text-align: center;
    background-position: absolute;
}
#sun{
    height: 72px;
    width: 72px;
    float: right;
    background-image: url('sun.png');
}

/*---NAVIGATION---*/
#home img:hover{
    background-image: url('buttons/home.png');
}
#info img:hover{
    background-image: url('buttons/info.png');
}
#designs img:hover{
    background-image: url('buttons/designs.png');
}
#contact img:hover{
    background-image: url('buttons/contact.png');开发者_运维技巧
}
#clouds ul, li{
    display: inline;
    margin: 40px;
}
#clouds ul{
    display: inline;
}
/*---NAVIGATION END---*/

#wrap h1{
    margin-top: 175px;
    font-family: code bold;
    color: white;
    text-align: center;
    text-shadow: 2px 2px 5px #000;
}
#wrap{
    font-family: code bold;
    color: white;
    text-shadow: 2px 2px 5px #000;
    width: 500px;
    text-align: center;
    margin: 0 auto;
}
#text{
    font-family: kartika;
    font-size: 22;
    -moz-column-count: 2;
    -moz-column-gap: 3em;
    -moz-column-rule: 1px dashed fff;
    -webkit-column-count: 2;
    -webkit-column-gap: 3em;
    -webkit-column-rule: 1px dashed fff;
}

I've decided to add the whole of my stylesheet because there might be an error in there somewhere that might be causing the list item margin to not work, although I can't seem to find any problems.

Thanks for the answers and comments so far.

It seems changing the margin of a single item only works if the list has float: left to it, it is possible to center the list instead of floating it to the left to make it work?


I have two solutions for you: http://jsfiddle.net/8avbq/5/

  1. You can use the :nth-child() selector from CSS3. You put the number of the li you want to target in the brackets (This method will not support IE8 and below though):

    ul li:nth-child(2){
         margin-top:15px;
         background: red;
    }
    
  2. Set a class on the li:

    HTML:

    <ul>
       <li>1</li>
       <li>2</li>
       <li>3</li>
       <li class="extra">4</li>
       <li>5</li>
    </ul>
    

    CSS:

    ul li.extra{
        margin-top:15px;
    }
    

EDIT : Based on new question requirements. This should do the trick: http://jsfiddle.net/A8mAY/7/


Change:

#clouds ul, li{
    display: inline;
}

to:

#clouds ul, li{
    display: inline-block;
}

That should allow you to use the top and bottom margins/padding the way you're trying to. (If you run into issues with Internet Explorer with inline-block, then try shifting the display and margins to the <a> tags inside your list items (#clouds ul li a), as old versions of IE should cooperate with elements that default to inline.

Also, is #clouds ul, li supposed to have the comma in it? If so, then you can remove the #clouds ul that follows it. As you have it, though, it will turn all your list items inline and give it the margin you have.


HTML:

<ul>
   <li>item 1</li>
   <li>item 2</li>
   <li class="certain_item">item 3</li>
   <li>item 4</li>
   <li>item 5</li>
   <li>item 6</li>
</ul>

CSS:

ul li
{
    float:left;
}

ul li.certain_item
{
    margin-top:15px;
}

JS Fiddle: http://jsfiddle.net/aH9ra/5/


if you have a static # of navigation items you could use nth-child to style them

so with this html:

<ul id="nav">
    <li><a href="#">Nav Item 1</a></li>
    ...
</ul>

use this css styling:

#nav :nth-child(1) { margin: 8px; }
#nav :nth-child(2) { margin: 5px; }
#nav :nth-child(3) { margin: 3px; }
#nav :nth-child(4) { margin: 12px; }
#nav :nth-child(5) { margin: 6px; }

note: browser support for nth-child is somewhat limited ( IE9+, Safari 3.1+, FF 3.5+, Chrome )

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜