Why does the container overflow when the child is floated in CSS?

Can you explain why a parent div element stops adjusting its height when its child is floated? Here is the HTML snippet:

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
  <link rel="stylesheet" type="text/css" href="index.css" />
</head>

<body>
  <div id="container">
    <div id="overflown_container">
      <button class="button">Button</button>
      <button class="button" style="height:80px; float: right">Overflown button</button>
    </div>
  </div>
</body>
</html>

And here's the CSS:

#container {
  background-color: blue;
  width: 60vw;
  margin: auto;
}

#overflown_container {
  background-color: red;
  margin-bottom: 10px;
}

.button {
  float: bottom;
  height: 44px;
  padding: 10px;
  border: 5px solid yellow;
  margin: 0 0 10px 0;
}

The current result is displayed in this image https://i.sstatic.net/2Jmi2.png.

If I add the overflow: auto attribute to the overflown_container, it adjusts its height. But I don't want to add overflow attributes to all containing elements up to the root. I also want to align both buttons at the bottom but floating Button1 to the overflown area seems impossible.

Why does floating a button to the right cause overflow? How can we avoid this and achieve a design where both buttons are at opposite ends of the container, may or may not have fixed dimensions, are aligned at the bottom, and allow the containers to grow without overflowing?

Answer №1

By default, a floating element may not be properly contained within its container.

To ensure that the container adjusts to the width and height of the floating element, you can add a clearfix property to it.

For more information, refer to

Include the following CSS code:

.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.clearfix:after {
    clear: both;
}

Additionally, remember that there is no float:bottom in CSS - only float left or right. To achieve a 'float' bottom effect, consider either adding sufficient margin-top to the floating element or using position: absolute instead.

    .parent{
        position: relative;
    }
    .child {
        position: absolute;
        bottom: 0;
    }

Answer №2

One challenge with floating elements is that they disrupt the regular flow of content on a webpage. While I've incorporated clearfix to address this issue in the example provided, it may not meet your specific requirements: https://jsfiddle.net/07sjLaLa/

In the demonstration below, eliminating the float: right from the button results in aligning the buttons at the bottom as per your request: https://jsfiddle.net/07sjLaLa/1/

Nevertheless, this adjustment leads to another issue where the button isn't aligned ideally. To achieve a proper right alignment without using float, you can apply right alignment to the parent element. In cases where diverse alignments are needed, adding new sets of parent elements can be a solution: https://jsfiddle.net/07sjLaLa/2/

Keep in mind that although the container element #container doesn't expand in these examples, applying clearfix to this element can resolve this limitation: https://jsfiddle.net/07sjLaLa/3/

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

Log into your account by choosing a button using Selenium with Python

While attempting to access my account via this specific link on the Market Watch webpage using Python and Selenium, I encountered a roadblock. Despite successfully selecting the "Sign In" button, no action is triggered, preventing me from entering the desi ...

Div with wide width and captivating perspective

Recently, I've been experimenting with perspective and 3D transformations, and I've realized why my div isn't displaying at full width despite setting it in the CSS. However, I'm interested in creating a responsive layout where the div ...

Utilize JQuery to identify and select every parent element, then retrieve the height of the first child element and adjust the height of the

Recently, I developed a PHP script that pulls news and case posts from a database. The HTML structure used for displaying these posts is as follows: <a href='/post/placeholder'> <div class='col nopadding col12-12 counter'> ...

Passing parameters from an HTML form using PHP

I have a background in C# and am still relatively new to PHP. I have an HTML form that submits its values to the PHP file below, but for some reason, the values are not being set. Can you help me identify what I'm doing wrong? <?php include(' ...

The stylesheet is linked, but no changes are taking effect

I've linked my template.php to style.css, and when I make changes in template.php, they show up fine. However, if I make changes in the stylesheet, nothing happens. What should I do? My website is not live; I'm working on it using a WAMP server. ...

Angular 13: Problems arise with custom theme in Angular Material version 13

I've set up a custom theme palette for my project that works perfectly with version ^12.2.13 of Angular Material, but not with ^13.2.3. Here's the SCSS code for my custom theming: my-custom-theme.scss @import '~@angular/material/theming&apo ...

Manipulating JSON objects within a map using jQuery and Gson

I am working with a Java object that contains several nested object fields with basic fields in them. Here is an example: Template { String name; EmailMessage defaultEmailMessage; } EmailMessage { String emailSubject; String emailBody; } ...

Tips for using JavaScript to style an array items individually

I have currently placed the entire array within a single div, but I would like to be able to display each element of the array separately so that I can style "date", "title", and "text" individually. This is my JSON structure: [ { "date": "Example ...

The function Jquery .stop does not exist

I am encountering an issue with the magicline.stop function while attempting to implement an underline sliding effect for my navbar. I have tried to troubleshoot the problem but have been unsuccessful so far. Below is the code snippet: <nav class=" ...

The dropdown menu button stubbornly remains open and refuses to close

Having an issue with a dropdown menu button where it should open when clicked on the icon and close when clicking off the icon or on the icon again, but instead, it remains open. Here is a screenshot for reference: https://i.stack.imgur.com/UX328.jpg I&a ...

Using a variable as a URL parameter in a jQuery ajax request: tips and tricks

$.ajax({ type:"POST", url:"hostname/projfolder/webservice.php?callback=statusReturn&content="+str_table, contentType: "application/json; charset=utf-8", crossDomain:true, dataType:'jsonp', succe ...

What values can be used for the CSS Property "padding-right-ltr-source"?

Currently, I am facing an issue with box padding specifically in Firefox. Upon inspecting the affected span element, I noticed a property called "padding-right-ltr-source" with the value "physical". Here is the snippet of code: >padding: 0px 15px; ...

Using FlexBox for Vertical Alignment of Elements

Experimenting with FlexBox (for demonstration purposes only). I am facing a challenge in vertically centering text within the parent .top-nav-bar. While using justify-content: space-between, I have successfully aligned two elements horizontally, but I am s ...

Is it possible to access the HTML template prior to Outlook applying any additional classes during rendering?

I recently received an email template created in a different platform via Outlook. I am attempting to recreate this email in the Salesforce Marketing Cloud Platform, but I am encountering numerous unnecessary tables, classes set by Outlook (MsoNormal, Ms ...

What type of icon would be best to use for my Android application compatible with versions 1.x and 2.x?

Back in the days of Android 1.5 and 1.6, we followed the Icon Design Guidelines. These guidelines specified how application icons should look. However, with the newer Android versions (2.0 and 2.1), there has been a shift towards a flat 2D style for icons. ...

Aligning the text in the middle of a button

Currently delving into Front-End development and in the process of coding my maiden site using bootstrap. Encountered a roadblock on something seemingly simple. How can I center text within a button? Here's the button, the default one utilizing an " ...

The dynamic styles set by CSS/JavaScript are not being successfully implemented once the Ajax data is retrieved

I am currently coding in Zend Framework and facing an issue with the code provided below. Any suggestions for a solution would be appreciated. The following is my controller function that is being triggered via AJAX: public function fnshowinfoAction() { ...

The readyState of Ajax is consistently anything but 4

I have encountered an issue with my JavaScript code. I am running these codes in order to display data based on user input. Despite there being no error connection and the connection happening properly, whenever I enter a name it always goes into the else ...

Converting HTML to PDF using AngularJS

Can anyone help me with converting HTML to PDF in Angular? I have tried using the angular-save-html-to-pdf package from npm, but encountered errors. Are there any other solutions or custom directives available for this task? ...

Can media queries styles be applied to a window of random size using JavaScript?

Can JavaScript be used to apply media queries style based on random window sizes? I have 5 buttons that I want to switch styles for using JavaScript according to the media queries defined in my CSS stylesheet. Is there a method to achieve this? ...