Is there a CSS alternative to using <br clear=all> on divs that do not have set heights?

Back in the day, I used to rely on <br clear=all> at the end of a div, without specifying a height value in CSS, just to control its height.

These divs would expand and contract based on the content within.

Is there a more elegant approach to make all divs' heights in sync with the tallest one, without resorting to <br clear=all>? Considering that the divs adjust their heights according to the content they contain.

Here's an instance of the code:

<div class="welcomeText">
  <div class="houseTiles2">
    <h1>TITLE<br><br></h1>
    <p><br>Body copy 1</p><br clear="all">
  </div>
  <div class="houseTiles">
    <h1 class="tileTitle">Title 2</h1><br>
    <p class="tileDesc">Text text text text text text text text</p>
 <br clear="all">
  </div>
  <div class="houseTiles">
    <h1 class="tileTitle">Title 3</h1><br>
    <p class="tileDesc">Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</p><br clear="all">
  </div><br clear="all">
</div>

Answer №1

It is recommended to utilize

<br style="clear: both">

instead of using the deprecated clear attribute. However, this method may not be very elegant.

A more sophisticated approach would be to create a class:

.clear {
  clear: both;
}

Alternatively, you can add the following class to your containing element:

.container {
  overflow: hidden; /* can also be set to "auto" */
}

Another technique is the "clearfix" method, which should be applied to the containing elements:

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

For further information, check out this resource: http://www.sitepoint.com/clearing-floats-overview-different-clearfix-methods/

UPDATE: If you desire the divs to have equal height and are targeting IE10+, consider using flexbox. It's quite simple:

Additionally, there's an interactive tutorial on flexbox available here:

Answer №2

A highly effective solution is to implement the clearfix method:

.welcomeText:before, .welcomeText:after {
   content:"";
   display:table;
}

.welcomeText:after {
  clear:both;
}

In the past, we utilized a div with a "clear:both" property in our CSS.

However, modern CSS allows us to utilize pseudo-elements that function like elements within the div but are generated using CSS.

You can explore various articles on how to clear floats, such as this one addressing float-related issues:

If you desire for all child divs to have the same height as the parent div, consider utilizing display:table on the parent and display:table-cell on its children, while eliminating float:left;

Answer №3

Creating equal height divs automatically using CSS without a fixed height is not achievable.

However, using jQuery allows for setting the divs to the same height:

var heights = [];

$(".houseTiles, .houseTiles2").each(function() {
    heights.push($(this).height());
});

var biggestheight = Math.max.apply(null, heights);

$(".houseTiles, .houseTiles2").each(function() {
    $(this).css("height",biggestheight);
});

Check out this example on JSFiddle demo.

To give floated elements a height, you can use the following CSS:

.houseTiles, .houseTiles2 {
    display:table;
    overflow:hidden;
}

This also clears the float similarly to using clear=all or 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

Use jQuery to switch the class of the designated DIV in the showcasing slider

Check out this jQuery slider example where I have two Divs - DIV ONE and DIV TWO. Can you help me figure out how to: 1) Automatically change the class of DIV ONE from "test" to "testc" when Slide 1 is displayed by the slider. 2) Automatically update DIV ...

The function TagBuilder.MergeAttributes is malfunctioning

My attempt at creating a custom helper in MVC is not working as expected. The custom attributes are not being added to the HTML: Custom Helper Function public static MvcHtmlString MenuItem(this HtmlHelper helper, string linkText, string actionName, strin ...

Duplicate the image from a specific div to another div, ensuring that only one clone is created

I'm trying to replicate an image in multiple spots within a frame, but I can only manage to get one instead of many. Can someone please tell me what I'm doing wrong? I am creating a map of terrain where I need to clone images from one div to anot ...

Optimizing the position of smart info windows in Google Maps

I am facing a challenge while attempting to switch the infowindow in Google maps to the smartinfowindow, as the position of the infowindow appears incorrect. This issue only occurs with the smartinfowindow and not with the standard infowindow. Upon furthe ...

The JSON output displayed my HTML `<br />` as unicode `u003cbr /u003e`, resulting in the `<br />` being shown as text instead of creating a line break

Utilizing ASP.net MVC3, I have retrieved a model in Json format using Jquery.AJAX and am passing it into a Jquery template for rendering. For instance, the Json returned by the server is {"Key":2,"Content":"I'm Jason\u003cbr /\u003ehow are ...

Locate the selected icon on one webpage and automate the clicking of a similar icon on a different webpage using Python and Selenium

I've exhausted all my resources and searched extensively online, but I still can't find a viable solution to my current dilemma. Any assistance in helping me navigate through this challenging situation would be greatly appreciated. (If possible, ...

Tips for changing the <title> in an AngularJS one-page application

I am working on a single-page AngularJS application. The index.html file is structured as follows: <html ng-app="myApp" ng-controller="MyCtrl as myctrl"> <head> <link rel="stylesheet" href="my-style-sheet.css"> <title>{{ ...

What are your preferred HTMLPurifier configurations for sanitizing CSS styles?

I'm currently in the process of developing an application that allows users to input their own custom CSS for their profiles, similar to platforms like MySpace, Friendster, and Blogger. However, I'm struggling to find an effective method to prot ...

Using AJAX to Access PHP Variables

I am working on creating a progress bar and have the following CSS code: #result{ background:#8c8f91; margin-left: 15px; margin-right: auto; table-layout: fixed; border-collapse: collapse; z-index: -1; position:relative; width: ...

What is the best way to horizontally align Bulma's level items on smaller screens?

My website's footer features a level layout with left and right sections that follow the guidelines provided in theBulma docs. Everything looks good on larger screens. However, on smaller screens, all items in the right section are stacked vertically ...

integrating the WordPress header into the WHMCS client portal

While working on a project, I am facing the challenge of aligning the WHMCS client area with my WordPress site. Progress has been made, but I have hit a roadblock. My goal is to integrate the header of my WordPress site into WHMCS. I initially attempted t ...

Leveraging the power of PHP and AJAX for seamless transmission and reception of JSON data through a

I am in the process of creating a basic form that consists of a single text field and a submit button. The goal is to have the entered value posted to a $.getJSON method upon clicking the button, which will then retrieve a JSON response and display it on t ...

Adding a border-top overlay to vertical navigation

I'm working on a vertical navigation bar and I'd like to make some styling changes. Here's the current setup (View jFiddle): <style> ul{ list-style-type: none; } li{ display: block; margin: 0; padding: 10; border-top: 1px d ...

Exploring the functionality of CodePen's code editor in relation to developing a 2D shooting game

Recently, I created a straightforward 2D shooter game with all the code neatly organized in a single HTML file: (file_gist). When I tested the game in my chrome browser, everything worked flawlessly according to my intentions. However, upon transferring th ...

Dynamic WordPress Website Designs

Seeking recommendations for responsive WordPress themes that offer extensive CSS and structural customization options. The goal is to retain all of WordPress's built-in features, such as blogging capabilities, while having the freedom to completely co ...

What is the best way to eliminate the space underneath a graph?

Despite trying multiple solutions, I'm still struggling to remove the excess blue space below the graph that appears when clicking the submit button. Any insights into what might be causing this issue? JSFIDDLE Below are the CSS styles related to t ...

Upon selecting multiple checkboxes, corresponding form fields will be displayed based on shared attributes

When multiple checkboxes are selected by the user, corresponding form fields should appear based on the checkboxes. For example, if both flight and hotel checkboxes are checked, then the full name and last name fields should be displayed while other fields ...

Is it possible to incorporate HTML and CSS into Kivy and Python 3 development?

Currently in the process of creating a multi-touch kivy program using Python 3.52 on Linux. While Kivy is effective, I've encountered challenges with GUI development and experienced laggy animations. I've noticed that my program tends to slow do ...

Revolutionary CSS and jQuery for an Exceptional Top Navigation Experience

I would like to create a top navigation similar to the one on Facebook, using CSS and jQuery. Here is an example: Additionally: Notice how the arrow in the popbox always remains beneath the selected navigation item, and all popboxes have the same width. ...

Designing versatile buttons with HTML

When accessing the website on a phone, I have trouble seeing and tapping on these two buttons because they are too far apart. I would like to change it so that once a file is selected, the 'choose file' button will be replaced by the upload butto ...