Better CSS for Run-in Headings - css

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.
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)
Many other academic styles also have run-in headings — for example, Chicago's five heading levels; and Harvard Extension's "C" level headings.
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. 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"
Original answer:
Could we not just use inline on both and apply a left-margin to the heading?
Then for spacing we just add a 0.15em margin to the left of the run-in paragraph.
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.
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).
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.
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.
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.
Finally I have set all spacing in REM, then if someone increases their browser size it will scale appropriately. 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).
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.
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.... 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>

Related

div class works with css but span class doesn't?

I've tried marking up a letter, and I'm wondering why when I use span class, css is not implemented (date doesn't move to the left), but when I use div class, css is implemented. (date moves to the left). I assumed both should work but one is a block level element and the other is an inline. I changed this line:
<div class="receiver-column"> 20 January 2016 </div>
to
<span class="receiver-column">20 January 2016 </span>
Heres the html code:
body {
max-width: 800px;
margin: 0 auto;
}
.receiver-column {
text-align: right;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.3em;
}
p,
ul,
ol,
dl,
address {
font-size: 1.1em;
}
p,
li,
dd,
dt,
address {
line-height: 1.5;
}
<p class="receiver-column"><strong>Dr. Eleanor Gaye </strong> Awesome Science faculty <br> University of Awesome <br> Bobtown, CA 99999, <br> USA <br>
<strong>Tel</strong>: 123-456-7890 <br>
<strong>Email</strong>: no_reply#example.com </p>
<div class="receiver-column"> 20 January 2016 </div>
A spanelement is inline, and therefore it is only as wide as its content (i.e. typically the text it contains). Now if you apply text-align: right to it, even if the text is aligned right, that would be only within the span's width, which is only as wide as its content. So that simply won't make a difference.
Therefore, if you want to use text alignment, use a block element like div, p , h1, h2, h3 etc.
Span is by default display: inline element while Div is Display : block

Cannot determine difference in fonts

I am working on a website where I would like the paragraphs and h2 tags to have the same appearance visually.
I have looked through the code so many times but cannot see why they appear (ever so slightly) different.
I am hoping a fresh pair of eyes may be able to spot it. I want the h2 tag to be styled the same as the paragraphs.
The website is here.
h2{
padding-bottom: 5px;
color: #808080;
**letter-spacing: -1px;**
line-height: 1em;
font-weight: normal;}
have letter-spacing: -1px; for p element and it will appear the same
Your header tags have a letter spacing of -1px (defined in style.css on line 56):
h1, h2, h3, h4, h5, h6 { padding-bottom: 5px; color: #808080; letter-spacing: -1px; line-height: 1em; font-weight: normal; }
I was able to determine this using Chrome's developer tools by inspecting the h2 and the p tags in the Elements panel, and looking through the computed styles in the Styles tab to see what's different about them. I noticed the h2 has letter-spacing, whereas the p does not:
It's about CSS trick.
For example:
<style>
body {
font-size:12px;
line-height:1.100;
font-family:arial;
}
h2 {
font-size:12px;
font-weight:300;
line-height:1.100;
display:inline;
font-family:arial;
}
</style>
we need <h2>our product</h2>
The focus you should take notice is the size of font, font-weight and display. Hoe this helps
Demo here
If you need the h2 tag inside the p tag
DEMO

CSS: Adjacent sibling inside a class

Trying to add a top-margin to an adjacent sibling.
<div class="container">
<h2>Header 1</h2>
<p>Some text and sentences...</p>
<h2>Header 2</h2>
<p>Some more text and sentences..</p>
</div>
Normally I would accomplish top-margin on "Header 2" by using css on the adjacent siblings.
p + h2{
margin-top: 12px;
}
However, h2 has margin defined via
.container h2{
margin: 24px 0px;
}
How can I make the adjacent siblings css work while overriding the .container h2 css?
Try the following:
.container > p + h2{
margin: 12px;
}
That is:
Select h2 elements...
...which immediately follow a p element...
...which is a child of an element with class container.
Since .container > p + h2 contains 1 class and 2 elements, it will override .container h2, which only has 1 class and 1 element.
This should work
.container p + h2 {
margin-top: 12px;
}
.container h2 is more specific than p + h2 so it is taken into consideration. You need a more specific selector to override the margin.
This should be "more specific" and override:
.container p + h2 {
margin-top: 12px;
}

CSS: "AND" and + operator?

I want to put a space after all the headers using CSS. Like this:
if h1 = add a space after
else if h1 + h2 = add a space after also but no space in between
This is my HTML code
<article>
<h1>Title 1</h1>
...
</article>
<article>
<h1>Title 1</h1>
<h2>Title 2</h2>
...
</article>
For the CSS
h1, h2 { padding-bottom: 20px; }
The problem is, there is a space also between h1 and h2. I tried this code below but only those articles with h1 and h2 have a space after.
h1 + h2 { padding-bottom: 20px;}
Is there a way to do this? Or I should just use the h1 + h2 in CSS and add < br > for h1 only?
There's a few ways to do this, none pretty.
/* #1 */
h1 + *:not(h2),
h2 + *:not(h3) { /* etc */
padding-top: 20px;
}
/* #2 */
h1, h2 {
padding-bottom: 20px;
}
h1 + h2 {
margin-top: -20px;
}
(and variations of these.)
The problem is there is currently no way to select "backward," i.e. apply styles to an element based on what appears after it. There is only the ability to select "forward," using the + or ~ sibling combinators. So you can't override the style on an <h1> based on the existence of a subsequent <h2>.
In the CSS4 selectors draft, there is a method of specifying the subject of the selector, using the $ sign. In that case the code would look like:
h1, h2 {
padding-bottom: 20px;
}
$h1 + h2 {
padding-bottom: 0;
}
/* or even */
h1:not($h1 + h2), h2 {
padding-bottom: 20px;
}
This isn't currently available in any browsers, however.
Is there no way to know when generating the HTML if the h2 is following the h1? That seems like the best way to handle this. Then you can make a simple css rule for h2.follows_h1 { margin-top: -5px } or whatever fits exactly what you need.

Multiline Link Formatting Conundrum

On a webpage, I have markup like the following:
<h3>Title Goes here</h3>
Link goes here
<h3>Next title</h3>
Next link
Some of these links have very long text and span multiple lines. I would like the following to occur:
There is spacing between the first heading's link and the second heading.
Links that span multiple lines have all lines but the first indented.
The way that this is accomplished currently is through the following CSS:
h2 + a, h3 + a, h4 + a, h5 + a, h6 + a {
margin: 0px 30px 20px 5px;
line-height:1.4;
display: inline-block;
padding-left: 10px;
text-indent: -10px;
}
The problem comes in because our links have the following formatting:
a {
color: #900;
text-decoration: none;
border-bottom: 1px dotted #333;
}
a:hover {
border-bottom: 1px solid #900;
}
Since the links under the headings have display: inline-block, the border-bottom does not go under the text of each line, but rather under the whole box that the link generates. I'm not sure if there is way to get what I want here, since display:inline-block seems necessary to get the margins and indenting that I want, but the border-bottom would only work with an inline element.
Is there a way to have my cake and underline it too, without altering the markup (eg wrapping the <a> elements with <p>)?
Shouldn't you just need to alter the line-height to be lower than 1.4? If not, please provide a visual.
I was able to use position: relative and a negative margin to achieve the indenting effect without resorting to text-indent, which required the inline-block. I added margins to the headers instead of the links in order to create the necessary spacing. The CSS is as follows:
h2 + a, h3 + a, h4 + a, h5 + a, h6 + a {
line-height:1.4;
margin-left: -10px;
position: relative;
left: 15px;
}
a+h2, a+h3, a+h4, a+h5, a+h6 {
margin-top: 20px;
}

Resources