The clear both property in CSS has an impact on the layout flow

Why is the placement of the <h1> tag with "clear both" at the bottom causing issues? Removing "clear both" resolves the problem, but how can I make it work when including "clear both"?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gbk"/>
<style>
#left{
    float:left;
    width:200px;
    height:400px;
    background:#cfc;
}
#main{
    margin-left:210px;
    background:#f9c;
    height:400px;
}
h1{
    clear:both;
}
</style>
</head>
<body>
<div id="left"></div>
<div id="main">
    <h1>test</h1>
</div>
</body>
</html>

Answer №1

The insightful explanation provided by Mr. Extraterrestrial sheds light on the root of your issue, offering a simple solution to rectify it - by incorporating the suggested code into your CSS and eliminating the clear: both from the h1 properties:

#main:after {
    visibility: hidden;
    content: " ";
    clear: both;
    height: 0;

}

If you find yourself embarking on a floating frenzy, perhaps consider developing a clearfix class that can be easily applied to your elements. You can refer to this link for detailed instructions on how to create one.

Answer №2

Here are some queries:

1- Why is the clear both tag placed at the bottom?

As per css specifications for clear:

both
It requires that the top border edge of the box be below the bottom outer edge of any right-floating and left-floating boxes that resulted from elements earlier in the source document.

Therefore, the clear:both; on h1 also clears the float on a previous element, which is not even within the same div as the h1.

2- How do I make it work with clear both?

If you wish to retain clear:both; on h1 and have it at the start of its container div, the easiest solution is to add overflow:auto; to the container.

Demo

#main {
    margin-left:210px;
    background:#f9c;
    height:400px;
    overflow:auto;
}

Answer №3

Functioning as intended, the H1 layout is being affected rather than the main layout according to W3C guidelines. The clear property specifies which sides of an element's box(es) cannot be adjacent to a previous floating box.

Therefore, the H1 will shift down until it meets this constraint, despite being a child of the main div. If you need to apply clear to both the H1 and avoid the constraint related to the previous floating box simultaneously, you must absolute position the main. Additionally, a containing block is required, so I have included a relatively positioned container div.

You can observe this in action in this JSFiddle

#container {
    position:relative;
}
#left{
    float:left;
    width:200px;
    height:400px;
    background:#cfc;
}
#main{
    position:absolute;
    left:210px;
    right:0px;
    background:#f9c;
    height:400px;

}

h1{
    clear:both;    
}

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

Looking for a reliable CSS aggregator for .Net that can merge and minimize style sheets effectively?

Is there an open source or free project that offers a CSS manager? I want to optimize performance and am hoping to find a pre-built solution rather than starting from scratch. Some key features I'm looking for include: Merging multiple .css files in ...

Automatic playback of the next video in a video slider

Looking to upgrade my video slider functionality - switching between videos, playing automatically when one finishes. Struggling to find a solution that combines manual control with automatic playback. My attempt: function videoUrl(hmmmmmm){ ...

Using BeautifulSoup to Retrieve JPEG from an Image Tag's Src Attribute

Struggling with scraping this webpage for personal use. I am having trouble extracting the thumbnails of each item on the page. Despite being able to see image tags containing the required .jpgs when using "inspect" to view the html DOM, I cannot find the ...

Tips for hiding the initial value in an HTML Select tag

My HTML Select Program is very basic. By default, it displays "Volvo" as the first value. Is there a way to not display any initial value and allow the user to make a selection? <!DOCTYPE html> <html> <body> <form action="demo_form ...

Place an element in relation to the vertical size of a div that includes written content

Currently, I am working on implementing a button with a popup that appears underneath it when the user hovers over it. The specific requirements for this setup are: The size of the button should not affect the size of the popup The popup should always be ...

The issue of undefined database columns arises when attempting to transmit data from an HTML form to a MySQL database via Express

My primary objective is to develop a RestAPI using Node.js and test it in a small HTML application. With the guidance of my instructor, I successfully created the RestAPI based on an example and customized it to work with my own MySQL database. Testing ea ...

jQuery counter no longer updates when scrolling

I'm having trouble with my jQuery counting function when using the scroll feature on a specific div ID. The numbers freeze if I keep scrolling before they finish updating on the screen. Whenever I scroll to the defined div ID, the counting function k ...

Is there a way to display MySQL data row by row within distinct divs using a loop?

I need to display more than 50 rows from a MySQL database in a grid view with different colors and sizes. Below is the code I am using to fetch the rows, but I'm struggling with assigning unique anchor tags to each row. <?php $products ...

Learn how to toggle multiple class elements with jQuery, and how to hide the ones that have

This is an example of my HTML code: <h4 class="list-group-item-heading"> @Model.Customer.FirstName @Model.Customer.LastName wrote: <span class="right"><span class="icon-file padding-right link"></span></span> </h4& ...

Retrieving POST information from a PHP script

I have 3 php files and I need to extract the month data from form1.php for use in form3.php. However, the data processing also involves form2.php. How can I specifically retrieve the month from form1 and display it in form3? form1.php <form action=&quo ...

How come my ejs files are all affected by the same stylesheet?

Currently, I am in the process of developing a nodeJS application utilizing Express, MongoDB, and EJS template view engine. The server.js file has been successfully created to establish the server. In addition, a separate header.ejs file has been implement ...

The image is not resizing correctly

Currently, the background-image on my website displays properly when the browser occupies the entire screen. However, when I resize the browser window, the image shrinks to a certain point and then suddenly stops adjusting. Here is a visual representation ...

Is there a way to dynamically refresh the options in a select dropdown using jQuery?

I'm facing an issue with updating the options in a select element. Here is how my current select element looks: <select id="productId"> </select> Below is the jQuery code I am using: ... if (data.success) { var items = []; $.ea ...

SELENIUM is unable to choose a name that is generated by JavaScript code

Hello, this is the HTML code I am trying to work with: <input type="text" name="input[220].pk[202].name['CODICE_ORDINE_OLO'].value" alias="" value="" pattern=".*" class="form-control form- ...

Empty input fields in Javascript calculations will result in a NaN output

I need to perform a calculation using values entered into form fields and JavaScript. The formula I'll be using is as follows: totalEarnings = income1 + income2 * 0.7 + income3 / 48 + (income4 * 0.7) / 48; The variables income1, income2, income3, an ...

What is the best way to show a background image behind the heading text?

I have a strip of images that I want to use as a background for my heading. However, I am encountering an issue where the first and last images of the strip are not displaying properly. Additionally, the image border appears straight instead of following ...

Utilizing D3.js to filter nodes and links by making multiple selections from a checkbox

I am interested in enhancing my current forced directed graph by adding more flexibility. You can access the Jsfiddle here. Currently, there are radio buttons that control the opacity of nodes and links. There are 4 types of nodes: Agent, Customer, Pho ...

Sequentially animate objects with a fade-out and fade-in effect

Attempting to achieve a fade-out, fade-in animation using JavaScript and CSS. Here is the CSS animation code: @keyframes fade-in{ 0%{ opacity: 0%; } 100%{ opacity: 100%; } } @keyframes fade-out{ 0%{ opacity: 100%; } 100%{ opacity: 0%; } } Impleme ...

Discover the secret to toggling Bootstrap 4 cards visibility when hovering over the navigation menu using CSS

Hey, I'm currently working on a project where I want to show and hide specific sections of cards by just hovering over the menu list. While I can successfully hide the cards using CSS, I am facing issues with displaying them using the display:block pr ...

Display the selected checkbox in the table

I am struggling to display the selected check value in a table. I am having difficulty in showing the check values corresponding to each row in the table based on my selected value. For instance, in the code snippet below, I have selected values as true,f ...