Right align multiple elements in the same row under div in css

Right align multiple elements in the same row under div in css

Method 1:

float:right
In addition, floating will make the layout more compact (no gaps)

<div style="background-color: red;width: 100%;height: 60px;/* text-align: right; */">
	<div style="width: 30px;height: 100%;background-color: yellow;float: right;">hello</div>
	<div style="width: 60px;height: 100%;background-color: blue;float: right;">hi</div>
</div>

The effect diagram is as follows:

Method 2:

display:inline-block+text-align:right
text-align:right affects the inline elements or text below it, so inline-block makes div have the characteristics of inline elements and can be right-aligned

<div style="background-color: red;width: 100%;height: 60px;text-align: right;">
	<div style="width: 30px;height: 100%;background-color: yellow;display: inline-block;">hello</div>
	<div style="width: 60px;height: 100%;background-color: blue;display: inline-block;">hi</div>
</div>

The effect diagram is as follows:

From the above two methods:

The float layout is more compact;
float: right will change the order, but text-align: right will not;
text-align will also affect the text alignment of the elements below it;

Therefore, combining the two methods, we can get the following combination:

<div style="background-color: red;width: 100%;height: 60px;text-align: right;">
	<div style="width: 30px;height: 100%;background-color: yellow;display: inline-block;">hello</div>
	<div style="width: 60px;height: 100%;background-color: blue;float: right;">hi</div>
</div>

The effect is as follows:

Further findings, after synthesis:
The layout is more compact;
When float and inline-block are placed together, there will be no overlap of floats, but they will be arranged in a regular manner.

This is the end of this article about how to right-align multiple elements in a div using CSS. For more information about how to right-align multiple elements in a div using CSS, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Summary of HTML Hack Tags in IE Browser

>>:  Solution to input cursor misalignment in Chrome, Firefox, and IE

Recommend

Introduction to version management tool Rational ClearCase

Rational ClearCase is a software configuration ma...

Detailed installation tutorial for MySQL zip archive version (5.7.19)

1. Download the zip archive version from the offi...

CentOS 7 configuration Tomcat9+MySQL solution

Configure Tomcat First install Tomcat Installing ...

HTML table_Powernode Java Academy

To draw a table in HTML, use the table tag tr me...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

In-depth analysis of Vue's responsive principle and bidirectional data

Understanding object.defineProperty to achieve re...

Vue realizes price calendar effect

This article example shares the specific code of ...

How to solve the abnormal error ERROR: 2002 in mysql

Recently, an error occurred while starting MySQL....

6 ways to view the port numbers occupied by Linux processes

For Linux system administrators, it is crucial to...

Why Seconds_Behind_Master is still 0 when MySQL synchronization delay occurs

Table of contents Problem Description Principle A...

How does MySQL connect to the corresponding client process?

question For a given MySQL connection, how can we...

Example of how to install nginx to a specified directory

Due to company requirements, two nginx servers in...

The most basic code for web pages

◆Add to favorites illustrate Click to add your we...

Detailed explanation of the available environment variables in Docker Compose

Several parts of Compose deal with environment va...