What causes the lack of upward movement for a div element when the div above it is floated to the left?

Recently, I've been delving into the world of float property in CSS.

This is the HTML code snippet I'm working with:

.left {
  float: left;
  text-align: center;
  width: 25%;
}
.normal {
  text-align: center;
  width: 25%;
}
<div class="left">
  <img src="Icon.png">
  <h1>Hi, my name is Left</h1> 
  <p>I'm a paragraph containing random characters. </p>
</div>
<div class="normal">
  <img src="Icon.png">
  <h1>Hi, my name is Normal</h1> 
  <p>I'm another paragraph of random letters.</p>
</div>

I'm puzzled as to why my div with the 'normal' class isn't aligning itself vertically alongside the div of 'left' class.

This question arises when I notice that if I insert a

<p>something</p>

right after my div with the 'left' class, the content of the p tag moves upwards, unlike what happens with a div. I wonder, why does this discrepancy exist?

Answer №1

The initial div has a width of 25%, meaning it occupies a quarter of the available horizontal space.
The following div also has a width of 25%, causing the first div to fill the entirety of the second div's potential area. Thus, there is no additional room on the right!

Solution: Adjust the second div's width from 25% to 50% (an increase of 25% relative to the first).

.left {
  float: left;
  text-align: center;
  width: 25%;
}
.normal {
  text-align: center;
  width: 50%;
}
<div class="left">
  <img src="Icon.png">
  <h1>Greetings from the left side</h1> 
  <p>This is a paragraph with some random words.</p>
</div>
<div class="normal">
  <img src="Icon.png">
  <h1>Hello from the normal side</h1> 
  <p>Another paragraph with more random content.</p>
</div>

Answer №2

Hello @Gopal Singh Rathore

After reviewing your query, I am confident that I have grasped the essence of it.

The issue lies in the fact that the width is defined within the div element which also has a display property set to block. By changing the display property to flex, the content will align next to each other. Similarly, if you assign a specific width to the p tag, it will exhibit the same behavior as the div element.

I trust that this explanation provides clarity on the matter.

Answer №3

The ordinary box shifts upwards, but its regular contents remain in place: Check out the standard expansion:

Professor Anderson offers a solution: you must increase the width of normal so that the content can align to the right of left

.left {
  float: left;
  text-align: center;
  width: 25%;
}
.normal {
  text-align: center;
  width: 27%;
  background-color: lightblue;
  border: solid 1px blue;
}
<div class="left">
  <img src="Icon.png">
  <h1>hi my name is left</h1> 
  <p>hi im a paragraph of abcd. ghjtyj yttyrkutk ukuykyilt uk</p>
</div>
<div class="normal">
  <img src="Icon.png">
  <h1>hi my name is normal</h1> 
  <p>hi im a paragraph of abcd. ghjtyj yttyrkutk ukuykyilt uk</p>
</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

I am looking to incorporate a 10 px border at the top of my column by utilizing bootstrap4

How can I add a border to the top of my column using Bootstrap 4? I've been struggling with it and can't seem to get it right. Here is the code I'm currently using, which includes <div> tags for the columns: <!doctype html> &l ...

Issue with Cascading Dropdowns in jQuery

I am currently experiencing issues with a piece of code on my website (also accessible via this link). The code consists of 2 select fields that should display different data based on the selection made. However, I have identified 2 bugs within the code. ...

augmentable form that can be expanded flexibly

Perhaps I'm missing something really simple here, but I just can't seem to figure this out. I'm attempting to create a form that can dynamically extend. The issue I'm facing is that I can't get this particular code to function: & ...

Having trouble loading image on web application

Currently, I am facing an issue while attempting to add a background image to an element within my Angular web application. Strangely enough, the relative path leading to the image seems to be causing my entire app to break. https://i.stack.imgur.com/b9qJ ...

Paypal Payment Plans on a Monthly Basis Utilizing the Bootstrap Pricing Slider

I came across this fantastic Bootstrap Pricing Slider that I found originally at Within my Bootstrap Pricing Slider, there is a "total amount" calculated after some math, resulting in a final score. The slider includes a "Process" button which currently ...

A simple demo showcasing interactive HTML pages using Django and AJAX

In my search for answers, I came across the following helpful posts: How to Reload table data in Django without refreshing the page Tutorial on Django dynamic HTML table refresh with AJAX Despite the insights from these posts, I am still facing chal ...

Div takes on the opacity of its parent element

I want to implement a modal popup using the Ajaxtoolkit's modalpopupextender, and so far it's working perfectly. While the popup is displayed, I need to dim out the rest of the page. To achieve this, I applied the modalPopup CSS class to the pop ...

Assign ratings to values based on stars

I am currently facing an issue with implementing a star rating system on my web application. The problem lies in retrieving previously rated values and displaying them as blinking stars. For example, if a user had previously rated something with 4 stars, t ...

Display the icon beneath the list item label

How can I display the ion-edit icon below the list names? The icons are currently appearing on the right side of each list name, but I want them to be aligned below with some spacing. When clicking on "Data Source Connections," a sublist will be shown whe ...

What is the best way to show an error message on a field when using a custom form validator?

In my JavaScript code, I have created a form validator that is capable of validating different input fields based on specific attributes. However, I need assistance with implementing error handling and scrolling to the erroneous field. For each <input& ...

In Safari, my layout is disrupted by the mere mention of the word 'City'

There's something strange happening in Safari that I can't quite figure out. Here's how my layout looks in Chrome: https://i.sstatic.net/lPhnP.png But when viewed in Safari, it looks like this: https://i.sstatic.net/IMnQb.png For some r ...

Determine the overall cost based on varying percentage increases for each step

I am currently working on developing a price estimation calculator. Here is the breakdown: For 1000 MTU, the price will be 0.035, resulting in a total of 35. For 2000 MTU, the price will be 0.034, making the total 67. For 3000 MTU, the price will be 0.03 ...

Customizing the display field in an ExtJs Combobox

I am working on a java web application that utilizes an entity class to populate a combobox with ExtJs. The issue I am facing is as follows: Some entries in the displayField may contain html code. To prevent any issues, I used flexjson.HTMLEncoder during ...

What is the best way to display HTML content from a stored object?

In my project using Next.js, I am faced with the challenge of rendering custom landing pages based on their unique URLs. While I have successfully retrieved all the necessary details from the database for each specific URL, there is a particular object con ...

Selecting a value from a populated dropdown and checking the corresponding checkbox in SQL Server

There are 8 checkboxes in this example: <table style="border-collapse: collapse; width: 100%;" border="1"> <tbody> <tr style="height: 21px;"> <td style="width: 25%; height: 21px;"><strong>Technology</strong& ...

Place a small image within a list item to ensure it matches the height of the list element

I need help aligning images to the right within list elements. The images are squares with equal height and width, and I want them to fit snugly within the height of the list element on the right side. My HTML code is quite simple for now (see example bel ...

Ways to customize a directive to show conditionally with no need for container elements

I have created a basic role-based security directive in angularjs. It's my first attempt at making a directive. My goal is to replace the following HTML: <authorize if-granted="GET_POSTS"> Hello WORLD!!!! {{name}} </authorize> with j ...

problem with arranging photos and captions

When viewing this page in a narrow browser window, an issue arises with how the photo captions are displayed at the bottom of the page. It's evident that the caption extends past the right edge of the photo. My preference would be for the photo and c ...

The function insertAdjacentHTML does not seem to be functioning properly when used with a

I am working with a parent element that contains some child elements. I create a DocumentFragment and clone certain nodes, adding them to the fragment. Then, I attempt to insert the fragment into the DOM using the insertAdjacentHTML method. Here is an ex ...

I'm having trouble getting Firefox to recognize the percentage height set on table rows. Any suggestions for a workaround?

The goal is to set the height of the first row to 70% of the screen. To prevent the scrollbar from appearing on the entire webpage, overflow has been set to auto within the div of the first row. This solution functions correctly on Chrome and IE, however, ...