【问题标题】:Move a floated box down one line将浮动框向下移动一行
【发布时间】:2021-03-26 23:37:22
【问题描述】:

我试图在文本中产生类似于“侧边框”的效果,其中段落开头的注释或标题“浮动”在左侧或右侧,周围有一些负空间,略低于顶一下这里的日期:

像这样的简单浮动不允许我将注释移动到第一行下方,因此我得到了一个完整的文本行:

.note {
  float: right;
  margin: 0.5em 0 0.5em 0.5em;
}
<p>
<span class="note">29 July, 1814.</span>
Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from that to which I had been accustomed;</p>

仅设置填充或边距只会使框变大,但我想将整个东西向下移动。设置绝对定位意味着它不再保留空间和重叠。

有什么方法可以实现这一点(并且仍然将注释放在段落中?)

【问题讨论】:

    标签: html css css-float word-wrap


    【解决方案1】:

    shape-outside 可以这样做:

    .note {
      float: right;
      margin: 0 0.5em;
      padding-top:1.1em; /* padding equal (or slightly smaller) to one line */
      shape-outside:inset(1.2em 0 0 0); /* this will do the magic */
    }
    
    p {
      line-height:1.2em; /* height of a line */
    }
    <p>
    <span class="note">29 July, 1814.</span>
    Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from that to which I had been accustomed; the first time the confused buzz of voices speaking a different language from that to which I had been accustomed; first time the confused buzz of voices speaking a different language from that to which I had been accustomed;</p>

    有边距:

    .note {
      float: right;
      margin: 0.5em 0 0.5em 0.5em;
      padding-top:1.2em; 
      shape-outside:inset(1.2em 0 0 0); 
    }
    
    p {
      line-height:1.2em; 
    }
    <p>
    <span class="note">29 July, 1814.</span>
    Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from that to which I had been accustomed; the first time the confused buzz of voices speaking a different language from that to which I had been accustomed; first time the confused buzz of voices speaking a different language from that to which I had been accustomed;</p>

    【讨论】:

      【解决方案2】:

      按照 Temani Afif 的描述,Shape-outside 绝对是最干净、最灵活的解决方案,我推荐它。但既然你被告知这是不可能的,我将向你展示一个较旧的解决方案。您可以在浮动跨度之前加上另一个零宽度的浮动和您想要将其向下移动的高度,由::before 伪元素构造。在这种情况下,我们将使用一个文本行的高度。然后只需设置浮动跨度以清除构造的浮动。

      .note {
        float: right;
        margin: 0.5em 0 0.5em 0.5em;
        clear: right;
      }
      
      p::before {
        float: right;
        content: '\200C';
      }
      
      body {
        width:375px;
      }
      <p>
      <span class="note">29 July, 1814.</span>
      Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from that to which I had been accustomed; and saw a costume very unlike</p>

      【讨论】:

      • 哦,太好了!我们还可以轻松控制行数,无需任何硬编码的行高:jsfiddle.net/dyf9kvtw
      【解决方案3】:

      恐怕这是不可能的,至少不会以一种在所有情况下将其向下移动一行(或任何数量的行或像素)的方式。

      您唯一能做的就是将span 元素本身移动到您的HTML 代码中,即将它插入到p 标记内的稍后位置:

      .note {
        float: right;
        margin: 0.5em 0 0.5em 0.5em;
      }
      <p>
      Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. <span class="note">29 July, 1814.</span>I heard for the first time the confused buzz of voices speaking a different language from that to which I had been accustomed;</p>

      【讨论】:

      • 该死,这就是我害怕的。
      猜你喜欢
      • 1970-01-01
      • 2011-07-24
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多