Indent starting from the second line of a paragraph with CSS

173,548

Solution 1

Is it literally just the second line you want to indent, or is it from the second line (ie. a hanging indent)?

If it is the latter, something along the lines of this JSFiddle would be appropriate.

    div {
        padding-left: 1.5em;
        text-indent:-1.5em;
    }
    
    span {
        padding-left: 1.5em;
        text-indent:-1.5em;
    }
<div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</div>

<span>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</span>

This example shows how using the same CSS syntax in a DIV or SPAN produce different effects.

Solution 2

This worked for me:

p { margin-left: -2em; 
 text-indent: 2em 
 }

Solution 3

Make left-margin: 2em or so will push the whole text including first line to right 2em. Than add text-indent (applicable to first line) as -2em or so.. This brings first line back to start without margin. I tried it for list tags

<style>
    ul li{
      margin-left: 2em;
      text-indent: -2em;
    }
</style>

Solution 4

There is a CSS3 working draft that will (hopefully soon) allow you to write just:

p { text-indent: 200px hanging; }

Keep an eye on: https://developer.mozilla.org/en-US/docs/Web/CSS/text-indent

Solution 5

I needed to indent two rows to allow for a larger first word in a para. A cumbersome one-off solution is to place text in an SVG element and position this the same as an <img>. Using float and the SVG's height tag defines how many rows will be indented e.g.

<p style="color: blue; font-size: large; padding-top: 4px;">
<svg height="44" width="260" style="float:left;margin-top:-8px;"><text x="0" y="36" fill="blue" font-family="Verdana" font-size="36">Lorum Ipsum</text></svg> 
dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
  • SVG's height and width determine area blocked out.
  • Y=36 is the depth to the SVG text baseline and same as font-size
  • margin-top's allow for best alignment of the SVG text and para text
  • Used first two words here to remind care needed for descenders

Yes it is cumbersome but it is also independent of the width of the containing div.

The above answer was to my own query to allow the first word(s) of a para to be larger and positioned over two rows. To simply indent the first two lines of a para you could replace all the SVG tags with the following single pixel img:

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" style="float:left;width:260px;height:44px;" />
Share:
173,548
Reuben
Author by

Reuben

Serial social entrepreneur, community builder, web/software design/dev, addicted to starting things, unlimited energy, compulsive problem solver. Via St. Pete! http://reubenpressman.com http://facebook.com/reubenpressman http://twitter.com/reubenpressman

Updated on June 12, 2020

Comments

  • Reuben
    Reuben about 4 years

    How can I indent starting from the second line of a paragraph?

    I've tried

    p {
        text-indent: 200px;
    }
    p:first-line {
        text-indent: 0;
    }
    

    and

    p {
        margin-left: 200px;
    }
    p:first-line {
        margin-left: 0;
    }
    

    and

    (with position:relative;)
    p {
        left: 200px;
    }
    p:first-line {
        left: 0;
    }
    
  • redditor
    redditor about 11 years
    @Reuben Based on your comment which has now been deleted, I assume you did in face mean FROM the second line - and for the sake of future visitors, perhaps this fiddle is better, where the syntax is P not div or span. jsfiddle.net/gg9Hx
  • EGL 2-101
    EGL 2-101 over 9 years
    welcome to Stackoverflow. Looks like you have some solution to this indentation problem. However it would be great help to everybody if you provide some markup and explain the solution. May be you can use jsfiddle.net or other service to create a working demo. All the best.
  • Don Cheadle
    Don Cheadle over 8 years
    Why the padding-left and then negative text-indent? Why must it work like that? Seems odd. Why not just a positive text-indent?
  • Alexander Holsgrove
    Alexander Holsgrove over 7 years
    This seems to do the complete opposite of what the OP asked. @techillage's version is correct. You need to swap the minus around
  • Smilediver
    Smilediver about 7 years
    The text-indent has no effect on the span. It can be removed from the span's styling for the same result.
  • Stu Furlong
    Stu Furlong over 6 years
    This is almost exactly what I needed. The accepted answer won't work for me because I do not have the freedom in my CMS to add spans that way. The only problem I had with the solution here, is that the margin left will pull the entire paragraph outside of a container. So I added 'position:relative' and 'left:2em' and it is perfect. Contrary to @AlexHolsgrove 's comment, techillage's answer did not work for me at all, but maybe because I am not doing this on a list element.