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

Implementation of k8s deployment of docker container

Environment: (docker, k8s cluster), continue with...

Example code for implementing equal height layout in multiple ways with CSS

The equal height layout described in this article...

Build a Docker image using Dockerfile

Table of contents Build a Docker image using Dock...

Summary of commonly used escape characters in HTML

The commonly used escape characters in HTML are s...

Summary of some tips on MySQL index knowledge

Table of contents 1. Basic knowledge of indexing ...

Summary of @ usage in CSS (with examples and explanations)

An at-rule is a declaration that provides instruc...

HTML+CSS to achieve responsive card hover effect

Table of contents accomplish: Summarize: Not much...

Detailed explanation of pid and socket in MySQL

Table of contents 1. Introduction to pid-file 2.S...

How to change the host name in Linux

1. View the current host name [root@fangjian ~]# ...

Detailed explanation of table return and index coverage examples in MySQL

Table of contents Index Type Index structure Nonc...

Docker container log analysis

View container logs First, use docker run -it --r...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...