Aligning the row div in the center horizontally

Is there a way to center all columns in a row horizontally in Bootstrap 4?

The first row is centered correctly, but the columns in the second row are not aligned properly even though they have the same class.

Here's my code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<style>

    .bg{
        background: url('bg.jpg') center / cover no-repeat;
        height: 600px;
    }
    
    .row1{
        padding-top: 200px;
    }
    
    .row2{
        padding-top: 100px;
    }
    
    img{
        box-shadow: rgba(50, 50, 93, 0.25) 0px 13px 27px -5px, rgba(0, 0, 0, 0.3) 0px 8px 16px -8px;
        border-radius: 6px;
    }
    
    .img1{
        height: 164px;
    }
    
    .img2{
        height: 258px;
        width: 238px;
    }
    
</style>

</head>

<body>

<div class="container-fluid bg">
    <div class="row justify-content-center row1">

          <img src="img1.jpg" class="img1" alt="Italian Trulli">

    </div>
    <div class="row justify-content-center row2">
        <div class="col col-lg-2 text-center">
          <img src="img1.jpg" class="img2" alt="Italian Trulli">
        </div>
        <div class="col col-lg-2 text-center">
          <img src="img1.jpg" class="img2" alt="Italian Trulli">
        </div>
        <div class="col col-lg-2 text-center">
          <img src="factory.jpg" class="img2" alt="Italian Trulli">
        </div>
  </div>
</div>

</body>
</html>

The image below shows that the second row is not correctly aligned: https://i.sstatic.net/r2kCw.png

Answer №1

The columns are being horizontally centered, but the issue lies in the image sizes set within your CSS. The images do not cover the entire width of the column, making it appear as though they are not centered visually. Remember, you are centering the col divs horizontally with the code provided, not the actual images.

Example below illustrates this:

While the cols (marked by red border) are indeed centered horizontally within the row (marked by blue border), the smaller widths of the images prevent them from appearing centered.

.bg{
        background: url('bg.jpg') center / cover no-repeat;
        height: 600px;
    }
    .col {
      overflow: hidden;
    }
    .row {
     border:1px solid blue
     }

  
    
    img{
        box-shadow: rgba(50, 50, 93, 0.25) 0px 13px 27px -5px, rgba(0, 0, 0, 0.3) 0px 8px 16px -8px;
        border-radius: 6px;
   
    }
    
    .img1{
        height: 164px;
    }
    
    .img2{
        height:50px;
        width: 50px;
    }
    
    .col {
    border:1px solid red;
    }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet"/>
 <div class="container-fluid bg">
   <div class="row justify-content-center row2">
     <div class="col col-2">
       <img class="img2" src="https://via.placeholder.com/150" />
     </div>
     <div class="col col-2">
       <img class="img2" src="https://via.placeholder.com/150" />
     </div>
     <div class="col col-2">
       <img class="img2" src="https://via.placeholder.com/150" />
     </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

Adjusting the screen width to activate a responsive nav bar with a centered title

I'm facing an issue with the alignment of the heading within a navigation bar, causing the links to wrap before the collapse of the nav-bar occurs - https://i.sstatic.net/bKyPM.png I am trying to make the links collapse earlier than they currently d ...

Unusual discovery: Mysterious object manifesting in my HTML on Chrome/Webkit (with visual evidence and interactive demonstration)

I'm at a loss with this peculiar artifact that has appeared in my HTML - take a look at this highly magnified screenshot: Highlighted in red is this bizarrely small line. I've scoured my code but can't seem to pinpoint its origin. I'v ...

What is the best way to create a div that maintains a consistent size while displaying images inside?

Struggling with a project and attempting to create a fixed div container for uniform picture sizes, I've tested various methods recommended by others but have had zero success in altering the size of my div container. Could it be due to my use of boot ...

What is the best way to add several icons at once?

Is there a way to insert multiple star icons directly below the review text? I attempted to use pseudo elements to add the icons, but I could only insert one icon when I actually need to include multiple icons. Here is what I have tried so far: .review:: ...

Despite having the correct image URLs, the images are failing to display in the custom section of Shopify

Currently, I'm in the process of developing a unique Shopify section that showcases text alongside images. To enable users to upload images via the theme customizer, I have implemented the image_picker settings within the schema. The issue I am facing ...

Creative approach to achieving responsive borders with Bootstrap 5

Looking to create a dynamic border using Bootstrap 5 that will hide at specific breakpoints. I tried the following code: <p class="border-bottom border-lg-0-bottom border-lg-end">text here</p> Unfortunately, this code did not work a ...

Discover how to apply unique styles to specific HTML elements using their index values in jQuery

In the process of creating a party game similar to Tambola or Housie, I am faced with the challenge of automatically detecting and highlighting the numbers called out on each ticket in the game. For instance, if number 86 is announced, I aim to visually di ...

The scroll bar will be automatically reset once the content inside is updated by using the $selector.html() function

I am looking to create a dynamic scrollable content feature. Initially, I retrieve the data and set the content as follows: $("#sub_menu li").on("click", function () { $("#gallery").html(get_html($(this).attr("id"))); $("#gallery").cs ...

There was an issue loading the map on an HTML code that was returned from an ajax request

I'm currently working on building my own personal website, which is also my first webpage, and I'm encountering some difficulties with displaying a Google map. Here's a breakdown of my situation: my webpage consists of four links that load ...

Avoiding the stretching of images in a bootstrap carousel is a top priority

I'm facing an issue with my Bootstrap 4 carousel that takes up a large portion of the page. The images are loaded through an ajax request once received. Since all the photos are taken on a phone, they are either in landscape or portrait style. The p ...

When scrolling, apply a CSS class to a div element once it becomes visible on the

I'm in the process of developing a timeline feature for my website, and I am facing an issue where the addClass function is being applied to all sections, even those that are not currently visible on the screen during scrolling. If you would like to ...

Can someone please explain the purpose of row-sm and row-cols-sm-n (where n<12)? I've come across the col-sm-n (where n<12) but I'm unsure about how row-sm is used

From my understanding, when using col-sm-n in Bootstrap, the columns in a row will stack on top of each other when the device screen width is less than the specified size for sm. But what about row-sm? Does it function similarly to col-sm? And what exactly ...

Eliminate the option to display/hide password icon on Safari

Is there a method to eliminate the show/hide icon in Safari that pops up when entering symbols into an input field with type "password"? I was able to achieve this in IE/Edge by incorporating the following CSS rule: input[type=password]::-ms-reveal, input ...

Tips for creating a dynamic header background using custom CSS for my website design

As I embark on creating my very first WordPress website using the Blossom Pin Pro theme, I'm encountering an issue where the background image of the header disappears when adjusting the window width. Click here to visit the site If you have a code s ...

Clicking on a flex card will trigger the opening of a new page where the parsed data will be displayed in a stylish manner using JavaScript

Currently, I am attempting to showcase the data retrieved from the following URL: . My objective is to format the parsed data with a specific style. However, I have encountered difficulties accomplishing this using `window.open()`. Any assistance in resolv ...

Prevent Text Overflow in CSS, Material-UI, and React styling efforts

I am facing an issue where the cardTitle is overlapping the Card when the text is too long. Currently, I am manually setting a static maxWidth, but I would prefer for it to adjust based on the size of the Card. Is there a way to achieve this dynamically? ...

Angular UI grid: Arranging numbers in a straight line at the decimal point

I am interested in aligning decimal numbers in Angular UI Grid as shown below. 11.293 .89 233424 .34345 I have considered different approaches such as using a cell template with aligned divs or transparent 0s. Has anyone successfully imp ...

Arrangement of tables using HTML and CSS on the outdoor patio

Looking for advice on how to align buttons in the last column of a table outside of the table but in the same row. The attached image shows the current layout: https://i.sstatic.net/Ex5AR.png ...

A CSS rule to display a nested list on the left-hand side

I created a menu that you can view here. When hovering over "tanfa demo example," the sublist appears on the right side. The issue I'm facing is that my menu is positioned all the way to the right, which means the sublist also appears on the extreme ...

Is it possible to have a button within a table that, when clicked, opens a card overlaying the entire table?

I'm having an issue with a table and card setup. When I click the button in the table, the card that appears only covers part of the table. I want it to cover the entire table area based on the content inside the card. How can I make this happen? I&a ...