这是我到目前为止的JSFiddle:
HTML 的
<div class="results">
<h2>Some data</h2>
<ul style="margin:0;padding:0;">
<li class="resultsListItem" style="list-style-type: none;">
<div class="answerDiv"> text </div>
<div class="histogramBar"> </div>
<div class="resultDiv"> 7 </div>
</li>
<br>
<li class="resultsListItem" style="list-style-type: none;">
<div class="answerDiv"> text </div>
<div class="histogramBar"> </div>
<div class="resultDiv"> 1 </div>
</li>
<br>
<li class="resultsListItem" style="list-style-type: none;">
<div class="answerDiv"> text </div>
<div class="histogramBar"> </div>
<div class="resultDiv"> 4 </div>
</li>
<br>
<li class="resultsListItem" style="list-style-type: none;">
<div class="answerDiv"> text </div>
<div class="histogramBar"> </div>
<div class="resultDiv"> 4 </div>
</li>
<br>
</ul>
</div>
CSS
.answerDiv, .resultDiv, .histogramBar {
display: inline-block;
}
.answerDiv, .histogramBar {
float: left;
}
.answerDiv {
margin-right: 10px;
width: 100px;
}
.histogramBar {
height: 6px;
width: 100px;
background-color: #66dd66;
margin-top: 9px;
border-radius: 5px;
transition: width 1s;
}
.histogramBar:hover {
width: 150px;
}
/*text*/
h2 {
font-size: 40px;
color: black;
}
/*alignment*/
.results {
width: 400px;
height: 400px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
然而,我遇到了一些麻烦,让所有对齐的东西都正确。我的目标是让.answerDiv中的文本全部浮动。如果文本较长,在某一点它将包裹到第二行。然后,histogramBar也会向左浮动并且恰好位于文本的右侧。然后,结果编号将浮动到包含div的另一侧。另外,当histogramBar的宽度发生变化时,我无法弄清楚如何让右边的数字保持不变。
不幸的是,我无法弄清楚如何让它正常工作。我对造型比较陌生,所以我很清楚我的代码可能真的很难看。
我如何做到这一点?
回顾 - 文本向左浮动,直方图条向左浮动(文本右侧)数字向右浮动。当您将鼠标悬停在条形图上且尺寸发生变化时,右侧的数字不会受到影响。
答案 0 :(得分:2)
要使文本右对齐,在您的配置中,绝对基于<li>
的定位是最简单的方法:
.resultDiv {
text-align: right;
position: absolute;
right: 0;
}
要实现这一点,您必须将position: relative;
添加到.resultsListItem
。
我在样式方面稍微改变了你的例子,以更好地展示元素。
.answerDiv,
.resultDiv,
.histogramBar {
display: inline-block;
font-size: 14px;
vertical-align: top;
}
.answerDiv {
margin-right: 10px;
width: 100px;
}
.histogramBar {
height: 6px;
width: 100px;
background-color: red;
margin-top: 9px;
border-radius: 5px;
transition: all 1s;
}
.histogramBar:hover {
width: 150px;
}
/*text*/
h2 {
font-size: 40px;
color: black;
}
/*alignment*/
.results {
width: 400px;
height: 400px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
font-size: 0;
}
.resultsListItem {
list-style-type: none;
position: relative;
}
.resultsListItem:nth-of-type(even) {
background-color: #f8f8ff;
}
.results ul {
margin: 0;
padding: 0;
}
.resultDiv {
text-align: right;
position: absolute;
right: 0;
}
<div class="results">
<h2>Some data</h2>
<ul style="">
<li class="resultsListItem">
<div class="answerDiv">text</div>
<div class="histogramBar"></div>
<div class="resultDiv">7</div>
</li>
<li class="resultsListItem">
<div class="answerDiv">text that will wrap to a new line</div>
<div class="histogramBar"></div>
<div class="resultDiv">821</div>
</li>
<li class="resultsListItem">
<div class="answerDiv">text</div>
<div class="histogramBar"></div>
<div class="resultDiv">4</div>
</li>
<li class="resultsListItem">
<div class="answerDiv">text</div>
<div class="histogramBar"></div>
<div class="resultDiv">14</div>
</li>
</ul>
</div>
答案 1 :(得分:0)
如果是我,我会做这样的事情:
$mainColor: #66dd66;
.answerDiv, .resultDiv, .histogramBar
{
display: inline-block;
border:1px solid red;
}
.results
{
border:1px solid red;
}
.answerDiv, .histogramBar
{
float: left;
}
.answerDiv
{
margin-right: 10px;
width: 100px;
}
.histogramBar
{
height: 6px;
width: 100px;
background-color: $mainColor;
margin-top: 9px;
border-radius: 5px;
transition: width 1s;
&:hover
{
width: 150px;
}
}
.resultDiv
{
}
.resultsListItem
{
}
/*text*/
h2
{
font-size: 40px;
color: black;
}
/*alignment*/
.results
{
width: 400px;
height: 400px;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.answersListItem
{
position:relative;
top:30px;
left:20px;
width:100px;
border:1px solid red;
float:left;
}
.histogramListItem
{
position:relative;
top:10px;
left:50px;
width:100px;
height:72px;
border:1px solid red;
float:left;
}
.resultListItem
{
position:relative;
top:-10px;
float:right;
border:1px solid red;
width:10px;
margin-right:50px;
}
<html>
<body>
<div class="results">
<h2>Some data</h2>
<ul style="margin:0;padding:0;">
<li class="answersListItem" style="list-style-type: none;">
<div class="answerDiv"> text </div>
<div class="answerDiv"> text </div>
<div class="answerDiv"> text </div>
<div class="answerDiv"> text </div>
</li>
<br>
<li class="histogramListItem" style="list-style-type: none;">
<div class="histogramBar"> </div>
<div class="histogramBar"> </div>
<div class="histogramBar"> </div>
<div class="histogramBar"> </div>
</li>
<br>
<li class="resultListItem" style="list-style-type: none;">
<div class="resultDiv"> 7 </div>
<div class="resultDiv"> 1 </div>
<div class="resultDiv"> 4 </div>
<div class="resultDiv"> 4 </div>
</li>
<br>
</ul>
</div>
</body>
</html>
答案 2 :(得分:0)
愿这对你有所帮助。
将class="histogramBar
div包装在另一个div中。并设置宽度。
HTML
<div class="results">
<h2>Some data</h2>
<ul style="margin:0;padding:0;">
<li class="resultsListItem" style="list-style-type: none;">
<div class="answerDiv">text</div>
<div class="x">
<div class="histogramBar"></div>
</div>
<div class="resultDiv"> 4</div>
</li>
<br>
<li class="resultsListItem" style="list-style-type: none;">
<div class="answerDiv">text</div>
<div class="x">
<div class="histogramBar"></div>
</div>
<div class="resultDiv"> 4</div>
</li>
<br>
</ul>
萨斯
$mainColor: #66dd66;
.answerDiv, .resultDiv, .x
{
display: inline-block;
}
.answerDiv, .histogramBar
{
float: left;
}
li div{border:1px solid red}
.answerDiv
{
margin-right: 10px;
width: 100px;
}
.x{width:160px} /*Now .histogramBar is inside .x*/
.histogramBar
{
height: 6px;
width: 100px;
background-color: $mainColor;
margin-top: 9px;
border-radius: 5px;
transition: width 1s;
&:hover
{
width: 150px;
}
}
/*text*/
h2
{
font-size: 40px;
color: black;
}
/*alignment*/
.results
{
width: 400px;
height: 400px;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
删除边框并将鼠标悬停在.x
上
这会将您的数字文本固定在右侧,当您将鼠标悬停在.x
上时,它也不会影响数值。
如果你遇到任何问题,请回来。
答案 3 :(得分:-1)
检查更新小提琴。 https://jsfiddle.net/e1xrwyLv/4/
如果我清楚地了解您的问题,那么关注css可以解决您的问题。
<强> CSS 强>
$mainColor: #66dd66;
.resultsListItem{
margin-bottom:5px;
&:after{
clear:both;
content:'';
display:block;
}
}
.answerDiv, .resultDiv, .histogramBar
{
display: inline-block;
}
.answerDiv, .histogramBar
{
float: left;
}
.answerDiv
{
margin-right: 10px;
width: auto;
max-width:200px
}
.histogramBar
{
height: 6px;
width: 100px;
background-color: $mainColor;
margin-top: 9px;
border-radius: 5px;
transition: width 1s;
&:hover
{
width: 150px;
}
}
.resultDiv
{
float:right;
}
.resultsListItem
{
}
/*text*/
h2
{
font-size: 40px;
color: black;
}
/*alignment*/
.results
{
width: 400px;
height: 400px;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}