


How to use CSS Flex to achieve the same height border of left and right layout?
Tips for implementing the left and right columns contour borders under CSS Flex layout
When using CSS Flex layout, you often need to divide the page into two parts, and the lower part is then subdivided into two columns. If the heights of the left and right columns are not fixed, how can we ensure that the borders that divide them can automatically extend to the highest height? This article provides two solutions to solve this problem.
Scene: The page is divided into two parts, the upper and lower parts, and the lower part adopts a Flex layout. The height of the left and right columns is uncertain, so a border of equal height is needed between the two columns.
Solution 1: jQuery adjustment based on existing structure
If the HTML structure cannot be modified, this method can be used. First, set height: min-content;
for the right element (assuming it is .rht
) so that its height is adaptable to the content. Then, use jQuery to get the height of .rht
and apply it to the child elements of the left element (assuming it is .lft
):
let h = $(".rht").height() 'px'; $(".lft div").css({ height: h });
This method relies on JavaScript and needs to be executed after the page is loaded.
Solution 2: Adjust HTML structure and CSS style
This method is simpler and more efficient, without JavaScript. Contour borders can be implemented more elegantly by adjusting HTML structure and CSS styles.
Improved HTML structure:
<div class="wrapper"> <div class="top">Top content</div> <!-- Top area--> <div class="content"> <!-- Flex layout container--> <div class="lft"> <div>Content on the left</div> </div> <div class="rht"> <div>Content on the right</div> </div> </div> </div>
Corresponding CSS style:
* { padding: 0; border: 0; margin: 0; box-sizing: border-box; /* It is recommended to add to avoid borders affecting element size calculation*/ } .wrapper { height: 100vh; /* Occupies the entire viewport height*/ display: flex; flex-direction: column; /* Vertical layout*/ } .top { height: 100px; background: #e3e3e3; } .content { display: flex; flex: 1; /* occupy the remaining space*/ min-height: 0; /* Prevent minimum height from affecting the layout*/ } .lft { flex: 1; /* occupy the remaining space*/ } .rht { width: 600px; border-left: 1px solid red; min-height: 0; /* Prevent minimum height from affecting the layout*/ }
In this plan:
-
.wrapper
usesflex-direction: column;
to achieve vertical layout. -
.content
usesflex: 1;
to occupy the remaining space to ensure its height is adaptable. - Both
.lft
and.rht
usemin-height: 0;
to prevent the minimum height from limiting its height. - The split line is
border-left
directly on.rht
.
Solution 2 is implemented through pure CSS, which has better performance and is easier to maintain. It is recommended to give priority to the use of Plan 2.
The above is the detailed content of How to use CSS Flex to achieve the same height border of left and right layout?. For more information, please follow other related articles on the PHP Chinese website!

The fact that anchor positioning eschews HTML source order is so CSS-y because it's another separation of concerns between content and presentation.

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
