Position several elements in alignment within Bootstrap columns on a single row

Looking for a way to align multiple items horizontally across two columns within a row in Bootstrap? Check out the sample HTML code below:

<div class="container">
  <div class="row">
    <div class="col-sm-6">
        <h1>Short Heading</h1>
        <p>Some really long content that might throw off the alignment</p>
        <button class="btn btn-primary">Button</button>
    </div>
    <div class="col-sm-6">
        <h1>Very long Heading that needs 2 rows</h1>
        <p>Some short content</p>
        <button class="btn btn-primary">Button</button>
    </div>
  </div>
</div>

Check out this fiddle for a live demo http://jsfiddle.net/6pYhx/689/

Here are the two conditions I'm trying to meet:

  1. Ensure that the header, paragraph, and button elements are all aligned at the same height horizontally. In the example, the right header spans two rows, causing the right paragraph to be misaligned.
  2. When the screen size changes, all elements in column 1 should stack vertically followed by the elements in column 2. I've attempted to address this with multiple rows, but that messes up the order. I want header 1, paragraph 1, button 1, followed by header 2, paragraph 2, button 2.

Any suggestions on how to achieve this with CSS?

Answer №1

Here are three possible solutions to the problem:

  1. Utilize flexbox for screen sizes media >= 768px.

@media (min-width: 768px) {
  .make-it-flex {
    display: flex;
    flex-wrap: wrap;
  }
  .flex-item-1 { order: 1; }
  .flex-item-2 { order: 2; }
  .flex-item-3 { order: 3; }
  .flex-item-4 { order: 4; }
  .flex-item-5 { order: 5; }
  .flex-item-6 { order: 6; }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5…

http://jsfiddle.net/glebkema/uuLqokhm/


  1. Create duplicate data and develop two layouts for various screen sizes.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au…

http://jsfiddle.net/glebkema/cjs1q42m/


  1. Include the script and transfer data from one layout to another.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au…

http://jsfiddle.net/glebkema/cjwc6uev/

Answer №2

Are you looking to center them within the columns? Consider applying the text-center class to each column.

<div class="col-sm-6 text-center">

If you prefer vertical centering, there are various methods to achieve this. One approach is to change the display to table-cell and utilize vertical-align: middle; in CSS (see example here). Alternatively, you can explore flexbox, although compatibility may vary across browsers.

Answer №3

Are you attempting to achieve this? Expand the preview screen to view the result when testing in jsfiddle:

<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <div class="row">
        <h1 class="col-md-3">Short Heading</h1>
        <p class="col-md-7">Some lengthy content that will push the button downward, causing misalignment with the next column</p>
        <button class="btn btn-primary col-md-2">Button</button>
      </div>

    </div>
    <div class="col-sm-6">
        <div class="row">
          <h1 class="col-md-3">Very Long Heading Requiring 2 Rows</h1>
          <p class="col-md-7">Some brief content</p>
          <button class="btn btn-primary col-md-2">Button</button>
        </div>
    </div>
  </div>
</div>

You can adjust the inner columns to fit your needs, ensuring they remain on the same row. This layout is optimized for medium or wider screens, defaulting to single-column layout on small screens. Feel free to modify the md section or add additional classes like col-xs-... col-sm-... col-lg-... as needed.

Preview of the layout: https://i.sstatic.net/ds5VM.png

To horizontally align your content, you can try this somewhat complex but effective approach:

https://plnkr.co/edit/s01bZi2OJc8xm2QlKuII?p=preview

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

Implement dynamic page titles with breadcrumbs in Laravel 8 for a seamless user experience

I am currently utilizing the laravel-breadcrumbs package created by diglactic in conjunction with laravel 8, which is a fork from davejamesmiller/laravel-breadcrumbs. My goal is to incorporate a dynamic page title that aligns with this breadcrumbs package. ...

Javascript: object continuously moving despite key being released

When I leave the keyword new on line: var myGamePiece = new makeComponent(myContext, 30, 30, "red", 10, 120); The red square continues to move even when I release the arrow key. However, if I remove the new element from that line, the square stops moving ...

Failure of Outlook 2007, 2010, and 2013 to Recognize Conditional CSS

I am currently working on a design for a responsive email. The layout is functioning well in all email clients tested using Litmus, except for Outlook 07/10/13 due to its challenging word rendering engine versions. To ensure correct display across differen ...

Tips on saving the background image URL value into a variable

How can I save the URL value of a background image in a variable? For example: image: url(images/9XkFhM8tRiuHXZRCKSdm_ny-2.jpg); I store this value as value = images/9XkFhM8tRiuHXZRCKSdm_ny-2.jpg in a variable and then use this variable in an href tag. ...

Is there a way to update a PHP include file without needing to refresh the page?

I've been learning as I go, so some of my questions may seem basic or beyond my current knowledge level. However, I find that peer explanations, examples, and advice are the most effective way for me to learn. Thank you in advance for your help. Usin ...

Tips on eliminating certain text from a hyperlink

I need assistance with removing the text  Title from my link while preserving the image. <tr id="group0"> <td colspan="100" nowrap="" class="ms-gb"> <a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', ...

How to style text in CSS with a numbered underline beneath

Is there a way to apply underlining to text and include a small number underneath the underline in a similar style shown in this image, by utilizing css, html, or javascript? ...

Retrieve the input data following validation of an AngularJS form

Hello there! I am relatively new to utilizing AngularJS and Bootstrap. Recently, I have created a login form using AngularJS and Bootstrap CSS. Below you will find the code for my form: <form name="userForm" ng-submit="submitForm(userForm.$valid)" nova ...

How to extract a value from the bootbox confirm callback function

When I use bootbox, console.log(r) returns nothing. How can I successfully pass the result to the r variable? function customConfirm(message){ bootbox.confirm({ message: message, buttons: { 'cancel': { ...

Utilizing Javascript to set the innerHTML as the value of a form

I have written a JavaScript code that utilizes the innerHTML function, as shown below: <script> var x = document.getElementById("gps"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showP ...

Eliminating the need for horizontal scrolling in the window causes a change in the height of an internal DIV element

Check out this snippet of code, a simplified version of a larger piece: html, body { margin: 0 !important; padding: 0 !important; /* overflow-x: hidden; /* uncomment this line */ } .app { position: relative; width: 100%; text-align: center !important; } ...

Updating certain fields in AngularJS with fresh data

I am facing a challenge with the following code snippet: <div ng-controller="postsController"> <div id = "post_id_{{post.postid}}" class = "post" ng-repeat="post in posts"> ...... <div class="comments"> < ...

Unable to Send Messages through HTML Email Form

I have recently obtained a Bootstrap website template and I am facing an issue with the functionality of the form that is supposed to send messages to my email. Despite filling in all the required fields, the message confirms submission but no actual email ...

Calculating the time difference between the current datetime and a value retrieved from

I am having trouble calculating the difference between the current date and a date stored in my database. My goal is to display an expiry date message if the difference is 30 days. Here is the code snippet I currently have: var timeDifference = DateTime.N ...

Is it possible to show elements from an ngFor loop just once when dealing with a two-dimensional string array in a display?

I have an array of nested strings, and I am attempting to display the contents of each inner array in a table format. My goal is to have the first table show the values from the first index of each inner array and the second table to display the values fro ...

Animation of scrolling in a horizontal layout

Currently, I am utilizing ng-repeat 1.4 to dynamically generate elements within a div that has polymer shadow classes and uses the 'layout horizontal' style, resulting in code similar to: <div class='layout horizontal'> < ...

The Jquery script is ineffective for newly added elements

After creating an Ajax Form that functions well, I noticed that when the result of the form is another form, my script does not work for the new form generated. Is there a way to make the new form function like the old one? $(document).ready(function() ...

The smooth scrolling feature fails to function properly in Lenis when clicking on links with specified IDs

Is there a reason why the scroll-behavior: smooth property doesn't function properly when Lenis library for smooth scrolling is included? It seems to work fine when the Lenis code is commented out, but once it's added back in, the scrolling isn&a ...

Restrict the size of the numerical input in AngularJS

<input class="span10" type="number" max="99999" ng-maxLength="5" placeholder="Enter Points" ng-change="myFunc($index)" ng-model="myVar"> This code snippet adjusts the value of form.input.$valid to false if the number entered exceeds 99999 or is long ...

Ways to create space between the columns in a bootstrap carousel with multiple items and the previous and next buttons

I'm currently utilizing Bootstrap 4 and have created a multiple item carousel as shown in the image below: https://i.sstatic.net/NbO7u.png While the carousel is functioning properly, I'm facing the issue of not being able to add spacing between ...