段落第一行后的文本缩进

发布于 2024-09-01 09:01:28 字数 572 浏览 15 评论 0原文

- 楠格哈尔省苏克罗德地区的一名路透社记者称,袭击发生在该地区,村民称袭击发生在当地,并称在一些人开始向当地政府大楼投掷石块后,阿富汗警方向人群开枪。

在上面的段落中,我想使用 CSS 使第一行之后的所有行自动缩进一些空格,以便每一行都保留在第一行中的 - 之后。

HTML

<p> - A Reuters reporter in Surkhrod district in Nangarhar province, where 
villagers said the raids took place, said Afghan police fired at the crowd 
after some of them started throwing stones at local government buildings.</p>

它类似于列表位置设置为外部的列表项,但我不想使用列表。

您能想到实现这种效果的最简单方法是什么?额外的 html 标记越少越好。

非常感谢大家。

- A Reuters reporter in Surkhrod district in Nangarhar province, where villagers said the raids took place, said Afghan police fired at the crowd after some of them started throwing stones at local government buildings.

In the above paragraph, I would like to use CSS to make all lines after the first line to automatically indent some space so that each line stays right after the - in the first line.

HTML

<p> - A Reuters reporter in Surkhrod district in Nangarhar province, where 
villagers said the raids took place, said Afghan police fired at the crowd 
after some of them started throwing stones at local government buildings.</p>

It's similar to a list item with list position set to outside, but I don't want to use a list.

What is the simplest way you can think of to achieve this effect? Less extra html markups will be better.

Many thanks to you all.

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

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

发布评论

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

评论(5

鹤仙姿 2024-09-08 09:01:28

您需要 text-indent。通常 text-indent 会将第一行向内推,但如果你给它一个负数并使用正边距,你就可以达到你想要的效果。

text-indent: -10px;
margin-left: 10px

You need text-indent. Normally text-indent pushes the first line inwards, but if you give it a minus figure and use a positive margin, you can achieve the effect you're after.

text-indent: -10px;
margin-left: 10px
我不在是我 2024-09-08 09:01:28
p {
  text-indent: -1em;
  padding-left: 1em;
}
p {
  text-indent: -1em;
  padding-left: 1em;
}
梦旅人picnic 2024-09-08 09:01:28

目前不可用,但如果您在 Chrome 上激活“实验性 Web 平台”标志(通过转到 chrome://flags/#enable-experimental-web-platform-features ),您可以使用 text-indent:1em every-line; 这应该完全符合你的要求。

MDN 上的文本缩进

Not usable right now but if you activate the "Experimental Web platform" flag (by going to chrome://flags/#enable-experimental-web-platform-features ) on Chrome you can use text-indent:1em each-line; which should do exactly what you want.

text-indent on MDN

不醒的梦 2024-09-08 09:01:28

我注意到,在内联元素上它的工作方式可能有所不同。我需要一个“outdent”(第一行比段落其余部分更左边),并注意到上面的建议做了相反的事情——创建了一个常规缩进(其中第一行比其他行更右边)。以下是适用于内联元素的内容,例如“a”标签:

#myDiv ul li { margin-left:1em; }
#myDiv ul li a { color:#333; text-indent:0.5em; margin-left:-1em; line-height:1; }

我还将包含的块级元素设置为具有 1em 的边距,以便内联“a”元素开始的位置具有负边距,他们实际上准确地定位了我在乱搞“突出”之前原本应该放置的位置。

希望这对某人有帮助!我还注意到我的内联元素上的文本缩进本身没有大小控制......

JSFiddle 示例< /a>

On inline elements maybe it works differently, I have noticed. I was needing an "outdent" (first line further left than rest of paragraph) and noticed the suggestions above did the opposite -- created a regular indent (where first line is further RIGHT than other lines). Here is what works for an inline element, for example, an "a" tag:

#myDiv ul li { margin-left:1em; }
#myDiv ul li a { color:#333; text-indent:0.5em; margin-left:-1em; line-height:1; }

I also set the containing block-level element to have a margin of 1em, so that where the inline "a" elements commence, with their negative margins, they actually position exactly where I things would have been originally before messing around to make an "outdent".

Hope this helps someone! I also noticed there's no size control on the text-indent itself on my inline element, either....

JSFiddle Example

蓝礼 2024-09-08 09:01:28

以@kennytm 的答案为基础;如果要对齐等宽文本,请使用 ch 单位而不是 em 单位。例如:

<div class="query-select">SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10</div>

使用基于 ch 的填充/负缩进

.query-select {
  padding-left: 8ch;
  text-indent: -7ch;
} 

将呈现(基于页面宽度)为:

  SELECT Field1, Field2, Field3, Field4, Field5, Field6, 
         Field7, Field8, Field9, Field10

Building on @kennytm's answer; if you want to align monospaced text, use ch units instead of em units. For example:

<div class="query-select">SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10</div>

with ch-based padding/negative indents

.query-select {
  padding-left: 8ch;
  text-indent: -7ch;
} 

Will render (based on page width) as:

  SELECT Field1, Field2, Field3, Field4, Field5, Field6, 
         Field7, Field8, Field9, Field10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文