What is the best way to position elements in a horizontal line?

I am trying to align three elements (DIVs) horizontally using Bootstrap's guidelines, but it doesn't seem to be working.

This is the desired layout: https://i.sstatic.net/7lPKU.png

However, this is what it currently looks like, aligned to the left: https://i.sstatic.net/v1Rzu.png

Here is the code I have been using:

.price-selection {
border: 1px solid #8ABE57;
display: inline-block;
 }
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

<div class="row m-0">
  <div class="col-12 product-info-subtitle cuanto-pagar">
    <p>¿Cuánto quieres pagar?</p>
  </div>
  <div class="col-12">
    <div class="row justify-content-center">
      <div class="col-3 price-selection text-center">
        <p class="price">one</p>
        <p class="period">one</p>
      </div>
      <div class="col-3 price-selection text-center">
        <p class="price">two</p>
        <p class="period">two</p>
      </div>
      <div class="col-3 price-selection text-center">
        <p class="price">three</p>
        <p class="period">three</p>
      </div>
    </div>
  </div>       
</div> 

Answer №1

Below is the accurate markup for achieving your desired result:

.price-selection {
  border: 1px solid #8ABE57;
  display: inline-block;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col-12 product-info-subtitle cuanto-pagar">
      <p>How much do you want to pay?</p>
    </div>
    <div class="col-12">
      <div class="row justify-content-around">
        <div class="col-3 price-selection text-center">
          <p class="price">one</p>
          <p class="period">one</p>
        </div>
        <div class="col-3 price-selection text-center">
          <p class="price">two</p>
          <p class="period">two</p>
        </div>
        <div class="col-3 price-selection text-center">
          <p class="price">three</p>
          <p class="period">three</p>
        </div>
      </div>
    </div>
  </div>
</div>

The key aspect here is that I enclosed the col elements within a row with the class of justify-content-around.

The inclusion of the row class resolves any margin or padding issues related to Bootstrap grid on different screen sizes.

The use of justify-content-around provides the functionality equivalent to justify-content: space-evenly. While Bootstrap currently supports only justify-content-around and justify-content-between, flexbox introduced a new value, space-evenly, which might be incorporated into Bootstrap in the future.

If you wish to evenly space your elements, you can add this CSS class manually:

.price-selection {
  border: 1px solid #8ABE57;
  display: inline-block;
}
.justify-content-evenly {
  display: flex;
  justify-content: space-evenly;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col-12 product-info-subtitle cuanto-pagar">
      <p>How much do you want to pay?</p>
    </div>
    <div class="col-12">
      <div class="row justify-content-evenly">
        <div class="col-3 price-selection text-center">
          <p class="price">one</p>
          <p class="period">one</p>
        </div>
        <div class="col-3 price-selection text-center">
          <p class="price">two</p>
          <p class="period">two</p>
        </div>
        <div class="col-3 price-selection text-center">
          <p class="price">three</p>
          <p class="period">three</p>
        </div>
      </div>
    </div>
  </div>
</div>

To better understand the distinctions between various spacing methods in flexbox, refer to this visual guide: https://css-tricks.com/almanac/properties/j/justify-content/#article-header-id-1

Answer №2

When removing the horizontal bar, use m-0.

Avoid making columns direct children of other columns. Instead, use a .row wrapper to divide columns.

Solution 1

To create gaps between columns, replace justify-content-center with justify-content-around.

.price-selection {
  border: 1px solid #8ABE57;
  display: inline-block;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

<div class="row m-0 justify-content-around element">
  <div class="col-3 price-selection text-center">
    <p class="price">one</p>
    <p class="period">one</p>
  </div>
  <div class="col-3 price-selection text-center">
    <p class="price">two</p>
    <p class="period">two</p>
  </div>
  <div class="col-3 price-selection text-center">
    <p class="price">three</p>
    <p class="period">three</p>
  </div>     
</div>

Solution 2

If you want equal gaps between columns without using Bootstrap classes like space-evenly, you can achieve this with CSS:

.element {     
    justify-content: space-evenly;
}

.price-selection {
border: 1px solid #8ABE57;
display: inline-block;
}
.element {     
    justify-content: space-evenly;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

<div class="row m-0 element">
  <div class="col-3 price-selection text-center">
    <p class="price">one</p>
    <p class="period">one</p>
  </div>
  <div class="col-3 price-selection text-center">
    <p class="price">two</p>
    <p class="period">two</p>
  </div>
  <div class="col-3 price-selection text-center">
    <p class="price">three</p>
    <p class="period">three</p>
  </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

Synchronized scrolling and equal height for multiple divs

Looking to create a system similar to GitHub's conflict resolver for my project. I have multiple versions represented by values in columns, and I want to be able to select different values from each column to create a new solution. It's important ...

Establishing a PHP session variable and then initiating a redirect to a new page through the HREF method

I am working on a table that pulls IDs from a MySQL table named auctiontable and displays them in rows. Currently, I can easily append ?aid="1"or ?aid="2" to the end of the href attribute to utilize $_GET. However, I want to prevent users from controllin ...

Exploring AngularJS: Implementing CSS media query for responsive screen resizing

I currently have a CSS code that triggers a message box through media query when the screen is resized to a specific size. I am looking for a way to make the message box disappear automatically after 5 seconds. Do you have any suggestions on how to achiev ...

Struggling to retrieve the image URL from a TypeScript file

I'm currently developing a basic app in Ionic that will include a feature to display a list of registered users along with their profile pictures. These profile images are stored in Firebase storage. Although I have successfully retrieved the URLs fo ...

The collision function is currently not functioning properly, causing a hindrance in movement. There are no other codes restricting movement

const rightPressed = false; const leftPressed = false; const upPressed = false; const downPressed = false; const players = []; players[0] = new victim(1234); const arrayw = 50; const arrayh = 50; const canvas = document.getElementById("myCanvas"); const ...

Is there a way to add a class (1) when scrolling to a specific div, and then switch from class (1) to class (2) when moving away from the

How can I create a JavaScript function that adds the class "animated fadeInDown" to div id hover1 when I reach or scroll to it? Also, when I scroll down or leave div id hover1, how do I change the class from "animated fadeInDown" to "animated fadeOutUp"? ...

How to use CSS to dynamically adjust the maximum width of an overflow container based on its children's content

I have a container with an overflowing <div> that displays multiple <ul> <li> lists. Each list always contains the same number of bordered <li> items, resembling a table. However, there may be instances where a <ul> has only ...

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

Automatically pre-fill and send hidden form

I'm working on a form and have set up a handler for the submit button like this: $( '#submit_btn' ).click( function( data ){ theForm = document.getElementById( 'realForm' ); theForm.meetingName.value = document.getElement ...

What is the best way to make my a tag background appear above my div background?

Beginner question here. I am experimenting with z-index, but it doesn't seem to be functioning as expected. Below is the code snippet I am working with: <a id="favoritelink" href="#" style="margin-left: 600px" class="addtofavorites" title="Add to ...

Changing the class of a div element in a jQuery array or JSON object with a click event

I am trying to create a function where clicking on a div adds its child's class to either a JQuery array or a JSON object. Clicking on the div again should remove the class from the array. Multiple divs should be able to be "selected" and deselected. ...

Exploring reviews with an innovative combination of Node.js and MongoDB

Recently diving into the world of Nodejs and mongoDB, I am excited to create two distinctive pages: one for users to update their account details such as profile picture, email, and name - all of which would be saved into the database; and another for re ...

How can you hide all siblings following a button-wrapper and then bring them back into view by clicking on that same button?

On my webpage, I have implemented two buttons - "read more" and "read less", using a Page Builder in WordPress. The goal is to hide all elements on the page after the "Read More" button upon loading. When the button is clicked, the "Read More" button shoul ...

Unveiling the Secret: Empowering a Span to Secure its Territory Amidst an Adv

I have a comment section header where I want the senders name(s) (on the left) and time (on the right) to align at the top. <div style="float:left;">John Doe, Suzie Q</div><span class="pull-right"> Jan 30 at 10:29 PM</span> On s ...

Guide to resetting a CSS animation with pure JavaScript

My flexbox contains 4 images, each triggering an animation on hover. However, the animation stops if I hover over the same image again. I have tried various JavaScript solutions found online to restart the animation, but none seem to be working for me. S ...

I'm struggling to get the checkbox to transmit correctly. Every time I hit submit, it consistently shows as unchecked in the database

My code aims to update the database based on checkbox information. When unchecked and submitted, or when checked and submitted, the value is set to zero in the database. In the 'users' database table, the allow_email field is represented as an i ...

Joomla website layout using Bootstrap columns set to MD-6 but not contained within a single

I seem to be experiencing an issue with Joomla SP page builder. After creating a row and adding 2 columns MD-6, they are not lining up properly in one row. Any assistance would be greatly appreciated. You can view the issue at: Here is a screenshot of t ...

Obtaining the class value using the getAttribute function

Is there a way to extract and store the value of the class (for example, "AAABC") in a variable? I've attempted various keywords with the getAttribute method, but have had no success. Using terms like "class" returned "gwt-Label", while others yielded ...

Display a progress bar in an application to visualize the loading of database records

Within my application, I am accepting a numerical input that is used to generate results on a separate page. The process involves running multiple sequential JDBC queries against Oracle database tables, resulting in a significant delay (1-2 minutes) for th ...

Customizing the size of the selectInput widget in R/Shiny

I'm trying to adjust the size of the selectInput() widget in shiny using selectize.js. I've attempted to tweak various attributes listed on this page (https://github.com/selectize/selectize.js/blob/master/dist/css/selectize.css) but I can't ...