When a child element with float:left is present, the parent div fails to accurately detect the child's height

The center-content element should adjust its height according to its child elements. However, if I apply float:left to one of its child elements, like the leftContent element, leftContent will extend 95% of its body outside the center-content. This is because the center-content's height does not adjust to contain the leftContent element.

If I remove the float:left from leftContent, the center-content element will adjust its height to keep the leftContent element inside. But why does applying float:left cause this issue?

In simpler terms, I want two small boxes - one floating left and another floating right - inside a larger box. The larger box should adjust its height based on the size of the child elements.

HTML:

#header, #footer, #content-wapper, section {
  max-width: 100%;
  margin: 0 auto;
  text-align: center;
}
#leftContent{
  display: inline-block;
  width: 49%;
  height: auto;
  float: left;
}

input{
  width: 98%;
  height: 40px;
  border: solid 1px gray;
  background-color: white;
}

.center-content {
  width: 960px;
  max-width: 100%;
  margin: 0 auto;
  padding: 2vw 0 2vw 0;
  background-color: #E8E8E8
}
<section class="center-content">
    <div id="leftContent">
        <a><input name="income" type="text" id="income0" placeholder="Main Applicant Annual Income"></a><br>
        <a><input name="income" type="text" id="income1" placeholder="Main Applicant Any-other Income"></a><br>
        <a><input name="income" type="text" id="income2" placeholder="Second Applicant Annual Income"></a><br>
        <a><input name="income" type="text" id="income3" placeholder="Second Applicant Any-other Income"></a><br><br>
        <a><button class="btnCal" onclick="calculateMort()">Calculator</button></a>
    </div>
</section>

Answer №1

To ensure proper alignment, it is recommended to clear the float element first before using :after and :before in conjunction with clear:both

#header, #footer, #content-wapper, section {
  max-width: 100%;
  margin: 0 auto;
  text-align: center;
}
#leftContent{
  display: inline-block;
  width: 49%;
  height: auto;
  float: left;
}
.center-content:before, .center-content:after {
  content: "";
  clear: both;
  display: table;
}

input{
  width: 98%;
  height: 40px;
  border: solid 1px gray;
  background-color: white;
}

.center-content {
  width: 960px;
  max-width: 100%;
  margin: 0 auto;
  padding: 2vw 0 2vw 0;
  background-color: #E8E8E8
}
<section class="center-content">
    <div id="leftContent">
        <a><input name="income" type="text" id="income0" placeholder="Main Applicant Annual Income"></a><br>
        <a><input name="income" type="text" id="income1" placeholder="Main Applicant Any-other Income"></a><br>
        <a><input name="income" type="text" id="income2" placeholder="Second Applicant Annual Income"></a><br>
        <a><input name="income" type="text" id="income3" placeholder="Second Applicant Any-other Income"></a><br><br>
        <a><button class="btnCal" onclick="calculateMort()">Calculator</button></a>
    </div>
</section>

Answer №2

To solve the issue of clearing floats, you can implement a technique known as clearfix. Another option is to use the clear:both property.

#header, #footer, #content-wapper, section {
   max-width: 100%;
   margin: 0 auto;
   text-align: center;
}
#leftContent{
   display: inline-block;
   width: 49%;
   height: auto;
   float: left;
}

input{
   width: 98%;
   height: 40px;
   border: solid 1px gray;
   background-color: white;
}

.center-content {
   width: 960px;
   max-width: 100%;
   margin: 0 auto;
   padding: 2vw 0 2vw 0;
   background-color: #E8E8E8
}

.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}
<section class="center-content">
    <div id="leftContent clearfix">
        <a><input name="income" type="text" id="income0" placeholder="Main Applicant Annual Income"></a><br>
        <a><input name="income" type="text" id="income1" placeholder="Main Applicant Any-other Income"></a><br>
        <a><input name="income" type="text" id="income2" placeholder="Second Applicant Annual Income"></a><br>
        <a><input name="income" type="text" id="income3" placeholder="Second Applicant Any-other Income"></a><br><br>
        <a><button class="btnCal" onclick="calculateMort()">Calculator</button></a>
    </div>
</section>

Answer №3

Dealing with this issue is quite common, just remember to incorporate a clearfix in your main div element:

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

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

Customizing the DatePicker with a unique button in material-ui

For my current project, I am utilizing a Datepicker component. I am looking to incorporate a custom information button in the upper right corner of the calendar layout, similar to the example image provided below: https://i.stack.imgur.com/fHMbn.png Unfo ...

I am curious about how to implement overflow:hidden along with position:sticky in React.js

My attempt to address the white space issue caused by a navbar I created led me to try using overflow:hidden. The navbar is generated using the Header component, and I desired for it to have a position: sticky attribute. However, I realized that I cannot ...

Eliminate the standard blue border that appears when control-clicking on table elements

I've encountered this question before, but unfortunately none of the solutions provided have worked for me. Some things I've attempted are: Using event.preventDefault() - did not produce the desired result. Removing user-select from CS ...

What is the best way to transfer data from an HBS template (HTML) to an Axios POST request?

Just diving into nodejs and express! I want to pass the user input (an email) to axios.post request's data when the "submit" button is clicked. I've verified that hard-coded data in the axios request is functioning correctly. data_email.hbs &l ...

Can Jquery use .hover to add a class or Child:hover to impact its parent element?

I have been searching for a solution tailored to my specific needs, and I am encountering difficulties in getting a seemingly simple jQuery code to function as expected. Here is the link to my basic fiddle: http://jsfiddle.net/LBHxc/ (I apologize for the ...

Experience the ultimate in slider automation with the cutting-edge power of

Here is an example of a website plugin I used on my site. You can check it out by clicking here. However, I'm having trouble automating the events that occur when the item slider is clicked. I would like to make these events happen automatically with ...

Tips for uploading an image file from Django Admin to the HTML

If I have already used a for loop in my code, how should I write the image address if I don't want to use 'for'? Can I directly write something like '/img/1.jpg'? Here is the directory where my images are stored: https://i.sstatic ...

The jQuery navbar's active class fails to function properly when an href is added

I'm facing a puzzling situation. I created a navbar using jQuery Mobile following the instructions on their official website: jQuery Mobile Navbar Demos Everything functions smoothly until I introduce href attributes in the list elements within the u ...

Ionic2 - Ion-select malfunctioning on a particular page

I have encountered an issue with the ion-select component in my Ionic 2 app. Specifically, when navigating to a certain page, the ion-select does not display properly. Strangely enough, on other pages of the app, this component works perfectly fine. Below ...

Unable to modify the .top attribute style in elements within an arraylist using JavaScript

I need to align multiple images in DIVs on the same line by setting their Top value to match the first image in the array. Here is the code I am struggling with: (addBorder function is called when divs are clicked) <div class="DRAGGABLE ui-widget-cont ...

Tips for handling null input values when saving to a database

<!DOCTYPE html> <html> <head> <title>Data</title> </head> <body> /* Enter your data here */ <form action="this.php" method="post"> <input type='text&a ...

Following an update, Storybook is failing to apply JSX styles

After upgrading my project from NextJS 10 to NextJS 12, everything is functioning normally except for Storybook, which is now missing its styles. I utilize the styled-jsx library to create embedded css, implementing it in this way: const SearchResult = ({ ...

`A brief disruption in loading the visual style of Google Maps`

Using the Ionic Framework, AngularJS, and Google Maps API, I encountered an irritating issue with my mobile app. Every time the map loads, there is a noticeable delay in loading the map style. This delay is particularly problematic as my map style converts ...

Is there any effort being made to utilize jQuery for the css @media screen and (max-width: 768px) function?

Is there a solution for handling the @media screen and (max-width: 768px) in CSS? I have created a responsive navigation bar, but when I change the display property of an element from none to block on mobile screens, it also appears on larger screens. Is ...

Managing information in a fresh window from the main source

I'm looking to use the window open() function to display a default calibration image initially Then have the ability to switch out images from a gallery on the parent page whenever needed My coding skills are a bit rusty, and I'm struggling to ...

Making a full-width browser bar by utilizing negative margins

I've been following a tutorial and need help with my header element. I want it to have a max-width of 800px and be centered in the middle of the page. Inside the header, there will be a #filtersBar div that spans the entire width of the page. Here&apo ...

Incorporating HTML snippets into files using PHP

When attempting to insert an HTML line into the "userlist.php" file using PHP code, I encountered an issue: <?php $username = "pajlok"; $type = ".jpg"; $html = <<<EOD <div onclick="window.location.href = 'user/pajlok/'" styl ...

Using Python and Selenium to Scroll Down the Page (with Two Separate Scrollbars)

I need help scrolling down the conversation section on Facebook Messenger. There are 2 scroll bars on the page, and I specifically want to scroll down scroll bar number 1 (see image) Click this link to access the page (make sure you are logged in and hav ...

I am encountering an issue with my register button not being able to submit in PHP+AJAX, while the

Having trouble with the registration page in PHP and AJAX. The login section works fine as I can sign in, but the registration button appears disabled or inactive, preventing form submission. I suspect an error somewhere, but unable to pinpoint it. Login ...

Adding spacing between <li> elements

My horizontal menu is made up of <li> elements styled with display: inline. These elements are designed to sit beside each other seamlessly. For ease of debugging, I prefer to have each li element on its own line in the source code: <li class=" ...