Center the image along with its caption in a div container

I am testing out this method (first answer) to vertically align an image over a div that has the following additional css properties so it occupies the entire space:

    position: absolute; 
    top: 0px; 
    bottom: 0px;
    left: 0px;
    right:0px;

The issue is, even with the fiddle mentioned in the original question (here), when you add another span with text separated by a <br/>, this span ends up outside of the containing div.

I aim to have both the image and caption positioned within the div.

Any suggestions?

HTML:

<html>
<head>
<title>Splash screen</title>
<style>
.frame {
    position: absolute; 
    top: 0px; 
    bottom: 0px;
    left: 0px;
    right:0px;
        text-align: center; 
}
.helper {
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}
img {
    vertical-align: middle;
    max-height: 25px;
    max-width: 160px;
}
</style>
</head>
<body>
<div class="frame">
    <span class="helper"></span>
      <img class="center" src="img/splash/splash.png" />
      <br />
      HELLO WORLD
</div>
</body>

Answer №1

When adjusting with the use of <br />, remember to switch from using height to min-height

check out the updated fiddle here

Below is your CSS code:

frame {
    min-height: 25px;      /* should match maximum image height */
    width: 160px;
    border: 1px solid red;
    text-align: center; margin: 1em 0;
}

In the example fiddle provided, the class is assigned incorrectly as shown:

<div class=frame>

This is incorrect

<div class="frame">
      /*___^_____^ make sure to include the quotes */

EDIT

In your case, keep the span set to inline and apply position:relative

span.helper {
    display: inline;
    height: 100%;
    vertical-align: middle;
    position: relative;
}

View working demo here

EDIT 2

Instead of using position, consider utilizing display:table to achieve your desired outcome.

Note: CSS table is IE8 compatible!

Check out the working fiddle here

html, body {
    width:100%;
    height:100%;
    margin:0;
    padding:0;
}
.table {
    display:table;
    width:100%;
    height:100%;
    margin:0;
    padding:0;
}
.frame {
    display:table-cell;
    vertical-align:middle;
    border: 1px solid red;
    text-align: center;
}
.helper {
    display: inline;
    height: 100%;
    vertical-align: middle;
    position: relative;
}
img {
    background: #3A6F9A;
    vertical-align: middle;
    max-height: 25px;
    max-width: 160px;
}

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

Using Google scripts for sending POST requests with HTMLRequest

I am attempting to authenticate my account for DirectEnergie using Google App Script. When I make the request on POSTMAN with Chrome, it works perfectly fine. However, when I try to run the same script as shown below, it simply returns the HTML of the firs ...

How can I make the menu appear only on a certain page in a React application, instead of showing on every page?

Currently, I am in the process of learning React and developing an admin panel. My main challenge is to display the menu only on specific inner pages such as dashboard, list, or add, rather than displaying it on every page including the login page. Is the ...

Incorporating styling preferences within a directive's template

One of the directives I am currently working on looks like this: app.directive('MessageChild', function($timeout) { return { restrict: 'E', scope: { pos: '=?', msg: '=' ...

Display the image on the right side when a specific number of pixels are available

Looking to have text wrapped around an image? Using float:right; can do the trick. However, is it possible to only float the image to the right when there is more than 200px of space available? For example: _____ |img | | | |_ ...

The never-ending scroll feature in Vue.js

For the component of cards in my project, I am trying to implement infinite scrolling with 3 cards per row. Upon reaching the end of the page, I intend to make an API call for the next page and display the subsequent set of cards. Below is my implementatio ...

Tips for creating a menu bar where the entire item area is clickable

Currently, I am working on implementing a menu bar control. Below is the CSS and design code that I have used: CSS : .Menu { background:transparent url(../images/blueslate_background.gif) repeat-x; text-align:center; font-family : "lucida g ...

Employing absolute picture placement

I have a collection of clipped images, each with an absolute position: <img style='position:absolute;clip(xpx xpx xpx xpx);'/> <img style='position:absolute;clip(xpx xpx xpx xpx);'/> <img style='position:absolute;cl ...

What is the procedure for adding my API key to the URI provided by British Airways?

Here is the British Airways developer site link: How can I apply my API key to this link in order to access information? URI Format: To access a resource, use the URI format below: https://api.ba.com/rest-v1/{resourceVersion}/{resourceName};{filters} ...

Is there a method to obtain the compiled CSS for an Angular 2+ component?

Is it possible to retrieve compiled CSS (as a string) from an Angular2+ component? @Component({ selector: 'test-graph', templateUrl: './test-graph.component.html', styleUrls: ['./test-graph.component.scss'] }) export cla ...

Is it possible to mix units within the 'path' element in SVG?

Utilizing SVG's rectangle element, you can specify dimensions as a percentage of its owner's dimensions and set a radius in pixels. By using the code below: <div style="position: relative;"> <object class="AIRound" t ...

Resolving the issue: "Unable to destructure the 'Title' property of 'fac' as it is undefined" in React JS

I'm facing an issue with react-bootstrap mapping in my component whereby I encounter the following error: Cannot destructure property 'Title' of 'fac' as it is undefined. It seems like there's an error when trying to de ...

Numerous volume sliders catering to individual audio players

I'm currently working on implementing volume sliders for multiple audio players, similar to what you can find on . With the help of Deepak, I've managed to create code that allows me to control play/pause and adjust the volume of various audio pl ...

Configuring the space beneath a multi-line text with a bottom border distance or shadow

I am looking to add a bottom border with less spacing than default for specific text, creating a look similar to this image: https://i.sstatic.net/sCQii.png Unfortunately, it seems the border property cannot achieve this effect. I am experimenting with t ...

How come my jQuery validation needs two clicks to activate?

I am experiencing an issue with a comments form that has 2 fields: Author and Message. Upon clicking the submit button, the validation does not trigger initially. However, if I click the submit button again, then the validation works as expected. Any tho ...

Utilizing background styles for enhancing the appearance of Ionic side menus

I've been experimenting with ionic and angular.js, trying to figure out the best way to apply background styles to side menus. I currently have a blurred background image with content boxes placed on top of it. My goal is to keep the background fixed ...

Looping through ViewData strings and populating a form with their values

I am currently utilizing ASP.NET and MVC to work on a project where I need to take a list of strings and populate a form with them. The goal is for users to import account numbers, have the form prefilled with these numbers, and then submit the data. I&apo ...

Utilize JSON data fetched from a URL to dynamically populate an HTML content

There is a JSON file located at a URL that looks like this: [{"tier":"SILVER","leagueName":"Tryndamere's Wizards","queueType":"RANKED_SOLO_5x5","playerOrTeamId":"91248124", "playerOrTeamName":"NunoC99","leaguePoints":18,"wins":411,"losses":430,"rank" ...

Tips on emphasizing all div elements and incorporating navigation to each of them

I am seeking a method to enhance a div with highlighting when there is a click or mouseover event. For instance, changing or adding a border color using JavaScript on click or mouseover is straightforward. Recently, I have been contemplating the idea of a ...

The Cheerio.js .each() method produces a single long string if paired with .find('a').text()

When using CheerioJS to scrape a list of text from a website, I encountered an issue with the .each method on an array of table rows. Despite finding the text correctly, all the data was returned as one big string instead of an array of strings. Below is ...

get the queryset for a dropdown menu item in Django

Currently, I am in the process of creating an HTML calendar page with a dropdown feature to select different months. Accessing this calendar page can be done through the navigation bar set up in my base.html base.html - Direct access to the calendar page. ...