Jump to content

center an auto-width div?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
jimiwa

jimiwa

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi, I'm having trouble because I have a div I want to center and what I have
usually been told to do is this:

width: 700px;
margin-left: auto;
margin-right: auto;

the trouble is, this is for if you want the div to be a fixed width. I want the div
to adjust its size based on the text in the div, and still be centered. I tried:

width: auto;
margin-left: auto;
margin-right: auto;

but this didn't work. It stretches the div when I do this.

Anyone know what to do here?

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
Which way do you want the size to adjust, vertically, horizontally, or both?
sudo rm -rf /

#3
InitVI

InitVI

    Newbie

  • Members
  • PipPip
  • 11 posts
This might work. I haven't tested it but in theory it should.

CSS:
#wrapper{
text-align:center;
}
#content{
width: auto; 
display: inline;
text-align:left;
}

HTML:
<div id="wrapper">
<div id="content">
CENTERED DIV
</div>
</div>


#4
jimiwa

jimiwa

    Newbie

  • Members
  • PipPip
  • 14 posts
InitVI: I tried it, and actually the div stretches the whole page that way, if you put a background color,
you can see that this happens.

dargueta: I want the size to adjust horizontally, but I would like to know how to do it either way.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
Try these:

How do I center a div and make it expand around the divs inside of it? - Stack Overflow
100% vertically expanding div - HTML / CSS answers
sudo rm -rf /