How can I correct the changing order of buttons when floating to the right?

When I align a few buttons to the right, the order of those buttons changes. I have included an image for reference. View Navbar Image

This is the code I am currently using:

.btn{
    float: left;
    padding: 1.7em;
    text-align: center;
    display: inline-block;
    border: none;
    cursor: pointer;
    color: white; 
}
.navbar{
    float: left;
    background-color: #47476b;
}
.btnff{
    -webkit-transition-duration: 0.7s;
    transition-delay: 0.1s;
}
.btnff:hover{
    background-color: black;
}
.active{
    background-color: limegreen;
}
.right{
    float: right;
}
<div>
            <button class="btn btnff navbar active" type="submit">Button 1</button>
            <button class="btn btnff navbar" type="submit">Button 2</button>
            <button class="btn btnff navbar" type="submit">Button 3</button>
            <button class="btn btnff navbar" type="submit">Button 4</button>
            <button class="btn btnff navbar" type="submit">Button 5</button>
            <button class="btn btnff navbar" type="submit">Button 6</button>
            <button class="btn btnff navbar" type="submit">Button 7</button>
            <button class="btn btnff navbar right" type="submit">Button 8</button>
            <button class="btn btnff navbar right" type="submit">Button 9</button>
        </div>

How can I ensure that Button 9 and 8 appear in the correct order? Also, how can I make the color of the buttons stretch the entire width of the screen to eliminate the white space between the left and right buttons?

Answer №1

To align the two buttons to the right, you can encapsulate them in a container and set the container to float to the right.

.btn{
    float: left;
    padding: 1.7em;
    text-align: center;
    display: inline-block;
    border: none;
    cursor: pointer;
    color: white; 
}
.navbar{
    float: left;
    background-color: #47476b;
}
.btnff{
    -webkit-transition-duration: 0.7s;
    transition-delay: 0.1s;
}
.btnff:hover{
    background-color: black;
}
.active{
    background-color: limegreen;
}
.float-container{
    float: right;
}
        <div>
            <button class="btn btnff navbar active" type="submit">Button 1</button>
            <button class="btn btnff navbar" type="submit">Button 2</button>
            <button class="btn btnff navbar" type="submit">Button 3</button>
            <button class="btn btnff navbar" type="submit">Button 4</button>
            <button class="btn btnff navbar" type="submit">Button 5</button>
            <button class="btn btnff navbar" type="submit">Button 6</button>
            <button class="btn btnff navbar" type="submit">Button 7</button>
            <div class="float-container">
                <button class="btn btnff navbar" type="submit">Button 8</button>
                <button class="btn btnff navbar" type="submit">Button 9</button>
            </div>
        </div>

Answer №2

To align text, you can use text-align and float the desired element for left alignment only.

If there is unwanted white-space, you can eliminate it by adding a comment like in the snippet below.

.btn{
    float: left;
    padding: 1.7em;
    text-align: center;
    display: inline-block;
    border: none;
    cursor: pointer;
    color: white; 
}
.navbar{
    float: left;
    background-color: #47476b;
}
.btnff{
    -webkit-transition-duration: 0.7s;
    transition-delay: 0.1s;
}
.btnff:hover{
    background-color: black;
}
.active{
    background-color: limegreen;
}
.right{
    float: none;
}
div {text-align:right;}
<div>
            <button class="btn btnff navbar active" type="submit">Button 1</button>
            <button class="btn btnff navbar" type="submit">Button 2</button>
            <button class="btn btnff navbar" type="submit">Button 3</button>
            <button class="btn btnff navbar" type="submit">Button 4</button>
            <button class="btn btnff navbar" type="submit">Button 5</button>
            <button class="btn btnff navbar" type="submit">Button 6</button>
            <button class="btn btnff navbar" type="submit">Button 7</button>
            <button class="btn btnff navbar right" type="submit">Button 8</button><!-- 
                   Comment added to remove white-space from the code indentation 
         --><button class="btn btnff navbar right" type="submit">Button 9</button>
        </div>

Answer №3

Rearrange the positioning of the buttons aligned to the right in the HTML code. Introduce a new class to the encompassing div and incorporate overflow: auto to prevent collapse when containing floated child elements.

.buttons {
    background: #000;
    overflow: auto;
}
.btn{
    float: left;
    padding: 1.7em;
    text-align: center;
    display: inline-block;
    border: none;
    cursor: pointer;
    color: white; 
}
.navbar{
    float: left;
    background-color: #47476b;
}
.btnff{
    -webkit-transition-duration: 0.7s;
    transition-delay: 0.1s;
}
.btnff:hover{
    background-color: black;
}
.active{
    background-color: limegreen;
}
.right{
    float: right;
}
<div class="buttons">
            <button class="btn btnff navbar active" type="submit">Button 1</button>
            <button class="btn btnff navbar" type="submit">Button 2</button>
            <button class="btn btnff navbar" type="submit">Button 3</button>
            <button class="btn btnff navbar" type="submit">Button 4</button>
            <button class="btn btnff navbar" type="submit">Button 5</button>
            <button class="btn btnff navbar" type="submit">Button 6</button>
            <button class="btn btnff navbar" type="submit">Button 7</button>
            <button class="btn btnff navbar right" type="submit">Button 9</button>
            <button class="btn btnff navbar right" type="submit">Button 8</button>
        </div>

Answer №4

To achieve this layout efficiently, flexbox can be used instead of floats:

Set the DIV container as a flex container (display: flex), remove float: right from buttons 8 and 9, and apply margin-left: auto to button 8 (to align everything from that point to the right end). In the code snippet below, this is accomplished by utilizing the additional classes .x and .y

.btn{
    float: left;
    padding: 1.7em;
    text-align: center;
    display: inline-block;
    border: none;
    cursor: pointer;
    color: white; 
}
.navbar{
    float: left;
    background-color: #47476b;
}
.btnff{
    -webkit-transition-duration: 0.7s;
    transition-delay: 0.1s;
}
.btnff:hover{
    background-color: black;
}
.active{
    background-color: limegreen;
}
   .x {
  display: flex;
   }
   .y { 
   margin-left: auto; 
   }
<div class='x'>
            <button class="btn btnff navbar active" type="submit">Button 1</button>
            <button class="btn btnff navbar" type="submit">Button 2</button>
            <button class="btn btnff navbar" type="submit">Button 3</button>
            <button class="btn btnff navbar" type="submit">Button 4</button>
            <button class="btn btnff navbar" type="submit">Button 5</button>
            <button class="btn btnff navbar" type="submit">Button 6</button>
            <button class="btn btnff navbar" type="submit">Button 7</button>
            <button class="btn btnff navbar y" type="submit">Button 8</button>
            <button class="btn btnff navbar" type="submit">Button 9</button>
        </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is preventing my HTML/CSS code from generating a button on the webpage?

There seems to be an issue with the image on my webpage - it's displaying a broken page icon instead of the actual image, even though it was showing fine before. The image name and location have not been changed. I'm also having trouble creating ...

Focusing on the initial element of every line within a flex container that spans multiple lines

Looking for a solution to style the first item on each line of a multiline flexbox container with display: flex; flex-wrap: wrap. Preferably seeking a CSS-only approach rather than Javascript iteration. It's uncertain how many items there will be or t ...

Is there a way to dynamically toggle the visibility of a floating textarea between on and off?

I have my own blog website: Essentially, what I am aiming for is When a user clicks on the search button, a floating semi-transparent textarea window should appear inside the designated rectangle area (as shown in the image, that red orange rectangle). ...

"Bordering with Rounded Edges: A CSS Styling

Applying rounded corners to my list navigation elements using CSS. These elements also have a border. Here's how it currently appears: The quality of the rounded corner and border combination looks off, with some white shining through. Any suggesti ...

Vue 3 throws an error stating: "An uncaught DOMException occurred because the string contains an invalid character."

Exploring the capabilities of vue.js on a basic website. The setup consists of a single index.html file and all JavaScript is housed in an index.js file. The website is a work in progress, but there are no blockers preventing the JavaScript functionality ...

Tips for evenly spacing items in a navigation bar

I'm having trouble centering the logo on the navbar as it always leans more towards the left. Is there a way to resolve this problem using Bootstrap classes? The navbar consists of a dropdown toggle button on the left, the logo in the center, and two ...

Set the input of a component in Angular to determine its width

I am working on a basic angular component. Here is the code snippet: <div class="k-v-c" fxFlex fxLayout = "row" > <div class="k-c" fxFlex = "widthOfTable" > {{ key | translate }} </div> < div class="separator" > ...

Display a unique and unconventional image as a dynamic full-screen background, ensuring the entire image is visible using React/RedwoodJS

Looking for a somewhat unusual solution here - I want to use an image as a background with all edges of the picture visible at all times. Typically, I would use the "cover" setting for this, but that doesn't achieve what I need (image included). After ...

Using MySQL data in an EJS file to create a personalized Profile Page

In the midst of a personal project aimed at honing my web development skills and gaining a deeper understanding of the intricacies of the field, I have hit a roadblock. The focus of this project is to create a profile page, and while I have successfully co ...

The utilization of the Open Sans font family results in certain alterations to the user

Currently, I am working on developing a button widget using react. I have utilized the 'Open Sans' font family for the HTML body in order to maintain consistent UI styling. However, some of the styling elements break down when the 'Open Sans ...

Exporting CSS file from css-loader in Webpack: A step-by-step guide

My application structure is set up like this: /src /app.js /styles.css /images /img.png The content of the app.js file is as follows: const styles = require('./styles.css'); console.log(styles) // => I want a URL to t ...

Techniques for animating background image using jQuery

Hello there! I'm currently experimenting with animating a background image using jQuery. Despite following a tutorial, I haven't been successful in getting my test page to work as intended. The blue Facebook icon displays, but it doesn't ani ...

Ways to extract information from JSON files

Currently, I am working on a script to extract viewer count and follower count data from Twitch. While I have successfully retrieved the viewer count information, I am encountering issues with extracting the follower count. The essential information can be ...

Angular's parent div height can be adjusted using CSS transitions

I have implemented a CSS transition to create a sliding effect in my pagination. However, I am facing an issue where the height of the containing div changes even when I set the position of the transitioned elements. Here is a snippet of my CSS: .slide { ...

HTML5 Embedding a redirect iframe for connection status validation [UPDATE]

I've created an offline HTML file and embedded an iframe within it that redirects to a site if there is an available internet connection. However, in the event of no internet connection, the iframe will redirect to an offline page. Could someone plea ...

Is it possible to incorporate a 960 grid system into a div container?

Can I contain an entire 960 grid within a div on my webpage? I am looking to create a page wider than 960px, and considering placing sidebars outside of the 960 grid while having it manage the central column. ...

Submit Button Field - HTML ButtonFor

Being relatively new to MVC Razor and web development, both front-end and back-end, I'm in need of a button that can send a stored value to the controller/model. I attempted to mimic the functionality of Html.TextBoxFor by giving it attributes similar ...

Personalizing CSS for a large user base

My website allows users to choose themes, customize background, text, and more. I am seeking the best method to save all of these changes. Is it preferable to use a database read or a file read for this purpose? Any recommendations are greatly appreciate ...

What is the best way to align the items in the center of this container?

Check out this link: http://jsfiddle.net/zCXMv/18/ I'm having trouble getting this to work properly. Any assistance would be greatly appreciated. Here is the HTML code snippet: <div id="button" > <a class="button1 active" rel="1">&l ...

Error encountered when using Symfony 2 command line interface

Just started learning Symfony2 and encountered a problem. I made changes to my CSS and when I try to re-install them in Eclipse shell, I get this error message: 'C:\wamp\www\Symfony' is not recognized as an internal or external ...