Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can I change the width of a div based on content (autosize)?

Posted on 2011-03-22
8
Medium Priority
?
204 Views
Last Modified: 2012-05-11
I have a content management site specific for school districts.  On the home page, there are buttons for each campus and there can be from 3 - 10 buttons.  

We want this to be entered within the width of the main content area but I can't figure out how to do this so that the results are automatic no matter how many buttons there are.

Is there a way to center a div and have the width autosize to match the width of the buttons?

Thanks!


0
Comment
Question by:lthames
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35193741
'<div align="center">...</div>' will normally do that.  What do you have to work with?  Can you post a link?
0
 
LVL 16

Expert Comment

by:rbudj
ID: 35193832
You will need to set the width of your div to 100%: <div style="width:100%">

You also give it a center value for text align: <div style="width:100%;text-align:center"></div>

This will cause the buttons to center in the div.
0
 

Author Comment

by:lthames
ID: 35194019
When I tried <div align="center">.....</div> the buttons all displayed in the center (as in on top of each other).  

region811.systemicplan.com  and look towards the bottom of the screen where there are 2 rows of buttons that need to be centered.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35194181
This little bit of code tells them to 'float left' which is effectively aligning to the left.  You would have to rewrite that and the HTML section to do what you want.

'float: left;' is used to put 'div's side by side starting at the left.  Normally, a div will occupy a 100% horizontal slice of the container it is in.  You can also do 'float: right;' but there isn't any 'float: center;' although some people keep trying it and hoping.  It's not a bad idea but it doesn't exist.
.schools-btn {
	width:108px;
	height:68px;
	background-image:url(../images/school-btn.jpg);
	background-repeat:no-repeat;
	margin:8px auto 0 auto;
	float:left;
}

Open in new window

0
 

Author Comment

by:lthames
ID: 35194246
I tried removing the float:left line, but that causes the buttons to stack on top of each other, as you can see right now at region811.systemicplan.com

Thanks for your input :)
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35194312
No, you need to change much more than just the one line.  It is easy to get either text or images to center but getting them both together and automatically resize for different amounts is going to take a little bit more effort.  Where is the info for the buttons coming from?  A database?
0
 

Author Comment

by:lthames
ID: 35194381
Yes, a database.  Is it other CSS lines that need to be changed or actual code in the php that builds the html that would need to be changed?

0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 35194421
Both need to be changed.  Actually the method of doing it will have to be changed to get them to center.  I noticed that there are different amounts of text for the buttons.  That can affect the way it works too.  If there is too much text, it will cause the container it's in to expand.  Sometimes that can throw off the alignment so you need to know what you're trying to put there.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question