简体   繁体   English

更好的 CSS 用于插入标题

[英]Better CSS for Run-in Headings

Edit 3/18/22: I recently checked several Terms of Use and similar documents and found run-in headings are frequent in this type of document but always incorrectly formatted as <strong> rather than as headings, so this issue appears to be much broader than academia style guides.编辑 22 年 3 月 18 日:我最近检查了几个使用条款和类似文档,发现此类文档中经常出现插入标题,但总是错误地格式化为<strong>而不是标题,所以这个问题似乎很多比学术风格指南更广泛。


I have been wrestling with the format of run-in headings for years.多年来,我一直在努力解决插入式标题的格式问题。 This is not a minor problem, because one of the top styles in academia and scholarly work, APA, requires (APA is strict, not flexible like some style guides) run-in headings for both level 4 and 5 headings ( https://apastyle.apa.org/style-grammar-guidelines/paper-format/headings )这不是一个小问题,因为学术界和学术工作中最重要的 styles 之一 APA要求(APA 是严格的,不像某些风格指南那样灵活)第 4 级和第 5 级标题的插入标题( https:// apastyle.apa.org/style-grammar-guidelines/paper-format/headings )

Many other academic styles also have run-in headings — for example, Chicago's five heading levels ;许多其他学术 styles 也有磨合标题——例如, 芝加哥的五个标题级别 and Harvard Extension's "C" level headings .Harvard Extension 的“C”级标题

I used to just format run-in headings as bold, but that does not correctly interpret the document structure for assistive technologies.我过去只是将插入标题的格式设置为粗体,但这并不能正确解释辅助技术的文档结构。 So I now use the CSS below, since display: run-in is not currently a CSS option.所以我现在使用下面的 CSS,因为display: run-in目前不是 CSS 选项。 The results are glitchy.结果是小故障。

For instance, a break appears after the heading when the heading wraps on the browser page.例如,当标题在浏览器页面上换行时,标题后会出现一个中断。 Any suggestions for better code?对更好的代码有什么建议吗?

H4.enpara {
  text-indent: 2em;
  display: inline-block;
  break-before: column;
  margin-bottom: -1em;
}

H4.enpara + P {
  display: inline;
  margin: 0 0 .7em 0;
}

Original Answer原始答案

Note: the concept was correct, but it had flaws, see the h2 heading "Edit, it was more complicated than I thought"注意:这个概念是正确的,但它有缺陷,见h2标题“编辑,它比我想象的更复杂”

Original answer: Could we not just use inline on both and apply a left-margin to the heading?原始答案:我们不能只在两者上都使用inline并在标题上应用左边距吗?

Then for spacing we just add a 0.15em margin to the left of the run-in paragraph.然后对于间距,我们只需在插入段落的左侧添加0.15em的边距。

The only down side is that you can end up with a double space as an extra character is automatically added for an inline paragraph and heading in Chrome.唯一的缺点是您最终可能会得到双倍空格,因为在 Chrome 中会自动为行内段落和标题添加一个额外的字符。

The only solution I have for that would be to add a spacer class to headings that take up more than one line manually (the margin-left ).我唯一的解决方案是将间隔符 class 添加到手动占用多行的标题( margin-left )。

It is minor though as with a bit of fine-tuning you can see it is barely noticeable.尽管稍作微调,您会发现它几乎不引人注意,但它很小。 It is still more accessible than using bold as you pointed out and I don't think the spacing difference is large enough to cause any problems to people with dyslexia etc.正如您所指出的那样,它仍然比使用粗体更容易访问,而且我认为间距差异不会大到足以给有阅读障碍等的人带来任何问题。

 h4, h5 { margin-left: 2em; display: inline; } h4 + p, h5 + p { display: inline; margin-left: 0.15em; }
 <h4>a long heading that will wrap onto two lines to ensure that browser wrapping does not break our run-in system</h4><p>a paragraph</p> <p>another paragraph that does not have a run-in</p> <h4>Another heading</h4> <p>another paragraph with a run-in.</p> <p>a longer paragraph to ensure there isn't an issue with the run-in happening for the heading itself on a paragraph <h5>a heading level 5 to test it works for that also</h5> <p>a further paragraph that should have a run-in also</p>

Edit, it was more complicated than I thought编辑,这比我想象的要复杂

The above kind of works, but you can get run-in where it is not intended if you happen to only have a single paragraph after a h4 or h5 and then use another h4 or h5 .上述类型的作品,但如果你碰巧在h4h5之后只有一个段落然后使用另一个h4h5 ,你可能会在不想要的地方磨合。

I may have made a mistake and this is not extensively tested, but this appears to be the correct formatting requirements for all heading levels.我可能犯了一个错误并且没有经过广泛测试,但这似乎是所有标题级别的正确格式要求。

I had to use a couple of tricks to account for run-in on a h4 followed by a h5 with only a single paragraph and to counter a spacing issue that introducing the ::after pseudo element caused.我不得不使用一些技巧来解决 h4 后接 h5 只有一个段落的插入问题,并解决引入::after伪元素引起的间距问题。

I think the below behaves correctly, but you would need to test it extensively as I said.我认为下面的行为是正确的,但你需要像我说的那样广泛地测试它。

The only thing it doesn't cover is correcting Title Case as that isn't possible without JS as far as I am aware.它唯一没有涵盖的是更正标题大小写,因为据我所知,如果没有 JS,这是不可能的。

Finally I have set all spacing in REM, then if someone increases their browser size it will scale appropriately.最后,我在 REM 中设置了所有间距,然后如果有人增加他们的浏览器大小,它会适当缩放。 The only thing that you may have to look at there is whether a 2rem margin on the left works or whether that should be a fixed amount (if you quadrupled the font size for example the left margin would be too large probably).您可能需要注意的唯一一件事是左边的 2rem 边距是否有效,或者它是否应该是一个固定的数量(如果您将字体大小增加四倍,例如左边距可能会太大)。

The fix would be to set the left margin in a fixed unit such as px , I will leave that up to you to investigate / decide.解决方法是将左边距设置为固定单位,例如px ,我将由您自行调查/决定。

 h1, h2, h3, h4, h5{ font-size: 1rem; font-weight: bold; } h1{ text-align: center; } h3, h5{ font-style: italic; } h4, h5 { margin-left: 2rem; display: inline; } h4 + p::after, h5 + p::after{ content: ""; height: 1rem; display: block; clear: both; } h4 + p, h5 + p { display: inline; margin-left: 0.15rem; } h4 + p + p, h4 + p + h1, h4 + p + h2, h4 + p + h3, h4 + p + h4, h4 + p + h5, h5 + p + p, h5 + p + h1, h5 + p + h2, h5 + p + h3, h5 + p + h4, h5 + p + h5{ margin-top: 0rem; }
 <h1>H1 - Centered, Bold, Title Case Heading</h1> <p>Text begins as a new paragraph.</p> <h2>H2 - Flush Left, Bold, Title Case Heading</h2> <p>Text begins as a new paragraph.</p> <h3>H3 - Flush Left, Bold Italic, Title Case Heading</h3> <p>Text begins as a new paragraph.</p> <h4>H4 - Indented, Bold, Title Case Heading, Ending With a Period.</h4> <p>Text begins on the same line and continues as a regular paragraph.</p> <h5>H5 - Indented, Bold Italic, Title Case Heading, Ending With a Period.</h5> <p>Text begins on the same line and continues as a regular paragraph.</p> <h2>H2 - Flush Left, Bold, Title Case Heading</h2>

@Graham Ritchie 's answer seems to work well, but not a fan of the h4 + p + p, h4 + p + h1... . @Graham Ritchie的回答似乎很有效,但不是h4 + p + p, h4 + p + h1...的粉丝。 Hopefully this works well for you:希望这对你有用:

 h1, h2, h3, h4, h5 { font-size: 1rem; font-weight: bold; } p { text-indent: 2rem; } h1 { text-align: center; } h3, h5 { font-style: italic; } h4, h5 { display: inline; margin-inline-start: 2rem; } h4 + p, h5 + p { display: inline; margin-inline-start: 0.5rem; } h4 + p::after, h5 + p::after { content: ""; display: block; margin: 1rem; }
 <h1>H1 heading</h1> <p>Text following h1</p> <h2>H2 heading</h2> <p>Text following h2</p> <h3>H3 heading</h3> <p>Text following h3</p> <h4>H4 heading</h4> <p>Text following h4</p> <p>Text following h4 + p</p> <h4>H5 heading</h4> <p>Text following h5</p> <h5>H5 heading following h5 + p</h5> <p>Text following h5</p> <h2>H2 heading following h5 + p</h2> <p>Text following h2</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM