When the children within a CSS container are floated, set the height of the container to

I've established a simple layout

<div class="container">
    <div class="sidebar">
    </div>
    <div class="content">
    </div>
    <div style="clear:both;"></div>
</div>

where .sidebar and .content are styled with float: left

I have searched through numerous responses on S.O., but they all seem to work ONLY IF the .content does not exceed the screen size. To emphasize my issue, I present two examples:

http://jsfiddle.net/pleasedontbelong/h35vc/2/ (small content) http://jsfiddle.net/pleasedontbelong/56C9v/1/ (big content)

When the .content div is too large, the height:100% property in the container fails to function.

In both scenarios, the gray div should fill 100% of the height. It appears that the browser computes the window height prior to floating the elements.

Is there a CSS-only solution to this problem? (I could resort to JS, but it feels unclean)

Answer №1

Get rid of the height attribute

.box{
    background-color: #555;
    margin: 10px;
    //no height specified
}

PREVIEW http://jsbin.com/45C8u/8/

If you want the box to always take up 100% then set min-height: 100% like so

.box{
        background-color: #555;
        margin: 10px;
        min-height: 100%;
    }

Answer №2

It seems like you're trying to ensure that the container/content never exceeds 100% of the screen height.

To achieve this, simply include overflow:hidden; in your container CSS.

If you need more details on the overflow property, you can check out: https://developer.mozilla.org/en-US/docs/Web/CSS/overflow

Answer №4

It seems like you're looking to make the container always 100% of the document's height, while also ensuring that floats stretch down without increasing the container's height.

To prevent the body from exceeding 100% height due to margins, you can do the following:

html, body {
  height: 100%;
  margin: 0;
}

If your container has padding that exceeds 100% height, you can use the border-box property as follows:

.container{
  background-color: #999;
  padding: 20px;
  height: 100%;
  box-sizing: border-box;
}

To ensure that floats are 100% height and handle text overflow with a scroll bar, use the following CSS:

.sidebar, .content {
  height: 100%;
  overflow: auto;
}

Does this solution align with what you had in mind? Check out a demo here.

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

What is the best way to set a specific image as the initial image when loading 'SpriteSpin'?

I'm currently working on creating a 360-degree view using the SpriteSpin API. Everything is functioning as expected, but I have an additional request. I need to specify a specific image to be the initial landing image when the page loads. The landing ...

What is the best way to conceal a specific class of DIV within a container, and how can I also hide another DIV within the same container by

I have a DIV class containing around 10 elements within a container. I am looking to implement a feature where these elements are hidden and shown one by one every 15 seconds using jQuery with a smooth fadeOut() effect. Your assistance in achieving this wo ...

What causes IE11 to sporadically run inappropriate media queries?

My website has been displaying incorrectly on IE11 due to a media query being executed for widths less than 767px, even though the window width is actually more than that. Interestingly, this issue only occurs randomly and never when the debug window is op ...

Can anyone help me with coloring Devanagiri diacritics in ReactJS?

I am currently working on a ReactJS project and I have come across an issue. I would like for the diacritic of a Devanagiri letter to be displayed in a different color than the letter it is attached to. For example: क + ी make की I was wondering ...

What is the best method for extracting the innerHTML value of html tags using a css selector or xpath?

I am currently struggling with retrieving the HTML inner text value using CSS selector or XPath. While I can achieve this using document.getElementById, I am unable to do so using selectors. I can only print the tag element but not the text from it. For e ...

When we typically scroll down the page, the next section should automatically bring us back to the top of the page

When we scroll down the page, the next section should automatically bring us back to the top of the page without having to use the mouse wheel. .bg1 { background-color: #C5876F; height: 1000px; } .bg2 { background-color: #7882BB; height: 1000px; } .bg3 ...

Guide to collapsing all menus in an Angular 4 Accordion

I am a beginner with Angular 4 and I have implemented an accordion with the structure of Categories, Groups, and Subgroups. When I click on a category, it displays all the groups within that category. Similarly, when I click on a group, it shows all the s ...

Tips for ensuring the final row remains fixed at the bottom of the table while maintaining a minimal body height:

I'm currently working on designing an invoice table using HTML and CSS. I have dynamic rows that are added to the table, and I want the body of the table to have a minimum height to fit around 5-6 rows, after which it should extend based on the number ...

CSS Styled Product Index

Currently, I am working on creating a product catalog using React and CSS. Everything is going smoothly except for the last row of products. The issue with the last row is that it only contains 1 element, and due to the flex-grow: 1 setting, it ends up ta ...

What is the best way to relocate an image or link using CSS?

I've been attempting to adjust the position of this image using CSS, but no matter how many times I try, it just refuses to budge. Can someone please help me troubleshoot what might be causing this issue? #yahoo1 { position: absolute; top: 100p ...

What is the reason for the presence of white space surrounding the div element

In the td element, I have four nested div elements. However, there seems to be a small margin or space created between them that I cannot figure out how to remove. If this spacing is due to the behavior of the inline-block, then how can I override it? ...

How to Place Buttons on the Left, Center, and Right in Bootstrap Framework

I'm currently working on developing some pages using bootstrap. In the form at the bottom, I have three buttons - Save, Cancel, and Commit. The issue I'm facing is that while the left and right buttons are perfectly aligned, the Cancel button is ...

Toggle checkbox feature in Bootstrap not functioning properly when placed within ng-view

When attempting to embed a bootstrap toggle checkbox within <ng-view></ng-view>, an issue arises where a regular HTML checkbox is displayed instead of the expected bootstrap toggle. Strangely, the same checkbox functions as a bootstrap toggle w ...

What is the best way to maintain two distinct background effects that each require different pixel sizes?

I am attempting to create a gradient that goes from the top-right corner to the bottom-left corner, and I also want to add a grid of dots. However, the method I am using to create the grid of dots is through a radial gradient which requires a small backgro ...

What is the best method for styling arrays displayed by React in version 15 using CSS?

React v15 has made a change where elements of arrays in JSX are now rendered as <!--react-text:some_id-->text<!--/react-text--> instead of spans. I've searched for a solution but haven't been able to figure out how to apply CSS styles ...

maintaining the alignment of three div elements

I am struggling to keep three divs side by side within a container, each containing an image and text below it. I had this layout working perfectly before, but now the right div falls underneath the other two when the screen size is reduced, followed by th ...

Adjusting the background color of the custom input range thumb when the input is disabled

I've customized the input thumb on my range slider, and I'm looking to change its color when it's disabled. I attempted adding a class to the thumb like this: input[type=range]::-webkit-slider-thumb.disabled and also tried adding the disa ...

Tips for concealing overflowing CSS content

How can I prevent CSS content from overflowing? I am trying to display a price tag in the CSS element, but it is protruding out of my card. .content{ content:''; position: absolute; left: 309px; ...

"Optimizing XSL stylesheets by implementing conditional row styles to prevent redundant

Is there a way to display rows that are older than 2 years from today in bold without repeating the code? I know the condition I need, but I'm struggling with avoiding duplicating all the code just for a different style. This is the code I have: & ...

Troubleshooting the inability to set percentage values in Bootstrap styling

My goal is to create a bar with thin bars, but my use of Bootstrap limits my control over sizes. <template> <div class="container-fluid mx-auto"> <div class="row"> <div class="square rounded-pill&qu ...