Flexbox忽略了填充和放大链接上的宽度

时间:2017-07-06 14:58:06

标签: html css css3 sass flexbox

基本上,我有一个使用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>

以下是codepen:https://codepen.io/danielvickerssa/pen/ZyjqRr

2 个答案:

答案 0 :(得分:3)

如果您将display: flex添加到列表项中,它们会强制子元素(在这种情况下为<a>)来拉伸容器的整个高度。

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

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

li {
    display: flex;
}

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

答案 1 :(得分:0)

你可以建立链接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>