同一段落内有多种样式
我试图弄清楚是否有一种方法可以以多种方式设置段落样式,以便格式具有响应性并且不同样式的文本保持在同一行。我是初学者,已经搜索了其他答案但无法弄清楚,所以如果答案很明显,请原谅我。
例如:我讨厌热狗!
“我”需要为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如有些人已经说过的,在这种情况下,您不会使用
,而是需要
,这将允许在同一
内使用不同的样式。您可以使用 CSS 定义特定的文本样式:
定义字体大小的 CSS 类
内联样式
这种方法可能看起来更短,但在设计整个段落时效果就不太好。
编辑(@tacoshy 注释): 也不应在电子邮件模板之外使用。这将导致主要的特定权重问题,导致可读性低并且无法缓存,从而导致加载时间更长。
请记住,您还可以设置其他样式,如字体颜色、斜体、粗体、背景颜色等。
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
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.
Remember you can also set other styles as font color, italic, bold, background color, etc.
在这种情况下,您不会使用
。您将使用
元素。它旨在将不同的样式应用于同一段落。这是您在问题中给出的示例:
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:没有人注意到的一件事是,您可以(并且应该,IMO)在可能的情况下使用本机语义 HTML 元素,在失败时回退到通用 HTML 元素。
不需要两个以上的元素;包含的段落,以及强调的“仇恨”一词。请注意,我已在根级别 (
html
) 定义了 16px 字体,并相对于此定义了 40px (2.5rem
)。这样,如果用户改变了字体大小,仇恨仍然会相对较大。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.您应该使用内联元素,例如
。您可以向它们添加类,也可以仅使用
:nth-child
选择器。在这里使用 float 是完全错误的。也没有理由position:absolut
或relative
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 forposition: absolut
orrelative
您可以使用
span
。所以...HTML
CSS
You could use
span
. So...HTML
CSS