Three ways to align div horizontal layout on both sides

Three ways to align div horizontal layout on both sides

This article mainly introduces three methods of implementing the alignment of div horizontal layout on both sides, and shares them with you, as follows:

Method 1

The parent container div uses position: relative;, and the child div uses position: absolute; to position, pay attention to the margin problem

html

<div class="div-container">
  <div class="div1">1</div>
  <div class="div2">2</div>
</div>

CSS

/* Method 1 */
.div-container {
  margin: 10px 0;
  padding: 10px;
  width: 400px;
  border: 2px solid #ccc;
  position: relative;
}
.div1 {
  width: 100px;
  height: 50px;
  border: 2px solid red;
}
.div2 {
  width: 100px;
  height: 50px;
  border: 2px solid red;
  position: absolute;
  /* Margin settings */
  right: 10px;
  top: 10px;
}

Method 2 Recommended

The parent container div uses display:flex; justify-content:space-between;

html

<div class="div-container2">
  <div class="div3">3</div>
  <div class="div4">4</div>
</div>

CSS

/* Method 2 */
.div-container2 {
  margin: 10px 0;
  padding: 10px;
  width: 400px;
  border: 2px solid #ccc;
  display: flex;
  justify-content: space-between;
}
.div3 {
  width: 100px;
  height: 50px;
  border: 2px solid red;
}
.div4 {
  width: 100px;
  height: 50px;
  border: 2px solid red;
}

Method 3

The parent container div uses display: flex; to achieve horizontal arrangement, and the child div sets the width to fill the space.

html

<div class="div-container3">
  <div class="div5">5</div>
  <div class="div7">Placeholder div</div>
  <div class="div6">6</div>
</div>

CSS

/* Method 3 */
.div-container3 {
  margin: 10px 0;
  padding: 10px;
  width: 400px;
  border: 2px solid #ccc;
  display: flex;
  justify-content: space-between;
}
.div5 {
  width: 100px;
  height: 50px;
  border: 2px solid red;
}
.div6 {
  width: 100px;
  height: 50px;
  border: 2px solid red;
}
.div7 {
  width: calc(100% - 100px - 100px);
  height: 50px;
  border: 1px solid #ccc;
}

GitHub full code link

https://github.com/gywgithub/exercise01/blob/master/div-flex/index.html

This concludes this article on three ways to achieve horizontal alignment of div layouts. For more information on horizontal alignment of div layouts, please search previous articles on 123WORDPRESS.COM or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Detailed explanation of meta tags and usage in html

>>:  Introduction to building a DNS server under centos7

Recommend

JavaScript two pictures to understand the prototype chain

Table of contents 1. Prototype Relationship 2. Pr...

JavaScript implements large file upload processing

Many times when we process file uploads, such as ...

JavaScript Array Detailed Summary

Table of contents 1. Array Induction 1. Split a s...

MySQL 5.7.18 MSI Installation Graphics Tutorial

This article shares the MySQL 5.7.18 MSI installa...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...

JavaScript implementation of the back to top button example

This article shares the specific code for JavaScr...

Detailed explanation of data sharing between Vue components

Table of contents 1. In project development, the ...

Solution for adding iptables firewall policy to MySQL service

If your MySQL database is installed on a centos7 ...

Vue3+el-table realizes row and column conversion

Table of contents Row-Column Conversion Analyze t...

Practical basic Linux sed command example code

The Linux stream editor is a useful way to run sc...

A brief understanding of the three uses of standard SQL update statements

1. Environment: MySQL-5.0.41-win32 Windows XP Pro...

Detailed explanation of adding dotted lines to Vue element tree controls

Table of contents 1. Achieve results 2. Implement...