【问题标题】:Create heading text with a weak same text behind the heading with CSS使用 CSS 在标题后面创建具有弱相同文本的标题文本
【发布时间】:2019-12-04 12:41:32
【问题描述】:

我真的不知道,怎么称呼它。我需要做这样的事情:

h2 {
text-shadow: 5px 5px #000;
}
<h2>My Text</h2>

所以有我的标题文本和背景上的相同文本。它应该是可自动升级的,所以当有人更改“KONTAKT”时,背景文本也会自动更改为相同。

理想情况下,是否可以仅使用 CSS 来做到这一点?

我想,这可以通过 text-shadow 属性来完成,但似乎不行。

【问题讨论】:

  • 希望您至少尝试自己编写代码。我建议你做一些additional research,无论是通过谷歌还是通过搜索,尝试一下。如果您仍然遇到问题,请返回您的代码并解释您尝试过的操作。
  • 你需要 2 段文本,它可以从一个数据属性中产生并通过一个伪元素生成。到目前为止,您的 HTML 和尝试是什么?
  • 你好,我唯一的尝试是 text-shadow: 5px 5px #000; HTML 只是

    MyText

  • 您应该将代码尝试添加到您的问题中以避免被否决。
  • @G-Cyr 对不起。

标签: css html-heading


【解决方案1】:

您需要两条可以应用不同样式的文本。

要键入一次,您可以使用数据属性并通过 ::before 和 :: after 生成文本。 (可能的问题,HTML 中缺少文本)

带有数据属性的示例(在我看来不是最好的,因为缺少文本)

h2 {
  display: grid;
  height: 100px;
}

[data-text]::before,
[data-text]::after {
  content: attr(data-text);
  margin: auto;
  grid-column: 1;
  grid-row: 1;
  z-index: 1;
  color: brown;
  text-shadow: 0 0 2px white, 0 0 2px white, 0 0 2px white;
}

::before {
  z-index: 0;
  transform: scale(2.5);
  opacity: 0.25
}
<h2 data-text=" My text to show"></h2>

另一个例子,你需要输入两次文本。

h2 {
  display: grid;
  height: 100px;
  justify-content: center;
  text-align: center;
  align-items: center;
  color: brown;
}

[data-text]::before {
  content: attr(data-text);
  position: absolute;
  grid-row: 1;
  grid-column: 1;
  left: 0;
  right: 0;
}

::before {
  z-index: 0;
  transform: scale(2.5);
  opacity: 0.25
}
<h2 data-text="My text to show">My text to show</h2>

【讨论】:

    【解决方案2】:

    使用文本作为数据属性,然后你可以在每个伪元素中使用两次,轻松实现你想要的。

    h2 {
      font-family:arial;
      position:relative;
      display:table;
      margin:auto;
      color:#bda000;
    }
    
    h2:before {
      content:attr(data-text);
      font-size:3em;
      opacity:0.25;
    }
    h2:after {
      content:attr(data-text);
      position:absolute;
      top:50%;
      transform:translateY(-50%);
      left:0;
      right:0;
      text-align:center;
    }
    <h2 data-text="MY Text"></h2>
    
    <h2 data-text="Another text"></h2>

    【讨论】:

    • 这行得通,但是 :after cause,那里的文字是三倍的。
    • @Mego 你在哪里看到的文字是三倍的?
    猜你喜欢
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2013-02-16
    相关资源
    最近更新 更多