简体   繁体   English

显示具有百分比宽度和不同图案的浮动div

[英]Show floated div with percentage width and different pattern

I am not very good with CSS, so I am posting my problem here hoping for a solution: 我对CSS不太满意,所以我在这里发布问题,希望找到解决方案:

What I need is following: I need an HTML row/div/line that shows a date and after the date it shows a bar which will be a percentage of the remaining screen width. 我需要的是:我需要一个显示日期的HTML行/ div /行,并在日期之后显示一个条,该条将是剩余屏幕宽度的百分比。

Like so: 像这样:

2015-11-17 ---------------------(50%)
2015-11-18 ------------------------------------------- (80%)
2015-11-19 ==================================================== (100%)

If you will please consider in the dashes as a proper bar (like 10px height for eg). 如果您愿意,请在破折号中将其视为一个合适的条(例如,高度为10px)。 You might notice that 50% and 80% have ---- while 100% has ==== . 您可能会注意到50%和80%具有----而100%具有====

This is because for any percentage less than 100 I want the bar to be a mixed color like blue and white combo. 这是因为对于任何小于100的百分比,我都希望条形像是蓝色和白色组合这样的混合色。 For the 100% it will be a solid blue color bar. 对于100%,它将是纯蓝色条。

I am trying to achieve this using HTML/CSS only, but I find my expertise to be lacking. 我试图仅使用HTML / CSS来实现这一点,但是我发现我缺乏专业知识。

So far, I have following HTML/CSS: 到目前为止,我有以下HTML / CSS:

<div style="padding-top: 10px;padding-bottom:10px; width:100%">
  <div style='float:left'><strong>Date </strong>{{Today.date}}</div>
  <div style='float:left;background-color:red;width:100%'></div>
</div>

The above code does not even show the second div with red background :( 上面的代码甚至没有显示带有红色背景的第二个div :(

Any pointers in helping me solve this is very much appreciated! 非常感谢您帮助我解决此问题的任何指示!

Flexbox could help here depending on your browser support requirements. 根据您的浏览器支持要求,Flexbox可以在这里提供帮助。

 .wrap { width: 80%; margin: 1rem auto; font-size: 24px; display: flex; align-items: center; border: 1px solid grey; } .date { padding: 0 1em; } .bar { height: 10px; background: #f00; } .bar-50 { flex: .5; } .bar-80 { flex: .8; } .bar-100 { flex: 1; } 
 <div class="wrap"> <div class="date">2015-11-17</div> <div class="bar bar-50"></div> </div> <div class="wrap"> <div class="date">2015-11-18</div> <div class="bar bar-80"></div> </div> <div class="wrap"> <div class="date">2015-11-19</div> <div class="bar bar-100"></div> </div> 

Sneaky version with pseudo-element instead of extra HTML 带有伪元素而不是额外HTML的鬼nea版本

 .bar { width: 80%; margin: 1rem auto; font-size: 24px; display: flex; align-items: center; border: 1px solid grey; } .date { padding: 0 1em; } .bar::after { content: ''; height: 10px; background: #f00; } .bar-50:after { flex: .5; } .bar-80:after { flex: .8; } .bar-100:after { flex: 1; } 
 <div class="bar bar-50"> <div class="date">2015-11-17</div> </div> <div class="bar bar-80"> <div class="date">2015-11-18</div> </div> <div class="bar bar-100"> <div class="date">2015-11-19</div> </div> 

Perhaps more semantically, use a progress element and a label. 也许从语义上讲,使用progress元素和标签。

Progess Element @ MDN 进行元素@ MDN

 div { margin: 10px; } 
 <div> <label for="alpha">2015-11-17</label> <progress id="alpha" value="50" max="100">50 %</progress> </div> <div> <label for="beta">2015-11-18</label> <progress id="beta" value="70" max="100">70 %</progress> </div> <div> <label for="gamma">2015-11-19</label> <progress id="gamma" value="100" max="100">70 %</progress> </div> 

You can try this 你可以试试这个

<div style="padding-top: 10px;padding-bottom:10px; width:100%;position: relative;">
  <div style="float: left;width: 50%;display: inline-block;"><strong>Date </strong>{{Today.date}}</div>
  <div style="float:left;width: 50%;height: 10px;display: inline-block;">
 <div class="progress" style="background-color:red;width: 100%;height: 100%;">
</div></div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM