Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

padding for <a> inside <li> pushes through the top of edge of <li>

Tags:

css

padding

I have this code:

<ul>
   <li><a>my link 1</a></li>
   <li><a>my link 2</a></li>
   <li><a>my link 3</a></li>
</ul>

When I apply padding to <a> like so:

ul {
  list-style: none;
  margin: 30px 0 0 2.6em;
}
ul li {
  height: 41px;
  width: 196px;
  background: url(images/image.png) no-repeat; 
  position: relative;
}
ul li a {
  padding-top: 5px;
}

The padding works but it collapses and goes through the top edge of <li> instead of pushing the <a> down. How ca I resolve this?

like image 437
jilseego Avatar asked Jul 05 '11 16:07

jilseego


People also ask

How do I give padding to Li?

Complete HTML/CSS Course 2022 Left padding padding-left is to be added to <ul> tag list item i.e. <li> tag. Through this, padding gets added, which will create space between list bullets and text in HTML.

How do you move the Li element to the left?

You can add padding: 0 to the ul element to force it to stick to the left border of the parent nav element.

How do you move UL to the right?

To make a right-aligned version of the list, only three changes need to occur. First, set the "UL" "text-align" to "right". Second, change the left "background-position" from "0" to "100%" - which makes the image align up with the right edge. And finally change "padding-left" to "padding-right".

How do I remove margins from UL?

The amount varies on each browser. Some browsers use padding (Mozilla, Netscape, Safari) and others use margins (Internet Explorer, Opera) to set the amount of indentation. To remove this left-indentation consistently across all browsers, set both padding and margins to "0" for the "UL".


2 Answers

you should give your "a" display:inline-block

anchors are "inline" by default and won't show padding properly.

jsFiddler demo: http://jsfiddle.net/PqajF/3/

like image 65
Mo Valipour Avatar answered Oct 15 '22 10:10

Mo Valipour


ul li a
{
    padding-top: 5px;
    display: inline-block;
}
like image 37
Robert Koritnik Avatar answered Oct 15 '22 10:10

Robert Koritnik