How to Align Image in the Middle using Bootstrap 4

After attempting to use mx-auto to center an image within a Bootstrap 4 card just beneath the card-text, I found that the image remained left-justified.

<img src="my.svg" class="mx-auto" alt="...">

I also tried placing the img inside a div and using text-center, but this approach was unsuccessful.

What is the best way to center it?

Answer №1

For centering an image, utilize the classes mx-auto d-block

<div class="container">
    <div class="row>
        <div class="col">
            <img class="mx-auto d-block" src="...">  
        </div>
    </div>
</div>

Refer to this link for more details.

View live example on Codeply

Answer №2

To center an image, simply add the class .center-block within the img tag like so:

<div class="container">
  <div class="row">
    <div class="span4"></div>
    <div class="span4"><img class="center-block" src="picture.png"/></div>
    <div class="span4"></div>
  </div>
</div>

Bootstrap already includes a CSS style called .center-block for this purpose:

.center-block {
    display: block;
    margin-left: auto;
    margin-right: auto;
 }

You can view a working example here

If the .center-block class doesn't work, you can also try using mx-auto d-block.

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 is the process for choosing every child (regardless of level) of a parent using jQuery?

Is there a way to unbind all elements from a parent node using jQuery? How can I effectively select all children, regardless of their nesting level, from a parent node? I attempted the following: $('#google_translate_element *').unbind('c ...

Creating dynamic names for radio buttons in Bootstrap forms: A guide to generating unique identifiers

This is a continuation of the discussion in this post. To better understand the concept, refer to this JSfiddle example. Check out the final solution provided by @Scaramouche The challenge at hand involves dynamically generating unique names for radio b ...

Customizing Material UI Themes

Is there a way to customize the MuiTheme overrides for a specific named element like this? .MuiStepLabel-label.MuiStepLabel-active { color: rgba(0, 0, 0, 0.87); font-weight: 500; I've attempted modifying various classes such as MuiStepLabelLa ...

The inclusion of Bootstrap 4 in the header seems to be causing issues with mouse events not functioning correctly

Exploring the functionalities of Bootstrap 4 and jQuery has been an interesting journey. I recently worked on creating a Navbar with 4 links, where the 4th link has a submenu that opens upon hover. However, I encountered an issue where this functionality o ...

picture protrudes from the frame [WordPress template]

Check out my website: If you scroll to the bottom of the site, you'll see an image of a bride sitting on a couch. I recently added this code to the stylesheet: .novia img {min-width:1000px; float:none;} This code was meant to maintain a fixed heigh ...

What is the method for altering the background color of an input field upon entering text?

As a beginner in Javascript and jQuery, I am struggling to understand why my code is behaving the way it does. I have written two similar functions aimed at changing the background color of an input field. The objective is to set the background color of t ...

Achieving a full width vertical navigation on the left side using Bootstrap's grid system: a step-by

I am looking to design an admin panel using bootstrap. To start off, I want to create a navigation menu on the left side. However, I encountered an issue where my navigation menu with a red background does not stretch the full width of the left column. Her ...

In HTML, adjust column widths for multiple tables according to the widest column present in any of them

With Python, I am creating an HTML page that contains multiple tables with the same number of columns, all holding the same type of data. While the generated page is functional, I would like to improve readability by ensuring that all tables have the same ...

Swap the content of one div with another div using client-side code only

Currently, I am in the process of developing my personal website. To enhance user experience, I have implemented a vertical navigation bar on the left side of the page. My goal is to replace the content of a specific div with content from other HTML files ...

What is causing the cards to overlap on a tablet-sized screen?

Is anyone else experiencing issues with bootstrap cards overlapping on tablet views? I had to adjust the height and width in the HTML code as opposed to CSS. Check out my HTML code below: <div class="container"> <div class="update& ...

The Bootstrap col-md class causes the label text to be truncated

I am encountering an issue where a label extends beyond a certain length and ends up being placed under other elements. Check out the image for reference: form input I would like the entire label text to be visible. I believe the col-md-1 class from boot ...

Alignment of button within bootstrap column is skewed when screen size is reduced

My button is perfectly centered in one of my bootstrap columns, but things start to get messy when the screen size shrinks and the button overlaps with another column containing text. Adjusting the margins only seems to make things weirder. .center-btn- ...

Why does my Node.js server-hosted site get downloaded as a file by IE9, Opera, and older versions of Safari?

I'm feeling completely stuck. This whole situation just doesn't make any sense to me. Even though I'm new to HTML5, CSS, and Javascript, I've put together what I think is a decent (albeit unfinished) website design and a simple (also u ...

How to access a bootstrap 4 tab panel on a different page

How can I successfully open a specific tab panel on the service.html page when clicking a link on my index page? I'm using bootstrap nav-pills and have tried the following, which unfortunately did not work as expected: service.html <div class="na ...

Tips for successfully implementing a basic JQuery code in Internet Explorer

Having trouble getting this to work on my website, especially in Internet Explorer. Check it out here: http://jsfiddle.net/b43hj/24/ Any tips on how to make it compatible with all browsers? Thank you edit - I've only used the code from the jsfidd ...

The ng-bootstrap modal fails to appear when incorporating [formGroup]="FormName" or formControlName="elementName"

Utilizing ng-bootstrap to create a popup modal has been a challenge for me. When I import FormsModule and ReactiveFormsModule in src/app/modal-basic.module.ts, the code inside it looks like this: import { NgModule } from '@angular/core'; import { ...

Positioning divs around a circle can be quite challenging

Among my collection of divs with various classes and multiple child divs representing guests at a table, each main div symbolizes a specific restaurant table type. I have created a jsfiddle for demonstration. http://jsfiddle.net/rkqBD/ In the provided ex ...

Picking a live Selected choice within an HTML form

I currently have a dropdown menu that displays a list of employee names: <select class="form-control" id="assignee" name="assignee" style="max-width: 300px;"> <option value="Employee 1">Employee 1</option> <option value="Em ...

Instant messaging platform designed for communication and sharing of files

I am currently tasked with creating a messaging app for people working on the same project to communicate and occasionally share files. It would be ideal if I could incorporate video chat as well. Due to budget constraints, we need to use an open-source pl ...

Is there a way to create a div element that allows both scrolling and dragging of the inner content?

I'm having trouble with a game board on my webpage. The board is 20x20 squares, but the container can only fit 10x10. I want to make the game board draggable so that the user can move it within the limits of the container. I tried using jQuery UI&apos ...