Align the image in the middle of a flexible container, ensuring it remains

I'm struggling with centering an image vertically between two floated internal divs within a containing div. I've searched online for solutions, but none seem to work for me. Below is the code I'm using:

HTML:

<div id="rt-banner2">
        <div id="rt-contact_card">
            <h1 class="_site-title">Title</h1>
            <h2 class="site-subtitle">SubTitle</h2>
            <h2 class="_site-title">Mob: +123 453 769</h2>
            <h2 class="_site-title"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="107e717d753e6365627e71...
        </div>  
                <div id="site-header">
                    <img src="http://mydomain.com/wp-content/themes/themeName/images/stock-photo-dried-rose-buds-tea.jpg" width="531px" height="246px" alt="">
                </div>
                    <span><img id="rt-arrow-div" src="http://mydomain.com/wp-content/themes/themeName/images/arrow.png" alt=""></span>                  
        <div class="rt-clear"/>


        </div>

Css:

#rt-banner2 {
border-top: 25px solid #be2331;
border-bottom: 25px solid #be2331;

height:246px;
width:100%;

text-align: center;
}

#rt-contact_card
{
float:left;
width:431px;
background:teal;

height:100%;
}


#rt-contact_card h1
{
text-align:center;
font-size:1.3em;
}

#rt-contact_card h2
{
text-align:center;
font-size:1.2em;
}

#rt-banner2 span {
display: inline-block;
height: 100%;
vertical-align: middle;
}

#rt-banner2 span img
{
vertical-align: middle;
max-height: 246px;
}

#site-header
{
float:right;
width:531px;
}

.rt-clear
{
clear:both;
}

I have included a link to a screenshot of the current result.

The arrow in the center is the image I am trying to vertically center. Can anyone help me figure out what I am doing wrong? Thank you for your assistance.

Answer №1

Alternatively, another approach is to use a background image by following these steps:

#the-img { 
    width: 100%; /* adjust as needed */
    height: 100%; 
    background: url(image.jpg) center center no-repeat;
}

Answer №2

Give it a shot:

try including the following parameter in the div that contains the image:

position: relative;

and for the image itself:

position: absolute;
top: 50%;
margin-top: -(half of the image height)%;

This method should do the trick!

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 designing a dropdown box that showcases text details?

I am in the process of building an FAQ page and I am looking to incorporate a dropdown menu that lists all the frequently asked questions. When a user clicks on a question, I want the corresponding answer to be displayed without redirecting to a new page. ...

The img:first-of-type selector targets specifically the first image within a group

When a user visits my website using a touch device or without Javascript support, I want the first of two images to display. To achieve this, I am targeting them using Modernizr. The menu button at the top of the page: <img src="menubutton.png" alt="M ...

Rebel Spirit on a Wordpress Platform

I've encountered a strange issue on my Wordpress blog. For some reason, the code "&nbsp" is being inserted right after the opening <body> tag. I have checked the header.php file and there is no instance of this code present there. It's ...

Vuetify Container with a set maximum width that remains fixed

I recently started developing a web application using Vue.js and Vuetify (https://vuetifyjs.com/en/). Currently, I have a basic layout with 3 columns. However, I noticed that the width of these columns is restricted to a maximum of 960px due to some defau ...

Demonstrating the transformation of child elements into parent elements through angular 6 ngFor

I have a JSON array dataset where each object may contain nested arrays. In order to display the inner nested array elements as part of the parent array using Angular's NgFor, I need to format the input like this: [{ 'id': 1, 'tit ...

Guide for positioning the p-checkbox label to the left of the checkbox using PrimeNG

<p-checkbox name="group1" [value]="true" label="Outsourcing" style="margin: 0 25px"></p-checkbox> I am looking to change the position of the label from the right to the left ...

Finding JPG images using Jquery selector

I am looking to utilize jQuery to specifically target <a href=""> elements that have 'href' attributes with file extensions '.JPG' or '.jpg' For instance: To only select these 'links' <a target=& ...

How to approach the situation with semantic markup and responsive design in mind?

Currently, I am developing a small website employing the mobile-first strategy. This means that I design for small screens first and then adapt as the screen size increases. To save space, I either show or hide certain information on the page or swap it ou ...

The JSON file is incomplete without its brackets

Exploring JSON format testing using Postman { "localTimeStamp": "2021-08-14T08:19:17.000Z", "ipAddress": "10.0.5.26", "subnetMask": "N/A", "defaultGateway": "N/A", "productType": "UNIVERGE BX9000", "versi ...

Struggling to design a form to incorporate changing weekly elements into a homepage

Hey, I'm looking to simplify my life by creating a form that allows me, as the administrator/creator, to easily update a group of weekly variables from datalists without needing to manually change the code every time. <form method="post" action= ...

The $(window).load(function() function is unable to run once the entire document has finished loading

I have not been able to find the solution in the following circumstances: In an HTML document, I successfully load multiple other HTML files. However, within one of these included HTML files, specifically "navmenu.html," I want to execute a script with a ...

Is there a way to verify and send notifications when duplicate entries are added to my table?

Whenever a make and model are added using the "add" button, I need to ensure that there are no duplicates. If a duplicate is found, an alert should be displayed, and the entry should not be added to the table. Unfortunately, I have been unable to find a so ...

Click event form validation not functioning in JavaScript

Hello, I am currently setting up validation for input fields on keyup and when the submit button is clicked. I have opted not to use input type submit to avoid reloading the page after submitting the form. The logic is that if the input field is empty or ...

Effortless Basketball Movement Script in Javascript

What happens: Pressing the space button causes the ball to move up and then stop. When pressed again, it moves down. What I need: I want the ball to move up and then move down when I press the space button! I'm trying to figure out how to repeat thi ...

Is it possible to create horizontal scrolling lanes using HTML and CSS?

Simple code snippet: <div id="page-wrap"> <div class="post horizontal"> <h2>Headline 01</h2> <div class="entry"> <p>Lorem ipsum dolor...</p> <p class="image"><img class= ...

Populate your HTML with JSON data

I need some guidance on how to achieve a specific task. I have a PHP file that retrieves data from a MySQL database and returns it in JSON format. Now, I want to display this data in HTML with "data_from_json" replaced by "18.5". Any assistance would be gr ...

Spacing issues with inline-block elements when dealing with a large quantity of items

Currently, I am tackling the JS/jQuery Project for The Odin Project and I am confident that my solution is working exceptionally well. However, the issue I am facing is that when dealing with larger amounts of elements (around 40-45 in JSFiddle and 50-52 ...

Sticky CSS - A guide to making aside elements fill the entire available height

How can I create a grid layout where the aside is sticky to top: 0 and fills all remaining height (e.g. 100% of height), but when scrolling down, the aside height should be changed to 100% minus the footer's height? Is it possible to achieve this with ...

Is it possible to restrict WordPress Blog archives to only display the current page?

I am currently in the process of creating a WordPress website that will feature both News and Blog sections. These sections will be on separate pages, with News and Blog posts categorized accordingly. For example, I have implemented the following code on ...

What is the best way to send HTML tag content to mark.js and delimit them with a space or comma?

I have been utilizing the mark.js library to highlight keywords on a webpage, and it's been working well. However, I now need to insert an extra space or a comma after each tag such as h1, h2, etc. Initially, I thought about using a loop like the one ...