Uncertainty surrounds the interaction of computed height and margins with CSS float right

What is the reason behind this HTML code:

<html>
    <head>
        <style type="text/css">
            .left { background-color: yellow; }
            .right { float: right;  background-color: lightgray; width: 150px; }
        </style>
    </head>
    <div>
        <div class="right">
            <p>right</p>
        </div>

        <div class="left">
            <p>left</p>
        </div>
    </div>
</html>

... causing div.right to have a greater height than div.left (in FF and chrome):

Answer №1

Adjust the styling of your <p> tag.

p {margin:0;padding:0;}

To learn more about resetting CSS, visit this link:

Answer №2

the reason for this issue is that you have applied a float property to the right-side division instead of the left-side division. Consequently, the margin specified for the paragraph element is contained within the right-side division, while in the case of the left div, it extends beyond the boundaries of the division.

Answer №3

What is happening here illustrates the relationship between collapsing margins and floats.

Take a look at this HTML snippet:

<div class="wrap">
    <div class="right">
        <p>right</p>
    </div>
    <div class="left">
        <p>left</p>
    </div>
</div>

This code resembles your setup but includes a wrapper element.

Now, examine the CSS rules:

.wrap {
    outline: 2px dotted red;
    padding: 1px;
}
p {
    outline: 1px dotted blue;
    margin: 2.00em 0;
}
.left {
    background-color: yellow;
}
.right {
    float: right;
    background-color: lightgray;
    width: 150px;
}

I introduced the .wrap class with 1px padding to highlight the margin effects clearly.

Here's the demonstration link for better understanding: http://jsfiddle.net/audetwebdesign/9hhkk/

The content is displayed with both texts aligned on the same baseline, and the right text floated to the right as intended. Both left and right elements share identical margins.

In your case, the top margin of the .left collapsed with its container's top margin. However, since the .right element is floated, its margins do not collapse, resulting in additional padding space.

I configured my example so that margins stay separate for in-flow elements.

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

Uninstall webpack-dev-server version 1.14.1 and replace it with version 1.14.0

Can someone help me with the process of uninstalling webpack-dev-server 1.14.1 and installing version 1.14.0 on Ubuntu using just commands? Error message: Uncaught TypeError: Cannot read property 'replace' of null at eval (eval at globalEval (jq ...

As you scroll, the top block in each of the three columns will remain fixed within its

I need assistance with a problem involving three columns and multiple blocks within each column. Specifically, I want the first block in each column to remain fixed at the top when scrolling. However, once you reach the bottom of each column, the first blo ...

Tips for implementing styling in a material table within a reactjs application

Is there a different way to set the display property to inline-block since cellStyle doesn't seem to recognize it? How can I adjust the width of a specific column, let's say with the name title, without affecting the width of all other co ...

I'm having trouble with the calculator, unable to identify the issue (Typescript)

I'm struggling with programming a calculator for my class. I followed the instructions from our lesson, but it's not functioning properly and I can't pinpoint the issue. I just need a hint on where the problem might be located. The calculat ...

Identifying bots with Python's Requests and BeautifulSoup in web scraping tasks

Attempting to scrape all the HTML elements from a page using requests & beautifulsoup. Utilizing ASIN (Amazon Standard Identification Number) to extract product details from a page. The code snippet is as shown below: from urllib.request import urlope ...

Looking to adjust the positioning of text being inserted into a column - unable to utilize text-align or float properties

I'm struggling to position my text in a specific area within a column using CSS. Our app displays a price when a quantity is selected, but I can't seem to center the $14 under 'Item Total'. Even using float: center; hasn't worked f ...

Increasing the nth-child Selector in Jquery

Referring to this I am looking to cycle through the nth-child selector, which involves: var i = 1; var tmp = $(this).find('td:nth-child(i+1)'); // I wonder how this can be achieved i++; I have rows with dynamically generated columns i ...

Customizing Thickness for Tab Labels in MUI 5

I have been trying to adjust the thickness of the label inside a tab using MUI 5, but so far I haven't had success. Here is what I have attempted: interface TabPanelProps { children?: React.ReactNode; index: number; value: number; } funct ...

Efficiently formatting text in a div container

How can I ensure that the text within a span is word-wrapped inside a div? https://i.sstatic.net/W691d.png Here is the code snippet: <div class="col-xs-6 choice btn btn-default" > <span class="pull-left">(Kepala) Bagian Purchasing (not lis ...

Is there a way to retrieve a numerical value from within two specific elements when web scraping?

I am new to web scraping and looking to extract the numbers located between the strong tags. Currently, I am using Python 3.8 along with BeautifulSoup for this task. <li class="price-current"> <span class="price-current-label"> </s ...

How can you exhibit various images without relying on the <img> tag?

Is there a more efficient way to display over 500 images from a folder on an HTML page without the need to manually write out each img src tag? I have searched online for solutions, but most suggestions involve using PHP or "glob", which I am hesitant to ...

What is the best way to horizontally center items within an unordered list (ul li) inside a div class

I'm attempting to center a horizontal lineup of small icons. In the Launchrock platform, users have the ability to customize all code. Unfortunately, I do not have access to their default CSS, but we are able to override it. <div class="LR-site-co ...

I am having trouble debugging the Twitter Bootstrap v4-alpha grid. The col-sm-offset-* class doesn't seem to be

Something has been altered somewhere, causing col-sm-offset-*s to stop functioning entirely. For instance, when attempting to split the screen in half and only display content on the right side, I typically would use: <div class="container"> < ...

Having trouble with the filtering feature in Material UI Datagrid

I'm currently using Material UI Data Grid to display a table on my website. The grid has an additional filter for each column, but when I click on the filter, it hides behind my Bootstrap Modal. Is there a way to bring it to the front? https://i.stac ...

What are the steps for implementing if-else statements in JavaScript when working with multiple dropdown lists?

I have encountered an issue while trying to display options in multiple drop-down lists. Only two words (CHENNAI AND IOS-XE, BANGALORE AND IOS-XR) are being displayed and the rest of the words are not appearing. Can someone provide assistance on fixing thi ...

Avoid repeating media queries in MUI JSS by combining them efficiently

Is there a more efficient way to apply the same set of CSS styles to multiple media query rules without repetition? Currently, the only workaround seems to be: [theme.breakpoints.up('xl')]: { color: 'red', backgroundColor: 'gre ...

Creating a website with a seamless image background that fills the entire page

! Is it possible to achieve the same effect using AngularJS? Below is the CSS code: .bg { background: url(holiday-car-rental.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-s ...

How can I showcase the selected file using jQuery's querySelector?

I have successfully implemented a file upload feature using jQuery querySelector. Now, I am looking for a way to display the selected file name in a span element after the user selects the file. Here is my HTML code: <span id="filename"></span&g ...

Adjust the form input box height to a different size

Is there a way to adjust the height of the form input box so that the text is positioned close to the top and bottom borders of the box? I attempted to modify the CSS but was unsuccessful. Any suggestions for a Bootstrap CSS solution would be greatly appre ...

Force the page to refresh if the user navigates back using the browser's back button

Similar Question: Cross-browser onload event and the Back button I am looking for a way to automatically refresh a view or page when a user navigates back to it using the browser's back button. Any suggestions? ...