How to Vertically Center an Image in a Row using Bootstrap

I've been struggling for a while to properly center this image within the row that contains the div with the image. Here is the HTML code I am using:

            <div class="row">
            <div class="col-md-9">
                <div class="col-md-12">
                    <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                    <p>Text</p>
                    <br>
                    <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                    <p>Text</p>
                    <br>
                    <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                    <p>Text(2 male, 1 female, 3 mixed characters)</p>
                </div>
            </div>
            <div class="col-md-2">
                <img class="img-full center-block" src="img/img.jpg" alt="">
            </div>
        </div>

I have tried various suggestions but none of them seem to work. https://css-tricks.com/centering-css-complete-guide/

Since this is a responsive website, I do not know the height of the element. I attempted to use translate to move the image, which resulted in this:

https://i.sstatic.net/4rt0w.jpg

Using flexbox caused issues with my mobile design as the columns did not collapse correctly, resulting in this:

https://i.sstatic.net/12uGE.jpg

I also tried the following code:

.vcenter {
display: inline-block;
vertical-align: middle;
float: none;
}

However, it did not yield any results:

https://i.sstatic.net/apCCu.jpg

At this point, I feel like I have exhausted all known solutions. Does anyone have any ideas on how to resolve this issue? I suspect it may be related to the way I have structured my columns within columns, but changing that breaks my overall design...

Edit: This is the desired look:

https://i.sstatic.net/4qOYU.jpg

Answer №1

When dealing with vertically aligning an image within a col-md-2 container, it's important to note that the height of the container will adjust to the height of the image. To ensure proper alignment, you need to set a larger height value for the parent element of the image. This can be achieved by statically defining the height in your CSS or dynamically calculating it using JavaScript. Below is an example of how you can achieve this (the CSS provided is compatible with IE 9 and newer versions; I also swapped out the bootstrap class to xs for demonstration purposes):

.vcenter {
position: relative;
  top: 50%;
  transform: translateY(-50%);
}
.taller{ height:300px;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="container">
<div class="row">
            <div class="col-xs-9">
              <div class="row">
                <div class="col-md-12">
                    <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                    <p>Text</p>
                    <br>
                    <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                    <p>Text</p>
                    <br>
                    <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                    <p>Text(2 male, 1 female, 3 mixed characters)</p>
                </div>
              </div>
            </div>
            <div class="col-xs-2 taller">
                <img class="img-full center-block vcenter" src="img/img.jpg" alt="">
            </div>
        </div>
</div>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

For those looking for a JAVASCRIPT solution:

$(document).ready(function(){
   matchColHeight();
});

$(window).resize(function(){
    matchColHeight();
}); 

function matchColHeight() {
  var col1_height = $('.col1').height(); 
  $('.col2').css('height', col1_height);  
}
.vcenter {
    position: relative;
      top: 50%;
      transform: translateY(-50%);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <div class="container">
    <div class="row">
                <div class="col-xs-9">
                  <div class="row">
                    <div class="col-md-12 col1">
                        <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                        <p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
                        <br>
                        <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                        <p>Text</p>
                        <br>
                        <h3 style="text-transform: uppercase"><strong>Title</strong></h3>
                        <p>Text(2 male, 1 female, 3 mixed characters)</p>
                    </div>
                  </div>
                </div>
                <div class="col-xs-2 col2">
                    <img class="img-full center-block vcenter" src="img/img.jpg" alt="">
                </div>
            </div>
    </div>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

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

Loading logos dynamically using Jquery upon selection of an option

In the form I created, there is a dropdown logo selection at the bottom. The options in the dropdown are populated from folders in the root directory using the following code: $dirs = glob('*', GLOB_ONLYDIR); Each of these directories contain ...

Step-by-step guide on using nodemon on a web hosting server

I am currently designing a website that includes a login form and I am in the process of uploading it to a web hosting service In order to activate my login form, I have to use node index Is there a way to change the destination of this tag: <li class ...

Transferring selected text to a Cordova application from a different application

Can I extract highlighted text from a different application, such as a browser, and send it to my Intel XDK Cordova app in JSON format for utilization? (Potentially utilizing a context menu) ...

Here are the steps to effectively incorporate CSS classes in a form using Laravel Collective 5.2 framework

Hi there, I am currently working on developing an application and find myself in need of implementing CSS classes to a form using Collective Laravel "5.2.*". The snippet of code that I have at the moment is as follows: {!! Form::model($user, array(&apos ...

Steps to deactivate a ladda button

I'm currently utilizing Ladda UI for bootstrap in my project. My goal is to disable a button using jQuery after the user clicks on it. Despite attempting to use ajax OnComplete / OnSuccess / OnBegin, I've had no luck - the button remains enable ...

What could be causing my browser to display twice the height value?

After running the code below on my browser, I noticed that the height value is rendered double. To start off, I tested the following code in about:blank. In the HTML: ... <canvas id="canvasArea" style=" width: 500px; height: ...

Utilizing Javascript to Open a New Tab from Drupal

I'm attempting to trigger the opening of a new tab when a specific menu link is clicked within a Drupal website. My initial approach was to incorporate JavaScript directly into the content of the page, but unfortunately this method has not been succes ...

Submitting an AJAX form redirects to a different page instead of keeping the user on the current page

I've encountered an issue with my popup ajax form. Instead of staying on the same page after data submission, it redirects me to send.php file. This behavior is different from another website where I successfully implemented a similar form. I'm n ...

Is there a way to show an element on a website only when the height of the HTML content exceeds the height of the viewport?

My webpage serves as a dynamic to-do list, allowing users to add an endless number of tasks of all types. As the list grows in size, the page height increases and a scrollbar appears. Positioned at the bottom right corner of the page is a fixed button that ...

Connect an EventListener in JavaScript to update the currentTime of an HTML5 media element

*update I have made some changes to my code and it is now working. Here's the link: I am trying to pass a JavaScript variable to an HTML link... You can find my original question here: HTML5 video get currentTime not working with media events javscr ...

Challenge with Alignment of Fields in Bootstrap's Horizontal Form

I am working on an Angular project with a horizontal form using Bootstrap. I have encountered a slight alignment issue with one of the date fields. The IsMessedUp datepicker input field is not aligned properly, positioned slightly to the left compared to t ...

Effortlessly showcase JSON data in an HTML owl carousel

Is there a way to display JSON data in HTML format? I have a JSON file containing reviews from Facebook, and I would like to showcase them on my website. Each review should be placed in its own div element. The JSON data below is extracted from the Faceboo ...

menu with dropdown options within dropdown options

I am experiencing an issue with creating a nested menu within my navigation header. Specifically, under the "Reports" section, I want to have two submenu items named "Global Shop" and "Ndustrios". When hovering over either of these submenus, additional men ...

Does the CSS overflow property affect the borders of an element?

Consider a situation where a "div" element has a border applied to it. When the overflow rule is set to 'hidden', any content that falls directly on the border will vanish. Is there a solution for this issue? It is crucial in my case to ensure t ...

How can I align the CSS Horizontal Menu directly under the parent menu instead of its current incorrect x-position?

I am working on creating a horizontal drop-down menu using CSS. However, I'm facing an issue where the submenu either appears all the way to the left of the website (when I use position: absolute) or directly to the left of the menu (when set to posit ...

Activate the radio button upon page load in angularjs

<div class="form-group" style="margin-top: 20px;" ng-repeat="row in skuDetails"> <label class="checkbox-inline"> <input type="radio" name="amount_{{$index}}" value="amount" ng-model="row.amount" ng-checked="row.reductionAmount != &apos ...

Positioning a div to the right of another div within a container (box)

I'm currently trying to line up two divs alongside each other within a box. Using angularJS, I am dynamically generating input boxes and looking to include an image for the delete option next to each input box. Despite using "display: inline-block", I ...

Is there a way to modify the styling for ListItemButton specifically when it is in a selected state?

My first experience with MUI involves trying to customize the styling of a button. After installing my project with default emotion as the styling engine, I attempted to override the existing styles using the css() method mentioned in the documentation: ...

What is the proper way to retrieve the JSON data?

How can I retrieve data from another table to use in a detail view if the value returned by the getData method is null? <th data-field="id" data-detail-formatter="DetailFormatter">ID</th> <th data-field="prefix&quo ...

Encountering a SyntaxError: Unexpected token < in the initial position of JSON during JSON parsing within Node.js

const express = require("express"); const bodyParser = require("body-parser"); const request = require("request"); const https = require("https") ; const app = express(); // store static files (e.g css,image files) ...