LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2009, 10:53 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
CSS: get div to expand with content


I am doing some CSS stuff and I have a div which I want to center using "margin: auto". However, the div automatically expands (horizontal) to fill the parent. I would like a way to make it just as wide as it needs to be, according to the content inside it (which will be normal text and <a>s).
 
Old 10-05-2009, 11:50 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Ephracis View Post
I am doing some CSS stuff and I have a div which I want to center using "margin: auto". However, the div automatically expands (horizontal) to fill the parent. I would like a way to make it just as wide as it needs to be, according to the content inside it (which will be normal text and <a>s).
Use float:left or float:right to accomplish this. This approach allows other content to flow around the DIV (that may be a good or bad thing).

Look at this page: Learning Sage and scroll down until you see a yellow box at the right entitled "Functions". That's a DIV that is floated right. Its CSS definition looks like this:

Code:
.sidebox {
  width:50%;
  float:right;
  padding:8px;
  border:1px solid black;
  background-color:#ffffe0;
  margin-left:8px;
  margin-top:8px;
  margin-bottom:8px;
}
But centering a DIV is more difficult (CSS entities naturally want to spread across the display). I sometimes use percentage margins along with percentage widths to accomplish this.

Here's my suggestion: visit Web sites until you see the effect you want. Look at the CSS. Copy it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
html / css footer <div> won't behave esteeven Programming 2 08-02-2009 08:04 AM
css div - fixed size + center placement Ephracis Programming 6 12-05-2008 05:54 PM
CSS div layout Chronothread Programming 1 09-29-2008 06:00 PM
simplest way to center a <div> on the screen via css tanoatlq Programming 4 06-02-2008 02:50 AM
Trying to create overlapping CSS div boxes. rose_bud4201 Programming 2 05-13-2007 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration