如何使同一个 "row" 中的两个 div 向不同方向浮动?

How do I make two divs in the same "row" float in different directions?

这是我目前拥有的 JSFiddle:

JSFiddle Link

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">      &nbsp;7   </div>
      </li>
      <br>
      <li class="resultsListItem" style="list-style-type: none;">
         <div class="answerDiv">       text   </div>
         <div class="histogramBar">   </div>
         <div class="resultDiv">      &nbsp;1   </div>
      </li>
      <br>
      <li class="resultsListItem" style="list-style-type: none;">
         <div class="answerDiv">       text   </div>
         <div class="histogramBar">   </div>
         <div class="resultDiv">      &nbsp;4   </div>
      </li>
      <br>
      <li class="resultsListItem" style="list-style-type: none;">
         <div class="answerDiv">       text   </div>
         <div class="histogramBar">   </div>
         <div class="resultDiv">      &nbsp;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 中的文本全部浮动。如果文本较长,在某一点它会换行到第二行。然后,直方图栏也会向左浮动并恰好位于文本的右侧。然后,结果数字将浮动到包含 div 的另一侧。另外,我不知道如何在 histogramBar 的宽度变化时让右边的数字保持不变。

不幸的是,我不知道如何让它正常工作。我对样式设置比较陌生,所以我很清楚我的代码可能真的很丑。

我该如何完成?

回顾 - 文本向左浮动,直方图条向左浮动(就在文本的右侧)数字向右浮动。当您将鼠标悬停在栏上并且它的大小发生变化时,右侧的数字应该不会受到影响。

如果是我,我会这样做:

$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">      &nbsp;7   </div>
   <div class="resultDiv">      &nbsp;1   </div>
   <div class="resultDiv">      &nbsp;4   </div>
         <div class="resultDiv">      &nbsp;4   </div>
      </li>
      <br>
   </ul>
</div>

</body>
</html>

检查 更新 fiddle。 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;
}

希望对您有所帮助。 将 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">&nbsp;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">&nbsp;4</div>
    </li>
    <br>
</ul>

Sass

$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 上时,它也不会影响数值。 如果您遇到任何问题,请回来。

对于右对齐的文本,在你的配置中绝对基于<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>