【问题标题】:Flexbox ignores padding & width on linksFlexbox 忽略链接上的填充和宽度
【发布时间】:2017-12-10 16:08:06
【问题描述】:

基本上,我有一个使用 flexbox 的导航,并且一切正常。但是,最近回顾它时,我注意到 Li 中的 <a> 与 Li 本身的大小不同,因此不能使整个 Li 可点击。

我尝试了许多不同的方法来使<a> 的大小相同,当我向它们在检查器中显示为具有填充但填充不可点击的链接添加填充时...

.header .navigation {
  display: block;
  width: 100%;
  position: relative;
}

.header .navigation ul {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: row;
}

.header .navigation ul li {
  float: none;
  display: inline-block;
  background-color: #9dabc0;
  border-right: 1px solid #7e899a;
  border-left: 1px solid #bdc6d4;
  text-align: center;
  flex-grow: 1;
}

.header .navigation ul li:last-child {
  background-color: #b4a042;
  border-right: none;
}

.header .navigation ul li:last-child:hover {
  background-color: #b49242;
}

.header .navigation ul li:last-child a {
  padding: 0 5px;
  text-align: center;
}

.header .navigation ul li:first-child {
  border-left: none;
}

.header .navigation ul li:hover {
  background: #3a495e;
}

.header .navigation ul li a {
  font-size: 16px;
  font-family: "Open Sans";
  color: #fff;
  font-weight: bold;
  line-height: 59px;
  text-shadow: 0.5px 0.866px 0 rgba(1, 1, 1, 0.39);
  padding: 0 5px;
  text-align: center;
  width: 100%;
}

.header .navigation ul li ul {
  left: 0px;
  top: 0px;
  margin-left: 0px;
  display: none;
  /* hide all sub menus from view */
  position: absolute;
  z-index: 100;
  background: #9dabc0;
  top: 59px;
  /* this should be the same height as the top level menu -- height + padding + borders */
  margin-left: 0px;
  width: 100%;
}

.header .navigation ul li ul li {
  border-top: 1px solid #7e899a;
  border-bottom: 1px solid #bdc6d4;
  border-left: none;
  border-right: none;
  background-color: #9dabc0;
  height: 42px;
  display: block;
}

.header .navigation ul li ul li:last-child {
  border-bottom: none;
  background-color: #9dabc0;
}

.header .navigation ul li ul li:last-child a {
  padding: 10px 24px;
}

.header .navigation ul li ul li a {
  font-size: 16px;
  font-family: "Open Sans";
  width: 100%;
  color: #fff;
  font-weight: bold;
  text-shadow: 0.5px 0.866px 0px rgba(1, 1, 1, 0.39);
  display: block;
  line-height: 18px;
  padding: 10px 24px;
}

.header .navigation ul li ul li:hover {
  background: #3a495e;
}

.header .navigation ul li {
  position: relative;
}

.header .navigation ul li:hover>ul {
  display: block;
}

.header .navigation ul li li.has-children>a:after {
  color: green;
  content: ' ►';
  font-size: 10px;
  float: right;
  padding-right: 10px;
  vertical-align: 1px;
}

.header .navigation ul .current-menu-item:after {
  color: green;
  content: '▲';
  position: absolute;
  top: 100%;
  left: 30%;
}

.header .navigation ul .sub-menu .current-menu-item:after {
  content: none;
}

.header .navigation ul li.has-children>a:after {
  color: green;
  content: '▼';
  font-size: 10px;
  padding-left: 5px;
  vertical-align: 1px;
}


}
<div class="header">
  <div class="navigation">
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">Engineering Castings</a>
        <ul>
          <li><a href="">Example</a></li>
          <li><a href="">Example</a></li>
          <li><a href="">Example</a></li>
        </ul>
      </li>
      <li><a href="">Architectural Castings</a>
        <ul>
          <li><a href="">Example</a></li>
          <li><a href="">Example</a></li>
        </ul>
      </li>
      <li><a href="">Decorative Castings</a>
        <ul>
          <li><a href="">Example</a></li>
          <li><a href="">Example</a></li>
          <li><a href="">Example</a></li>
          <li><a href="">Example</a></li>
        </ul>
      </li>
      <li>
        <a href="">Downloads</a></li>
      <li>
        <a href="">Gallery</a></li>
      <li>
        <a href="">Contact</a></li>
      <li>
        <a href="">Blog</a></li>
      <li>
        <a href="">FREE Castings Guide</a></li>
    </ul>
  </div>
</div>

这是代码笔:https://codepen.io/danielvickerssa/pen/ZyjqRr

【问题讨论】:

    标签: html css sass flexbox


    【解决方案1】:

    如果将display: flex 添加到列表项,它们将强制子元素(在本例中为&lt;a&gt;)拉伸容器的整个高度。

    这是 flex 容器中的默认设置 - align-items: stretch

    只需将其添加到您的代码中:

    li {
        display: flex;
    }
    

    https://codepen.io/anon/pen/JJBePq

    【讨论】:

    • 经过测试并且可以工作,谢谢 :) 我一定是在之前的工作中删除了这段代码。
    • 这里的答案很好。
    【解决方案2】:

    您可以制作链接inline-block,然后您可以给它们垂直填充/高度。 display: inline-block; height: 100%; 使它们填充父 li

    .header .navigation {
      display: block;
      width: 100%;
      position: relative;
    }
    
    .header .navigation ul {
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: row;
    }
    
    .header .navigation ul li {
      float: none;
      display: inline-block;
      background-color: #9dabc0;
      border-right: 1px solid #7e899a;
      border-left: 1px solid #bdc6d4;
      text-align: center;
      flex-grow: 1;
    }
    
    .header .navigation ul li:last-child {
      background-color: #b4a042;
      border-right: none;
    }
    
    .header .navigation ul li:last-child:hover {
      background-color: #b49242;
    }
    
    .header .navigation ul li:last-child a {
      padding: 0 5px;
      text-align: center;
    }
    
    .header .navigation ul li:first-child {
      border-left: none;
    }
    
    .header .navigation ul li:hover {
      background: #3a495e;
    }
    
    .header .navigation ul li a {
      font-size: 16px;
      font-family: "Open Sans";
      color: #fff;
      font-weight: bold;
      line-height: 59px;
      text-shadow: 0.5px 0.866px 0 rgba(1, 1, 1, 0.39);
      padding: 0 5px;
      text-align: center;
      width: 100%;
      display: inline-block;
      height: 100%;
    }
    
    .header .navigation ul li ul {
      left: 0px;
      top: 0px;
      margin-left: 0px;
      display: none;
      /* hide all sub menus from view */
      position: absolute;
      z-index: 100;
      background: #9dabc0;
      top: 59px;
      /* this should be the same height as the top level menu -- height + padding + borders */
      margin-left: 0px;
      width: 100%;
    }
    
    .header .navigation ul li ul li {
      border-top: 1px solid #7e899a;
      border-bottom: 1px solid #bdc6d4;
      border-left: none;
      border-right: none;
      background-color: #9dabc0;
      height: 42px;
      display: block;
    }
    
    .header .navigation ul li ul li:last-child {
      border-bottom: none;
      background-color: #9dabc0;
    }
    
    .header .navigation ul li ul li:last-child a {
      padding: 10px 24px;
    }
    
    .header .navigation ul li ul li a {
      font-size: 16px;
      font-family: "Open Sans";
      width: 100%;
      color: #fff;
      font-weight: bold;
      text-shadow: 0.5px 0.866px 0px rgba(1, 1, 1, 0.39);
      display: block;
      line-height: 18px;
      padding: 10px 24px;
    }
    
    .header .navigation ul li ul li:hover {
      background: #3a495e;
    }
    
    .header .navigation ul li {
      position: relative;
    }
    
    .header .navigation ul li:hover>ul {
      display: block;
    }
    
    .header .navigation ul li li.has-children>a:after {
      color: green;
      content: ' ►';
      font-size: 10px;
      float: right;
      padding-right: 10px;
      vertical-align: 1px;
    }
    
    .header .navigation ul .current-menu-item:after {
      color: green;
      content: '▲';
      position: absolute;
      top: 100%;
      left: 30%;
    }
    
    .header .navigation ul .sub-menu .current-menu-item:after {
      content: none;
    }
    
    .header .navigation ul li.has-children>a:after {
      color: green;
      content: '▼';
      font-size: 10px;
      padding-left: 5px;
      vertical-align: 1px;
    }
    
    
    }
    <div class="header">
      <div class="navigation">
        <ul>
          <li><a href="">Home</a></li>
          <li><a href="">Engineering Castings</a>
            <ul>
              <li><a href="">Example</a></li>
              <li><a href="">Example</a></li>
              <li><a href="">Example</a></li>
            </ul>
          </li>
          <li><a href="">Architectural Castings</a>
            <ul>
              <li><a href="">Example</a></li>
              <li><a href="">Example</a></li>
            </ul>
          </li>
          <li><a href="">Decorative Castings</a>
            <ul>
              <li><a href="">Example</a></li>
              <li><a href="">Example</a></li>
              <li><a href="">Example</a></li>
              <li><a href="">Example</a></li>
            </ul>
          </li>
          <li>
            <a href="">Downloads</a></li>
          <li>
            <a href="">Gallery</a></li>
          <li>
            <a href="">Contact</a></li>
          <li>
            <a href="">Blog</a></li>
          <li>
            <a href="">FREE Castings Guide</a></li>
        </ul>
      </div>
    </div>

    【讨论】:

    • @DanielVickers 我在写答案之前就看到了。这是另一种方式。
    猜你喜欢
    • 2022-07-05
    • 2018-09-24
    • 2014-02-19
    • 1970-01-01
    • 2015-05-21
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多