Formatting HTML headings with ol list style

I have a site that runs on CMS and I need to style the content headings (probably H2 and H3) with unique ol li style. I need this style to be applied only to the headings not the other ol li inside the content. I’ve this code but I’m having trouble making it only to be assigned to the headings. its kind of applying the style to any ol that exist withing the content with a css class given.

.g1-content-narrow {
   counter-reset: item;
 }
 .g1-content-narrow ol {
   list-style: none;
   margin: 0;
   margin-left: 1em;
   padding: 0;
 }
.g1-content-narrow ol li {
   counter-increment: item;
   margin-bottom: 5px;
   position: relative;
margin-bottom: 1.5em;
padding: 0.5em;
background-color: #E7FFE8;
padding-left: 58px;
border-radius: 1em;
 }

.g1-content-narrow ol li:hover{
box-shadow:inset -15em 0 #76A706;
-webkit-transition: box-shadow 0.5s; /* For Safari 3.1 to 6.0 */
transition: box-shadow 0.5s;
}

.g1-content-narrow ol li:before {
position: absolute;
margin-right: 10px;
   content: counter(item);
   background: lightblue;
   border-radius: 100%;
   text-align: center;
   display: inline-block;
top: -0.3em;
left: -0.5em;
width: 1.8em;
height: 1.2em;
font-size: 2em;
line-height: 1.2;
font-weight: bold;
text-align: center;
color: white;
background-color: #76A706;
transform: rotate(-20deg);
-ms-transform: rotate(-20deg);
-webkit-transform: rotate(-20deg);
z-index: 99;
overflow: hidden;

}

Where can I add the H3 and H2 inside the above code to make it applicable to only the given headings?

Could you please post the HTML that goes with the above CSS, preferably as a working page.

1 Like

Here is an example of the HTML code

<div class="g1-content-narrow">
<ol>
<li><h3> lorem ipsum </h3></li>
<p> and the text of the h3 goes here....probably with more paragraphs </p>
<ol>
<li> sub topics of h3 above</li>
<li> another sub topics of h3 above</li>
<li>and another sub topics of h3 above</li>
</ol>
<li> <h3> lorem ipsum another heading </h3></li>
<p> and the text of the h3 goes here....probably with more paragraphs </p>
<ol>
<li> sub topics of h3 above</li>
<li> another sub topics of h3 above</li>
<li>and another sub topics of h3 above</li>
</ol>
</ol>
</div>

So i need the H3 ol li ONLY to have the above CSS style.
Thank you.

Why don’t you just target the h3’s with your CSS like this:

.g1-content-narrow ol li h3 {
      /* your css code here */
 }

Have you tried that yet?

Let me try. how about at the li:before where can i put h3?

.g1-content-narrow ol li:before {
position: absolute;
margin-right: 10px;
   content: counter(item);
   background: lightblue;
   border-radius: 100%;
   text-align: center;
   display: inline-block;
top: -0.3em;
left: -0.5em;
width: 1.8em;
height: 1.2em;
font-size: 2em;
line-height: 1.2;
font-weight: bold;
text-align: center;
color: white;
background-color: #76A706;
transform: rotate(-20deg);
-ms-transform: rotate(-20deg);
-webkit-transform: rotate(-20deg);
z-index: 99;
overflow: hidden;

}

I 'm having problem where to place H3 from all css above

This .g1-content-narrow ol li:before does not target the h3 inside the li, so I don’t think you need to worry about that one. Although I may be corrected by one of the CSS gurus. But the .g1-content-narrow ol li:hover should be .g1-content-narrow ol li:hover h3. Again the h3 comes after li, because it is nested inside the li.

Try just changing the two and see if you get the results you are looking for.

That is invalid HTML, run that example through the validator and you will find the following errors.

Element p not allowed as child of element ol in this context.
/h3></li>↩<p> and t

Element ol not allowed as child of element ol in this context.
aphs </p>↩<ol>↩<li>

I’m not a fan nesting heading tags within list items. The heading is not part of the list of items, it should introduce the list of items. :slight_smile:

You can retain the H3 association to the OL by nesting them in a <section> element. Then you can use counters to number your H3 headings.

If your using H3 headings make sure you have an H2 that is introducing them.

Something like this will be valid html and you will find that it will be easier to style the H3 as you are wanting to do.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Headings for Ordered List Items</title>
<style>
.g1-content-narrow {
  counter-reset: section;
}
.g1-content-narrow h3 {
  background: #eee;
  margin: 0 0 1em;
}
.g1-content-narrow h3::before {
  counter-increment: section;
  content: counter(section)".";
  padding-right: .3em;
}
</style>

</head>
<body>

<h1>Level 1 Heading</h1>

<div class="g1-content-narrow">
  <h2>Level 2 Heading</h2>
  <section>
    <h3>Level 3 Heading</h3>
    <p>and the text of the h3 goes here....probably with more paragraphs</p>
    <ol>
      <li>sub topics of h3 above</li>
      <li>another sub topics of h3 above</li>
      <li>and another sub topics of h3 above</li>
    </ol>
  </section>
  <section>
    <h3>Level 3 Heading</h3>
    <p>and the text of the h3 goes here....probably with more paragraphs</p>
    <ol>
      <li>sub topics of h3 above</li>
      <li>another sub topics of h3 above</li>
      <li>and another sub topics of h3 above</li>
    </ol>
  </section>
</div>

</body>
</html>

The code above will produce the following document outline when run through the validator.

Here is a SO thread on the same subject…
How to semantically add heading to a list

5 Likes

The html and the semantics are a bit too muddled up to give a clear answer to what the correct semantics and structure should be.
It is the content that should dictate which html elements are the right ones, then the spec will dictate how to validly structure them.
Don’t choose elements based on their default appearance.

This could well be a nested list, or it could be a sectioned article, or an article containing lists.
But as we only have dummy content to go on, we can only guess at the context of the page.

Only once you have valid, semantic html will you go to work with css and make it look the way you want.

4 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.