How to add section numbers (1.2, 3.4.1) automatically using CSS?


Tags: html,css,formatting

Problem :

How to add section numbers (1.2, 3.4.1) automatically using CSS?

I currently have

h1, h2, h3, h4 {
  font-weight: normal;
}
h1 { font-size: 140%; }
h2 { font-size: 120%; color:#049;}
h3 { font-size: 110%; color:#06C;}
h4 { font-size: 105%; color:#09C;}

How to modify them so section numbers such as 1.3, 2.4.5 are automatically constructed depending on the nesting level and order of appearance of the section headers?

...

<h2>heading21</h2>
...

<h3>heading31</h3>
...

<h2>heading22</h2>

should show

  1. heading21

    1.1 heading31

  2. heading22

or something along those lines.



Solution :

Hey now you can used CSS counter-increment Property

As like this Css

   body {counter-reset:section;}
    h1 {counter-reset:subsection;}
    h1:before
    {
    counter-increment:section;
    content:"Section " counter(section) ". ";
        font-weight:bold;
    }
    h2:before
    {
    counter-increment:subsection;
    content:counter(section) "." counter(subsection) " ";
    }

HTML **

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>
<h1>Heading </h1>
<h2>One </h2>
<h2>Two</h2>

**

Live demo http://jsfiddle.net/PfcX2/1/

More info click here https://developer.mozilla.org/en-US/docs/Web/CSS/counter-increment


    CSS Howto..

    CSS - How to set height of absolute div same with neighbor image?

    How do i divide my webpage into 3 columns with css, frameset in html makes up too many pages to handle, how can i do it in css?

    multiselect.js via gem multi-select-rails - how best to set width

    How to correct div overlapping the title

    How to give outer glow effect to column in HTML/CSS?

    how to create a table layout with float in css

    How to apply CSS width to a submit input on Firefox?

    HTML/CSS - How to position my image to the right so that it can be aligned with my text on the left?

    nth-of-type css, how to setup it properly

    Telerik RadEditor for MOSS - How do I suppress min-width inline CSS?

    How can I center my

    when there is an next to it?

    How do browsers read and interpret CSS?

    problem with internet explorer showing html input button which is hidden

    How apply css to div in catch statement using jquery

    How to assign color obtained by CSS gradient generator

    How to make a div scroll able when window height is reached

    CSS & Button: How to override native CSS of html buttons element?

    How to have anchors to the side of the page? [closed]

    How to unfold or close content in html/JS - always assigned to different id

    How to add css into .scss file or theme

    How to show and hide fieldset content on click of the legend

    How to create inheritance with LESS?

    How to hide specific text from content wihtout calling any CSS?

    CSS: How to position elements in ng-for loop using position : “fixed”

    How to change the opacity background color of other two buttons once we click on a single button using css

    how to set width of a div in percentage minus certain pixels using javascript or css

    How to store “state” in CSS only?

    How to place 2 div side by side (alternative solution)

    How To Display first 50 characters Of Text [closed]

    How do you modify the modal height without removing the slide effect in Bootstrap Twitter?