仅缩进段落中的第一行文本?
我想缩进几个段落,尽管只是这些段落的第一行。
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用
text-indent
属性。jsFiddle。
Use the
text-indent
property.jsFiddle.
除了文本缩进之外,如果您想应用其他样式,还可以使用
:first-line
选择器。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.http://jsfiddle.net/Madmartigan/d4aCA/1/
使用 css 非常简单:
将在每个段落中创建 10 像素的缩进。
Very simple using css:
Will create an indentation of 10 pixels in every paragraph.
其他人提到了通过 CSS 实现此目的的最佳方法,但是如果您需要使用内联格式快速修复,只需使用以下代码:
来源: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:
Source: https://www.computerhope.com/issues/ch001034.htm
我在缩进段落的第一行(仅第一行)时也遇到问题,并尝试使用以下代码:
这不起作用。因此,我在 CSS 中创建了一个类,并在 html 中使用它,如下所示:
in CSS:
in html:
这就像一个魅力。我仍然不知道为什么第一个代码示例不起作用,并且我确实确保文本未对齐。
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:
This did not work. So, I created a class in my CSS and used it in my html as follows:
in CSS:
in html:
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.
给你:
对于 CSS 新手来说,没有看到明确的答案,所以这里有一个简单的答案。
Here you go:
Didn't see a clear answer for a CSS newbie, so here's an easy one.
首先缩进所有行 (1),然后缩进第一行 (2)
first indent all lines (1), than outdent the first line (2)
我遇到了同样的问题,只是我必须使用多个
标签。使用“text-indent”属性想要缩进所有
标记,但这不是我想要的。
我想将一个精美的报价图像添加到推荐列表中,每个报价的开头都有基于 CSS 背景的图像,文本位于图像的右侧。由于
text-indent
导致所有后续段落缩进,而不仅仅是第一段,因此我必须采取一些解决方法。如果您不想制作图像,则可以使用相同的方法。我通过首先在我想要缩进的段落的开头添加一个空 div 来完成此操作。接下来,我对其应用了较小的宽度和高度以创建不可见的框,最后应用了左浮动以使其与文本内联。如果您将其用于图像,请确保在右侧添加边距或将宽度设置得更宽一些以留出一些空白。
这是一个 CSS 内联示例。您可以轻松地创建一个类并将其添加到您的 CSS 文件中:
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: