Interactive sizing div

Take a look at this code:

<div id="content">
    <div id="firstrow">    

    </div>
    <div id="secondrow">
        <div class="newsrow">    </div>
        <div class="mediarow">    </div>
    </div>
    <div id="thirdrow">

    </div>
</div>

<div id="footer">
    <ul>
        <li>Home</li>
        <li>Link1</li>
        <li>Link2</li>


    </ul>
</div>
<div id="lastpart">Copyright 2004 - 2011 &copy; example.com , All rights reserved. </div>

I have removed the content of firstrow,newsrow,mediarow and thirdrow for simplicity. Here is the CSS I am using:

#content{
    width:1250px;
    position: relative;
    top: 0px;
    margin: 0 0 0 0;
    padding: 0 0 0 0;
    background: url("imgbg_body_03.png");
    right: 66px;
    height:2550px;

}


#firstrow{
    margin: 0px  20px 0 0;

    width:195px;
    float:right;
}

#secondrow{
    background-color:#772255;
    width:740px;
    float:right;
}
.newsrow{

    width:366px;
    float:right;
    margin: 2px 2px 2px 2px;
    text-align:right;
    color:#660066;
}.mediarow{
    width:366px;
    float:right;
    margin: 2px 2px 2px 2px;
}
#footer{
    background: url("imgbg_body_02.png");
    width:1250px;
    height:38px;
    position: relative;
    top: 0px;
    right:66px;
    margin: 0 0 0 0;
    padding: 0 0 0 0;
}
#footer ul{
    margin: 0px 0 0 0;
    padding: 5px;
}
#footer ul li{
    list-style-type:circle;
    display:inline;
    color:white;
    margin : 0 35px 0 35px;
    font:bold 12px Tahoma;
}
#footer ul li:hover{
    color:#FFFF00;
}
#lastpart{
    width:1250px;
    position: relative;
    top: 0px;
    margin: 0 0 0 0;
    padding: 20px 0 0 0;
    background: url("imgbg_body_04.png");
    right: 66px;
    text-align:center;
    font-weight:bold;
    color:#660033;
    height:56px;
    direction: ltr !important;
}

My issue is that I want the div with id content to adjust its length based on the largest div inside it. I have tried different values for the height property but nothing seems to work. What should I try next?

Thank you in advance.

Answer №1

Looks like you're dealing with a classic "CSS Guillotine" situation here. The float applied to the rows is causing #container to not adjust properly to their size.

Check out this informative page for more details:

In brief, simply add this CSS to your page and assign the class clearfix to your #content:

 <style type="text/css">

  .clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
    }

</style><!-- main stylesheet ends, CC with new stylesheet below... -->

<!--[if IE]>
<style type="text/css">
  .clearfix {
    zoom: 1;     /* triggers hasLayout */
    }  /* Only IE can see inside the conditional comment
    and read this CSS rule. Don't ever use a normal HTML
    comment inside the CC or it will close prematurely. */
</style>
<![endif]-->

(By using this solution, you won't need an additional div with clear:both)

Answer №2

Just before you finalize by closing <div id="content">, insert a div element with the class clear and include the following CSS:

.clear {
    clear: both;
}

This adjustment will ensure that your HTML structure appears like so:

<div id="content">
    <div id="firstrow">    

    </div>
    <div id="secondrow">
        <div class="newsrow">    </div>
        <div class="mediarow">    </div>
    </div>
    <div id="thirdrow">

    </div>
    <div class="clear"></div>
</div>

<div id="footer">
    <ul>
        <li>Home</li>
        <li>Link1</li>
        <li>Link2</li>
    </ul>
</div>
<div id="lastpart">Copyright 2004 - 2011 &copy; example.com , All rights reserved. </div>

Answer №3

To effectively handle floats, it is possible to use the overflow property, surprisingly;

#container {
    overflow: hidden;
}

For a more comprehensive solution that works across different browsers, check out this link:

Answer №4

Make sure to include the following code snippet :

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

inside every div on your webpage.

(Specifically within : <div id="content">, <div id="footer">, and <div id="lastpart">.)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

chart for visualizing two rows with matching IDs and dates

I have been attempting to accomplish the following two scenarios: 1) When both ID and dates are the same in the chart, but the descriptions differ, I want to display them on separate lines. 2) If there is not enough room to show the row label, I would li ...

What is the best way to position my h1 headings above my flexbox containers?

I have set up 3 flexbox containers, with one container controlling the other two. In the primary and secondary containers, I have placed 4 images and an h1 element together. Now, I'm trying to figure out how to move the h1 elements on top of the flex ...

Modifying Characteristics of Elements Added Dynamically

How do I change the type attribute for dynamically added buttons? In the code below, the label names are changing perfectly, but when I try to change button types, it applies to all dynamically added buttons. My requirement is to change every button type t ...

Unable to successfully Uploading an Image using PHP

I've encountered a new issue. I'm trying to upload an image from my webpage into a folder on the server (using XAMPP). Despite trying multiple examples, including those from php.net and w3schools.com, the code isn't working and I can't ...

What measures can be taken to ensure that scrollIntoView does not interrupt when the user begins scrolling on their own?

I have a navigation bar where I am using the scrollIntoView method to ensure that the active element is always centered in the list. However, I encountered an issue wherein scrolling the main content interrupts or stops the scroll action triggered by scrol ...

How can one ensure that the inset box shadow appears above child links while still allowing them to be clickable?

I am facing a challenge with a div that has an inset box shadow. The issue is that there are links within the div and I want the shadow to display over them while still allowing the links to be clickable. Here's an example: https://jsfiddle.net/5rsbn ...

Issues with the CSS styling

Struggling with styling my menu and can't seem to get it right. Feeling a bit lost as I'm new to this. .customHorizontalList { list-style: none; display: flex; flex-direction: row; } .customHorizontalList>li { margin-ri ...

Ways to increase the shrinking capacity of flex items when the entire container width is occupied

Recent Post Below: I've replicated a minimized issue: https://codepen.io/MH-123/pen/ExJWQWq?editors=1100 In the provided CodePen, the problem persists. Two divs are present, but flex shrink functionality is not working as expected. The main flex cont ...

The Reliability of Using CSS Pixel Font Sizes

Imagine I need to display two capital 'T's, each exactly one inch tall. One 'T' appears on a Macbook Pro with Retina Display (220 DPI, CSS pixel ratio 2), and the other on an iPhone 4S (326 DPI, CSS pixel ratio 2). To achieve this, I se ...

Combining multiple rows into a single row and inserting a new column

Hi there, I'm currently diving into the world of programming and tackling a project involving PHP and MySQL. However, I've encountered a bit of a roadblock. My current setup involves a table named marks, where each time a mark is added to a stud ...

The options in the drop-down menu appear to be stuck and remain in place

Can someone with expertise lend me a hand? I've been racking my brain over this issue, and while I feel like I'm on the right track, I just can't seem to make my sub-sub menu items drop down to the right on hover. I suspect there might be c ...

Modify the appearance of a specific <td> element using JavaScript to change its color

I am working on creating a quiz using HTML, displayed in a table format. Upon verification, I want to highlight the correct choice (which is represented by a <td> tag) in green, and the incorrect choice in red using the background property. Is there ...

I am encountering challenges with submitting the form

I am encountering an issue where I want to submit the form on button click and navigate to the next page, but instead I get an error message saying: "Form submission canceled because the form is not connected". Does anyone have a solution for this problem ...

How come my script that is dependent on the viewport does not apply classes?

I created a code to add a class to a div, but it's not working as expected. I need help troubleshooting this issue. The code works fine on codePen, but not on my WordPress website. How can I make sure the browser executes this code properly? Link to ...

When using Three.js, adjusting the TranslateX or Y values sometimes results in unexpected rotation instead of a straightforward movement along the axis,

In my Three.js project with TrackballControls, I am working on creating a dynamic 3D scene. One of the key components is the PerspectiveCamera that I have initialized: camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10 ...

What is the best way to eliminate the "onclick" attribute from an HTML element using JavaScript?

Is there a way to make my code only execute the onlick function after a button is pressed? I want to prevent any action from happening before that. <!-- deactivate onclick function --> <div class="boardsection2" id="2_8_7" oncl ...

How come I am unable to upload my local image using angular-cli?

When testing my application on localhost, everything runs smoothly. However, once I publish the app to GitHub, the image fails to load. The image is located within the src/resources directory, and I have also attempted moving it to the public folder. My ...

What steps can be taken to repair the footer area within this CSS document?

As a beginner ASP.NET developer, I am encountering CSS for the first time. The CSS file I have is fairly simple, but I am facing difficulties with the footer section. The current appearance of the footer is as follows: I am looking to make the following m ...

Adjust the height seamlessly on the homepage without the need for scrolling, while maintaining a stationary navigation bar and footer

Our latest project is designed specifically for mobile use. The fixed navigation bar is functioning perfectly. We also have a fixed footer with icons at the bottom. (All working well) The challenge we are facing is to make the content between the naviga ...

What is the best way to limit the width of a Template Field in a GridView?

Can anyone assist me in managing the width of the 'Link target' column within this gridview? When a long URL string is inputted, it causes the page to extend beyond the panel and creates an awkward appearance. I've tried adjusting the colum ...