Tips for aligning content in the center of a row using Bootstrap 4

I am struggling to center the content, such as an image, in both the row and column.

I have tried using justify-content-center, align-items, and text-align: center, but none of them are working for me.

My goal is to have the image centered within the row. For example, if I have 4 columns, I want all of them to be centered on the page.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap Lessons</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- My Styles -->
    <link rel="stylesheet" type="text/css" href="styles.css">

    <!-- Link to jQuery -->
    <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="crossorigin="anonymous"></script>

    <!-- Link to JavaScript file -->
    <script type="text/javascript" src="file.js" async></script>

    <!-- FontAwesome - Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  </head>
<body>

  <div class="wrapper">
    <div class="container">
        <div class="row justify-content-center">
                <div class="col-xl-12">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
        </div>
    <div class="row justify-content-center">
                <div class="col-xl-4">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
                    <div class="col-xl-4">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
                    <div class="col-xl-4">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
        </div>
    </div>
</div>

</body>
</html>

Answer №1

To style the images, you can apply the following CSS:

display: block;
margin: 0 auto

Here is the complete HTML code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Learning Bootstrap</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- My Styles -->
    <link rel="stylesheet" type="text/css" href="styles.css">

    <!-- jQuery File -->
    <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>

    <!-- JavaScript File -->
    <script type="text/javascript" src="file.js" async></script>

    <!-- FontAwesome Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  </head>
<body>

  <div class="wrapper">
    <div class="container">
        <div class="row justify-content-center">
                <div class="col-xl-12">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
        </div>
    <div class="row justify-content-center">
                <div class="col-xl-4">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
                    <div class="col-xl-4">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
                    <div class="col-xl-4">
                <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
        </div>
    </div>
</div>

</body>
</html>

Answer №2

Bootstrap 4 introduces a new class that can be used in conjunction with the row class to easily center content.

<div class="row justify-content-center">
  !-- add your content here --!
</div>

This tip may come in handy for you. You can also find more information on this topic by visiting Center Contents of Bootstrap row container.

Answer №3

To align the content of each column in the center, simply include the text-center class.


      <div class="col-xl-4 text-center">
          <img class="img-fluid" src="https://i.ibb.co/6WddWMk/test.png">
      </div>

Check out your code with this class applied to all columns:

Link

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 steps can I take to ensure that both of my codes are functioning properly in order to consistently operate the D3?

In my index.php file, I have HTML code that loads a d3 liquid gauge. When I open this in a browser with a static integer inserted into the code, it works perfectly. Then, I have another file called Tdata.php which queries MySQL and returns a table in JSON ...

What is the best method to incorporate Font Awesome icons into select options?

Is there a way to incorporate fontawesome icons into the user status indicators, such as active or inactive? I've been attempting it with the following code: <td> <select class="statusSelect" data id="{{$user->id}}&q ...

What is the best way to toggle the readonly attribute on an input within an ng-repeat row when a button is clicked?

Here is some HTML content: <!---- HTML Content ----> <div class="container-fluid" ng-app="myApp"><br> <div class="row" ng-controller="mainController"> <div class="col-md-12"> <div class="panel pa ...

hiding an "input type="file" element by using a button to conceal it

Is it possible to create a button that triggers file upload functionality? I couldn't find any solutions on Google. I've heard it can be done with labels, but I specifically need to use a button for better UX/UI. <button style="position:fixe ...

Incorporating fresh CSS styles or organizing the existing React-Bootstrap CSS

Currently, I am working on a project using React and react-bootstrap. Although react-bootstrap offers some flexibility, there are limitations when it comes to customizing components like the navbar. For instance, changing the color or dimensions of the n ...

Animating scrollTop using JQuery

There are two buttons with hidden parts related to each. When a button is clicked, its associated part will be displayed and the document will scroll to the top. Issue: The displayed part does not move to the top as intended. Your assistance in resolving ...

"Creating a CSS3 animation for changing the background image source - a step-by-step

Unfortunately, the background-image cannot be animated with keyframes in the following way: @keyframes kick{ 0%{background-image: url(images/img-man-1.png);} 25%{background-image: url(images/img-man-2.png);} 50%{background-image: url(images/im ...

What steps can I take to ensure my radio button functions according to my specified conditions?

Check out this code: <div className="form-group row mb-3"> <div className="col-sm-2" style={{ fontSize: "1rem" }}> Test:{" "} </div> <div className="col-sm-4"> <div ...

Having trouble getting the jQuery script to properly check file size in an HTML form before uploading?

I've created an HTML form with a jQuery script to verify the file size when the SAVE button is clicked. Despite my efforts, the check doesn't seem to be functioning properly in the HTML Form. Please assist me with this and thank you in advance ...

Changing the CSS Border of Thumbnails

Take a look at this screenshot of a thumbnail grid. I need to reposition the gray border so that it sits below the price and is consistently aligned across all products. The challenge arises when product titles vary in length, causing the price to be posit ...

Where can I find a style element using Selenium in Python?

Hey there! I'm looking to find a style element or style elements using Selenium in Python. <div style="flex-direction: column; padding-bottom: 65px; padding-top: 0px;"> I've tried various methods such as: self.driver.find_element ...

What is the method for writing the following line in CSHTML server-side code?

<script> function a(id) { var table = document.getElementById(id); .... } </script> @{ //Is there a way to rewrite the line "var table = document.getElementById(id)" here within the ser ...

The dropdown menu does not automatically align with its parent element's position

<div className="inner-dark-section"> <div className="search-section" style={{ width: "100%", position: "sticky", top: "0", ...

What is the reason for the varying sizes of my buttons and the irregular alignment of their contents?

Take a look at this demo using Bootstrap 4 and Material icons : https://i.sstatic.net/fdpr2.png It appears that only the first button is properly sized and its icon aligned correctly, while all icons are of the same size. I have observed that by removin ...

Button spanning the entire width, specifically designed for extra small screens

I am currently utilizing the Material UI framework to build a form, which includes a <Button> component nested inside a Grid. My objective is to maintain the standard width and height of the button on md screens and above, while ensuring it occupies ...

use vue.js to change the color based on a condition

When I implement this code: <div class="card-content"> <vue-tabs class="row" direction="vertical" value="Description"> <div v-for="(item, index) in siteObject.line_info" :key="index"> <v-tab :title=" ...

Is the embedded audio player not showing up on the screen?

I've been trying to add music to my blog page using the Billy audio player. I have successfully done this in the past, but for some reason, the player is not appearing on the finished page (I checked on both FireFox and Google Chrome) and I can't ...

Bootstrap 4's hamburger icon in the navbar toggler displaying only two lines, rather than three

I have created a navbar using Bootstrap 4. Although the navbar is functional, I am facing an issue that I have not been able to resolve on my own. When collapsed, the hamburger icon only displays two lines instead of three. I suspect that the problem lie ...

execute task to optimize CSS encoding in the build process

Creating a static project with yeoman -webapp I've implemented a UTF checkmark in my .scss files: [type="checkbox"]:checked + label:after { content: '✔'; position: absolute; top: 3px; left: 0px; font-size: 22px; color:$color-pr ...

Versatile CSS Navigation with Multiple Rows

Looking to implement a content panel switcher with 6 navigation items split between 2 rows, each containing 3 items. I want all columns to be evenly aligned to the left without excessive use of divs and clutter in the HTML code. Any suggestions on achiev ...