Ensure that two div elements always expand to occupy the entire width of the parent div container

Despite my research and understanding of floats, I am struggling to comprehend why I am unable to evenly float two divs within a wrapping div.

#wrap {
    border: 1px solid red;
    height: 200px;
    width: 100%;
}

#gr1 {
   border: 1px solid blue;
   float: left;   
}

#gr2 {
   border: 1px solid blue;
   float: left;
}
<div id="wrap">
<div id="gr1">Group 1
</div>

<div id="gr2">Group 2
</div>
</div>

My expectation is that Group 1 and Group 2 will always equally fill the width of the wrapping div as shown in the image below. I want this layout to remain consistent regardless of the browser size. Despite attempting to set gr1 and gr2 widths to 50%, gr2 consistently drops down below gr1. What aspect am I overlooking?

Answer №1

#container {
    border: 1px solid red;   
    height: 200px;
    width: 100%;
    
}

#group1 {
   border: 1px solid blue;
   float: left;
   
  
}

#group2 {
   border: 1px solid blue;  
   float: left;
}

#group1, #group2 {
  width: 50%;
  box-sizing: border-box
}
<div id="container">
<div id="group1">Group 1
</div>

<div id="group2">Group 2
</div>
</div>

Answer №2

Typically, when you set the size of an element, it doesn't take into account any borders or padding that are added to the element. This is why setting a 50% size may not work as expected, as the borders add extra pixels to the overall size.

To change this behavior and have the sizes include the border and padding, you can use the box-sizing property. Here is an example:

#wrap {
    border: 1px solid red;   
    height: 200px;
    width: 100%;
    
}

#gr1 {
   width: 50%;
   box-sizing: border-box;
   border: 1px solid blue;
   float: left;
   
  
}

#gr2 {
   width: 50%;
   box-sizing: border-box;
   border: 1px solid blue;  
   float: left;
}
<div id="wrap">
<div id="gr1">Group 1
</div>

<div id="gr2">Group 2
</div>
</div>

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

Using the Drupal Colorbox module with Internet Explorer

This problem has been driving me crazy! I'm struggling to make Colorbox show the borders correctly in IE7 (and even in IE6, but I'll settle for just IE7 at this point!). You can check out the issue here. When you click on a picture in the galler ...

Issues with Pdf.js functionality on Internet Explorer

Could someone shed some light on why this code isn't functioning in Internet Explorer? It works perfectly fine in Chrome. Snippet of my HTML: <head> <script type='text/javascript' src="//code.jquery.com/jquery-1.9.1.js"></sc ...

Tips for making the height of <select> and <button> elements equal even with padding

I'm trying to make sure that both the 'select' and 'button' elements have the same height. Check out my JS Fiddle here Here's the HTML code: <select><option>...</option></select><button>Button& ...

jQuery preventing form submission after returning false

There's a small issue that I need help with. After submitting the form and receiving a false response, it prevents resubmission. For example, if a user forgets to input their name, they will see an error message asking them to do so. However, the u ...

Excessive spacing in the <td> elements - TABLE

I am encountering some problems with my table layout. The spacing between the field names and text boxes appears to be too large. Could there be mistakes I'm making that are causing this issue? How can I minimize the gaps? Please refer to the image b ...

Issue encountered with JavaScript function within TypeScript file connected to HTML code

I am currently working on a simple SharePoint web part and encountering an issue with using a function from another module file in my main file. Snippet from the JSFunctions.module.js file (where I define my function): function getApi(){ [my code]... }; ...

Share the hyperlink to a webpage with someone else

In my SQL command, I have a unique feature that retrieves the complete URL value of the current page: [##cms.request.rawurl##]. This code returns the entire URL. I need to send this address to another page and load it into a special div called "load-fac". ...

iterative outcomes with ajax and jquery scripting in javascript

Hey there! I'm trying to create a JavaScript script using AJAX that will extract the id and value of a button when hovered over. The extracted value will then be sent to a PHP script on the same page, where it will be used as a variable for searching ...

steps for making personalized video embed dimensions in bootstrap

Update: I have successfully integrated a non-standard bootstrap video size with proportions of 4x5. However, unlike standard bootstrap embeds, it doesn't seem to adhere to a maximum height no matter where I place it - on the iframe or div elements ar ...

I am experiencing an issue where certain HTML classes are not applying the CSS code properly within my PHP file

I am facing an issue with my code where some of the HTML classes are not applying the CSS styles correctly, such as the first class "class="foot"". Below are the files that I have: * { margin: 0%; padding: 0%; ...

The Chrome Elements console fails to display a specific element when provided with an xPath

When I press F12, navigate to Elements, and then click ctrl+F to search for an xPath. If only one element is found, it is not highlighted (although Chrome will indicate 1 of 1). However, if multiple elements are found, Chrome highlights from the second one ...

Executing Selenium test cases based on HTML tag is a key aspect of automated testing

I need to run my selenium test scenarios based on the value of an HTML tag, for example: <meta name="survey name" content="ABC Car"> The content represents different event types, with a total of 4 possible event types. ...

Rotating the Orientation of Cellphone Images

Currently facing an issue where the images I am attempting to display on a basic html page appear rotated to the left, likely due to being taken by a cellphone. These images are stored on my computer. I have tried using the css property image-orientation: ...

The collapse button in Bootstrap 3.3 and jQuery 1.1 appears to be visible but unresponsive when clicked

This code isn't functioning properly as it displays the three horizontal bar button, but nothing happens when clicked. bootstrap-3.3 jquery-1.1 Toggle navigation CopyHere <div class="collap ...

Changing the visibility of a div with an onClick event in React

Can anyone help me figure out how to toggle the display of this div on click? I've been struggling with it for a while now. Any suggestions are welcome. https://i.sstatic.net/lPZtN.png https://i.sstatic.net/8Pybg.png ...

Achieve full height of parent with rotated text

Hey, I've been experimenting with some CSS transforms and I'm having trouble getting this to work properly. Check it out here I'm using Bootstrap 4 to create two columns; an image on the left and text on the right. I want the text to be ce ...

The Tab style in Mobile Angular UI does not get applied correctly when nested within an ng-repear

While working on a tabbed control with mobile-angular-ui (), I encountered an issue when trying to generate the tabs dynamically. Initially, everything looked good with the following code: <ul class="nav nav-tabs" ui-state='activeTab' ui-def ...

Having trouble locating the nivoslider IE8 jQuery bug, can't seem to track it

I am encountering an issue with the nivoslider script in IE8 and I am having trouble identifying what is causing it. The nivoslider works perfectly fine in all other browsers except for IE8. Any help or guidance on this matter would be greatly appreciated ...

store picture in a database: ServiceNow

When filling out a form, I often copy and paste images from webpages into a journal field. However, these images end up being saved as references to the original webpage. Is there a method to save these images directly to the form or record instead? ...

What is the best way to arrange cards in a horizontal stack for easy carousel viewing later on

My main objective is to design a unique card carousel where users can navigate through the cards by clicking on arrows placed on the left and right sides. The approach I am considering involves stacking multiple card-decks and smoothly transitioning to th ...