Making centred div hug content

This is a discussion on "Making centred div hug content" within the Web Page Design section. This forum, and the thread "Making centred div hug content are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 9th, 2007, 16:51
New Member
Join Date: Dec 2007
Location: Glasgow
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Making centred div hug content

Hi,

I have a page with images centred by putting them in a div with a class "center".

Code: Select all
.center { text-align: center; margin-left:auto; margin-right:auto; }
but when I try and put a div within this (I want to use 4 divs with background properties set to make a frame or a dropshaddow effect) I find that the div stretches right across the page rather than hugging the image.

Any ideas?
Reply With Quote

  #2 (permalink)  
Old Dec 9th, 2007, 17:02
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Could you post a link or your full code (including CSS)?
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Dec 9th, 2007 at 18:11.
Reply With Quote
  #3 (permalink)  
Old Dec 9th, 2007, 18:09
Aso's Avatar
Aso Aso is offline
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,003
Blog Entries: 2
Thanks: 5
Thanked 23 Times in 20 Posts
Send a message via Skype™ to Aso
Re: Making centred div hug content

If you don't set a width on .center, it will stretch across the entire page. Check out the A List Apart article on CSS drop shadows. This should help with what you want to acheive
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Reply With Quote
  #4 (permalink)  
Old Dec 10th, 2007, 02:00
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,608
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 3 Posts
Send a message via Yahoo to Monie
Re: Making centred div hug content

Yes, thats true aso186. You must set the width of the div elements (that you want to center), otherwise it will stretching to occupy it's Parent DIV
Last Blog Entry: ASP Programming Tips and Technique (Oct 26th, 2007)
Reply With Quote
  #5 (permalink)  
Old Dec 10th, 2007, 10:04
New Member
Join Date: Dec 2007
Location: Glasgow
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Oh. So it's impossible to make it hug without giving it a width.

So my code where the size of the image is not known is not possible.
Reply With Quote
  #6 (permalink)  
Old Dec 10th, 2007, 10:15
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Quote:
Originally Posted by mikeym View Post
Oh. So it's impossible to make it hug without giving it a width.

So my code where the size of the image is not known is not possible.
Thankfully, your logic is slightly flawed.

Yes, centred elements need an explicit width. But no, you do not have to set this width yourself. The image itself sets the width; all you need is for the box to "shrink-wrap" around it (but you should set the image's width explicitly in your <img> tag).

Floating an element causes it to "shrink-wrap". So to start with, float your outermost <div> with {float: left}. Then add another <div> around the outside, and position this in the centre with {margin: 0 auto}.

You'll probably run across some IE bugs, unfortunately. Test in Firefox first to see whether it works at all.
Reply With Quote
  #7 (permalink)  
Old Dec 10th, 2007, 16:26
New Member
Join Date: Dec 2007
Location: Glasgow
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Um....

I'm afraid neither of these are working for me.

the floating div with the center div round it does shrink wrap it but it just floats to the left anyway.

Giving the div a width doesn't make it shrink at all.

Code: Select all
.center { text-align: center; margin: 0 auto}
 .t {float: left; background: url(dot2.gif) 0 0 repeat-x } /* just remove float for non hugging version */ 
.b {background: url(dot2.gif) 0 100% repeat-x}
.l {background: url(dot2.gif) 0 0 repeat-y}
.r {background: url(dot2.gif) 100% 0 repeat-y}
.bl {background: url(bl2.gif) 0 100% no-repeat}
.br {background: url(br2.gif) 100% 100% no-repeat}
.tl {background: url(tl2.gif) 0 0 no-repeat}
.tr {background: url(tr2.gif) 100% 0 no-repeat; }

<div id="DSCN7518.jpg" class="center"><div class="t" width="620" height="470"><div class="b" width="620" height="470"><div class="l" width="620" height="470"><div class="r" width="620" height="470"><div class="bl" width="620" height="470"><div class="br" width="620" height="470"><div class="tl" width="620" height="470"><div class="tr" width="620" height="470">
<a href="Trip6/DSCN7518.jpg"><img src="Trip6/small/DSCN7518.jpg" border="0" alt="" width="600" height="450"></a>
</div></div></div></div></div></div></div></div>
<div class=comment>
<div id="DSCN7518.jpg" class="submitcomment">
<div class="total">
<p>
0 comments so far
</p>
</div>
</div></div>
</div>
Reply With Quote
  #8 (permalink)  
Old Dec 10th, 2007, 20:20
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Post a link for lazy people like me.
Reply With Quote
  #9 (permalink)  
Old Dec 11th, 2007, 13:36
New Member
Join Date: Dec 2007
Location: Glasgow
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Tricky to post a link because it's a live website - here's a version of it though http://www.getoutofglasgow.com/test.htm
Reply With Quote
  #10 (permalink)  
Old Dec 11th, 2007, 15:20
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

I don't see the problem on your test page, but note that you have a massive 113 XHTML errors. If you don't fix the errors, it makes a mockery of the whole debugging process.
Reply With Quote
  #11 (permalink)  
Old Dec 11th, 2007, 21:01
New Member
Join Date: Dec 2007
Location: Glasgow
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

OK,

It's taken all night (my pages are dynamically created - well not the test page) but they now adhere to the strict XHTML standard (my css is still a bit dodgy) but no change.

http://www.getoutofglasgow.com/test.htm
http://www.getoutofglasgow.com/test2.htm

Any ideas?
Reply With Quote
  #12 (permalink)  
Old Dec 11th, 2007, 21:07
New Member
Join Date: Dec 2007
Location: Glasgow
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

And now I've fixed the css with the css validator.

Still no joy!
Reply With Quote
  #13 (permalink)  
Old Dec 11th, 2007, 21:59
SuperMember

SuperMember
Join Date: Sep 2006
Location: Pink House
Posts: 3,946
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Is this what you want it to do?
CSS
Code: Select all
.center {
    text-align: center;
    margin: 0 auto;
    overflow: hidden;
    width: 600px;
}
 .t {float: left; background: url(dot2.gif) 0 0 repeat-x } /* just remove float for non hugging version */ 
.b {background: url(dot2.gif) 0 100% repeat-x}
.l {background: url(dot2.gif) 0 0 repeat-y}
.r {background: url(dot2.gif) 100% 0 repeat-y}
.bl {background: url(bl2.gif) 0 100% no-repeat}
.br {background: url(br2.gif) 100% 100% no-repeat}
.tl {background: url(tl2.gif) 0 0 no-repeat}
.tr {background: url(tr2.gif) 100% 0 no-repeat; }
No change to html
Reply With Quote
  #14 (permalink)  
Old Dec 11th, 2007, 22:23
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making centred div hug content

Hmm.

Perhaps the image needs to be set to {display: block}.

At this stage, I would consider creating a minimal test case, just to prove the concept of using a wrapper to centre a float. Prove the idea right or wrong, and then move on from there.
Reply With Quote
Reply

Thread Tools

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems making the content area have a dynamic height blizeH Web Page Design 41 Aug 13th, 2007 01:19
div wrap height centred jillcary Web Page Design 1 Jul 24th, 2007 16:05
printing style sheets to provide A5 size printouts without making content smaller paulbarris Web Page Design 5 May 9th, 2007 19:37
centred table not touching the top of the screen? acrobat Web Page Design 9 Apr 14th, 2007 00:05
place following divs in one page centred div antonyx Web Page Design 12 Dec 30th, 2005 21:00


All times are GMT. The time now is 19:28.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

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