Can the "clear:right" property impact elements that are floated to the left?

Consider the following scenario:

<div class="right">Box right 1</div>
<div class="right">Box right 2</div>
<div class="left">Box left</div>

<p>... lengthy content ...</p>

and this CSS code:

.right { float:right; clear: right; }
.left { float:left; clear: left; }

div { /* some styles for the divs */ }

When viewed ([see demo](http://jsfiddle.net/uTRAr/1/)), the layout appears differently than expected.

I anticipated the left box to be top floated, as seen here:

Question: Why is the left box not being floated to the top? Even though it comes after in the markup, shouldn't div.left also start at the top?

Note: Removing clear: right from the CSS of class .right results in the desired top floating for the left box ([see demo](http://jsfiddle.net/uTRAr/3/)).

It seems that clear: right impacts left-floating elements as well...

Note 2: Rearranging the markup floats the boxes correctly ([demo](http://jsfiddle.net/uTRAr/4/)):

<div class="right">Box right 1</div>
<div class="left">Box left</div>
<div class="right">Box right 2</div>

<p>... lengthy text ...</p>

Answer №1

It should come as no surprise that using clear: right will only clear floats to the right, leaving left floats unaffected. Interestingly, this behavior is governed by rule #5 in the specifications outlined in the specification document:

These are the specific regulations that dictate how floats behave:

[...]

  1. The top boundary of a floating box cannot exceed the upper boundary of any block or floated box created by an element earlier in the document.

This limitation on clearance means that only floats preceding an element in the source will be impacted.

By clearing the second right float, it ends up positioned below the first one. Consequently, with the left float following both right floats in the source, it can't float higher than the second float due to the clearance effect on the latter.

If you were to place the left float at the very beginning of the source:

<div class="left">Box left</div>
<div class="right">Box right 1</div>
<div class="right">Box right 2</div>

<p>... long text ...</p>

You would observe the same anticipated outcome. This demonstrates the importance of distinguishing between the effects of clear: right and clear: left/both. Only when instructed to clear in both directions will the left float be affected by both right floats.

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

Placing a Div wrapper around the contents of two corresponding elements

I am currently attempting to wrap a div with the class name 'wrapped' around two other divs containing innerHTML of 'one' and 'two'. <div class='blk'>one</div> <div class='blk'>two</di ...

Issue with displaying Flask variables in HTML

I am currently utilizing a small flask app for the following purposes: Creating an HTML page with a leaflet map Collecting user input from the HTML page... Performing calculations on that input using specific python modules Returning those variables to J ...

Prevent the elongation of HTML select elements in Bootstrap

Struggling to set the size of an HTML "select" field in Bootstrap 3 (latest) to normal width instead of 100%? Looking for a cleaner solution than resorting to tables or adding fields within bootstrap columns that might cause indentation issues due to borde ...

Utilizing Django to extract values and dynamically inserting them as placeholders on a separate page

On my initial webpage, there is a submit button that includes an array of necessary information in the value tag. <button type="submit" name="editEntry" value="[{{ entry.id }}, {{ entry.first_name }}, {{ entry.last_name }}, {{ entry.country }}, {{ entr ...

Using jQuery to extract a specific portion of a URL path

Seeking assistance with extracting category information from lengthy URLs and assigning it to a variable. Consider the following URL: http://example.com/community/home/whatever.html The goal is to assign the variable to the folder path that follows /hom ...

Design a sleek and polished calendar for an online form that exudes professionalism

In the process of designing a form that involves booking, I am in need of a calendar component where users can input their desired date. Is there an existing calendar solution available rather than starting from scratch? My development stack includes html ...

Unable to bring in CSS module in a .tsx file

I am currently working on a basic React application with TypeScript, but I am encountering difficulty when trying to import a CSS file into my index.tsx file. I have successfully imported the index.css file using this method: import './index.css&apo ...

A tutorial on creating dynamic text animations: sliding text effects on web pages

Does anyone know how to create a sliding in and out effect like the one on this page: ...

iOS now supports PowerPoint presentations through the use of the .pptx file format

Attempting to incorporate a pptx presentation with videos and animations into an iOS native app has proved challenging. Although UIWebView successfully renders the presentation allowing for slide browsing, it lacks interactivity such as button clicks that ...

Angular 6 Calendar Template issues with parsing: Unable to link to 'view' as it is not recognized as a valid property of 'div'

I am in the process of developing an application that utilizes this angular calendar. My tech stack includes Angular 6 with AngularFire2 and Firebase. Below is my app.module.ts file: import { BrowserModule } from '@angular/platform-browser'; imp ...

Customizing Dropdown Menus with Bootstrap

Is there a way to make the select element appear as a list, similar to this image: https://i.sstatic.net/1IB8T.jpg Apologies, I'm still a beginner at this. Thank you in advance. ...

Creating 3 dynamic columns - a step-by-step guide

Is there a way to make these columns 3 columns wide on a desktop screen and 1 column wide on a mobile screen? They are not behaving as expected, and I'm not sure why. What can I do to achieve this? <section class="pricing-section py-5" data-aos= ...

Basic Java HTML parser for extracting CSS attributes

Is there a way to parse the style attribute of a basic <font> tag and then convert it back to simple html attributes? For example, if I have this string <font style="font-family:tahoma;font-size:24px;color:#9900CC;">, is it possible to convert ...

The CSS style functions properly in Chrome, however, it is not compatible with Internet Explorer

I can't figure out why this particular style is functioning properly in Chrome, but not in IE and FF. #show{top:10%; position:relative; margin: 0px auto; width:100%;} [Edit] If I want to achieve the same result in IE and FF, what steps should I tak ...

Tips for preventing H1 from taking up the entire div

I'm currently working on developing a new theme for my WordPress website. One issue I've encountered is that the h1 element is causing the top menu to appear below it instead of on the same line. Can anyone help me overcome this challenge? Here& ...

Limit Use of Special Characters in HTML and AngularJS

` ~ ! @ # $ % ^ & * ( ) _ + = { } | [ ] \ : ' ; " < > ? , . / I need to limit the use of special characters and numbers in the input field. To achieve this, I have employed the following pattern: ng-pattern="/^[a-zA-Z ]*$/" This patt ...

Aligning text to the left center

I am looking to center left-aligned text in the yellow box (txtbox). The current look is like this: and I want it to appear like this: HTML: <div class="content container small"> <div class="txtbox"> ...

Troubleshooting HTML Label Problems

I am facing an issue with my custom labels. .label-ras{ padding:3px 10px; line-height:15px; color:#fff; font-weight:400; border-radius:5px; font-size:75%; } .label-primar{background-color:#5c4ac7} <span style="background- ...

How to automatically embed a p5.js canvas into an HTML canvas with the drawImage() method

Whenever I attempt to draw the p5.js canvas into an HTML canvas using drawImage(), I always encounter this error: Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type ' ...

Steps for creating a two-dimensional arc

I want to input some pre-determined acr values from another program and replicate them in three.js Currently, I'm using code I found on this site to draw the arc, although it might not be the most optimal solution. function DRAWarc(){ ...