Solved

How do I get a div's width to display (and expand) based upon the text?

Posted on 2011-03-25
10
251 Views
Last Modified: 2012-05-11
I have created a css box-like display with borders, etc. (ie.: <div><div><div> </div</div></div>).  The outer div is currently displaying the full width of the page.  I only want the "box" to display with a bit of padding to the text.  Without setting a specific width for the outer div, how do I get the outer div's width to display (and expand) based upon the text? I'm trying to avoid using an additional "wrap" div to set a specific width.

.subTitleOuter {
	padding:2px 0 2px 0;
	background-color:#bf2f00;
	border:1px solid #811d05;
}
.subTitleInner {
	border-top:1px solid #d36f37;
	border-bottom:1px solid #d36f37;
	padding:3px 0 3px 0;
}
.subTitleText {
	color:#fff;
	font-weight:bold;
	text-indent:6px;
	text-shadow:0.1em 0.1em 0.05em #a42b00;
}

Open in new window


<div style="width:160px;">
<div class="subTitleOuter"><div class="subTitleInner"><div class="subTitleText">
Text to be displayed here
</div></div></div>
</div>

Open in new window

0
Comment
Question by:mar2195
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 16

Accepted Solution

by:
JF0 earned 50 total points
ID: 35216393
change your divs to spans.
0
 
LVL 16

Expert Comment

by:JF0
ID: 35216409
for padding just add it to your subTitleText class
0
 

Author Comment

by:mar2195
ID: 35216425
Nope.  Didn't work.  It's still displaying the "box" 100% with spans.
0
Technology Partners: 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!

 

Author Comment

by:mar2195
ID: 35216454
If this helps... This would be the difference between omitting the width param from a TABLE vs. adding 100% width to a TABLE.  Without the width param, the table's width is based upon the content.
0
 
LVL 16

Expert Comment

by:JF0
ID: 35216486
Did you change all of them to spans? Do you have a live link?
0
 

Author Comment

by:mar2195
ID: 35218033
I have no idea why using spans didn't work the first time.  It is working now using spans.  BUT... I have lost my CSS layout for the "box."  Using spans instead of divs killed the formatting.
0
 
LVL 16

Expert Comment

by:JF0
ID: 35218057
you will just need to tweak your margins and/or  paddings a bit. Give it a try and let me know if you need more help.
0
 

Author Comment

by:mar2195
ID: 35221499
OK.  So here's the latest... Everything is working correctly regarding the expanding width ... except... I cannot get padding or margin to be added to the top/bottom above/below the text.  For some reason, the 9px padding adds spacing to the the left & right... but does not add the padding to the top & bottom.

Here's the new coding:

.subTitleOuter {
	padding:2px 0 2px 0;
	background-color:#bf2f00;
	border:1px solid #811d05;
}
.subTitleInner {
	border-top:1px solid #d36f37;
	border-bottom:1px solid #d36f37;
	margin:3px 0 4px 0;
}
.subTitleText {
	color:#fff;
	font-size:15px;
	font-weight:bold;
	padding:9px;
	text-shadow:0.1em 0.1em 0.05em #a42b00;
}

Open in new window


<span class="subTitleOuter"><span class="subTitleInner"><span class="subTitleText">
Any text you want in a box-like image
</span></span></span>

Open in new window


I have tried adding another span ( and div just for yuks ).  I've also tried individual margin/padding, etc.  Nothing seems to add spacing above & below the text.



0
 

Author Comment

by:mar2195
ID: 35224117
Latest...

I have tried using an image as the background and using spans.  But still... the top/bottom of the text will not adjust using CSS.

.subTitleBanner {
	background:url('sm_banner_bkgd.gif') repeat-x;
	height:28px;
	border:1px solid #811d05;
}
.subTitleText {
	color:#fff;
	font-size:15px;
	font-weight:bold;
	padding:3px 24px 4px 9px;
	text-shadow:0.1em 0.1em 0.05em #a42b00;
}

Open in new window


<span class="subTitleBanner"><span class="subTitleText">
Any text you want in a ribbon-like image
</span></span>

Open in new window



 sm-banner-bkgd.gif

0
 

Author Comment

by:mar2195
ID: 35224133
I have no idea WTF is going on.  I know that I had coded the padding into the .subTitleBanner class before... now it IS working.

.subTitleBanner {
	background:url('sm_banner_bkgd.gif') repeat-x;
	height:28px;
	border:1px solid #811d05;
	padding:6px;
}

Open in new window


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eloquent ORM manual paginator defaults to simple 2 43
Create animated movies for web page 18 120
Adding items to a C# list incrementally 5 60
auto play video on web page 4 39
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

756 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