Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

CSS height/float issue

Featured Replies

Hi guys,

 

I am building a site and basically when I float the two content boxes within a content container, the container stays at a set height and doesn't expand with the other two boxes... see example:

 

 

 

 

Here is the example code (obviously it isn't the real site but you get the gist):

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<style type="text/css">
#main
{
background-color:#999999;
width:400px;
padding:20px;
}

#x1
{
width:120px;
background-color:#009900;
float:left;
}

#x2
{
width:120px;
background-color:#FF0000;
float:left;
margin-left:30px;
}
</style>

</head>

<body>

<div id="main">

<div id="x1">
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
</div>

<div id="x2">
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
</div>

</div>

</body>
</html>

 

Any ideas guys - when it is just the one bit of content in there with no float then it works fine.... weird!!

 

Cheers lads and ladies :)

You need to put some kind of footer or something inside the #main that will clear them both.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<style type="text/css">
#main
{
background-color:#999999;
width:400px;
padding:20px;
}

#x1
{
display:block;
width:120px;
background-color:#009900;
float:left;
}

#x2
{
display:block;
width:120px;
background-color:#FF0000;
float:left;
margin-left:30px;
}
.clear{
clear:both;
}

</style>

</head>

<body>

<div id="main">

<div id="x1">
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
</div>

<div id="x2">
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
<p>BLAH BLAH BLAH</p>
</div>
<div class="clear">

</div>
</div>

</body>
</html>

 

Something like that, probably needs tweaking but basically you need to add the 'clear both' after the floats to stretch your main to the bottom.

If I understand the problem correctly, add:

 

<div style="clear:both;"></div>

</div>

</div>

 

under the floated divs.

  • Author

thanks guys - problem solved!! That is what 2hrs sleep in 24hrs will do to you.... turn you brain to MUSH!! lol

 

Many thanks :)

you know 4 people have asked me that exact same question in the last week, it must be a common problem.

  • Author

Yeah it is something I have done a few times before - learned from Gareth Daines' tutorial, but for some reason last night my brain just wouldn't comprehend the Clear:both; lol

 

Cheers for your help :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.