Aligning an image and text in the middle vertically

Utilizing Twitter Bootstrap's grid layout, I am attempting to align an image to the right and text beside it while ensuring both are vertically centered within a single column. While adjusting the image with the "img-responsive" class and adding padding resulted in successful alignment, the text remains uncentered.

I attempted to center the text by applying the following CSS code snippet to the column:

.center{
    display : table-cell;
    vertical-align : middle;
    float:none;
}

This approach worked when only text is present in the column, but failed when including an image as well.

Below is my code for the column:

<div class="col-md-3 col-xs-6 col-md-push-4 equalcolumn" id="namecolumn">
        <img class="pull-right img-circle img-responsive" id="myimage" src="http://upload.wikimedia.org/wikipedia/en/0/03/Super_Mario_Bros._box.png" alt="">
        <h4 class="pull-right" id="nametext">Welcome!</h4>
</div>

Corresponding CSS:

#namecolumn {
    padding: 1vh; 
}
.img-responsive {
height: 100%;
margin: 0 auto;
}

Your help is greatly appreciated!

Answer №1

Heading 4 acts as a block element, so make it an inline-block and apply vertical-align:middle; to both the image and heading 4, allowing them to center align with each other on the baseline.

#myimage, #nametext {
    display:inline-block;/* default display for img tag */
    vertical-align:middle;
}

Answer №2

To achieve vertical alignment, you must create two wrapper divs with the CSS properties of display: table and display: table-cell. Then apply the style vertical-align: middle.

Check out the DEMO here

CSS

.wrapper {
    height: 100%;
    display: table;
}
.wrapper-inner {
    height: 100%;
    display: table-cell;
    vertical-align: middle;
}

HTML

<div class="wrapper">
    <div class="wrapper-inner">
        <div class="col-md-3 col-xs-6 col-md-push-4 equalcolumn" id="namecolumn">
            <img class="pull-right img-circle img-responsive" id="myimage" src="http://upload.wikimedia.org/wikipedia/en/0/03/Super_Mario_Bros._box.png" alt="" />
            <h4 class="pull-right" id="nametext">Welcome!</h4>
        </div>
    </div>
</div>

Answer №3

To easily vertically center float elements such as col-md-3, check out this helpful guide at

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 are some strategies to prevent a fixed sidebar from obscuring content while zooming in?

I've been working on adding a fixed sidebar for a table of contents (TOC) to a website, where the sidebar stays in place as the user scrolls up and down. My implementation includes the following HTML code: https://i.sstatic.net/jAY8l.png and CSS co ...

Which element is undefined in this scenario - the Event Object or the inputs?

While working on OTP verification in Reactjs, I encountered an error message when running my code: Error: Cannot read properties of undefined (reading 'value') import { useState, useEffect, useRef } from 'react' import '../src/ ...

Incorporating a background image into a Material UI theme with CSS styling

When customizing a theme in material UI, one can adjust or override it using the function createMuiTheme. Below is the complete file where this process is carried out: import { createMuiTheme } from '@material-ui/core/styles'; import purple from ...

Change all lowercase characters to uppercase in the font file

: I recently created a .ttf font file, but unfortunately only included capital characters. Is there a tool or simple method I could use to automatically create lowercase versions of each letter? For instance, when typing "hello" the font should display ...

Upon the initial loading of the site, the Material Icon will be displayed as the title

Encountering a problem with Angular 7 and materials icon where the name of the icon is visible during the initial load of the site. Does anyone know how to address this issue? https://i.sstatic.net/SkNVY.png ...

Grid of domes, fluid contained within fixed structures and beyond

I've been grappling with this issue for a while now, and have had to rely on jQuery workarounds. I'm curious if there is a way to achieve this using CSS or LESS (possibly with javascript mixins). The page in question consists of both fixed and f ...

"Implement a smooth scrolling animation on the page to automatically move a div element upward and lock

I am attempting to create a unique animation for a header div that involves moving it to the top of the screen when the user scrolls down. Once the header div reaches the top, I want it to stay fixed in that position. As the header div moves upwards, it wi ...

The div element should stretch to occupy the entire height of the screen

I have structured my webpage with three different divs. The left one is defined with col-md-2, the middle one with col-md-8, and the remaining right div with col-md-2. I want the right div to take up 100% of the screen height, even when no controls are pre ...

Difficulty filling height with Bootstrap 4 flex-grow

I am in need of creating a layout with a header div at the top (that can be filled with content), a footer div at the bottom, and a middle div with an adaptable height to fill the entire screen. Check out my code below: <div class="container-fluid p-0 ...

Tips for incorporating a transition effect on group hover using Tailwind CSS

I've been working on creating a cool animated transition effect for my button where a chevron icon appears when the user hovers over it. I'm currently developing the website using NextJS and TailwindCSS, and I've implemented a group hover fe ...

Concealing collapsible Bootstrap 4 navbar with a single click

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top" id="myNavbar"> <div class="container"> <a class="navbar-brand" href="#Home"></a> <button class="navbar-toggler" type="button" data-toggle="collap ...

The adhesive navigation bar isn't adhering

I'm having issues with making my navbar inside a header sticky over the whole page. Despite trying various solutions like removing overflow, adjusting the flexbox, and setting a specific height on the parent element, I still can't get it to work. ...

Align several images vertically inside a container

I'm currently attempting to vertical align/center some images within a div, but it's not coming out perfectly. The images seem to be slightly too low to be considered vertically centered. What could be causing this issue? .Container { wid ...

Hovering over an element and eliminating any extra space

Hey there, I'm looking for some assistance with removing the whitespace on hover. Below is the code along with a screenshot for reference: div.show_sizes_onhover { background: rgba(255,255,255,.9); bottom: -1px; color: #a1a1a1; left: 0; padding: 5px ...

Creating a div container with an image and icon using Bootstrap 4

I'm attempting to create a div page with an icon positioned next to the text and button, but for some reason it's not appearing as intended. I've tried using the Flex documentation with d-flex justify-content-around in the div class, however ...

I am having trouble with my :hover selector being interrupted by text, what steps should I take to resolve this

My issue revolves around an image with a hover effect that changes the box shadow color. However, there is text on the image that also changes when clicked, creating a problem where the area occupied by the text prevents the box shadow from displaying prop ...

React Native buttons are displaying at a fixed width and not adjusting as expected

Within a row, I have two buttons with a padding of 20 between them, and they are only taking up the necessary width for the button text. The width remains constant! Here is the code for the buttons: <View style={styles.buttonContainer}> &l ...

Tailwindcss 3 fails to start on Windows 10 operating system

Let me share my experience: When I installed tailwindcss on my PC with Ubuntu Linux at home, it worked seamlessly. However, when I tried to set it up on my Windows 10 machine at work, it just refused to cooperate. I am really eager to kick off a new proj ...

JavaScript Reference for the HTML DOM Style's height property and revert back to the initial style height

Hey there! I have a simple fix for those who know JavaScript. I have some code that changes the height style on the first click, but I'm looking for a way to revert back to the original height style on the second click. Here's the code snippet: ...

How to Achieve This Effect Using Engine Limited's HTML and CSS - Step-by-Step Guide

As I continue to learn the art of article writing on wiki webpages, I have been exploring ways to replicate a certain effect that I found on another site using the same Gamepedia engine. I have spent days searching for solutions with no luck. Here is the ...