How to vertically center an image and text in a div using Bootstrap 4

Bootstrap 4:
Is there a way to horizontally center a div that includes both an image and text positioned to the right of the image?

                                 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                                 |     | Heading                     |
                                 + IMG +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
                                 |     | Lead                        |
                                 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

I attempted to create three columns within a row,

<div class="row">
    <div class="col"></div>
    <div class="col-auto">
        <img src="..." width="..." height="..." />
        <h1 class="display-5 m-0">...</h1>
        <p class="lead">...</p>
    </div>
    <div class="col"></div>
</div>

However, I faced an issue where the heading text was too long and wrapped onto the next line. Adjusting the size of the middle column caused it to no longer be centered. How can I ensure that it remains centered regardless of the length of the text?

Answer №1

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row justify-content-center">
    <div class="col-3">
        <img src="https://uniquephoto.com/design-image-11-1.jpg" style="width: 100%;height: 100%;" />
    </div>
    <div class="col-9">
        <h5>Greetings Universe</h5>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed hendrerit adipiscing blandit. Aliquam placerat, velit a fermentum fermentum, mi felis vehicula justo, a dapibus quam augue non massa.</p>
    </div>
</div>

Answer №2

I finally understood the solution.

<div class="row">
  <div class="col-12 d-flex flex-row justify-content-center">
    <img src="..." width="..." height="..." />
    <div>
      <h1 class="display-5 m-0">...</h1>
      <p class="lead">...</p>
    </div>
  </div>
</div>

Answer №3

Check out this snippet

  <div class="text-center">
      <div class="row justify-content-md-center">
        <div class="col-md-auto">
          <img src="https://placehold.it/100x100">
        </div>
        <div class="col-md-auto">
            <h1 class="display-5 m-0">...</h1>
            <p class="lead">...</p>
        </div>
      </div>
    </div>

To see a live demo, visit: https://www.bootply.com/UHz1ACh2CI

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

Understanding the res.render method in JavaScript can be a bit tricky at first

In my spare time, I have been immersing myself in coding lessons and have encountered some puzzling aspects of the code: Firstly, there is a confusion surrounding the action attribute in HTML Secondly, this particular piece of code is causing me some b ...

The form reappears on the screen once the value has been successfully transferred to the PHP script through AJAX

My main challenge was sending data from an HTML form to PHP code to compare the selected value with database information and display the result. I managed to get everything working correctly, but now I am facing a peculiar issue where the drop-down box is ...

Troubleshooting responsive navigation bar in VueJs: Why are elements not appearing when ToggleButton is clicked?

I'm developing a VueJs single page application and I'm struggling to implement a responsive NavBar. No matter what I try, it just doesn't seem to work as expected. I've experimented with several solutions, and the closest I have come t ...

Tips for Removing Padding in Material UI Container Component

I'm currently working on creating a hero banner using the material-ui framework and I've encountered an issue. Here's what I have so far: https://i.stack.imgur.com/UXTDY.png However, I'm facing an irritating problem with left and rig ...

Facing the issue of missing data-icons while retrieving HTML content through AJAX requests

My php function generates grid data that appears like this: <div id="_gridData"> <div class="ui-grid-b"> <div class="ui-block-a mobile-grid-header">&nbsp;</div> <div class="ui-bloc ...

The styles are not being rendered on the Angular application

I have successfully implemented a Modal service with working HTML/CSS (Check it out on StackBlitz) div.cover { align-items: center; background-color: rgba(0, 0, 0, 0.4); bottom: 0; display: flex; justify-content: center; left: 0; ...

How to turn off pinch to zoom feature in Mozilla Firefox on a Microsoft tablet or touch screen display

My goal is to enable zooming and panning for an SVG across all devices and browsers. I've successfully implemented this using panzoom.js. However, I'm encountering an issue specifically with Mozilla Firefox on tablet devices and Windows touch sc ...

Navigate to another HTML division using data-toggle

I have 2 HTML documents index.html and services.html In services.html, I have the following code: <a class="nav-link" data-bs-toggle="tab" href="#ds-tab">Data Science</a> <a class="nav-link" data-bs- ...

Can someone assist me in figuring out how to solve selecting multiple radio buttons at once

<script type="text/javascript"> let x = "1.html"; let y = "2.html"; function redirectPage(form){ for(let i=0; i<form.length; i++) { if(form.answerq[i].checked && form.answerw[i].checked && f ...

What is the best way to ensure a dropdown in an input group occupies the full width?

One of the challenges I'm facing involves a row with 2 columns - the first column contains a title and the second column features an input group. Within the input group, there is an icon and a dropdown menu. Unfortunately, the dropdown menu is not oc ...

Numerous features

My goal is to create a program with two functions - one for addition and one for multiplication. For example, if I input 5 and 6, the calculate button should output 11 and 30, respectively. Should I combine both functions into one or keep them separate? An ...

Incorporating SCSS into HTML files when they are situated at the same hierarchy

I have <h1 class='title home'> My Title </h1>, and to apply styles I can use the following CSS: .title { font-size: 1.95em; } .title .home { color: black; } While this method works, I a ...

Website glitch encountered on Flash when using Firefox and IE9, though surprisingly functions perfectly on IE6!

My website isn't functioning properly on Firefox versions 4 and 7. Nothing is appearing, not even the alternative content. In IE9, the issue is that the content is condensed at the top of the page... although it works fine on IE6! Here is the HTML c ...

How to properly connect a Bootstrap Navbar with Next.js

Help! I've been struggling for hours trying to set up a basic Bootstrap Navbar with Nextjs. I also feel like there are other issues besides the error message being displayed. Any suggestions on how to improve this setup overall? Should I use _app.js ...

Unable to determine the final price, as it is unclear if a discount is applicable in Ionic

I am currently developing an Ionic Ecommerce App that is utilizing an API created in Laravel. The issue I am facing involves displaying product prices, including the actual price, discount, and final price. The problem arises when the discount is set to 0; ...

Image hover effect displayed when hovering over an image

Is it possible to create a hover effect using purely CSS when hovering over an image, like in this example: https://i.sstatic.net/jjAWm.jpg I've come across several similar examples online, but they all seem to rely on jquery or javascript. I'm ...

Can someone tell me where I can locate the CSS file once I've finished using Scene Builder?

Currently, I am working on my database project using Scene Builder. I have applied some CSS styling and would like to locate the CSS file in order to attach it to my IntelliJ project for further code editing. Can anyone guide me on where to find the CSS ...

Issue with datetimepicker functionality in asp.net core

Attempting to utilize a DateTimePicker in an Asp.net core Razor page, I scoured the internet and stumbled upon this webpage which seemed promising. Consequently, I navigated to NuPackage and procured Bootstrap.v3.datetimepicker 4.17.45. Following installat ...

Utilize animation effects in jQuery to make text slide upwards and smoothly transition to a new text

I am currently working on developing a website that requires dynamic data changes. To enhance user experience, I aim to display the information in an animated format where text gracefully transitions by moving up and being replaced by new content emerging ...

Adjust the width of a modal in Bootbox for a specific modal only

Currently, I am successfully using Bootbox 4 along with Bootstrap 3 on IE (IE 8 / IE 9) without any issues. In Bootstrap 3, it seems that you can adjust the modal width using CSS like this, but the drawback is that this change will apply to all modals: . ...