views:

1097

answers:

5

Hi all,

So I have a DIV that contains some dynamic text. Let's say I know the text and font-size but I don't know the size of the DIV. I'd like the display of the text in the DIV to be intelligent enough to show indentation when text wraps.

Say my original text looked something like this:

Lorem ipsum dolor sit amet, 
consectetur adipisicing 
elit, sed do eiusmod 
tempor incididunt

Instead, I want it to look like this:

Lorem ipsum dolor sit amet, 
   consectetur adipisicing 
   elit, sed do eiusmod 
   tempor incididunt

What's the best way to do this if I don't know the size of the DIV a priori? And what's the best way to do it if I do know the size?

Thanks!

+7  A: 

If I understand what you're asking for, this works for me:

div {
    padding-left: 2em;
    text-indent: -2em;
}
Allain Lalonde
A: 

Have you tried 'margin-left'?

<div>
Lorem ipsum dolor sit amet,
<div margin-left="5px"> consectetur adipisicing 
   elit, sed do eiusmod 
   tempor incididunt
</div>
</div>
Dave Swersky
There's no need to add extra markup.
davethegr8
A: 

This should work equally well for both variable and fixed size DIVs.

<div style="width: 150px; text-indent: -2em; padding-left: 2em;">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
    incididunt.
</div>
Noldorin
I've tested the code on Firefox 3 and IE7 and it seems to render perfectly well on both.
Noldorin
A: 

Not sure of the cross-browser support, but you could use the first-line pseudo-element:

p {padding:10px;}
p:first-line {padding-left:0px;}

<p>Hello World, I'm Jonathan Sampson</p>

Would be diplayed as

Hello World I'm
    Jonathan
    Sampson

Other than that, you could give the element left-padding, and then a negative text-indent.

Jonathan Sampson
+2  A: 

W3C says you just have to use text-indent property.

source

.indentedtext
{
    text-align:start;
    text-indent:5em;
}
lucian.jp
Supported on IE6?
Allain Lalonde
W3Schools say it works since IE4 source : http://www.w3schools.com/css/css_text.aspSome site on the web suggest to make text-indent correctly tho in IE6 you need to apply it with text-align:left; and display:block;
lucian.jp