【问题标题】:How do I make two divs in the same "row" float in different directions?如何使同一“行”中的两个 div 向不同方向浮动?
【发布时间】:2015-09-01 14:26:04
【问题描述】:

这是我目前所拥有的 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 中的文本全部向左浮动。如果文本更长,在某个点它会换行到第二行。然后, histogramBar 也会向左浮动并位于文本的右侧。然后,结果编号将向右浮动到包含 div 的另一侧。此外,当 histogramBar 的宽度发生变化时,我无法弄清楚如何使右侧的数字保持不变。

不幸的是,我无法弄清楚如何让它正常工作。我对样式比较陌生,所以我很清楚我的代码可能真的很难看。

我该如何做到这一点?

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

【问题讨论】:

  • 旁注:您的 HTML 无效&lt;li&gt;&lt;ul&gt; 唯一允许的直接子元素。你在那里有非法的&lt;br /&gt;

标签: html css alignment css-float


【解决方案1】:

对于要右对齐的文本,在您的配置中绝对基于&lt;li&gt; 定位它是最简单的方法:

.resultDiv {
    text-align: right;
    position: absolute;
    right: 0;
}

为此,您必须将position: relative; 添加到您的.resultsListItems。

我在样式方面对您的示例进行了一些更改,以更好地展示元素。

.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>

【讨论】:

    【解决方案2】:

    希望对您有所帮助。 将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>
    

    萨斯

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

    【讨论】:

    • 不需要额外的 HTML 元素。都在那里。
    【解决方案3】:

    如果是我,我会这样做:

    $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>

    【讨论】:

      【解决方案4】:

      检查 Updated 小提琴。 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;
      }
      

      【讨论】:

      • 你和 OP 的小提琴产生完全相同的视觉效果。
      • @connexo 哎呀..这可能是因为我分享了错误的小提琴链接。现在请看我的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多