同一段落内有多种样式

发布于 2025-01-10 22:40:48 字数 1091 浏览 0 评论 0原文

我试图弄清楚是否有一种方法可以以多种方式设置段落样式,以便格式具有响应性并且不同样式的文本保持在同一行。我是初学者,已经搜索了其他答案但无法弄清楚,所以如果答案很明显,请原谅我。

例如:我讨厌热狗!

“我”需要为 16 像素,“讨厌”需要为 40 像素,而“热狗!”需要再次为 16px,但如果可能的话,我希望所有文本都在同一个

中,或者如果不可能,至少看起来好像它们都在同一个 <代码>

。 (我使用的实际文本是相当长的一段,需要响应不同的浏览器宽度,因此文本能够表现得像一个段落一样很重要。)

我偷工减料了一个极其混乱和使用 div 的解决方案只是半成功,但我确信应该有一个更优雅的答案:

.container1 {
  position: absolute;
  font-size: 16px;
}

.container2 {
  position: relative;
  float: left;
  padding-top: 22px;
}

.container3 {
  position: relative;
  float: right;
  font-size: 40px;
  padding-left: 4px;
}

.container4 {
  position: relative;
  float: right;
  padding-left: 4px;
  font-size: 16px;
  padding-top: 22px;
}
<div class="container1">
  <div class="container2">I</div>
  <div class="container3">HATE
    <div class="container4">hot dogs!</div>
  </div>
</div>

I'm trying to figure out if there's a way to style a paragraph in multiple ways such that the formatting is responsive and the text of different styles remains on the same line. I am a beginner, and have searched for other answers but can't figure it out, so please forgive me if the answer is quite obvious.

For example: I HATE hot dogs!

"I" would need to be 16px, "HATE" would need to be 40px, and "hot dogs!" would need to be 16px again, but I want all of the text within the same <p> if possible, or if not possible, at least to LOOK as if they're all in the same <p>. (The actual text I'm using is rather a long paragraph that needs to respond to different browser widths, so it's important for the text to be able to act as if it's one paragraph.)

I've jerry-rigged an extremely messy and only semi-successful solution using divs but I feel sure there should be a more elegant answer:

.container1 {
  position: absolute;
  font-size: 16px;
}

.container2 {
  position: relative;
  float: left;
  padding-top: 22px;
}

.container3 {
  position: relative;
  float: right;
  font-size: 40px;
  padding-left: 4px;
}

.container4 {
  position: relative;
  float: right;
  padding-left: 4px;
  font-size: 16px;
  padding-top: 22px;
}
<div class="container1">
  <div class="container2">I</div>
  <div class="container3">HATE
    <div class="container4">hot dogs!</div>
  </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

够运 2025-01-17 22:40:48

正如有些人已经说过的,在这种情况下,您不会使用

,而是需要 ,这将允许在同一

内使用不同的样式。您可以使用 CSS 定义特定的文本样式:

定义字体大小的 CSS 类

.textA {
  font-size: 16px;
}

.textB {
  font-size: 40px;
}
<p>
  <span class="textA">I</span>
  <span class="textB">HATE</span>
  <span class="textA">hot dogs!</span>
</p>

内联样式
这种方法可能看起来更短,但在设计整个段落时效果就不太好。

编辑(@tacoshy 注释): 也不应在电子邮件模板之外使用。这将导致主要的特定权重问题,导致可读性低并且无法缓存,从而导致加载时间更长。

<p>
  <span style="font-size: 16px;">I</span>
  <span style="font-size: 40px;">HATE</span>
  <span style="font-size: 16px;">hot dogs!</span>
</p>

请记住,您还可以设置其他样式,如字体颜色、斜体、粗体、背景颜色等。

As some people already said, you don't use <div> for this type of situation, instead, you need <span></span> which will allow different styles inside the same <p>. You can use CSS to define specific text styles:

CSS Classes defining font size

.textA {
  font-size: 16px;
}

.textB {
  font-size: 40px;
}
<p>
  <span class="textA">I</span>
  <span class="textB">HATE</span>
  <span class="textA">hot dogs!</span>
</p>

Inline Style
This approach may seem shorter but it won't be as nice when styling a whole paragraph.

Edit (note by @tacoshy): should also never been used outside of email-templates. This will cause major specificty weight issues, cause low readbility and cant be cached which causes longer loading times.

<p>
  <span style="font-size: 16px;">I</span>
  <span style="font-size: 40px;">HATE</span>
  <span style="font-size: 16px;">hot dogs!</span>
</p>

Remember you can also set other styles as font color, italic, bold, background color, etc.

污味仙女 2025-01-17 22:40:48

在这种情况下,您不会使用

。您将使用 元素。它旨在将不同的样式应用于同一段落。这是您在问题中给出的示例:

p {
  font-size: 16px;
}

.i {
  padding-top: 22px;
}

.hate {
  font-size: 40px;
  padding-left: 4px;
}

.hd {
  padding-left: 4px;
  font-size: 16px;
  padding-top: 22px;
}
<p>
  <span class='i'>I</span>
  <span class="hate">HATE</span>
  <span class="hd">hot dogs!</span>
</p>

In this case, you would not use <div>. You would use the <span> element. It's meant for applying different styles to the same paragraph. Here the example from the one you gave in the question:

p {
  font-size: 16px;
}

.i {
  padding-top: 22px;
}

.hate {
  font-size: 40px;
  padding-left: 4px;
}

.hd {
  padding-left: 4px;
  font-size: 16px;
  padding-top: 22px;
}
<p>
  <span class='i'>I</span>
  <span class="hate">HATE</span>
  <span class="hd">hot dogs!</span>
</p>

送舟行 2025-01-17 22:40:48

没有人注意到的一件事是,您可以(并且应该,IMO)在可能的情况下使用本机语义 HTML 元素,在失败时回退到通用 HTML 元素。

不需要两个以上的元素;包含的段落,以及强调的“仇恨”一词。请注意,我已在根级别 (html) 定义了 16px 字体,并相对于此定义了 40px (2.5rem)。这样,如果用户改变了字体大小,仇恨仍然会相对较大。

html { font-size: 16px; }
em { font-size: 2.5rem; font-style: normal; }
<p>I <em>HATE</em> hot dogs!</p>

One thing no one has noted is that you can (and should, IMO) use native, semantic HTML elements where possible, falling back to generic HTML elements where those fail.

There's no need for more than two elements; the containing paragraph, and the emphasized HATE word. Note I've defined the 16px font at the root level (html) and defined the 40px in relative terms to that (2.5rem). That way if a user changed their font sizes, the HATE would still be relatively large.

html { font-size: 16px; }
em { font-size: 2.5rem; font-style: normal; }
<p>I <em>HATE</em> hot dogs!</p>

安穩 2025-01-17 22:40:48

您应该使用内联元素,例如 。您可以向它们添加类,也可以仅使用 :nth-child 选择器。在这里使用 float 是完全错误的。也没有理由 position:absolutrelative

p :nth-child(1) {
  padding-top: 22px;
}

p :nth-child(2) {
  font-size: 40px;
}

p :nth-child(3) {
  font-size: 16px;
}
<p>
  <span>I</span>
  <span>HATE</span>
  <span>hot dogs!</span>
</p>

You should use inline-elements such as a <span>. You can either add classes to them or just use the :nth-child selector instead. Using float here is completely wrong. Also no reason for position: absolut or relative

p :nth-child(1) {
  padding-top: 22px;
}

p :nth-child(2) {
  font-size: 40px;
}

p :nth-child(3) {
  font-size: 16px;
}
<p>
  <span>I</span>
  <span>HATE</span>
  <span>hot dogs!</span>
</p>

再浓的妆也掩不了殇 2025-01-17 22:40:48

您可以使用span。所以...

HTML

<div class="container1">
   <p>I
      <span class="text-effect-1">HATE</span>
      <span class="text-effect-2">HOT DOGS</span>
   </p>
</div>

CSS

.container1 p{
  font-size: ---;
  font-weight: ---;
  color: ---;
}

.text-effect-1{
  font-size: ---;
  font-weight: ---;
  color: ----;
}

.text-effect-2{
  font-size: ---;
  font-weight: ---;
  color: ----;
}

You could use span. So...

HTML

<div class="container1">
   <p>I
      <span class="text-effect-1">HATE</span>
      <span class="text-effect-2">HOT DOGS</span>
   </p>
</div>

CSS

.container1 p{
  font-size: ---;
  font-weight: ---;
  color: ---;
}

.text-effect-1{
  font-size: ---;
  font-weight: ---;
  color: ----;
}

.text-effect-2{
  font-size: ---;
  font-weight: ---;
  color: ----;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文