Search code examples
htmltwitter-bootstrapcssbreadcrumbs

Trouble increasing width of breadcrumb (ol list) to fill parent


I'm trying to build a simple breadcrumb system on my Bootstrap-powered website.

I have used the red-team-design CSS3 breadcrumbs as inspiration.

This what I have got so far:

screenshot

What I am trying to do is ensure that the width of the last, active breadcrumb will always fill the parent ol element. So the final product should look something like this:

screenshot of wanted result

I have tried through messing with the margin and padding and a couple of other things but nothing seems to work.

The 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>

(NOTE - I have decided to use a .current class instead of the Bootstrap .active class)

Here's the 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;
}

Solution

  • This can be solved using flex (comprehensive guide). I have

    1. set the ol to have display: flex; to enable flexbox layout;
    2. removed the float directives from the list items;
    3. removed padding-right from the .current item;
    4. made anchors inside items display: block; and fill width;
    5. made it possible for the .current item to grow with flex-grow: 1; max-width: 100%;;
    6. created a little demo, included below.

    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;
    }
    <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>