"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Does `float: right` Reverse the Order of Spans in HTML?

Why Does `float: right` Reverse the Order of Spans in HTML?

Published on 2024-11-06
Browse:666

Why Does `float: right` Reverse the Order of Spans in HTML?

Float: Right Reversing Order of Spans

Given the HTML markup:

applying the CSS:

span.button {
    float: right;
}

span.label {
    margin-left: 30px;
}

results in an unexpected display order in the browser, with the spans displaying as "Import Export Settings" instead of the order in the HTML source.

Solution

To resolve this issue without modifying the HTML, the CSS can be adjusted. One approach is to reverse the order of the right floated elements:

span.button:nth-child(1) {
    float: right;
}

span.button:nth-child(2) {
    float: right;
    margin-right: 5px;
}

span.button:nth-child(3) {
    float: right;
    margin-right: 10px;
}

span.label {
    margin-left: 30px;
}

This solution retains the left floated label element while reversing the order of the right floated buttons, resulting in the desired display order.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3