Help - Search - Members - Calendar
Full Version: text-indent equivalent on the right side
HTMLHelp Forums > Web Authoring > Cascading Style Sheets
Christian J
I'm trying to add decorational space after a right-aligned caption. Are there any CSS alternatives to using a non-breaking space entity like below?

CODE
p {
border: solid;
width: 100px;
text-align: right;
}

p img {
width: 100px;
display: block;
}

<p>
<img src="...">
Caption&nbsp;
</p>

CSS padding-right on the P will affect the IMG as well, while generated content doesn't work in IE7.
Frederiek
You could put a <span> around the Caption text and give that a padding-right. That won't affect the image.

Or are you trying to use less mark-up?
Christian J
Yes, less markup. cool.gif
asmith
What's the benefit of using less markup?
moo
You could restrict the width of the P. In most browsers the image will overflow outside of the p but the text will not.

hence:
p {border: solid; width: 80px; text-align:right;}
p img {width: 100px; display: block;}
will give you a pseudo 20px indent on the right.
moo
QUOTE(asmith @ Mar 12 2009, 04:31 PM) *

What's the benefit of using less markup?

Less markup = cleaner, faster.
Frederiek
As the image will spill out that way, that's not a very elegant option, IMO.

I only could achieve a similar effect as your & nbsp; (space added for forum rendering), Christian, with:
CODE
p:after {
    content: " ";
}

But I don't think that's enough space. I'm afraid you'll have to add some tag, which wouldn't hurt semantics, I suppose.
moo
QUOTE(Frederiek @ Mar 12 2009, 09:12 PM) *

As the image will spill out that way, that's not a very elegant option, IMO.

I only could achieve a similar effect as your & nbsp; (space added for forum rendering), Christian, with:
CODE
p:after {
    content: " ";
}

But I don't think that's enough space. I'm afraid you'll have to add some tag, which wouldn't hurt semantics, I suppose.

You're right, its a horrible solution!
Christian J
QUOTE(asmith @ Mar 12 2009, 05:31 PM) *

What's the benefit of using less markup?

I'm mostly asking out of curiosity. E.g. a SPAN wouldn't hurt anything and wouldn't be hard to add.
Christian J
QUOTE(Frederiek @ Mar 12 2009, 10:12 PM) *

As the image will spill out that way, that's not a very elegant option, IMO.

I only could achieve a similar effect as your & nbsp; (space added for forum rendering), Christian, with:
CODE
p:after {
    content: " ";
}

But I don't think that's enough space.

In my browsers the space collapses unless I add "display: inline-block". Then I can also give the block a width if I want:

CODE

p:after {
content: ' ';
display: inline-block;
width: 100px;
}

But alas IE7 doesn't support it (and probably not older versions of the other ones either).

moo
You could add padding-right to the <p> and offset the <img> using a negative margin.
Christian J
QUOTE(moo @ Mar 13 2009, 12:09 AM) *

You could add padding-right to the <p> and offset the <img> using a negative margin.

Seems to work! At least in the browsers I have. If you specify a P width you must subtract the padding from it though, which limits you to identical units for both width and padding+margin:

CODE
p {
width: 90px; /* 90+10=100 */
border: solid;
padding-right: 10px;
text-align: right;
}
p img {
width: 100px;
display: block;
margin-right: -10px;
}


Or you can make the P shrink to fit the IMG's width by e.g. floating it:

CODE
p {
float: left;
border: solid;
padding-right: 1em;
text-align: right;
}
p img {
width: 100px;
display: block;
margin-right: -1em;
}
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.