Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make last element in a series of wrapped inline-block elements fill the available horizontal space?

Tags:

css

I have an element which will contain an unspecified number of inline-block elements which may wrap if there are enough elements.

I want the last element to fill the remaining space on the line. How can this be accomplished?

enter image description here

enter image description here

enter image description here

Example HTML

<div class="tags">
  <span class="tags__item">First Tag</span>
  <span class="tags__item">Another One</span>
  <span class="tags__item">Long Tag Name Here</span>
  <span class="tags__item">Last tag should fill</span>
</div>

Example CSS

.tags { border: solid 1px #000; padding: 0; }
.tags__item {
  display: inline-block;
  margin: 2px;
  padding: 1px 5px;
  background: #eee;
  border: solid 1px #eee;
}
.tags__item:last-child {
  background: #fff;
  border: dashed 1px #eee;
}

Attempt #1 (doesn't work)

One answer (which was deleted) mentioned trying table-cell layout like this..

.tags {
  border: solid 1px #000; 
  display: table-row;
  white-space: nowrap;
}
.tags__item {
  display:table-cell;
  width:auto;
  margin: 2px;
  padding: 1px 5px;
  background: #eee;
}
.tags__item:last-child {
  background: #fff;
  border: dashed 1px #ccc;
  width:99%
}

This solution works reasonably well for a single line. However, it doesn't allow wrapping. http://cdpn.io/omFuy

Attempt #2 (doesn't work)

Someone else linked to another SO answer as a possible solution.

.tags {
  border: solid 1px #000; 
}
.tags__item {
  display: block;
  float: left;
  margin: 2px;
  padding: 1px 5px;
  background: #eee;
}
.tags__item:last-child {
  float: none;
  display: block;
  border: dashed 1px #ccc;
  background: #fff;
}
.tags__item:last-child::after {
  clear:both;
}

But it doesn't work. See my implementation.

like image 210
jessegavin Avatar asked Nov 21 '13 16:11

jessegavin


People also ask

How do you make a div fill a remaining horizontal space?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

Do block elements fill the width of the entire window stacking on top of each other?

You can visualize them as a stack of boxes. Note: A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can).

How do you put a space between inline elements?

You can add space to the left and right on an inline element, but you cannot add height to the top or bottom padding or margin of an inline element. Inline elements can actually appear within block elements, as shown below. I've added white padding on the left and right side of each inline element.

Does inline element take up the available width?

Note: An inline element does not start on a new line and only takes up as much width as necessary.


1 Answers

For browsers that support it, the natural solution is to use the flexible layout module, aka flexbox—this is exactly the sort of scenario it is intended for. Here are the bare essentials:

Demo on Dabblet

.tags {
    display: flex;
    flex-wrap: wrap;
}

.tags__item:last-child {
    flex: auto;
}

The (not insignificant) downside to this approach is the lack of browser support and the attendant mess of prefixes and fallbacks if you need to support older browsers. As suggested by Rafael in the comments, this CSS Tricks article outlines the required prefixes and the legacy syntax.

like image 158
Jordan Gray Avatar answered Dec 01 '22 22:12

Jordan Gray