无法增加面包屑(ol列表)的宽度以填充父级

时间:2017-05-06 18:15:16

标签: html css twitter-bootstrap css3 breadcrumbs

我正在尝试在我的Bootstrap驱动的网站上构建一个简单的面包屑系统。

我使用红队设计的CSS3面包屑作为灵感。

这是我到目前为止所得到的:

screenshot

我要做的是确保最后一个活动面包屑的宽度始终填充父ol元素。所以最终的产品应该是这样的:

screenshot of wanted result

我试图弄清楚边距和填充以及其他一些事情,但似乎没有任何工作。

HTML:

<ol class="breadcrumb">
    <li><a href="home.html"><i class="fa fa-home"></i><span> </span></a></li>
    <li><a><span>Travel </span></a></li>
    <li class="current"><a><span>Destination </span></a></li>
</ol>

注意 - 我决定使用.current类而不是Bootstrap .active类)

这是CSS:

ul {
  margin:0;
  padding:0;
  list-style:none;
}

.breadcrumb {
  overflow:hidden;
  width:100%;
}

.breadcrumb > li + li:before {
  content:none;
}

.breadcrumb li {
  float:left;
  margin:0 .5em 0 1em;
}

.breadcrumb a {
  background:#ddd;
  padding:.7em 1em;
  float:left;
  text-decoration:none;
  color:#444;
  text-shadow:0 1px 0 rgba(255,255,255,.5);
  position:relative;
}

.breadcrumb a:hover {
  background:#efc9ab;
}

.breadcrumb a::before, .breadcrumb a::after {
  content:'';
  position:absolute;
  top:0;
  bottom:0;
  width:1em;
  background:#ddd;
  transform:skew(-10deg);
}

.breadcrumb a::before {
  left:-.5em;
  border-radius:5px 0 0 5px;
}

.breadcrumb a:hover::before {
  background:#efc9ab;
}

.breadcrumb a::after {
  right:-.5em;
  border-radius:0 5px 5px 0;
}

.breadcrumb a:hover::after {
  background:#efc9ab;
}

.breadcrumb .current {
  padding-right:500px;
}

.breadcrumb .current, .breadcrumb .current:hover {
  font-weight:bold;
}

.breadcrumb .current::after, .breadcrumb .current::before {
  content:normal;
}

1 个答案:

答案 0 :(得分:1)

这可以使用flex(综合guide)来解决。我有

  1. ol设置为display: flex;以启用Flexbox布局;
  2. 从列表项中删除了float指令;
  3. padding-right项目中删除了.current;
  4. 在项目display: block;内填充锚点并填充宽度;
  5. 使.current项目可以与flex-grow: 1; max-width: 100%;;
  6. 一起成长
  7. 创建了一个小型演示,包含在下面。
  8. &#13;
    &#13;
    ol {
      margin: 0;
      padding: 0;
      list-style: none;
    }
    
    .breadcrumb {
      display: flex;
    }
    
    .breadcrumb li {
      margin: 0 .5em 0 1em;
      max-width: 100%;
    }
    
    .breadcrumb a {
      position: relative;
      display: block;
      box-sizing: border-box;
      width: 100%;
      background: #ddd;
      padding: .7em 1em;
      float: left;
      text-decoration: none;
      color: #444;
      text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
    }
    
    .breadcrumb a:hover {
      background: #efc9ab;
    }
    
    .breadcrumb a::before,
    .breadcrumb a::after {
      content: '';
      position: absolute;
      top: 0;
      bottom: 0;
      width: 1em;
      background: #ddd;
      transform: skew(-10deg);
    }
    
    .breadcrumb a::before {
      left: -.5em;
      border-radius: 5px 0 0 5px;
    }
    
    .breadcrumb a:hover::before {
      background: #efc9ab;
    }
    
    .breadcrumb a::after {
      right: -.5em;
      border-radius: 0 5px 5px 0;
    }
    
    .breadcrumb a:hover::after {
      background: #efc9ab;
    }
    
    .breadcrumb .current,
    .breadcrumb .current:hover {
      flex-grow: 1;
      font-weight: bold;
    }
    &#13;
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-home"></i></a></li>
      <li><a href="#">Travel</a></li>
      <li class="current"><a href="#">Destination</a></li>
    </ol>
    &#13;
    &#13;
    &#13;