Attempting to align three divs to the left, center, and right by utilizing Bootstrap CSS

I've been attempting to align three divs horizontally by floating them to the left, center, and right. Here is my code so far:

HTML

//to be centered div
<div style="float:center">

    <form>
    </form>

    <form>
    </form>

</div>
//to be floated to the left
<div style="float:left" id="active-container" class="row">
    <div class="col-lg-1 col-centered">
        <p style="color:#fff; font-size:15px;"><strong>FLOATED TO THE LEFT </strong></p>

        <div id="active_users">
        </div>


    </div>
</div>

//to be floated to the right
<div style="float:right" id="engaged-container" class="row">

    <div class="col-lg-1 col-centered">
        <p style="color:#fff; font-size:15px;"><strong>FLOATED TO THE RIGHT </strong></p>

        <div id="engaged_users">
        </div>

    </div>
</div>

CSS

.col-centered {
  float: none;
  margin: 0 auto;
}

One problem I've encountered is that although the left and right divs are aligned properly, the center div doesn't start at the same position. How can I make all three divs float to the left, center, and right while ensuring they align horizontally?

Answer №1

It seems like you may be new to Bootstrap. Here, I'll provide you with this answer and suggest you explore the documentation on your own.

<div class="container">
  <div class="row">

    // Centered div
    <div class="col-md-4 col-md-push-4">
        <form>
        </form>
    </div>

    // Floated to the left
    <div id="active-container" class="col-md-4 col-md-pull-4">
        <p style="color:#fff; font-size:15px;"><strong>FLOATED TO THE LEFT </strong></p>
        <div id="active_users">
        </div>
    </div>

    // Floated to the right
    <div id="active-container" class="col-md-4">
        <p style="color:#fff; font-size:15px;"><strong>FLOATED TO THE RIGHT </strong></p>
        <div id="engaged_users">
        </div>
    </div>

  </div>
</div>

Answer №2

When using Bootstrap, you can make use of the .center-block class for centering elements.

To center an element, set it to display: block and use margin property. This can be applied as a mixin or a class.

// Example of Class
.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

// Usage as a mixin
.element {
  .center-block();
}

Example:

<div class="row">
    <div class="col-xs-12">
        <div class="your-custom-class center-block">
            // your code here
        </div>
    </div>
</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 could be causing a Bootstrap JavaScript error to appear within my Chrome Extension?

I am currently working on developing a Chrome extension that will display a button on Google's search results page, and when the user hovers over the button, a modal window will appear. While I have been able to make the modal window partially appear ...

JS-generated elements do not automatically wrap to the next line

For my first project, I've been working on a to-do list and encountered an issue. When I create a new div with user input, I expect it to start on a new line but it remains stuck on the same line. Can anyone point out where I might have gone wrong? I ...

Using Jquery to hide or show objects within a <div> when clicked

My goal is to create a webpage that displays three different contents based on which button is clicked. Below is the code for reference: I want the page to show three specific sections: A search bar only when the 'search' button is clicked, T ...

What are some ways I can decrease the background width and shrink the div width?

I am hoping to maintain the current box size and borders, but I want to add a touch of color. Specifically, I would like to have a red line running through the center of the box without coloring the entire background. Although I know one way to achieve thi ...

Overriding the w-4xl with sm:text-2xl in Tailwind CSS

Struggling to achieve responsive design on my Pages, especially with changing text size when the screen is small. I've been following all the correct steps and maintaining the right order. However, whenever I set 'sm', it seems to override a ...

Is Html.Raw necessary for handling ragged html generated by Razor?

When it comes to generating HTML in a dynamic grid model using Razor, the following code snippet is often used: @{ int blockCounter = 0;} @foreach (var item in Model.Items) { if (blockCounter++ % 3 == 0) { //ragged html open here, when needed ...

Utilizing document.write to switch up the content on the page

Every time I attempt to utilize document.write, it ends up replacing the current HTML page content. For instance, consider this HTML code: <body> <div> <h1>Hello</h1> </div> and this jQuery code: var notif = document. ...

A more concise validation function for mandatory fields

When working on an HTML application with TypeScript, I encountered a situation where I needed to build an error message for a form that had several required fields. In my TypeScript file, I created a function called hasErrors() which checks each field and ...

Make the button appearance change when being clicked different from when being hovered over

I'm trying to create a button that changes background color when hovered over, but maintains the button color without a background color when clicked. This is what my current code looks like: .windowButton:hover { background-color:#1a82b8; } .win ...

Creating dynamic charts in Javascript using Firebase

My dad recently asked me to enhance our motor home by integrating an Arduino with Firebase to monitor the water and propane tanks. He's hoping to be able to check tank levels from his phone so he can see when they need refilling. I've successful ...

Stop div from exceeding the page boundaries

Here's my current dilemma: I have a simple css3 tooltip-menu that is hidden by default and appears on mouseover. It has a fixed width and is positioned absolutely to the right (with an arrow in the middle pointing to the hovered element). The issue I ...

Modifying .vue files within Laravel seems to unexpectedly impact unrelated CSS styles

I've been working on a Laravel project that involves building Vue components. Strangely, every time I update a .vue file and add it for a commit, modifications are also made to the app.css and app.js files. These changes seem to be undoing a particula ...

CSS - Button with upwards pointing arrow, requiring it to point downwards when hovered over

Struggling with the following issue: I have a button with a downward-pointing arrow in its description. When I hover over the button (or any content within it), I want the arrow to point upwards. However, currently, the arrow only points up when I hover ...

Tips for retrieving the selected option from a dropdown menu

I have a dropdown menu with checkboxes that can be selected using SumoSelect. Here is the markup for the dropdown menu: <select multiple="multiple" name="somename" id="uq" class="select"> <option value="volvo">Volvo</option> <o ...

Adjust the size of the container DIV based on the content within the child DIV

I have been searching for a solution to my question without success. Here is the issue: I have a DIV that acts as a container for a Post in my project. Inside this post are classes for the poster avatar, name, timestamp, text, and more. The problem arise ...

Saving similar and dissimilar information in a database

I have been working on implementing a Like and Unlike feature for users to interact with products. Following this tutorial at , I've completed the necessary steps. However, I'm facing an issue where the likes are not being stored in the database ...

Utilizing tab navigation with Twitter Bootstrap to streamline website navigation

I've been struggling with implementing Twitter Bootstrap's pill navigation feature, but unfortunately, it's not behaving as expected. Even though I have added the "active" class to my tab-content, nothing seems to change. The content remain ...

Incorporating an Angular 2 Directive within the body tag of an

My goal is to create a custom directive that can dynamically add or remove a class from the body element in HTML. The directive needs to be controlled by a service, as I want to manage the visibility of the class from various components. Question: How ca ...

Styling Challenge: Making the button inside a form match the design of the button outside the form

I have noticed that the button inside a form has a lot more padding around it compared to the one outside of the form. I am trying to adjust the within-form button so that it matches the padding of the without-form button. HTML/PHP <?php if (! ...

Is there a way to modify the styling for ListItemButton specifically when it is in a selected state?

My first experience with MUI involves trying to customize the styling of a button. After installing my project with default emotion as the styling engine, I attempted to override the existing styles using the css() method mentioned in the documentation: ...