Go Back   Velocity Reviews > Newsgroups > HTML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

HTML - <div> automatic width according to their contents?

 
Thread Tools Search this Thread
Old 06-24-2004, 09:15 PM   #1
Default <div> automatic width according to their contents?


Hello again,

I'm struggling with another basic problem:

<div class="mainbox-title-content">
<h4>Content !</h4>
</div>

if I use this stylesheet:

..mainbox-title-content {
background-image: url("images/mainbox-tc.png");
background-repeat: repeat-x;
height: 28px;
display: block;
}

The <div> takes a width of 100% by default.

If I replace display: block with display: inline, the <div> adapts to
the width of the text it contains. That is the behaviour I want. BUT
setting it inline makes the browser ignore the height.

What I'd like is that the width adapts automatically according to the
content with a fixed height, in fact, emulating the tables behaviour.
There's a display:table tag, which works well with gecko, but won't
work with IE. I can't rely on it for obvious reasons. :-/

Is there another way to do that or will I have to revert to a good old
table for that?

Thanks,
JFLac


Jean-Fran?ois Lacrampe
  Reply With Quote
Old 06-24-2004, 11:20 PM   #2
Toby A Inkster
 
Posts: n/a
Default Re: <div> automatic width according to their contents?
Jean-Fran?ois Lacrampe wrote:

> If I replace display: block with display: inline, the <div> adapts to
> the width of the text it contains. That is the behaviour I want. BUT
> setting it inline makes the browser ignore the height.


Nasty hack:

<div class="mainbox-title-content">
<h4>Content!<img src="transparent.gif" alt="" width="1" height="28"></h4>
</div>

..mainbox-title-content {
background-image: url("images/mainbox-tc.png");
background-repeat: repeat-x;
display: inline;
}

That work?

--
Toby A Inkster BSc (Hons) ARCS
Contact Me - http://www.goddamn.co.uk/tobyink/?page=132



Toby A Inkster
  Reply With Quote
Old 06-25-2004, 10:27 AM   #3
Mitja
 
Posts: n/a
Default Re: <div> automatic width according to their contents?
Toby A Inkster <>
(news ) wrote:
> Jean-Fran?ois Lacrampe wrote:
>
>> If I replace display: block with display: inline, the <div> adapts to
>> the width of the text it contains. That is the behaviour I want. BUT
>> setting it inline makes the browser ignore the height.

>
> Nasty hack:
>
> <div class="mainbox-title-content">
> <h4>Content!<img src="transparent.gif" alt="" width="1"
> height="28"></h4> </div>


That's down to personal taste, but I'd rather do with a table than a
transparent gif.

> .mainbox-title-content {
> background-image: url("images/mainbox-tc.png");
> background-repeat: repeat-x;
> display: inline;
> }
>
> That work?





Mitja
  Reply With Quote
Old 06-26-2004, 06:52 PM   #4
Jean-Fran?ois Lacrampe
 
Posts: n/a
Default Re: <div> automatic width according to their contents?
"Mitja" <> wrote in message news:<PbSCc.5634$>...
> Toby A Inkster <>
> (news ) wrote:
> > Jean-Fran?ois Lacrampe wrote:
> >
> >> If I replace display: block with display: inline, the <div> adapts to
> >> the width of the text it contains. That is the behaviour I want. BUT
> >> setting it inline makes the browser ignore the height.

> >
> > Nasty hack:
> >
> > <div class="mainbox-title-content">
> > <h4>Content!<img src="transparent.gif" alt="" width="1"
> > height="28"></h4> </div>

>
> That's down to personal taste, but I'd rather do with a table than a
> transparent gif.
>
> > .mainbox-title-content {
> > background-image: url("images/mainbox-tc.png");
> > background-repeat: repeat-x;
> > display: inline;
> > }
> >
> > That work?


I can't say I love the idea of transparent gifs (CSS is supposed to
help us to get rid of these nasty hacks, right?), but the idea lead me
to this:

<div class="mainbox-title-encaps">
<img src="images/mainbox-tl.png" width="8" height="28" alt="(" />
<div class="mainbox-title-content">
Content here
</div>
<img src="images/mainbox-tr.png" width="8" height="28" alt=")" />

div.mainbox-title-encaps {
background: #ffffff url("/images/mainbox-tc.png") repeat-x;
height: 28px;
display: inline;
float: left;
}

div.mainbox-title-content {
vertical-align: 8px;
display: inline;
}

I had to add a starting and an ending png anyway, so it does the trick
for the height.

There's one problem left, though. I wanted this title to be
horizontaly centered, but the float tag only allows left or right
alignment. I've tried a lot of tricks (one or two more level of
nesting, text-align:center, margin -left and -right: auto, etc.). No
success, the float predominates. If I remove the float, my initial
problem comes back.

Any idea?
JFlac


Jean-Fran?ois Lacrampe
  Reply With Quote
Old 06-27-2004, 11:09 AM   #5
Toby A Inkster
 
Posts: n/a
Default Re: <div> automatic width according to their contents?
Jean-Fran?ois Lacrampe wrote:

> I can't say I love the idea of transparent gifs (CSS is supposed to
> help us to get rid of these nasty hacks, right?),


Well the ideal solution is "display:inline-block", but only Opera supports
that right now.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me - http://www.goddamn.co.uk/tobyink/?page=132



Toby A Inkster
  Reply With Quote
Old 10-29-2009, 01:31 PM   #6
suyashxp
Junior Member
 
Join Date: Oct 2009
Posts: 1
Default
i also faced the same problem : but i resolved it by

margin: 0 auto;
understand by both Ie and Mozilla firefox


suyashxp
suyashxp is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
print all the contents of the iframe lebohang The Lounge 0 03-04-2009 07:50 AM
Re: Security+ Course Available in Northern Virginia, Starts March 15 Rowdy Yates A+ Certification 0 03-03-2005 07:08 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46