Home >Web Front-end >CSS Tutorial >How to Align DIV Elements to the Right While Maintaining Vertical Stacking?
Aligning DIV Elements to the Right
Problem:
In an HTML document, aligning multiple DIV elements to the right can lead to issues where the elements lose their vertical stacking and end up arranged horizontally.
Analysis:
Using "float: right" to align DIVs can cause this problem, especially if the DIVs are contained within a parent DIV. In such cases, the floating DIVs will be removed from the normal flow of the document and positioned adjacent to each other.
Solution:
To achieve right alignment while preserving vertical stacking, an alternative approach is to use the following CSS properties:
margin-left: auto;
margin-right: 0;
Example:
Here's an improved version of the provided code using these properties:
<code class="css">#button { position: relative; float: right; } #addEventForm { position: relative; margin-left: auto; margin-right: 0; border: 2px solid #003B62; font-family: verdana; background-color: #B5CFE0; padding-left: 10px; }</code>
By using these properties, the button and the form will align to the right, stacking vertically one after the other as intended.
The above is the detailed content of How to Align DIV Elements to the Right While Maintaining Vertical Stacking?. For more information, please follow other related articles on the PHP Chinese website!