0

我有以下 html 和 css 代码,我希望标签使用 css<a>环绕标签。<li>

这是jsfiddle

html

<section class="row header">
            <ul class="nav">
                <li class="navfirst"><a href="#">Contact</a></li>
                <li><a href="#">Home</a></li>
                <li><a href="#">Resume</a></li>
                <li><a href="#">Interests</a></li>
                <li><a href="#">Work</a></li>
                <li class="navlast"><a href="#">Comments</a></li>
            </ul>
        </section>

css

.header .nav {
 margin: 0;
 padding: 0; }
  .header .nav li {
list-style-type: none;
display: inline-block;
padding: 0.5% 1%;
border: 1px solid green; }
.header .nav li.navfirst {
  margin-right: 4%; }
.header .nav li.navlast {
  margin-left: 4%; }
.header .nav li a {
  display: block;
  text-decoration: none;
  border: 1px solid red;
  width: 100%;
  height: 100%; }

这是scss。上面的css是从scss文件转换而来的

.nav {
    margin: 0;
    padding: 0;
    li {
        list-style-type: none;
        display: inline-block;
        padding: 0.5% 1%;
        border: 1px solid green;
        &.navfirst {
            margin-right: 4%;
        }
        &.navlast {
            margin-left: 4%;
        }
        a {
            display: block;
            text-decoration: none;
            border: 1px solid red;
            width: 100%;
            height: 100%;
        }
    }
}
4

1 回答 1

1

li标签中删除填充:

...
display: inline-block;
padding: 0.5% 1%;   <-- padding: 0
border: 1px solid green; 
...

http://jsfiddle.net/qrXWQ/1/

将填充添加到a

.header .nav li a {
     display: block;
     text-decoration: none;
     border: 1px solid red;
     padding: 5px; 
}

从上面的规则中删除width: 100%and height: 100%,否则填充会弄乱布局......

http://jsfiddle.net/qrXWQ/2/

In the rule above the percent unit will not work because it has no base width/height to apply the % to. You have 3 options: 1) leave the padding in the LI, 2) put the padding in the a with a unit other than % (em fits well here), or 3) use media queries to set a diferent padding for each screen size.

于 2012-09-05T13:34:31.963 回答