【问题标题】:CSS float left, without wrapping and fixed sizeCSS向左浮动,不换行和固定大小
【发布时间】:2020-06-20 20:00:08
【问题描述】:

我有一个名为 timerow 的容器,它和它的父容器一样大。在它里面我定位时间,所以很清楚它上面的方框中的距离意味着什么。问题是,float: left; 完美地与 % 中的边距一起工作,但最后一个元素总是被包裹。我不能让容器变宽,因为那时 % 的边距关闭了。

我还尝试了display: inline-block;,但没有成功(因为空格改变了位置)和我也提供的 flexbox。 Flexbox 非常接近,但是当我将所有内容缩放到小,或添加另一个时间标签(另一个标签)时,它不仅位于最右侧,而且会改变容器的宽度并再次改变定位。

我的预期结果是所有标签都相对于它们的左邻居定位为时间行宽度的一小部分(因为我计算了该 % 值),并且任何溢出都在同一行中可见并将框溢出到对。

我提供了一个最小的jsFiddle

body {
  background-color: #DDD;
}

.block {
  display: block;
  background: #ccc;
  height: 100%;
  font-size: .75em;
  float: left;
}

.block:nth-child(n+2) {
  margin-left: 0.5%;
}

.barchart {
  grid-area: all;
  display: grid;
  grid-template-columns:
  [viewport-start] minmax(9px, 1fr)
  [container-start] minmax(20em, 35em)
  [container-end] minmax(9px, 1fr) [viewport-end];
  grid-auto-rows: 30px;
}

row {
  padding: 5px;
  box-sizing: border-box;
  grid-column: container;
  grid-row: span 4;
  line-height: 120px;
  text-align: center;
}

timerow {
  grid-column: container;
  grid-row: span 1;
  line-height: 16px;
  margin: 0 5px;
  
  background: rgba(0,0,0,0.2);
  display: flex;
  justify-content: flex-start;
  flex-flow: row nowrap;
}

timerow a {
  height: 20px;
  width: 40px;
  font-size: 15px;
  color: #919191;
  text-align: center;
  background: #fff;
}

timerow a:first-child, timerow2 a:first-child {
  margin-left: -21px;
}

timerow2 {
  grid-column: container;
  grid-row: span 1;
  line-height: 16px;
  margin: 0 5px;
  
  background: rgba(0,0,0,0.8);
}
timerow2 a {
  height: 20px;
  width: 40px;
  font-size: 15px;
  color: #919191;
  text-align: center;
  background: #fff;
  float: left;
}

和html

<div class="barchart">
  <row style="animation: none;width:100%;">
    <a style="width:28.9444444444%;" class="block"></a>
    <a style="width:63.805555555%;" class="block"></a>
    <a style="width:6.25%;" class="block"></a>
  </row>
  
  <timerow>
  <a>00:00</a>
  <a style="margin-left:calc((28.94444 + 0.25)*1% - 40px);">07:30</a>
  <a style="margin-left:calc((63.8055 + 0.5)*1% - 40px);">22:30</a>
  </timerow>
  
  <timerow>
  <a>00:00</a>
  <a style="margin-left:calc((28.94444 + 0.25)*1% - 40px);">07:30</a>
  <a style="margin-left:calc((63.8055 + 0.5)*1% - 40px);">22:30</a>
  <a>24:00</a>
  </timerow>
  
    <timerow2>
  <a>00:00</a>
  <a style="margin-left:calc((28.94444 + 0.25)*1% - 40px);">07:30</a>
  <a style="margin-left:calc((63.8055 + 0.5)*1% - 40px);">22:30</a>
  <a>24:00</a>
  </timerow2>

</div>

【问题讨论】:

    标签: css css-float css-position


    【解决方案1】:

    为避免这种情况,为最后一个元素添加负边距,使其宽度不计入总宽度,并且可以保留在第一行(但可以溢出):

    timerow a:last-child, timerow2 a:last-child {
      margin-right: -40px;
    }
    

    完整代码:

    body {
      background-color: #DDD;
    }
    
    .block {
      display: block;
      background: #ccc;
      height: 100%;
      font-size: .75em;
      float: left;
    }
    
    .block:nth-child(n+2) {
      margin-left: 0.5%;
    }
    
    .barchart {
      grid-area: all;
      display: grid;
      grid-template-columns:
      [viewport-start] minmax(9px, 1fr)
      [container-start] minmax(20em, 35em)
      [container-end] minmax(9px, 1fr) [viewport-end];
      grid-auto-rows: 30px;
    }
    
    row {
      padding: 5px;
      box-sizing: border-box;
      grid-column: container;
      grid-row: span 4;
      line-height: 120px;
      text-align: center;
    }
    
    timerow {
      grid-column: container;
      grid-row: span 1;
      line-height: 16px;
      margin: 0 5px;
      
      background: rgba(0,0,0,0.2);
      display: flex;
      justify-content: flex-start;
      flex-flow: row nowrap;
    }
    
    timerow a {
      height: 20px;
      width: 40px;
      font-size: 15px;
      color: #919191;
      text-align: center;
      background: #fff;
    }
    
    timerow a:first-child, timerow2 a:first-child {
      margin-left: -21px;
    }
    timerow a:last-child, timerow2 a:last-child {
      margin-right: -40px;
    }
    
    timerow2 {
      grid-column: container;
      grid-row: span 1;
      line-height: 16px;
      margin: 0 5px;
      
      background: rgba(0,0,0,0.8);
    }
    timerow2 a {
      height: 20px;
      width: 40px;
      font-size: 15px;
      color: #919191;
      text-align: center;
      background: #fff;
      float: left;
    }
    <div class="barchart">
      <row style="animation: none;width:100%;">
        <a style="width:28.9444444444%;" class="block"></a>
        <a style="width:63.805555555%;" class="block"></a>
        <a style="width:6.25%;" class="block"></a>
      </row>
      
      <timerow>
      <a>00:00</a>
      <a style="margin-left:calc((28.94444 + 0.25)*1% - 40px);">07:30</a>
      <a style="margin-left:calc((63.8055 + 0.5)*1% - 40px);">22:30</a>
      </timerow>
      
      <timerow>
      <a>00:00</a>
      <a style="margin-left:calc((28.94444 + 0.25)*1% - 40px);">07:30</a>
      <a style="margin-left:calc((63.8055 + 0.5)*1% - 40px);">22:30</a>
      <a>24:00</a>
      </timerow>
      
        <timerow2>
      <a>00:00</a>
      <a style="margin-left:calc((28.94444 + 0.25)*1% - 40px);">07:30</a>
      <a style="margin-left:calc((63.8055 + 0.5)*1% - 40px);">22:30</a>
      <a>24:00</a>
      </timerow2>
    
    </div>

    更好的解决方案是考虑 0 宽度元素,这样你的所有计算都会更容易:

    body {
      background-color: #DDD;
    }
    
    .block {
      background: #ccc;
      height: 100%;
      font-size: .75em;
      float: left;
    }
    
    .block:nth-child(n+2) {
      margin-left: 0.5%;
    }
    
    .barchart {
      display: grid;
      grid-template-columns:
      [viewport-start] minmax(9px, 1fr)
      [container-start] minmax(20em, 35em)
      [container-end] minmax(9px, 1fr) [viewport-end];
      grid-auto-rows: 30px;
    }
    
    row {
      padding: 5px;
      box-sizing: border-box;
      grid-column: container;
      grid-row: span 4;
      line-height: 120px;
      text-align: center;
    }
    
    timerow {
      grid-column: container;
      line-height: 16px;
      margin: 0 5px;
      background: rgba(0,0,0,0.2);
      display: flex;
    }
    
    timerow a {
      height: 20px;
      width: 0;
      font-size: 15px;
      text-align: center;
      position:relative;
    }
    timerow a::before {
      content:attr(data-time);
      position:absolute;
      top:0;
      left:50%;
      transform:translateX(-50%);
      background: #fff;
      color: #919191;
    }
    <div class="barchart">
      <row style="animation: none;width:100%;">
        <a style="width:28.9444444444%;" class="block"></a>
        <a style="width:63.805555555%;" class="block"></a>
        <a style="width:6.25%;" class="block"></a>
      </row>
      
      <timerow>
      <a data-time="00:00"></a>
      <a style="margin-left:calc((28.94444 + 0.25)*1%);" data-time="07:30"></a>
      <a style="margin-left:calc((63.8055 + 0.5)*1%);" data-time="22:30"></a>
      </timerow>
      
      <timerow>
      <a data-time="00:00"></a>
      <a style="margin-left:calc((28.94444 + 0.25)*1%);" data-time="07:30"></a>
      <a style="margin-left:calc((63.8055 + 0.5)*1%);" data-time="22:30"></a>
      <a style="margin-left:calc((6.25 + 0.5)*1%);" data-time="24:00"></a>
      </timerow>
      
        <timerow style="background:rgba(0,0,0,0.8)">
      <a data-time="00:00"></a>
      <a style="margin-left:calc((28.94444 + 0.25)*1%);" data-time="07:30"></a>
      <a style="margin-left:calc((63.8055 + 0.5)*1%);" data-time="22:30"></a>
      <a style="margin-left:calc((6.25 + 0.5)*1%);" data-time="24:00"></a>
      </timerow>
    
    </div>

    【讨论】:

    • 哇,这是一个很酷的解决方案。尽管它看起来有点 hacky,因为它只适用于最后一个元素。由于这实际上是一个 UI,我不知道会有多少标签,并且充其量它们被定位为“尽可能向左而不重叠,但如果有重叠,只需将它们放在一边,而不考虑总宽度”。我几乎已经放弃了,所以如果不出意外我会尽快接受你的回答。
    • @mike 这并不是真正的hacky,它只需要应用于最后一个元素以避免拉动其他元素。如果将其应用于其他元素,则需要更新计算以考虑它。请记住,总数是:所有边距 + 宽度 = 父宽度的 100%
    • 哦,是的,我明白了。但是如果有 2 个元素在外面很远,我需要将它们都作为负边距,以将宽度保持在 100%。然后我需要检查,是否有东西溢出,对吗?而这我不能只用 CSS+HTML 来做,对吗?据我所知,这实际上是我认为的更好的解决方案,但我有点担心某些展示位置会因为它们接近结尾而不是最后一项而失败。
    • 感谢您提供这个出色的解决方案。我永远不会想出这样的诡计。 CSS 包含许多继承的“特性”,以至于很难知道何时使用什么。这正是我想要的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    相关资源
    最近更新 更多