DIV that floats and P element

When utilizing the float attribute, two div elements align next to each other, but two paragraph elements do not; causing the 'p' element to display oddly.

Interestingly, when floating two 'div' elements and two 'p' elements, the alignment is much better.

For a detailed explanation, please refer to my 3 code examples (note: the link to my 3rd code is provided in my comment below as I currently do not have enough reputation points to include 3 links):

Code Example 1: https://jsfiddle.net/dipeshsukhani/v7r45zeg/

#superhero {
  background-color: yellow;
  float: left;
}

#supervillain {
  background-color: pink;
 }
<p id="superhero">
  SUPERHERO
</p>
<p id="supervillain">
  SUPERVILLAIN
</p>

Code Example 2: https://jsfiddle.net/dipeshsukhani/51mtncx8/

#hero {
  background-color: lightcoral;
  float: left;
}

#villain {
  background-color: lightcyan;
}
<div id="hero">
  HERO
</div>
<div id="villain">
  VILLAIN
</div>

Code Example 3 :

#hero {
background-color: lightcoral;
float: left;
}

#villain {
background-color: lightcyan;
}
  
  #superhero {
background-color: yellow;
float: left;
}

#supervillain {
background-color: pink;
}
  
<div id="hero">
HERO
</div>
<div id="villain">
VILLAIN
</div>
<p id="superhero">
SUPERHERO
</p>
<p id="supervillain">
SUPERVILLAIN
</p>

Why do we observe different outcomes when floating 'div' only, 'p' only, and 'div and p in a single code'?

Answer №1

Both instances you provided demonstrate a similar floating behavior. The reason why you may find the p version confusing is due to the default top and bottom margins set by the browser (-webkit-margin-before: 1em; in Webkit for top). These margins can make the first floated paragraph appear as if it is not floated, even though it is.

You can easily verify this by resetting the margin to 0:

#superhero {
  background-color: yellow;
  float: left;
  margin: 0;
}

#supervillain {
  background-color: pink;
}
<p id="superhero">
  SUPERHERO
</p>
<p id="supervillain">
  SUPERVILLAIN
</p>

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 ReactJS to create cross-browser inline styling with flexbox

I'm currently working on a React web application that relies heavily on inline styling. My goal is to ensure compatibility with the latest versions of major browsers (Safari, Chrome, Firefox, IE) while utilizing flexbox for layout. The issue I encou ...

Issue with Bootstrap tab display of content

I'm having trouble with the tabs in my page. When I click on each tab, the content doesn't display correctly. Here is my code: <div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;"> <ul id="myTab" class="nav nav ...

Transferring class titles between div elements

I would like to implement a feature where class names on div elements shift after a brief delay, perhaps 10 seconds. My goal is for the 3 classes listed below to cycle through the div elements and start over once it reaches the last one. For instance: & ...

Is there a way to ensure that the div is displayed on the same line?

Check out the jsfiddle link provided: http://jsfiddle.net/HE7yT/ I'm trying to align the Image, content, and Amount in the same line. How can I achieve this? The goal is to have "150" displayed on the same line as the picture. var HTML = $(' ...

Using CSS on a randomly selected div that is chosen after dividing the main div each time it is clicked

Imagine a square box displayed as a "div" element. When you click on it, it splits into an n x n grid with each square having a random background color. However, the issue I am encountering is that I want to apply additional CSS to each of these randomly c ...

Enliven the transition between grid sizes

I'm currently working on a project using React and Material UI. Seeking assistance on how to implement an animation for changing the size of a grid item. By default, the item is set to sm={3}, but when a user hovers over it, I want it to change to sm ...

Ways to expand logo space within the header of the Twentysixteen Theme on WordPress

I am facing an issue with my logo being resized to a much smaller size of 200px even though it is originally over 2000px wide. In the style.css file, I found the following code: .custom-logo { max-width: 180px; } Despite changing the max-width value t ...

Main navigation category as parent and secondary category as a child menu in WordPress

Hello there, I am looking to create a navigation menu with parent categories displayed horizontally and child categories as corresponding submenus. For example, The parent categories would be Apple, Orange, and Mango Under Apple, we would have apple1, ...

How to Create Smooth Transitions for Text Arrays using jQuery's Fade In and Fade Out Features

I need to loop through an array of text and apply jQuery's fadeIn and fadeOut functions to each element. var greetings = ["hi, I'm", "bonjour, je m'appelle", "hallo, ich heiße"] The HTML structure I want is as follows: <h2><span ...

CSS-Only tooltip that adjusts size dynamically

Having trouble getting a tooltip to work exactly as desired? I need to implement tooltips for the contents of dynamically built HTML tables. It must be done with CSS only, without relying on JavaScript or events for performance reasons. The challenge is ha ...

Resolving problems with the positioning of the navigation bar

I am in the process of creating a simple website, but I am encountering an issue with the menu bar shifting below the "Contact for Services" section when I view the page in different resolutions. Is there a way to lock the position of the menu bar so that ...

Is it possible to apply border-radius to selections, similar to how it is done in vscode

It was reminiscent of this... Visual Studio Code ::selection{ border-radius: 5px; } I attempted something similar to this, however it was not successful... ...

Photographs featuring a ripple effect in the columns of Zurb's Foundation framework

Working on a WordPress site using the Foundation 6 CSS framework, I have a design with a row containing two large-6 columns, each filled with an image featuring a wave shape. Check out the image I've linked below. View the row with 2 columns I' ...

My website's logo is not appearing on the navigation bar in the HTML code

Hi there! I recently tried adding a logo to my navigation bar in an HTML file, but for some reason, the logo doesn't appear when I run the file. I'm currently learning web development through a course on Coursera, and I am quite new to programmin ...

Guide on aligning a series of divs in a single row within a parent div

Within the confines of a 400px wide div called "container", there are six left-floated "box" divs, each 100px wide. The combined width of the "box" divs exceeds 400px, resulting in the divs wrapping onto two lines, with 4 on the first and 2 on the second ...

Error: The function explore() is missing a required parameter called 'path'

Here is my code snippet: def navigate(directory): # To avoid issues with forward slashes, normalize the path directory = os.path.normpath(directory) if os.path.isdir(directory): subprocess.run([FILEBROWSER_PATH, directory]) elif os.path.isfile(directo ...

How to perfectly align a button at the center and position it in the forefront?

I'm attempting to place a button in front of my video and center it on the video. I've tried using position-relative to bring it in front and flex to center it vertically and horizontally, but it's not working as expected. The z-index is cor ...

What methods can be utilized to ensure that my wistia video player/playlist is responsive on different devices

I need some assistance with making my Wistia player responsive while using a playlist. I want the video player to adjust its size based on the screen size. Here is an example: My current code utilizes their iframe implementation: <div id="wistia_1n649 ...

Alter the row's color using the selection feature in the module

Is there a way to change the color of a row when a specific property has a certain value? I found a solution for this issue on Stack Overflow, which can be viewed here: How to color row on specific value in angular-ui-grid? Instead of changing the backgro ...