Clear - CSS Property

clear

Summary of characteristics of the clear property

Quick description
Allows or prohibits the float of the elements.
Status
Standard
Predefined values
none | left | right | both | inline-start | inline-end | block-start | block-end
Percentages
Not applicable.
Initial value
none
Inherited by default
No.
Discrète: during an animation, the clear property passes from one value to another without transition.
Single: single value (no order).
W3C Module
CSS Page Floats
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: WD (Working Draft)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)

Syntax diagram of clear.

clear - Syntax DiagramSyntax diagram of the clear CSS property. Allows or prohibits placement next to declared floating elements. none none left left right right both both inline-start inline-start inline-end inline-end block-start block-start block-end block-endclear:;clear:;
Syntax diagram of the clear property.
The links in the diagram provide more details about the values.
Download the diagram.

Description of the clear property.

clear indicates whether the element is allowed to be next to the floating elements that come before it.

This property complements the float property. It limits the effect on the element that receives the clear property. When an element is set to float, the following elements can position themselves next to it. This is sometimes referred to as wrapping. This can apply to several elements, as long as there’s space available next to the floating element. The clear property prevents an element from sitting next to a floating element.

Special case of a floating element larger than the text.

Let's take the case of a series of floating elements accompanied by their explanatory text. These floating elements are of different sizes, or it could be that the text is more or less long. So it can happen that the floating element is taller than the text that goes with it. In that case, the second image will end up next to the first one, since there is space left. The result doesn't look very good, and in any case, it's not the expected outcome.

Floating element
Text that goes with the first image.
Floating element
Text that goes with the second image.


To solve the problem and get back a proper list, you just need to apply the clear property to the container. This technique is often called clearfix. However, it has a problem when the container has a background or a border (as you can see in our example).
The description2 class is applied to the container, and the description2a class to the image.

Floating element
Text that goes with the first image.
Floating element
Thanks to the clear property applied to the container, this second paragraph is correctly positioned.


Note that there's another method: apply the display property to the container with the value flow-root.

Floating element
Text that goes with the first image.
Floating element
The property display applied to the container with the value flow-root also fixes the problem.

Language support.

Logical values like inline-start or inline-end allow you to take writing direction into account depending on the language.

left
The direction and writing mode of this text adapt to the chosen language.
right

Values for clear.

  • clear: none;

    Default value. Allows floating. This item can sit next to floating items if there are any before it.

    Floating element.
    This paragraph is next to the floating element.

    This second paragraph gets the property clear with the value none. It also sits next to the floating element.

    clear:none;
  • clear: left;

    Does not allow floating elements on the left that come before it. But the element can sit next to floating elements on the right if any exist before it.

    Floating element.
    This paragraph is next to the floating element.

    This second paragraph gets the property clear with the value left. It doesn't sit next to the floating elements to the left that come before it.

    clear:left;
  • clear: both;

    Does not allow the floating elements that come before. The element positions itself against the margin, below any floating elements, as if they weren't floating.

    Floating element 1.
    Floating element 2.
    This paragraph is next.

    This second paragraph gets the property clear with the value both. It doesn’t sit next to any left-floating or right-floating elements that come before it.

    clear:both;
  • clear: inline-start; clear: inline-end;

    These two values are equivalent to left and right for Latin languages, but adapt to the writing direction for non-Latin languages such as Arabic or CJK languages.

    Floating element.
    This paragraph is next to the floating element.

    This second paragraph gets the property clear with the value inline-start. It doesn't sit next to the left-floating elements that come before it, but it depends on the language.

    clear:inline-start;
  • clear: block-start; ⚠ clear: block-end; ⚠

    These two values are not currently supported by any browser.

  • clear: initial; (none) clear: inherit; clear: revert; clear: revert-layer; clear: revert-rule; clear: unset;

    These values ​​are described in more detail on their respective page: initial, inherit, revert, revert-layer, revert-rule, unset.

Access the clear property via a program

With Javascript, change the value of clear.

Floating elements are determined by two properties: clear and float. Here's how to change the value of clear with JavaScript. This language allows two syntaxes: the first uses the typical CSS notation, using the style array, and the second syntax is more common in object-oriented languages.

Javascript
let el = document.getElementById('id'); el.style['clear'] = 'left'; // or let el = document.getElementById('id'); el.style.clear = 'left';

With Javascript, read the value of clear.

For this code to work, the property must have been applied directly to the element itself via its style attribute (that's what the previous code does), and not through a CSS selector.

Javascript
let el = document.getElementById('id'); let value = el.style['clear']; // or let el = document.getElementById('id'); let value = el.style.clear;

With Javascript, read the computed value of clear.

The computed value is the one that comes from the chain of inheritances. It's always defined; at worst, it's the property's initial value.

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('clear');

With JQuery, read or change the value of clear.

JQuery also offers two syntaxes to define or reread the clear property.

JQuery

// Read the value:
$('#id').prop('clear');
// Write the value:
$('#id').prop('clear', 'left');
$('#id').prop('{clear': 'left'});

With JQuery, read the computed value of clear.

JQuery
let value = $('#id').css('clear');

Other code examples.

You can find other examples of Javascript and JQuery code on the page Javascript and CSS: adding style sheets, adding or removing classes, etc.

Test it yourself.

By clicking the buttons below, you apply the entered value to the clear property and then display either the value as it was applied or the computed value. The second option lets you see how the value of clear is stored (serialized). Since the property only accepts predefined values, there won't be any difference between these two values for the clear property, unless the assigned value is incorrect. In that case, the assigned value is null, and the computed value reverts to the initial value (none).

Simulator.

The first image is set to float on the left (float:left); the second image, on the other hand, floats on the right. The two text paragraphs either "slip" between the two images or not, depending on the value of their clear property.

clear :
Floating image on the left
CSS (Cascading Style Sheets) is a declarative language meant for styling and laying out a document, usually a web page. Created by the W3C in the 1990s, it was fully supported by browsers by the 2000s. The first real style sheets proposal was made by Hakon Wium Lie. The final CSS1 specification was published on December 17, 1996. The first version of CSS mainly allowed handling typography: fonts, colors, typographic attributes, and so on.
Floating image on the right
In 1997, the study of CSS was entrusted to a W3C team led by Chris Lilley. This group included representatives from the biggest web browsers. It wasn't until 1998 that the second version of CSS came out, bringing over 70 new properties. The development of CSS3 started in 1999, but it was tough due to update and implementation issues. CSS kept evolving, though, and it took more than 10 years to finalize version 3.

Browsers compatibility with clear.

All current browsers handle the clear property correctly in its initial definition. When it comes to the level 3 definition of the specification, there are still some issues with the new values, especially block-start and block-end.

Column 1
Support for the clear property to prevent an element from floating.
1
clear
property
(CSS2)
Estimated overall support.
98%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Samsung Internet

Edge

Firefox

Chrome

Safari sur IOS

KaiOS Browser

Opéra

Safari

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Opéra mini

Historic of the clear property.

  • Cascading Style Sheets specification - Version 1

    This first level of the CSS specification describes the mechanism of cascading style sheets. Authors and readers can add styles to documents. Mainly intended for formatting HTML documents, CSS allows defining colors, fonts, text formatting, spacing (margins), etc. The CSS language is easy to understand, readable, and easy to write; it uses common terms from desktop publishing.

    Regarding clear:
    Initial definition of the float property (first specification of the CSS language).
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    This level 2 of the specification describes the mechanism of cascading styles in CSS. This language is used to format HTML or XML documents. It supports media-specific style sheets so that authors can adapt the presentation of their documents for visual browsers, auditory devices, printers, braille devices, portable devices, etc.

    Regarding clear:
    Acceptance of the inherit value.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Logical Properties and Values Level 1

    This specification module defines properties that indirectly define certain other properties depending on the writing mode: left to right, right to left, or top to bottom. They are useful in simple and generic style sheets, such as browser style sheets, but can also save a few lines of styles for documents containing both left-to-right and right-to-left text.

    Regarding clear:
    Additional details on the logical values accepted by clear.
    WD
    May 18, 2017
    Working Draft.
    CR
    PR
    REC
  • CSS Page Floats Level 3

    Regarding clear:
    Adding logical values inline-start and inline-end.
    Accepting new values for vertical write modes top, block-start, etc.
    WD
    September 15, 2015
    Working Draft.
    CR
    PR
    REC

See also, about floating elements.

The concept of floating elements has existed for a long time and has been described in many specifications. The most current definitions can be found in the CSS Page Floats module.

Properties:

Clear
Allows or prohibits the float of the elements.
float
Float defines the elements that can be placed side by side horizontally, or more generally along the length of a line.