仅缩进段落中的第一行文本?

发布于 2024-11-04 08:39:08 字数 61 浏览 1 评论 0原文

我想缩进几个段落,尽管只是这些段落的第一行。

如何使用 CSS 或 HTML 只定位第一行?

I have several paragraphs that I would like to indent, although only the first lines of these paragraphs.

How would I target just the first lines using CSS or HTML?

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

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

发布评论

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

评论(8

那一片橙海, 2024-11-11 08:39:08

使用 text-indent 属性。

p { 
   text-indent: 30px;
}

jsFiddle

Use the text-indent property.

p { 
   text-indent: 30px;
}

jsFiddle.

百善笑为先 2024-11-11 08:39:08

除了文本缩进之外,如果您想应用其他样式,还可以使用 :first-line 选择器。

p:first-line {
    color:red;
}

p {
    text-indent:40px;
}

http://jsfiddle.net/Madmartigan/d4aCA/1/

In addition to text-indent, you can use the :first-line selector if you wish to apply additional styles.

p:first-line {
    color:red;
}

p {
    text-indent:40px;
}

http://jsfiddle.net/Madmartigan/d4aCA/1/

画骨成沙 2024-11-11 08:39:08

使用 css 非常简单:

p {
    text-indent:10px;
}

将在每个段落中创建 10 像素的缩进。

Very simple using css:

p {
    text-indent:10px;
}

Will create an indentation of 10 pixels in every paragraph.

书间行客 2024-11-11 08:39:08

其他人提到了通过 CSS 实现此目的的最佳方法,但是如果您需要使用内联格式快速修复,只需使用以下代码:

<p style="text-indent: 40px">This text is indented.</p>

来源:https://www.computerhope.com/issues/ch001034.htm

Others have mentioned the best way to implement this via CSS, however if you need a quick fix with inline formatting, simply use the following code:

<p style="text-indent: 40px">This text is indented.</p>

Source: https://www.computerhope.com/issues/ch001034.htm

诗酒趁年少 2024-11-11 08:39:08

我在缩进段落的第一行(仅第一行)时也遇到问题,并尝试使用以下代码:

p::first-line { text-indent: 30px; }

这不起作用。因此,我在 CSS 中创建了一个类,并在 html 中使用它,如下所示:

in CSS:

.indent { text-indent: 30px; }

in html:

<p class="indent"> paragraph text </p>

这就像一个魅力。我仍然不知道为什么第一个代码示例不起作用,并且我确实确保文本未对齐。

I was also having a problem getting the first line of a paragraph (only the first line) to indent and was trying the following code:

p::first-line { text-indent: 30px; }

This did not work. So, I created a class in my CSS and used it in my html as follows:

in CSS:

.indent { text-indent: 30px; }

in html:

<p class="indent"> paragraph text </p>

This worked like a charm. I still don't know why the first code example did not work and I did make sure that the text was not aligned.

海未深 2024-11-11 08:39:08

给你:

p:first-line {
    text-indent:30px;
}

对于 CSS 新手来说,没有看到明确的答案,所以这里有一个简单的答案。

Here you go:

p:first-line {
    text-indent:30px;
}

Didn't see a clear answer for a CSS newbie, so here's an easy one.

︶葆Ⅱㄣ 2024-11-11 08:39:08

首先缩进所有行 (1),然后缩进第一行 (2)

padding-left: 0.4em /* (1) */
text-indent: -0.4em /* (2) */

first indent all lines (1), than outdent the first line (2)

padding-left: 0.4em /* (1) */
text-indent: -0.4em /* (2) */
微暖i 2024-11-11 08:39:08

我遇到了同样的问题,只是我必须使用多个

标签。使用“text-indent”属性想要缩进所有

标记,但这不是我想要的。

我想将一个精美的报价图像添加到推荐列表中,每个报价的开头都有基于 CSS 背景的图像,文本位于图像的右侧。由于 text-indent 导致所有后续段落缩进,而不仅仅是第一段,因此我必须采取一些解决方法。如果您不想制作图像,则可以使用相同的方法。

我通过首先在我想要缩进的段落的开头添加一个空 div 来完成此操作。接下来,我对其应用了较小的宽度和高度以创建不可见的框,最后应用了左浮动以使其与文本内联。如果您将其用于图像,请确保在右侧添加边距或将宽度设置得更宽一些以留出一些空白。

这是一个 CSS 内联示例。您可以轻松地创建一个类并将其添加到您的 CSS 文件中:

<div style="height: 25px; width: 25px; float: left;"></div>
<p>First Paragraph</p>
<p>Second Paragraph</p>

I ran into the same issue only I had multiple <p> tags I had to work with. Using the "text-indent" property wanted to indent ALL of the <p> tags and that's not what I wanted.

I wanted to add a fancy quote image to a list of testimonials, with the css background based image at the very beginning of each quote and the text sitting to the right of the image. Since text-indent was causing all subsequent paragraphs to indent, not just the very first paragraph, I had to do a bit of a workaround. The same method applies if you aren't looking to do an image though.

I accomplished this by first adding an empty div to the beginning of the paragraph I wanted indented. Next I applied a small width and height to it to create the invisible box and finally applied a left float to make it flow inline with the text. If you are using this for an image, make sure to add a margin to the right or make your width a bit wider for some white space.

Here's an example with the CSS inline. You can easily just create a class and add it to your CSS file:

<div style="height: 25px; width: 25px; float: left;"></div>
<p>First Paragraph</p>
<p>Second Paragraph</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文