0

Css question here: How do I set the width of a container just wide enough for how many characters are in the container?

Thanks for your help, -Matthew

Matthew905
  • 1,571
  • 3
  • 12
  • 7

3 Answers3

2

display: inline-block is an easy way to do this.

float: left is another option.

Make sure an explicit width is not being set, because that will disable the shrink-wrap behaviour.

http://jsfiddle.net/5jyAg/

thirtydot
  • 224,678
  • 48
  • 389
  • 349
  • 1
    It's funny, I usually say that myself. Anyway, you're correct. If IE7 is a concern, see: http://stackoverflow.com/questions/5838454/inline-block-doesnt-work-in-internet-explorer-7-6/5838575#5838575 – thirtydot Jun 10 '11 at 11:13
  • ..and the comment vanished into the ether. – thirtydot Jun 10 '11 at 11:14
  • It should be noted that < IE7 does not support inline-block on inline elements. (PS: I wasn't sure about it being < IE6 instead so I decided to remove the comment :) – Kevin Jun 10 '11 at 11:15
1

it depends on the container, if you use a span as the container, it will always be as wide as the characters in it.

but a span is inline element

Fender
  • 3,055
  • 1
  • 17
  • 25
0

You can try to put and even Padding into your style. For example if your markup looks like this.

<div>
     <p class="paddThis">Your text</p>
</div>

and in your style sheet set the padding of the right and/or left of the text your text will push the left and right border of your container a certain amount of pixels away from you text creating which should accomplish what you are trying to do.

Your style sheet might look like this

.paddThis
{
   padding: 0 5px 0 5px;
}
pat8719
  • 1,700
  • 1
  • 26
  • 47