CSS techniques for achieving multi-column equal height layout that the front end should master

CSS techniques for achieving multi-column equal height layout that the front end should master

1. Introduction

When we are writing a page, we sometimes encounter a multi-column layout. The content in each column may sometimes be different, which will cause the actual height of each column to be different. If each column has a background color, the bottom of each column will be misaligned, and the user experience will not be very good!

The actual problem effect is as follows:

2. The requirements are as follows

The effect we want to achieve is to ensure that each column is aligned regardless of the actual content of each column.

3. How to solve it

The HTML code is as follows:

<ul class="Article">
    <li class="js-equalheight">
      <p>
      A broker that puts the interests of its clients first.
      Provide customers with professional trading tools A broker that puts customer interests first.
      Provide customers with professional trading tools A broker that puts customer interests first.
      Provide customers with professional trading tools A broker that puts customer interests first and provides customers with professional trading tools</p>
    </li>
    <li class="js-equalheight">
      <p>A broker that puts the interests of its customers first and provides customers with professional trading tools. A broker that puts the interests of its customers first and provides customers with professional trading tools.</p>
    </li>
    <li class="js-equalheight">
      <p>A broker that puts clients’ interests first</p>
    </li>
  </ul>

(1) Pure CSS solution

The CSS code is as follows:

.Article{
  overflow: hidden;
}

.Article>li{
  float: left;
  margin: 0 10px -9999px 0;
  padding-bottom: 9999px;
  background: #4577dc;
  width: 200px;
  color: #fff;
}

.Article>li>p{
  padding: 10px;
}

Analysis and explanation: The padding-bottom of the element should be as large as possible, and a negative margin-bottom value of the same size should be set to offset the area enlarged by the padding-bottom. The positive and negative values ​​will offset each other, and there will be no impact on the page layout. In addition, you also need to set the parent element overflow: hidden to hide the extra color block background of the child element. The above CSS solution does not have any compatibility issues, so you can use it with confidence.

(2) js solution

The js code is as follows:

jQuery( document ).ready(function() {
  equalheight();
});

jQuery(window).resize(function() {
  jQuery('.js-equalheight').css('height','auto');
  equalheight();
});

function equalheight() {
  var heights = jQuery(".js-equalheight").map(function() {
    return jQuery(this).height();
  }).get(),

  maxHeight = Math.max.apply(null, heights);

  jQuery(".js-equalheight").height(maxHeight);
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  HTML web page hyperlink tag

>>:  React implements multi-component value transfer function through conetxt

Recommend

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

Vue3+script setup+ts+Vite+Volar project

Table of contents Create a vue + ts project using...

Three commonly used MySQL data types

Defining the type of data fields in MySQL is very...

Summary of pitfalls of using nginx as a reverse proxy for grpc

background As we all know, nginx is a high-perfor...

The difference between html empty link href="#" and href="javascript:void(0)"

# contains a location information. The default anc...

Share 9 Linux Shell Scripting Tips for Practice and Interviews

Precautions 1) Add interpreter at the beginning: ...

HTML tag default style arrangement

html, address,blockquote,body, dd, div,dl, dt, fie...

Source code reveals why Vue2 this can directly obtain data and methods

Table of contents 1. Example: this can directly g...

Detailed explanation of the use of MySQL group links

Grouping and linking in MYSQL are the two most co...

MySQL 5.7.17 installation and configuration method graphic tutorial (windows10)

MySQL 5.7.17 installation and configuration metho...

JS ES6 asynchronous solution

Table of contents Initially using the callback fu...

Idea deployment tomcat service implementation process diagram

First configure the project artifacts Configuring...

Understanding MySQL clustered indexes and how clustered indexes grow

In this note, we briefly describe What is the B+T...

Summary of some tips for bypassing nodejs code execution

Table of contents 1. child_process 2. Command exe...