Solved

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

Posted on 2011-03-25
10
248 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
  • 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Uploading files to the web server has become common part of almost any kind of web application. People use different technologies to solve this, but regardless of the technology used, it is always useful to have some kind of progress indicator shown…
Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now