【问题标题】:How can I make the content of pseudo :before indent along with its parent element?如何使伪 :before 的内容与其父元素一起缩进?
【发布时间】:2019-10-30 03:15:59
【问题描述】:

我有一个 HTML 标记,我使用伪 :before 选择器在每个 h1 元素之前注入文本 Chapter。我还使用了display:block 属性,以便标签Chapter 出现在单独的行上。但是当我尝试应用text-indent 样式时,只有通过:before 选择器插入的内容会缩进。 h1 元素的内容不会随着文本“Chapter”移动。

这是我原始 HTML 的简化版本。

.chapter{
  background-color:grey;
  height:100px;
  width:200px;
  text-indent:50px;
}
.chapter:before{
  content:"Chapter 1";
  display:block;
}
.l2{
  background-color:red;
  height:100px;
  width:200px;
}
.l3{
  background-color:orange;
  height:100px;
  width:200px;
  clear:both;
}
.container{
  margin:0.3in;
  clear:both;
}
<div class="container">
  <div class="chapter">
    Getting Started
  </div>
  
  <div class="l2">
  Level 2
  </div>

  <div class="l3">
  Level 3
  </div>
</div>

实际结果

只有标签“Chapter”是缩进的。

预期结果

h1 内容Getting Started 应与标签Chapter 一起缩进。

【问题讨论】:

  • Getting Started 仅在视觉上似乎位于单独的行上。在您的代码中,它位于Chapter 1 之后的同一行。因此,text-indent 工作正常,并在行首之前放置了一些空间。请注意,没有 h1 内容。如果您按照我的想法将Getting Started 包装在&lt;h1&gt; 中,它也会缩进,但对于块和换行来说也太大了。
  • stackoverflow.com/users/2948042/vince。 @文斯谢谢。坦率地说,我认为当我使用“display:block”时,伪 :before 和“Getting Started”都可以单独定位。我无法在 h1 中单独包装“入门”,因为我通过 :before 伪选择器插入“章节标签”。有没有办法插入标签,使它像一个独立的元素?或者有没有办法单独针对入门?任何帮助/输入将不胜感激。
  • 我不清楚你想做什么。如果您将Getting Started 包装在h1 中并使用.chapter:before 添加Chapter 1,并保持其他所有内容不变,则它们都会缩进并允许您独立定位它们。然后你只需要调整h1 的字体大小,使它不会在块内换行。
  • 知道了。我会尝试并回复您。
  • @Vince,它起到了神奇的作用。对我来说,这是一个复杂的场景。你的解释帮助我理解发生了什么。如果您可以根据您的输入返回并修复它。现在标签和 h1 元素可以按我想要的方式缩进。万分感谢。如果您提供说明作为解决方案,我会接受。在简单的 cmets 中没有办法做到这一点。再次感谢您。

标签: html css


【解决方案1】:

这是其他提供的答案的替代方法,可能有助于解决 cmets 中提到的一些问题...

  • 将章节标题(“入门”)放入单独的块中
  • Chapter 1 前缀附加到.chapter .heading:before 而不是包含块。
  • 在章节标题栏上使用position: relative 并将其移动到所需的任何方向。例如:left: 50px

.chapter {
  background-color: grey;
  height: 100px;
  width: 200px;
}

.chapter .heading {
  position: relative;
  left: 50px;
}

.chapter .heading:before {
  content: "Chapter 1";
  display: block;
}

.l2 {
  background-color: red;
  height: 100px;
  width: 200px;
}

.l3 {
  background-color: orange;
  height: 100px;
  width: 200px;
  clear: both;
}

.container {
  margin: 0.3in;
  clear: both;
}
<div class="container">
  <div class="chapter">
    <div class="heading">Getting Started</div>
  </div>
  
  <div class="l2">
  Level 2
  </div>

  <div class="l3">
  Level 3
  </div>
</div>

【讨论】:

  • 正如我之前评论的那样,您的建议起到了神奇的作用。我已经在我的实际代码中尝试过了。非常感谢 Vince 和 Hải Bùi,感谢你们抽出时间来帮助我。
【解决方案2】:

您不能将text-indent 用于伪,因为它仅用于文本,而不用于从伪和h1 合并的文本。因此,要解决此问题,您可以在 h1 之前留一个空格来放置 Chapter 文本。希望对您有所帮助

.chapter{
  background-color:grey;
  height:100px;
  width:400px;
  text-indent: 50px;
}
.chapter:before{
  content:"Chapter 1";
  display:block;
}
.l2{
  background-color:red;
  height:100px;
  width:400px;
}
.l3{
  background-color:orange;
  height:100px;
  width:400px;
  clear:both;
}
.container{
  margin:0.3in;
  clear:both;
}
<div class="container">
  <div class="chapter">
    <h1>Getting Started</h1>
  </div>
  
  <div class="l2">
  Level 2
  </div>

  <div class="l3">
  Level 3
  </div>
</div>

【讨论】:

  • 感谢您的回复。这将 h1 和 pseudo 放在一行中。我希望它们在不同的行上。添加填充将仅向一个方向(向右)缩进文本。
  • @antony 我更新了 sn-p,你的意图是这样吗?
  • 几乎,我正在寻找向左或向右缩进的灵活性?例如,如果我想要第 1 章和从页边距开始,则填充不起作用,因为填充不接受 - 整数值。这就是我尝试使用文本缩进的原因。谢谢你,Hải Bùi。
  • @Antony 这个答案在我看来就像你从一开始就想做的事情。 Hải Bùi 通过使块更宽来解决我之前提到的问题(h1 wrapping)。 paddingtext-indent 都使用相同的单位,并且所有单位都接受整数。如果您想从 beginning(由文档语言定义)以外的方向“缩进”事物,则需要使用 text-indent 以外的其他内容。您可以尝试将章节添加到.chapter h1:before。然后在h1上使用相对定位得到你想要的间距。
  • @Hải Bùi 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-31
  • 2018-09-03
  • 2013-01-09
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
相关资源
最近更新 更多