align text vertically over an image

I've come across this question many times, but none of the answers seem to solve my issue. My challenge is centered around aligning text on an image with regards to vertical positioning. Below is the code I'm working with:

<div class="col-sm-12 col-md-12 col-lg-12 main_category_container">
    <div class="col-sm-4 col-md-4 col-lg-4">
        <div class="wrap">
            <h3 class="sub_category_title"><a href="some link">Centered text</a></h3>
            <a href="some link"><img src="some image"></a>
        </div>
    </div>
</div>

and

.wrap {
height:auto;
margin: auto;
text-align:center;
position:relative;
}

h3.sub_category_title {
vertical-align: middle;
}

As it stands now, the text is horizontally aligned above the image, but I'm struggling to get it vertically centered within the image.

Your help would be greatly appreciated!

Answer №1

Typically, when you want to position one element on top of another, you'll need to use either position: absolute or position: relative. Otherwise, the elements will just stack in a block flow naturally.

You can experiment with this approach:

.image-container {
  position: relative;
  text-align: center;
  
  width: 300px; /* adjust as necessary */
  margin: auto;
}

.image-link { display: block; }

.image-caption {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  margin: auto;
  
  /* Additional styling for demonstration purposes,
  modify to suit your needs. */
  
  font-weight: bold;
  color: white;
  font-family: sans-serif;
  background: rgba(0, 0, 0, 0.5);
}
<div class="image-container">
  <a class="image-link" href="#">
    <img class="image" src="http://loremflickr.com/300/200" alt="placeholder image">
    <h3 class="image-caption">Centered Text Overlay</h3>
  </a>
</div>

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

Adding / Deleting a Row in a sequence of floated divs

I have a group of div elements arranged side by side with the float:left style as shown below: <div id="container"> <div id=0" style="float:left">> Stuff </div> <div id=1" style="float:left">> Stuff </div> < ...

In CSS, the primary consideration is consistently maintaining a uniform height for the main section

In the process of developing our website, we have a main section (div main and mainholder) with a maximum height set at 770px. Whenever the content in this section exceeds that height, it gets cut off on the page. However, we want the height of this sect ...

Tips on properly styling HTML using the BEM naming convention

I'm trying to implement BEM naming in my project, but I'm having trouble with how to name elements within elements. BEM guidelines state that elements of elements should not exist. How should I approach naming them correctly? <div class="cont ...

When using my webrtc technology, I configure my sdp to establish a recvonly direction

While attempting to make a video call using WebRTC, I encountered bugs during testing. My goal is to integrate this project into a webview for my Android app. I conducted testing using phone-pc and phone-phone scenarios. Scenario A: When the PC initialize ...

Adjusting the size of all elements on a webpage

Upon completing my project, I noticed that my localhost:3000 is zoomed in at 125%, causing it to appear less than ideal at 100% zoom. Is there a way to adjust the zoom/scale of my website to match how it appeared on my localhost environment? I came across ...

The footer section of my website seems to have gone missing in the HTML/CSS coding

I'm having trouble with my website footer not displaying. I've tried using "position: absolute; top: 50px; left:100px;" in my HTML/CSS, but it's not working. Can anyone help me fix this issue? Here is a link to the code I'm working on: ...

Unique custom CSS and meta tag options for enhancing iPad/iPhone user experience

Currently, I am developing a web application that integrates Extjs components, PHP, and MySQL. My main goal is to ensure that my application looks correct on the iPad. Are there any specific CSS rules or meta tags that I should be implementing for optima ...

Optimizing Bootstrap 4 Flex-Row Dimensions

Issue: I'm currently working on a relatively intricate ListGroup using bootstrap v4. The .flex-row div is displaying some mysterious white space at the bottom, and I need assistance in eliminating it. Attempts Made So Far: I've experimented w ...

Are you looking for a stunning vertical gallery with responsive design in Photospace Gallerific? Let's enhance it

Is there a way to make my gallery from http://wordpress.org/extend/plugins/photospace-responsive/ responsive? I want it to change from a vertical layout to horizontal when the browser is scaled down for mobile viewing. Can you advise on how to ensure that ...

Create a JavaScript and jQuery script that will conceal specific div elements when a user clicks on them

Within my navigation div, there exists another div called login. Through the use of JavaScript, I have implemented a functionality that reveals additional divs such as login_name_field, login_pw_field, register_btn, do_login_btn, and hide_login upon clicki ...

Tips for incorporating several d3 elements on a single webpage

I am currently facing an issue where I am attempting to add a line graph below a d3 map, but the map and line graph are appearing below where the map should be located. I have tried creating separate div tags with distinct id's, but I am unsure of wha ...

What is the method to activate a button from a different action?

I have set up the PayPal payment form on my website. <form name="form1" method="https://www.sandbox.paypal.com/cgi-bin/webscr"> <!-- Rest of input --> <!-- Button that triggers redirection to PayPal payment --> <input type="submit" ...

What distinguishes submitting a form from within the form versus outside of it?

When the code below, you will see that when you click btnInner, it will alert 'submit', but clicking btnOuter does not trigger an alert. However, if you then click btnInner again, it will alert twice. Now, if you refresh the page: If you first ...

Setting line height as a pixel value does not yield the desired result

$(element).css(options.css).attr(options.attr).addClass(options.class) //ensure line-height is correctly assigned if($(element).is('p') && _.isString(options.css['line-height'])){ console.log('line-height assigned: &apos ...

Selenium Scrolling: Improving Web Scraping Efficiency with Incomplete Data Extraction

I have been attempting to extract product data from a website that utilizes JavaScript to dynamically render HTML content. Despite using Selenium, implementing scrolling functionality to reach the end of the page, and allowing time for the page to reload, ...

When attempting to click the submit button, the action of submitting a form using jQuery/AJAX is not functioning as expected

When attempting to submit a form using jquery/AJAX, my function does not get called when clicking on the submit button. The structure of my website is as follows: CarMenu.php <html lang="en"> <html> <head> <meta charset="ISO-8859- ...

Inserting data into a Textbox by clicking on a Div

I'm currently working on creating a wallpaper changer for my website. Right now, I'm looking to input the URL of a wallpaper into a text box when the corresponding DIV option in a CSS menu is clicked. Below is the JQuery I am using: $("div.bg8 ...

Implementing smooth transitions on iPad screens with responsive text

I am currently working on a personal project that involves creating images with hover transitions. The transition effect works perfectly on all browsers except for iPads. I'm facing an issue where I can't click on the image due to the presence of ...

Contrasting the utilization of percentage width in relative vs fixed contexts

I am facing an issue with the sizing of two div elements on my page, one with relative positioning and the other with fixed positioning. <div class="outer1"> </div> <div class="outer2"> </div> Here is the CSS: .outer1{ width: ...

Achieving auto-height for two elements with CSS within a single block

I am attempting to combine two blocks into one fixed-height block in order to achieve the following design: ------------------------ UL (starts with height=0), expands when elements are added until maximum height is reached scroll bar should appear aft ...