html - 如何使列表项内的链接占用整个列表项空间?

标签 html css

有没有办法让列表项中的链接始终占据列表项的 100% 宽度和高度,无论屏幕大小如何?

.navList {
  list-style-type: none;
  overflow: hidden;
}

.navListItem {
  float: left;
}

.navListLink:link, .navListLink:visited {
  color: #000000;
  text-decoration: none;
  font-size: 100%;
  display: block;
  padding: 1em;
}

最佳答案

我已经编写了这段小代码来向您展示我将如何做到这一点。

<style>
    ul{
        background-color: orange;
        width: 78%;
        height: 8rem;

        /* Centered on Viewport*/
        position: absolute;
        top: 0; left: 0; right: 0; bottom: 0;
        margin: auto;

        padding: 1rem;
    }
    li{            
        outline: thin solid white;
        list-style: none;
        font: 1.2rem/2rem Serif;
    }
    a{
        box-sizing: border-box;
        background-color: #444;
        display: inline-block;
        width: 100%;
        padding: 0 1rem; 
        color: white;        
    }
    a:link{
        text-decoration: none;
    }
</style>

<ul>
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
    <li><a href="#">item 4</a></li>
</ul>

希望对你有帮助。

关于html - 如何使列表项内的链接占用整个列表项空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44577702/

相关文章:

html - 如何在 en amil(电子邮件 html)上将图标列表居中

jquery - 如何在 Bootstrap 中垂直对齐的同一列上拉和推两个 div

javascript - document.getElementById 起作用但 jquery 不起作用的原因是什么?

html - CSS3条形图是颠倒的?

javascript - 复制和粘贴时不允许在文本框中使用小数和文本值

html - float 和边距顶部

javascript - 在另一个元素上触发特定的鼠标事件

html - div 框对齐中的空间问题

javascript - 仅设置一个 div 以在滑动切换时打开

Javascript 如何将纯文本中的日期接收到公式中