How to apply CSS style only if the last child is visible - html

I have this issue where I need to apply some style to a div only if it's next to a visible item (even if there's non visible items in between, but not if all the items following the current one are all invisible), as shown here:
https://stackblitz.com/edit/js-3nulnw?file=style.css
.container {
display: flex;
flex-grow: 1;
padding: 5px;
}
.content {
border: 1px solid #000000;
border-radius: 4px;
width: 50%;
padding: 10px;
}
.addon {
width: 10%;
background-color: #ff0000;
border: 1px solid #000000;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
border-left: none;
}
.addon.hidden {
display: none;
}
.content:not(:last-child),
.addon:not(:last-child) {
border-right: none;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
<div class="container">
<div class="content">This is shown correctly (no addons)</div>
</div>
<div class="container">
<div class="content">This is shown correctly (one addon)</div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">
This is shown correctly (multiple addons, one is hidden)
</div>
<div class="addon hidden"></div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">This is shown correctly (multiple addons)</div>
<div class="addon"></div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">This should have all rounded borders</div>
<div class="addon hidden"></div>
</div>
<div class="container">
<div class="content">This should have all rounded borders</div>
<div class="addon"></div>
<div class="addon hidden"></div>
</div>
I have tried looking on the internet but to no avail, but I'm probably looking for the wrong answer (most of what I found were other SO questions saying there's no CSS selector for non visible item), so alternative ways to do this are welcome (possibly without JS)

Yes it is possible. I try to understand your question and get this result. Please expalin more your question
https://developer.mozilla.org/en-US/docs/Web/CSS/:is
We can get it through (:is) pseudo-class function. we can check if div has class like visible in below example case then we can apply style to its next div throught (+) adjacent sibling selector or (~) general sibling selector. I hope example will clear what I want to say
<div class="container">
<div class="content">This is shown correctly (no addons)</div>
</div>
<div class="container">
<div class="content">This is shown correctly (one addon)</div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">
This is shown correctly (multiple addons, one is hidden)
</div>
<div class="addon hidden"></div>
<div class="addon"></div>
</div>
<!-- [ADD 'visible' CLASS BEFORE OF TARGET CONTAINER] -->
<div class="container visible">
<div class="content">This is shown correctly (multiple addons)</div>
<div class="addon"></div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">This should have all rounded borders</div>
<div class="addon hidden"></div>
</div>
<div class="container">
<div class="content">This should have all rounded borders</div>
<div class="addon"></div>
<div class="addon hidden"></div>
</div>
and its css will be
.container {
display: flex;
flex-grow: 1;
padding: 5px;
}
.content {
border: 1px solid #000000;
border-radius: 4px;
width: 50%;
padding: 10px;
}
.addon {
width: 10%;
background-color: #ff0000;
border: 1px solid #000000;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
border-left: none;
}
.addon.hidden {
display: none;
}
.content:not(:last-child),
.addon:not(:last-child) {
border-right: none;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.container:is(.visible) + .container{
background-color: rebeccapurple;
color: white;
/* Your style here */
}
.container:is(.visible) + .container .addon{
background-color: brown;
color: white;
/* Your style here */
}

I found a horrible but apparently effective way of achieving what I wanted
https://stackblitz.com/edit/js-4hkorq?file=style.css
.container {
display: flex;
flex-grow: 1;
padding: 5px;
}
.content {
border: 1px solid #000000;
border-radius: 4px;
width: 50%;
padding: 10px;
}
.addon {
width: 10%;
background-color: #ff0000;
border: 1px solid #000000;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
border-left: none;
}
.addon.hidden {
/* display: none; */
visibility: hidden;
width: 0px;
}
.content:not(:last-child),
.addon:not(:last-child) {
border-right: none;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.container > div:last-of-type.hidden::before {
content: '';
border: 1px solid #000000;
border-radius: 4px;
border-left: none;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
height: 100%;
display: flex;
width: 4px;
visibility: visible;
position: relative;
top: -1px;
}
.container > .addon + div:last-of-type.hidden::before {
background-color: #ff0000;
}
<div class="container">
<div class="content">This is shown correctly (no addons)</div>
</div>
<div class="container">
<div class="content">This is shown correctly (one addon)</div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">
This is shown correctly (multiple addons, one is hidden)
</div>
<div class="addon hidden"></div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">This is shown correctly (multiple addons)</div>
<div class="addon"></div>
<div class="addon"></div>
</div>
<div class="container">
<div class="content">This should have all rounded borders</div>
<div class="addon hidden"></div>
</div>
<div class="container">
<div class="content">This should have all rounded borders</div>
<div class="addon"></div>
<div class="addon hidden"></div>
</div>
The trick is to use visibility: hidden and width: 0 instead of display: none, then add a ::before pseudo element that adds the border. It has the drawback of adding 4 pixels to the element (in order to have a 4 pixels border radius it needs to be at least 4 pixels wide) but it is acceptable in my case and (albeit with some tweaks) it also worked in my application.
For some reason I had to add top: -1px because it wouldn't align otherwise, but in my application I didn't need to do

These are the major changes to CSS
.container {display: table; table-layout: fixed; width: 100%...}
.addon {display: table-cell; position: relative; right: 5px;
z-index: 1; padding-right: 5px;...}
.addon+.addon {right: 10px; padding-right: 10px;...}
.content {display: table-cell;}
Since this is a pure CSS solution, the tags were changed so that :*-type-of works effectively. If you want a uniform height, add a div inside .content and give it an absolute height (ie px)
section:first-of-type > div {
border-top: 1px solid #000000;
}
section:last-of-type > div {
border-bottom: 1px solid #000000;
}
.container {
display: table;
table-layout: fixed;
width: 100%;
}
.content {
display: table-cell;
padding: 15px;
width: 50%;
border-left: 1px solid #000000;
border-right: 1px solid #000000;
border-top: 0.5px solid #000000;
border-bottom: 0.5px solid #000000;
border-radius: 4px;
}
.addon {
display: table-cell;
width: 10%;
position: relative;
right: 5px;
padding-right: 5px;
z-index: 1;
background-color: #ff0000;
border-left: 0;
border-right: 1px solid #000000;
border-top: 0.5px solid #000000;
border-bottom: 0.5px solid #000000;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}
.addon.hidden {
display: none;
}
.addon+.addon {
right: 10px;
padding-right: 10px;
}
<section class="container">
<div class="content">This is shown correctly (no addons)</div>
</section>
<section class="container">
<div class="content">This is shown correctly (one addon)</div>
<aside class="addon visible"></aside>
</section>
<section class="container">
<div class="content">
This is shown correctly (multiple addons, one is hidden)
</div>
<aside class="addon hidden"></aside>
<aside class="addon visible"></aside>
</section>
<section class="container">
<div class="content">This is shown correctly (multiple addons)</div>
<aside class="addon visible"></aside>
<aside class="addon visible"></aside>
</section>
<section class="container">
<div class="content">This should have all rounded borders</div>
<aside class="addon hidden"></aside>
</section>
<section class="container">
<div class="content">This should have all rounded borders</div>
<aside class="addon visible"></aside>
<aside class="addon hidden"></aside>
</section>

Related

CSS n'th class in another class

I just started to FE after a couple of years in BE.
Developing an analytics card with using Angular on FE and .net Core on BE.
Stucked on a point to find out the best practice about to change background-color: property of a class beneath of main class.
<div class="row">
<div class="journey-card-bottom">
<div *ngFor="let bottom of top.breakDown | keyvalue: originalOrder">
<div>
<span>{{bottom.key}}</span>
</div>
<div class="row">
<div class="col-lg-10">
<div class="progress">
<div class="progress-bar" role="progressbar"
[ngStyle]="{'width': bottom.value > 0 ? bottom.value + '%' : '8px'}"></div>
<span class="percentage-value">{{bottom.value}}</span>
</div>
</div>
</div>
</div>
</div>
</div>
Screenshot to the desired outcome
In my CSS;
.journey-card-bottom {
border: 1px solid #DADCDD;
box-sizing: border-box;
padding: 10px;
line-height: 3;
width: 100%;
height: 435px;
}
.progress {
background-color: white !important;
margin-top: 5px;
}
.progress-bar {
border-radius: 0.25rem;
height: 8px;
align-self: center;
background-color: #0B4886;
}
.journey-card-bottom .progress-bar:nth-of-type(1) {
background-color: #68D391;
}
.journey-card-bottom .progress-bar:nth-of-type(2) {
background-color: #FCAF65;
}
It seems :nth-of-type(1) changes the background color but not only for 1st bar, for all of them.
:nth-of-type(2)does not effect at all i guess because there is no direct parent-child relation between each other.
On the other hand i know i can achieve with using [ngStyle] based on the item index in [ngFor] however i'm not sure if it's the best practice or not.
:nth-of-type() applies to sibling elements. Since .progress-bar is the first sibling within .progress and has no other siblings with the class .progress-bar, only the styles declared in .journey-card-bottom .progress-bar:nth-of-type(1) will be applied. Here is a code snippet showing your CSS working by changing the HTML structure:
.journey-card-bottom {
border: 1px solid #DADCDD;
box-sizing: border-box;
padding: 10px;
line-height: 3;
width: 100%;
height: 435px;
}
.progress {
background-color: white !important;
margin-top: 5px;
}
.progress-bar {
border-radius: 0.25rem;
height: 8px;
align-self: center;
background-color: #0B4886;
}
.journey-card-bottom .progress-bar:nth-of-type(1) {
background-color: #68D391;
}
.journey-card-bottom .progress-bar:nth-of-type(2) {
background-color: #FCAF65;
}
<div class="journey-card-bottom">
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
<span class="percentage-value">90</span>
<div class="progress-bar" role="progressbar"></div>
<span class="percentage-value">60</span>
</div>
</div>
To solve your issue, I would suggest using :nth-of-type() on the top-level element of the for loop. In the below example, the top-level element has the class .target-class:
.journey-card-bottom {
border: 1px solid #DADCDD;
box-sizing: border-box;
padding: 10px;
line-height: 3;
width: 100%;
height: 435px;
}
.progress {
background-color: white !important;
margin-top: 5px;
}
.progress-bar {
border-radius: 0.25rem;
height: 8px;
align-self: center;
background-color: #0B4886;
}
.target-class:nth-of-type(1) .progress-bar {
background-color: #68D391;
}
.target-class:nth-of-type(2) .progress-bar {
background-color: #FCAF65;
}
<!-- Rendered HTML from the component -->
<div class="row">
<div class="journey-card-bottom">
<div class="target-class">
<div>
<span>Passed</span>
</div>
<div class="row">
<div class="col-lg-10">
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
<span class="percentage-value">90</span>
</div>
</div>
</div>
</div>
<div class="target-class">
<div>
<span>Referred</span>
</div>
<div class="row">
<div class="col-lg-10">
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
<span class="percentage-value">60</span>
</div>
</div>
</div>
</div>
</div>
</div>

How can I align these html elements in a row?

I'm creating a input to contain hours and minutes.I use this html elements:
Two pairs of up and down Buttons;
Input text.
I'm having trouble aligning the html elements.
This is the image with the best alignment I could apply:
This is my code:
.col-centered{
float: none;
margin: 0 auto;
}
.row-1{
padding-top: 3px;
padding-right: 0px;
padding-bottom: 1px;
padding-left: 0px;
}
.triangle-up {
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-bottom: 8px solid #555;
}
.triangle-down {
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 8px solid #555;
}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-xs-1">
<div class="row-1">
<div class = "triangle-up" id ="up1"></div>
</div>
<div class="row-1">
<div class = "triangle-down" id ="down1"></div>
</div>
</div>
<div class="col-xs-2">
<div class = "col-centered">
<input type="text" id="hora" size="5" maxlength="5">
</div>
</div>
<div class="col-xs-1">
<div class="row-1">
<div class = "triangle-up" id ="up"></div>
</div>
<div class="row-1">
<div class = "triangle-down" id ="down"></div>
</div>
</div>
</div>
</div>
I tried, but without success, lining up the elements with pulling to the right. Forcing the left pair of Buttons, to be near the html text entry.
I've changed this piece of code:
<div class="pull-right">
<div class="row-1">
<div class = "triangle-up" id ="up1"></div>
</div>
<div class="row-1">
<div class = "triangle-down" id ="down1"></div>
</div>
</div>
How can I align the html elements so that the two pairs of buttons are close to the input element of text?
This would be a great use case for flexbox! Before, you had things inside the bootstrap grid, which was probably adding the extra spacing you didn't want. For a custom component look, you'd need to just space things out the way you want them to look. Now the triangles are spaced out evenly vertically - even if the height of the <input> is increased!
.spinner-input-wrapper {
display: flex;
}
.spinner {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.spinner-input {
margin: 0 3px;
}
.triangle-up,
.triangle-down {
width: 0;
height: 0;
border: 4px solid transparent;
}
.triangle-up {
border-bottom-width: 8px;
border-bottom-color: #555;
}
.triangle-down {
border-top-width: 8px;
border-top-color: #555;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<div class="spinner-input-wrapper">
<div class="spinner">
<div class="triangle-up" id="up1"></div>
<div class="triangle-down" id="down1"></div>
</div>
<input type="text" class="spinner-input" size="5" maxlength="5">
<div class="spinner">
<div class="triangle-up" id="up2"></div>
<div class="triangle-down" id="down2"></div>
</div>
</div>
If you don't want to learn something else like flexbox all you really need is to clean up your code a bit. In this case the bootstrap layout is making it more difficult. Try simplifying to something like this. Here's my codepen sample
HTML
<div class="container">
<div class="triangles">
<div class="triangle-up" id="up1"></div>
<div class="triangle-down" id="down1"></div>
</div>
<div class="input-text">
<input type="text" id="hora" size="5" maxlength="5">
</div>
<div class="triangles">
<div class="triangle-up" id="up1"></div>
<div class="triangle-down" id="down1"></div>
</div>
</div>
CSS
.triangle-up {
margin-bottom: 6px;
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-bottom: 8px solid #555;
}
.triangle-down {
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 8px solid #555;
}
.triangles {
float: left;
display: inline-block;
padding: 2px 5px;
}
.input-text {
float: left;
display: inline-block;
}

CSS place N divs inline-block column by column (vertically)

The code below shows what I have but it is not user friendly way to show data(time). "display:inline-block" puts elements in a row and after it reaches parent div's width it puts his next child in the next row.
.rateDates {
display: inline-block;
margin: 10px;
font-family: consolas;
border: 1px solid #73d5e6;
border-radius: 15px;
padding: 4px 8px 4px 8px;
}
#ratesContainer {
border: 1px solid #73d5e6;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
margin-bottom: 20px;
height:200px;
}
<div id="ratesContainer">
<div class="rateDates">
<span>00:00:00</span>
</div>
<div class="rateDates">
<span>11:11:11</span>
</div>
<div class="rateDates">
<span>22:22:22</span>
</div>
<div class="rateDates">
<span>33:33:33</span>
</div>
<div class="rateDates">
<span>44:44:44</span>
</div>
<div class="rateDates">
<span>55:55:55</span>
</div>
</div>
The problem is I can't make it place child div's on top of each other and after the parent div's height is not enaugh start from top again. In other words start a new column.
I want to place rateDates class divs in column way. So they first fill up not the row but the column.
Page renders automatycally. I need a CSS solution. If it is posible.
Thank you.
use a flex property.
Here is updated code.
CSS
.rateDates {
margin: 10px;
font-family: consolas;
border: 1px solid #73d5e6;
border-radius: 15px;
padding: 4px 8px 4px 8px;
}
#ratesContainer {
border: 1px solid #73d5e6;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
margin-bottom: 20px;
height:200px;
display:flex;
flex-direction:column;
flex-wrap:wrap
}
HTML
<div id="ratesContainer">
<div class="rateDates">
<span>00:00:00</span>
</div>
<div class="rateDates">
<span>11:11:11</span>
</div>
<div class="rateDates">
<span>22:22:22</span>
</div>
<div class="rateDates">
<span>33:33:33</span>
</div>
<div class="rateDates">
<span>44:44:44</span>
</div>
<div class="rateDates">
<span>55:55:55</span>
</div>
</div>
You can do it the Flexbox:
#ratesContainer {
display: flex; /* displays flex-items (children) inline */
border: 1px solid #73d5e6;
padding: 15px;
border-radius: 10px;
margin: 20px 0;
}
#ratesContainer > .rateDates {
flex: 1;
text-align: center;
margin: 10px;
font-family: consolas;
border: 1px solid #73d5e6;
border-radius: 15px;
padding: 4px 8px;
}
#media (max-width: 768px) { /* adjust */
#ratesContainer {
flex-direction: column; /* stacks children vertically */
align-items: center; /* because of the changed direction this is now horizontal centering, otherwise it's vertical by default */
}
}
<div id="ratesContainer">
<div class="rateDates">
<span>00:00:00</span>
</div>
<div class="rateDates">
<span>11:11:11</span>
</div>
<div class="rateDates">
<span>22:22:22</span>
</div>
<div class="rateDates">
<span>33:33:33</span>
</div>
<div class="rateDates">
<span>44:44:44</span>
</div>
<div class="rateDates">
<span>55:55:55</span>
</div>
</div>
It's a bit different approach but the end result is what you want, i.e. display them in one column vertically.
Just to mention an alternative approach, this can be also done with multi-column layout:
.rateDates {
display: inline-block;
margin: 10px;
font-family: consolas;
border: 1px solid #73d5e6;
border-radius: 15px;
padding: 4px 8px 4px 8px;
}
#ratesContainer {
border: 1px solid #73d5e6;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
margin-bottom: 20px;
height:200px;
column-width: 110px;
column-fill: auto;
}
<div id="ratesContainer">
<div class="rateDates">
<span>00:00:00</span>
</div>
<div class="rateDates">
<span>11:11:11</span>
</div>
<div class="rateDates">
<span>22:22:22</span>
</div>
<div class="rateDates">
<span>33:33:33</span>
</div>
<div class="rateDates">
<span>44:44:44</span>
</div>
<div class="rateDates">
<span>55:55:55</span>
</div>
</div>
But I prefer the Flexbox solution. These items don't look like parts of the text flow, that inline-block was designed for. And with Flexbox you will not need to hardcode the width of the column.

Positioning elements with CSS

I know it is dumb question, but i am struggling with following problem
It is one of my menu buttons. I want div the represents icon (marked red with circle) be on the left side of the button and the text on the right (name (blue) above description (purple)). By the way, i am going to have plenty of those buttons and i want them appear in column (block).
My current problem is that icon div (red) and text div (green dashed) wont place inline.
My HTML is:
<style>
.HomeMenuNavbarButton {
text-decoration: none;
color : black;
border-style:outset;
border-width:4px;
border-radius:15px;
display:block;
padding:4px;
}
.circle {
width: 50px;
height: 50px;
-webkit-border-radius: 25px;
-moz-border-radius: 25px;
border-radius: 25px;
border-color: black;
border-style: dashed;
display: inline-block;
vertical-align: top;
}
</style>
<div class="container">
<div class="row">
#foreach (var node in Model.Nodes)
{
if (!(node.IsRootNode) && node.Children.Any())
{
<div class="col-md-4">
<button type="button" style="display:inline;" class="btn btn-info" data-toggle="collapse" data-target="#("#relatedNavList" + node.Title) ">#node.Title</button>
<div id="#("relatedNavList" + node.Title)" class="collapse">
#foreach (var child in node.Children)
{
<div class="HomeMenuNavbarButton" style="display:block;">
<div style="background-color:red;display:inline">
<div class="circle">
</div>
</div>
<div style="border-color: green; border-style: dashed; display:inline-block">
<div style="background-color:blue">#(child.Title)</div>
<div style="background-color:purple">#(child.Description)</div>
</div>
</div>
}
</div>
</div>
}
}
</div>
</div>
When you want to display stuff side by side in CSS, the easiest way is to use flexbox. Try to add display : flex; to your HomeMenuNavBar class.
Here is a complete document about flexbox from the Mozilla team.
Using a wrapper defined as a flexbox
.wrapper {
display: flex;
align-items: center;
justify-content: space-between;
width: 150px;
}
.circle {
height: 25px;
width: 25px;
border: medium dashed darkgray;
border-radius: 50%;
}
button {
background: dodgerblue;
color: white;
border-radius: 3px;
border: thin solid lightblue;
padding: 0.5em;
}
<div class="wrapper">
<button>Button</button>
<div class="circle"></div>
<span>Text</span>
</div>
You can do that just with display:inline-block on your button's elements
.elementButton
{
display:inline-block;
}
.circle {
width: 50px;
height: 50px;
-webkit-border-radius: 25px;
-moz-border-radius: 25px;
border-radius: 25px;
border-color: black;
border-style: dashed;
display: inline-block;
vertical-align: top;
}
.HomeMenuNavbarButton
{
display:block;
margin:5px;
}
<div class="HomeMenuNavbarButton">
<div class="circle elementButton">
</div>
<div class="elementButton">
<div>Title one</div>
<div>Content</div>
</div>
</div>
<div class="HomeMenuNavbarButton">
<div class="circle elementButton">
</div>
<div class="elementButton">
<div >Title two</div>
<div>Content</div>
</div>
</div>

Setting the variable percentage width of HTML elements next to other variable-width elements

I have a HTML structure with given CSS.
Both caption and progress elements should be rendered in same line. caption elements should not have fixed width and progress elements should fill up the rest of the space next to caption based on their inline-set width, which means that every progress element will have a different total pixel-width but should fill up only the given percentage of available space.
HTML structure and CSS rules can be changed in any way.
Is it possible to solve this problem with CSS only?
.table {
padding: 15px;
width: 280px;
border: 1px solid black;
font-family: sans-serif;
font-size: 12px;
}
.caption {
float: left;
}
.progress {
height: 14px;
border: 1px solid white;
border-radius: 4px;
background-color: green;
overflow: hidden;
}
.value {
margin-left: 5px;
}
<div class="table">
<div class="row">
<div class="caption">Short text: </div>
<div class="progress" style="width:11.65%">
<span class="value">11.65</span>
</div>
</div>
<div class="row">
<div class="caption">A bit longer text: </div>
<div class="progress" style="width:100%">
<span class="value">100.00</span>
</div>
</div>
<div class="row">
<div class="caption">X: </div>
<div class="progress" style="width:45.50%">
<span class="value">45.50</span>
</div>
</div>
</div>
Have you considered using Flexbox?
Just add this rule:
.row {
display: flex;
}
If your are concerned about browser support, an alternative would be using display:table. You should change your markup and CSS, like this:
.table {
border: 1px solid black;
font-family: sans-serif;
font-size: 12px;
padding: 15px;
width: 280px;
}
.inner-table {
display: table;
}
.row {
display: table-row;
}
.caption {
display: table-cell;
white-space: nowrap;
width: 1%;
}
.progress {
background-color: green;
border: 1px solid white;
border-radius: 4px;
display: table-cell;
height: 14px;
}
.value {
margin-left: 5px;
display:block;
width:0;
overflow: visible;
}
<div class="table">
<div class="inner-table">
<div class="row">
<div class="caption">Short text: </div>
<div style="width:1.65%" class="progress">
<span class="value">1.65</span>
</div>
<div class="remainder"></div>
</div>
</div>
<div class="inner-table">
<div class="row">
<div class="caption">A bit longer text: </div>
<div style="width:100%" class="progress">
<span class="value">100.00</span>
</div>
<div class="remainder"></div>
</div>
</div>
<div class="inner-table">
<div class="row">
<div class="caption">X: </div>
<div class="progress" style="width:45.50%">
<span class="value">45.50</span>
</div>
<div class="remainder"></div>
</div>
</div>
</div>
Please try this - padding-right: 5px; display:inline; add these properties in progress class and also remove width in progress.
Well, just for the future reference, I was playing a bit with the flexbox thingie and came up with this:
.table {
padding: 15px;
width: 280px;
border: 1px solid black;
font-family: sans-serif;
font-size: 12px;
}
.row {
display: flex;
}
.caption {
margin: 1px 5px 1px 0;
}
.progress {
flex-grow: 1;
margin: auto;
}
.progress-content {
height: 14px;
border-radius: 4px;
background-color: green;
}
.value {
margin-left: 5px;
}
<div class="table">
<div class="row">
<div class="caption">Short text:</div>
<div class="progress">
<div class="progress-content" style="width:11.65%">
<span class="value">11.65</span>
</div>
</div>
</div>
<div class="row">
<div class="caption">A bit longer text:</div>
<div class="progress">
<div class="progress-content" style="width:100%">
<span class="value">100.00</span>
</div>
</div>
</div>
<div class="row">
<div class="caption">X:</div>
<div class="progress">
<div class="progress-content" style="width:45.50%">
<span class="value">45.50</span>
</div>
</div>
</div>
</div>
If I get a solution without flexbox, will accept it as an answer :)