CSS - Make Span Extend to End of Its Container/Fill Empty Space

CSS - Make SPAN extend to end of its container / fill empty space?

I think I found a pure CSS solution. You only missed two things:

  • You have to use only display: inline-block in the <span> tags without float: left, because floating is actually contradictory with inline-block elements.
  • You have to use white-space: nowrap in the parent <div>.

This way you don't need to specify a width for anything. :)

JSFiddle demo

http://jsfiddle.net/yz9TK/

CSS

(I cleaned it up a little bit)

@import url(https://fonts.googleapis.com/css?family=Open+Sans);

body {
background: #212121;
color: #FFF;
}

#ctl00_breadcrumb {
height: 45px;
width: 960px;
background-color: #707070;
line-height: 45px;
font-size: 16px;
font-family: Helvetica, sans-serif;
border-radius: 10px;
border: 1px solid #585858;
text-shadow: 0px -1px 0px rgba(0,0,0,0.5);
-webkit-box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, .75);
box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, .75);
white-space: nowrap;
overflow: hidden;
}

#ctl00_breadcrumbContent span {
display: inline-block;
padding: 0px 10px;
line-height: 45px;
font-size: 18px;
font-family: Helvetica, sans-serif;
}

#ctl00_breadcrumbContent span a {
padding: 0;
margin: 0;
color: #FFF;
text-decoration: none;
line-height: 45px;
font-size: 18px;
font-family: Helvetica, sans-serif;
}

#ctl00_breadcrumbContent span:nth-child(even) {
width: 0;
height: 0;
padding: 0;
margin: -22px -4px -16px -4px;
overflow: hidden;
}

#ctl00_breadcrumbContent span:nth-child(1) {
border-radius: 10px 0px 0px 10px;
background-color: #404040;
}

#ctl00_breadcrumbContent span:nth-child(2) {
border-top: 22px solid #505050;
border-bottom: 23px solid #505050;
border-left: 15px solid #404040;
}

#ctl00_breadcrumbContent span:nth-child(3) {
background-color: #505050;
}

#ctl00_breadcrumbContent span:nth-child(4) {
border-top: 22px solid #606060;
border-bottom: 23px solid #606060;
border-left: 15px solid #505050;
}

#ctl00_breadcrumbContent span:nth-child(5) {
background-color: #606060;
}

#ctl00_breadcrumbContent span:nth-child(6) {
border-top: 22px solid #707070;
border-bottom: 23px solid #707070;
border-left: 15px solid #606060;
}

#ctl00_breadcrumbContent span:nth-child(7) {
background-color: #707070;
}

#ctl00_breadcrumbContent span:nth-last-child(1) {
background-color: #707070;
}

#ctl00_breadcrumbContent span:nth-last-child(2) {
border-top: 22px solid #707070;
border-bottom: 23px solid #707070;
}

Getting a span element fill the space in a div

If you reorder your HTML, you can get a simple solution:

<div class="container">
<span class="left">Title</span>
<span class="right">value</span>
<span class="center"> </span>
</div>

Place the two floated elements ahead of the .center element. The .center element will be in regular content flow and wrap around the left and right content.

The CSS:

.center {
display: block;
border-bottom: 1px dotted blue;
overflow: auto;
position: relative;
top: -4px;
}

.right {
float: right;
margin-left: 10px;
}

.left {
float: left;
margin-right: 10px;
}

.container {
width: 200px;
border: 1px dotted red;
padding: 5px;
}

When you float an element, the display type computes to block, so no need to declare it.

Also, for .center, if you add overflow: auto, you constrain the block so it does not extend beyond the edges of the floated elements. As a result, your bottom border does not underline the title and value text.

Finally, you can add position: relative and move the .center up a few pixels to align the border closer to the baseline of the text.

Fiddle: http://jsfiddle.net/audetwebdesign/DPFYD/

Fill the remaining height or width in a flex container

Use the flex-grow property to make a flex item consume free space on the main axis.

This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.

A common example is flex-grow: 1 or, using the shorthand property, flex: 1.

Hence, instead of width: 96% on your div, use flex: 1.


You wrote:

So at the moment, it's set to 96% which looks OK until you really squash the screen - then the right hand div gets a bit starved of the space it needs.

The squashing of the fixed-width div is related to another flex property: flex-shrink

By default, flex items are set to flex-shrink: 1 which enables them to shrink in order to prevent overflow of the container.

To disable this feature use flex-shrink: 0.

For more details see The flex-shrink factor section in the answer here:

  • What are the differences between flex-basis and width?

Learn more about flex alignment along the main axis here:

  • In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

Learn more about flex alignment along the cross axis here:

  • How does flex-wrap work with align-self, align-items and align-content?

How to make a div fill a remaining horizontal space?

This seems to accomplish what you're going for.

#left {  float:left;  width:180px;  background-color:#ff0000;}#right {  width: 100%;  background-color:#00FF00;}
<div>  <div id="left">    left  </div>  <div id="right">    right  </div></div>

Expand a div to fill the remaining width

The solution to this is actually very easy, but not at all obvious. You have to trigger something called a "block formatting context" (BFC), which interacts with floats in a specific way.

Just take that second div, remove the float, and give it overflow:hidden instead. Any overflow value other than visible makes the block it's set on become a BFC. BFCs don't allow descendant floats to escape them, nor do they allow sibling/ancestor floats to intrude into them. The net effect here is that the floated div will do its thing, then the second div will be an ordinary block, taking up all available width except that occupied by the float.

This should work across all current browsers, though you may have to trigger hasLayout in IE6 and 7. I can't recall.

Demos:

  • Fixed Left: http://jsfiddle.net/A8zLY/5/
  • Fixed Right: http://jsfiddle.net/A8zLY/2/

div {
float: left;
}

.second {
background: #ccc;
float: none;
overflow: hidden;
}
<div>Tree</div>
<div class="second">View</div>

Make a child HTML element fill it's parent container, both vertically and horizontally - (CSS & Flexbox)

If you just want to fill the parent just add the style for #child3

width: 100%;
height: 100%;

But if you want to fill the whole space of the parent, you should remove first the padding of the last child of the ul, add something here:

#container nav ul li:last-child{
margin-left: auto;
padding 0;
}

Then add 100% height and width in the #child3. But this will change the width of the parent of #child3 so you add padding to the #child3

Your code will look like this

#container{
height: 50vh;
width: 50%;
min-width: 500px;
}

#container nav{
height: 5rem;
}

#container nav ul{
height: 100%;
padding-left: 1rem;
display: flex;
}

#container nav ul li{
display: flex;
align-items: center;
padding: 0rem 2rem;
height: 100%;

/* just to view the size of the element */
background: black;
opacity: 50%;
}

#container nav ul li:first-child{
margin-left: -1rem;
}

#container nav ul li:last-child{
margin-left: auto;
padding: 0;
}

#child3{
height: 100%;
width: 100%;
padding: 0rem 2rem;

/* just to view the size of the element */
background: red;
opacity: 50%;
}
<section id="container">
<nav>
<ul>
<li id="child1">Settings</li>
<li id="child2">Account</li>
<li><span id="child3" class="material-icons">close</span></li>
</ul>
</nav>
</section>

Expanding to fill remaining space in a CSS Grid layout

The 100% for the 2nd column in your grid-template-columns is based on the width of the container - rather than occupying the space outstanding within the container, it will push out to the right because the 2nd column is trying to match the width of the container.

Try changing this to auto and this should rectify the issue, as it will only take up the space up to the end of the container and no further.

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns

Make a div fill the height of the remaining screen space

2015 update: the flexbox approach

There are two other answers briefly mentioning flexbox; however, that was more than two years ago, and they don't provide any examples. The specification for flexbox has definitely settled now.

Note: Though CSS Flexible Boxes Layout specification is at the Candidate Recommendation stage, not all browsers have implemented it. WebKit implementation must be prefixed with -webkit-; Internet Explorer implements an old version of the spec, prefixed with -ms-; Opera 12.10 implements the latest version of the spec, unprefixed. See the compatibility table on each property for an up-to-date compatibility status.

(taken from https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes)

All major browsers and IE11+ support Flexbox. For IE 10 or older, you can use the FlexieJS shim.

To check current support you can also see here:
http://caniuse.com/#feat=flexbox

Working example

With flexbox you can easily switch between any of your rows or columns either having fixed dimensions, content-sized dimensions or remaining-space dimensions. In my example I have set the header to snap to its content (as per the OPs question), I've added a footer to show how to add a fixed-height region and then set the content area to fill up the remaining space.

html,body {  height: 100%;  margin: 0;}
.box { display: flex; flex-flow: column; height: 100%;}
.box .row { border: 1px dotted grey;}
.box .row.header { flex: 0 1 auto; /* The above is shorthand for: flex-grow: 0, flex-shrink: 1, flex-basis: auto */}
.box .row.content { flex: 1 1 auto;}
.box .row.footer { flex: 0 1 40px;}
<!-- Obviously, you could use HTML5 tags like `header`, `footer` and `section` -->
<div class="box"> <div class="row header"> <p><b>header</b> <br /> <br />(sized to content)</p> </div> <div class="row content"> <p> <b>content</b> (fills remaining space) </p> </div> <div class="row footer"> <p><b>footer</b> (fixed height)</p> </div></div>

Fill remaining vertical space - only CSS

You can do this with position:absolute; on the #second div like this :

FIDDLE

CSS :

#wrapper{
position:relative;
}

#second {
position:absolute;
top:200px;
bottom:0;
left:0;
width:300px;
background-color:#9ACD32;
}

EDIT : Alternative solution

Depending on your layout and the content you have in those divs, you could make it much more simple and with less markup like this :

FIDDLE

HTML :

<div id="wrapper">
<div id="first"></div>
</div>

CSS :

#wrapper {
height:100%;
width:300px;
background-color:#9ACD32;
}
#first {
background-color:#F5DEB3;
height: 200px;
}


Related Topics



Leave a reply



Submit