How does adding padding cause two horizontal divs to become vertical?

I noticed that when I remove the padding from the code below, two vertical divs become horizontal. This is confusing to me because I thought padding only affects internal spacing. I was expecting the two divs to be aligned horizontally.

<style>
#wrapper {
width:100%;
margin : 0;
}

#first-div {
width:50%;
margin : 0;
float : left ;
padding: 10px;
background-color: green;
color: white;
}

#second-div {
width:50%;
margin : 0;
float : left ;
background-color: blue;
color: white;
padding: 10px;
}
</style>
<div id="wrapper">
        <div id="first-div" >
            First  div content here...
        </div> 
        <div id="second-div" >
            Second div content here...
        </div>
    </div>

Answer №1

  <div id="container">
            <div id="left-column" > ;
                Content for left column goes here...
            </div>
            <div id="right-column" > ;
                Content for right column goes here...
            </div>
        </div>
<style>
#container {
width:100%;
margin : 0;
}

#left-column {
width:50%;
margin : 0;
float : right ;
padding: 10px;
background-color: green;
color: white;
box-sizing: border-box;
}

#right-column {
width:50%;
margin : 0;
float : right ;
background-color: blue;
color: white;
padding: 10px;
box-sizing: border-box;

}
</style>

Make sure to set the box-sizing property of your div elements as border-box so that padding is accounted for inside the border.

Answer №2

The reason for the element collapsing to a new line is because the width is set at 50% and extra padding is added on top of that, causing it to overflow (especially when using float: left)

To prevent this issue, applying box-sizing: border-box to each div will ensure that padding is included within the width of the element

Answer №3

Consider using display flex instead of float:

#wrapper {
width:100%;
margin : 0;
display: flex;
justify-content: flex-start;
flex-direction: column;
}

#first-div {
width:50%;
margin : 0;
background-color: green;
color: white;
padding: 10px;
}

#second-div {
width:50%;
margin : 0;
background-color: blue;
color: white;
padding: 10px;
}

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 keep fixed input at the bottom of the page?

Let me begin by stating that I struggled to come up with a more appropriate name for this question than the one above. My issue is not limited to a fixed input; it's much more complex. Let me delve into the details of my problem here. The Scenario I ...

An additional "?" symbol found in the PHP file at the top left corner

I am experiencing an issue with a PHP page where a "?>" symbol appears in the top left corner. The code snippet looks like this: <?php include_once("DBconnect.php"); $getuser = $_POST["RegUsername"]; $getpass = $_POST["Pass"]; $getrepass = $_POST["ReP ...

Tips on arranging the layout to prevent text from shifting alongside the image

Is there a way to prevent the positioning and size of an image from affecting the layout of my list? I'm new to coding and struggling with this issue in my first project. Any advice or guidance would be greatly appreciated. <!DOCTYPE html> <h ...

Customize the X and Y position of the CSS background with specific degrees

Looking for help customizing the background position in a PSD image to adjust the slope at the top, right-left side, and bottom. Here is my PSD: https://i.stack.imgur.com/Jr4h7.png Below is some of my CSS code: html{ background:#aea99f; } body{ backgroun ...

Personalized HTML characteristics

Within the realm of AngularJS (and possibly jQuery UI), I've come across tags such as ui:some_random_name and ng:some_random_name. It seems that according to the HTML specification, non-standard attributes are not allowed. So how do these libraries m ...

What is the best way to display content at a specific time using Angular?

Whenever a user searches something and no results are found, I display a message. However, when there is content to show, an error message pops up for about 3-4 seconds before disappearing and showing the search results. This is my HTML: <div> &l ...

Having difficulty passing a variable between two different PHP files

When attempting to modify data in the database for a specific user column, I encountered an issue. The code that I created only alters the data for the last user in the database, rather than the current user. For instance: Let's say I am logged in as ...

Using jQuery to animate a div when clicked by the mouse

Here's the scenario: I have 3 block elements arranged in a specific order: Image Hidden Text Block (.hidden) Footer Block (.blockimage) When the page loads, the image is displayed over the hidden text block (which contains further infor ...

Applying Tailwind styles to dynamically inserted child nodes within a parent div

Currently, I am in the process of transitioning my website stacktips.com from using Bootstrap to Tailwind CSS. While initially, it seemed like a simple task and I was able to replace all the static HTML with tailwind classes successfully. However, now I ha ...

Entering text into a field / Inserting data into a MySQL database / Presenting

I am facing a challenge that I'm uncertain how to resolve. The issue lies with a textarea on my form that permits the user to input data in the following format: Windows XP Home and Professional<br />(32 and 64 bit) Windows 7 Home and Professi ...

Show results based on the selection of multiple checkboxes upon submission

When creating PHP pages to fetch data from databases, each checkbox is linked to a different page. I want to be able to check one or multiple checkboxes and then click the submit button to display the PHP pages that are linked to the checked checkboxes. Ho ...

Gathering Servlet details from non-form elements

I am currently in the process of developing an application that is capable of parsing JSON strings. At this stage, I am able to input a JSON string using Java, write it to an HTML document, and then have a JavaScript program read and parse it before ultima ...

Extract Information from a Website

Is there a way to extract data from another website using JavaScript and save it into a .TXT or possibly an XML file? If JavaScript is not the best solution, I am open to other suggestions. I am specifically interested in extracting the price and item na ...

Boost your website's loading time by utilizing the async and defer attributes

Looking to enhance the speed of my webpage, particularly focusing on improving initial page speed. Research suggests that using the async and defer attributes for JavaScript can be beneficial. All JavaScript scripts are currently placed just above the cl ...

Is there a way for me to personalize the background of the mui-material datagrid header?

I am looking to update the background design of Mui Datagrid from this image to this image However, I am encountering an issue where the background color does not fully cover the header. I have attempted using "cellClassName:" in the columns but it has n ...

Guide on Applying a Dynamic Color in VueJs 3 Composition API/Vuetify Using CSS

Currently, my project utilizes Vue 3 with the composition API and Vuetify for the UI. I am looking to utilize a color that is already defined in a Vuetify theme variable within my CSS, similar to how I have done it previously in JavaScript. Although I at ...

How to animate a left border shifting to the center using JavaScript

As I'm modifying my current div, I need to add a vertical line in the center of it. I've come across various solutions where a left border is added and then shifted using the left property by 50% (which effectively places it in the middle). Here ...

Looking for assistance in designing SVG tabs?

I'm looking to create tabs that have a design similar to the one shown in this image. Initially, I attempted to achieve this using plain CSS by creating a triangle with the border trick but encountered challenges in adding the gray border. I am now co ...

Tips on presenting messages to users after clicking the submit button?

I am trying to extract data from a table. I am unsure if my current code is able to retrieve the value from the table. <script> function validateForm() { var x = document.forms["assessmentForm"]["perf_rating11"].value; var y = document.f ...

Dynamic menu bar accompanied by primary content

I'm facing an issue with my active navigation bar. Whenever I open the hamburger menu, the main content remains fixed and does not move along with the open navigation menu. I tried searching online for a solution but couldn't find anything helpfu ...