Solved

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

Posted on 2011-03-25
10
253 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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.

724 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