/    Sign up×
Community /Pin to ProfileBookmark

Quick simple problem

For my main content, I have the following heirarcy:

[CODE]<div id=”main”>

<div class=”mainleft”>
<p>This is a bit of text</p>
<div class=”maininnerleft”>
<p>This is text</p>
</div>
<div class=”maininnerright”>
<p>This is text</p>
</div>

</div>

<div class=”mainright”>
<img src=””/>
</div>

</div>[/CODE]

OK, and in my style.css I have something like this:

[CODE]#main {width: 800px; background-color: #eeeeee; margin: 0 auto;}
.mainleft {float: left; width: 500px;}
.mainright {float right; width: 250px;}
.maininnerleft {float: left; width: 220px;}
.maininnerright {float: right; width: 220px;}[/CODE]

So the desired effect would be a big box, split in to 2 columns, and in the left column it would have some text and then 2 more bits of text side by side below.

This layout is fine, the problem is with the background color coming from #main, it doesnt seem to display unless I actually set a height or put a few &nbsp things in but I dont want to set a height because I want the height to be determined by how much text is in the divs within.

Is there an alternative way to do this?

Thanks.

to post a comment
CSS

7 Comments(s)

Copy linkTweet thisAlerts:
@auntniniMar 21.2012 — Read up on FLOAT property, such as http://coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/

“A floated box is positioned within the normal flow, then taken out of the flow and shifted to the left or right as far as possible. Content may flow along the side of a float. [...] When a box is taken out of normal flow, all content that is still within normal flow will ignore it completely and not make space for it.” [Float Positioning]

“When you float an element it becomes a block box. This box can then be shifted to the left or right on the current line. The markup options are float: left, float: right or float: none.” [Floatutorial: Float Basics]

“You should always set a width on floated items (except if applied directly to an image – which has implicit width). If no width is set, the results can be unpredictable.” [Floatutorial: Float Basics]
[/quote]


The floated element is partially taken out of normal HTML flow so the parent element collapses We used to foat the parent element, nw they say to add

overflow: auto to parent element.
Copy linkTweet thisAlerts:
@djadejonesauthorMar 21.2012 — So if I were to use the floats, I would have to add the height element to my "main" ID?

What does overflow: auto; actually do?
Copy linkTweet thisAlerts:
@auntniniMar 21.2012 — overflow: auto; is a fix for collapsing parent div.
Copy linkTweet thisAlerts:
@djadejonesauthorMar 21.2012 — Brill, have added that and my background-color has displayed to the bottom of my text, thanks for your advice :-)
Copy linkTweet thisAlerts:
@hyperionXSMar 22.2012 — Great fix with overflow:auto. I always used clear:both to clear the floats.

Tested in all major browsers and IE 9 and it works wonderfull. Do you have any ideea if this fix works also for older versions of IE?
Copy linkTweet thisAlerts:
@djadejonesauthorMar 22.2012 — I have just noticed, that when adding border-radius to my "main", it has only actually added the rounded corners to the top bit and not the whole "main", so where I wanted a block with some text and below the text is split in to two columns and the left column containing text side by side, the rounded corners are only around the text at the top and everything below is just square.

Any ideas?
Copy linkTweet thisAlerts:
@auntniniMar 22.2012 — I have not used CSS3 border:radius. Wonder if you can correct this by specifying individual corners and browser prefix (-moz-*)

See for instance http://www.css3.info/preview/rounded-border/.

W3C Specification Mozilla Implementation

border-radius -moz-border-radius

border-top-left-radius -moz-border-radius-topleft

border-top-right-radius -moz-border-radius-topright

border-bottom-right-radius -moz-border-radius-bottomright

border-bottom-left-radius -moz-border-radius-bottomleft
[/quote]


I can only guess and wonder how the float would affect this?
×

Success!

Help @djadejones spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...