How can I extend the background to the right using flexbox and a container with limited width?
The spans are the tags that must contain a background-color, not the parent tags. (Because the color can be different for each span)
I know that display table and table-row fixes it, but it gives me another problems with paddings and borders, so I would like to use flexbox model.
There is another requeriment, the background should extend to the widest span, so all spans should have the same width and extend the background to the same position.
pre {
width: 300px;
}
code {
display: flex;
flex-direction: column;
overflow: auto;
}
span {
background-color: #ddd;
}
<pre>
<code>
<span>sadfasdsad asd adsads das ads dasdasdas dasd as das</span>
<span>sadfdas das adsdsad sa dasda s adsasdsad asd adsads das ads dasdas</span>
</code>
</pre>
If you rearrange your CSS rules/properties a little it will work
pre {
width: 300px;
display: flex;
overflow: auto;
}
code {
display: flex;
flex-direction: column;
}
span {
background-color: #ddd;
}
span ~ span {
background-color: #aaa;
}
<pre>
<code>
<span>sadfasdsad asd adsads das ads dasdasdas dasd as das</span>
<span>sadfdas das adsdsad sa dasda s adsasdsad asd adsads das ads dasdas</span>
</code>
</pre>
Updated
The unwanted extra spacing that occurs in IE11/Edge (and maybe a few more) can be fixed by removing the line breaks/space in the markup
<pre>
<code><span>sadfasdsad asd adsads das ads dasdasdas dasd as das</span><span>sadfdas das adsdsad sa dasda s adsasdsad asd adsads das ads dasdas</span></code>
</pre>
Another option is to change the pre/code to div's (tested with success on Chrome, Firefox, Edge, IE11)
div.pre {
width: 300px;
display: flex;
overflow: auto;
}
div.code {
display: flex;
flex-direction: column;
white-space: nowrap;
}
span {
background-color: #ddd;
}
span ~ span {
background-color: #aaa;
}
<div class="pre">
<div class="code">
<span>sadfasdsad asd adsads das ads dasdasdas dasd as das</span>
<span>sadfdas das adsdsad sa dasda s adsasdsad asd adsads das ads dasdas</span>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With