Is there a way to perfectly align text both vertically and horizontally on an image with dynamic percentage width and height?

Struggling to center text over a div with percentage width and height? Flexbox didn't work out, so I turned to a tutorial for guidance: http://jsfiddle.net/4be9cp23/. Here's the code snippet from the tutorial:

html

<div id="header">
        <h1>Home</h1>
</div>

CSS

#header {
        background-color: red;  
        color: white;
        text-align: center;
        vertical-align: middle;
        width: 100%; 
        height: 15%;
    }
#header h1{ display:inline-block; }

View the demo I'm working on here http://codepen.io/maureenv/pen/dMEPap?editors=1100. Trying to center white text over an image of a dragon without affecting other elements.

Followed the tutorial but struggling with: HTML

 <div class="masthead-container">
      <img src="http://maureenvogel.com/images/charizard.jpg" class="masthead-img">

      <div class="quote-container">
        <div class="quote"> "This is my quote that won't center no matter what I do. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " </div>
        <div class="quote-author"> -quote by me</div>
      </div>
    </div> <!-- close masthead container div -->

CSS

.masthead-container .masthead-img{
  width: 100%;
  top: 0;
  left: 0;
  display: block;
  position: relative;
}

.masthead-container {
  text-align: center;
  vertical-align: middle;
}

.masthead-container .quote-container{
  display: inline-block;
}

.quote-container{
  top: 0;
  left: 0;
  color: white;
  position: absolute;
  text-align: center;
  width: 100%;
}

The text stubbornly refuses to center, and display-inline block isn't cooperating. Seeking assistance. Much appreciated!

Answer №1

Here is an example to consider:

.quote-container{
  position: absolute;
  top: 50%;
  left:  50%;
  color: white;
  text-align: center;
  display:inline-block;
  transform:translateX(-50%) translateY(-50%);
}

Answer №2

One potential solution could be to set your image as the background of the div, and then add the following CSS:

.header-container{
    display: table;
    position: relative;
    width: 100%;
    height: 100%;
    background: url(...);
}

.text-center{
    display: table-cell;
    text-align: center;
    vertical-align: middle;   
}
<div class="header-container">
  <div class="text-center"><p>hello-world</p></div>
</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

Troubleshooting problem with text overlaying images upon hovering in CSS

Several months ago, I successfully implemented a cool effect on a Shopify website that I had designed. However, recently the client decided to switch to a new theme without my knowledge, and now one of my CSS tricks is no longer working. If you look at t ...

In Safari, an animated link does not respond to pointer-events, making it unclickable

Exploring the world of CSS animations, I have created a snippet where elements appear sequentially. Take a look at it first: .wrapper { pointer-events: none; /* disable pointer events until animation completes */ } .text { position: absolute; } ...

Tips for transforming a date into a time ago representation

Can someone help me with converting a date field into a "timeago" format using jquery.timeago.js? $("time.timeago").timeago(); var userSpan = document.createElement("span"); userSpan.setAttribute("class", "text-muted"); userSpan.appendChild(document.crea ...

Tips for automatically resizing a canvas to fit the content within a scrollable container?

I have integrated PDF JS into my Vue3 project to overlay a <canvas id="draw_canvas"> on the rendered pdf document. This allows me to draw rectangles programmatically over the pdf, serving as markers for specific areas. The rendering proces ...

Is there a way to retrieve php session in a javascript file?

Here's the code snippet for reference: Contents of index.php file Javascript code from index.php function Result() { var marks = 55; document.getElementById("hdnmarks").innerHTML= marks; window.location = "results.php"; } HTML code from ind ...

Error: Unable to access the 'resource' property as it is undefined

I am currently working on a project that involves fetching the latest 4 results from Craigslist using a query. Although I have successfully retrieved all the relevant information, I seem to be encountering an issue with loading the URL of the image from th ...

Bootstrap4 lacks responsiveness

html, body { background-color: #E3E3E3; width: 100%; height: 100%; margin: 0; padding: 0; } /* Custom Styling */ .section1 { background: url("../images/laptop-table1920-gray.jpg") no-repeat center center fixed; -webkit-background-size: cover ...

After showcasing every photo in the album, remember to include a line break

I am currently using the Flickr API to fetch images and display them on my website. The issue I am facing is that after displaying all the photos of each album, there needs to be a line break so that the name of the next album is displayed on the next line ...

Changing the text color of a selected text in HTML

I have a feature to change the color of selected text using Javascript. Here is the method I am currently using: function marking_text(replacrmenthtml){ try { if (window.getSelection) { sel = window.getSelection(); var ...

Rectify the functionality of collapsing and expanding sections

I am utilizing the bootstrap 4 alpha 6 version in my project. On my page, I have multiple blocks that I would like to expand/collapse by clicking on a main button (with the id of 'expand-collapse'). Additionally, each block has its own individual ...

The steps to properly load "child.vue" into the correct position within "parent.vue" are as follows

Currently I am developing a single page web application using Vue.js. This app consists of 4 "page.vue" files, each with a right and left child .vue component nested inside. For instance, the Page1.vue file is structured as follows (omitting style and scr ...

Leveraging the css child selector within Ruby's Selenium-Webdriver to locate multiple elements

I prefer to minimize the usage of XPath in webdriver when locating elements, but still want the ability to access child elements from previously identified ones. For example: Consider the following HTML structure: <div id="myelement"> <table ...

Position the Mui Card Action at the Upper Right corner

Struggling to align a checkbox within a MUI card to the top right? It has been a challenge for me as well. Here is an image of my current layout https://i.sstatic.net/BwH9o.png. I really want that button to be placed in the top right corner, even when the ...

I'm puzzled as to why the text-align center doesn't seem to be functioning correctly when combined with margin 0

Jsfiddle (expand the window) https://jsfiddle.net/aq9Laaew/5793/ The phrase hello world isn't centered below the image. By switching to a larger image https://i.sstatic.net/F79On.jpg, this problem can be avoided. What's the best way ...

Can you provide guidance on how to perfectly align a div within another div both vertically and horizontally?

Similar Inquiry: Method to center an image both vertically and horizontally within a larger div I'm working on positioning a div that is 300px in both height and length. My goal is to have another div inside it, sized at 100px for both dimensions ...

Can someone provide guidance on creating a calculator using the Switch statement in Javascript?

Introduction to HTML: <div id="content"> <div id="calculator-container"> <form name="calc"> <!-- The form is named "calc" --> <label for="output">A Simple Calculator</label> & ...

As the text in the textarea grows, the dimensions of the div containing it will

When text is entered in the div, its height increases accordingly. However, I would like the height to also adjust based on the size of the text when I press enter. Additionally, I need the width to remain constant until the text reaches the maximum width ...

When echoing SESSION in PHP, the previous value is displayed

<script language="javascript"> function handleEmergency() { if(document.getElementById('emer').checked == true) { <?php $_SESSION['total2']= $_SESSION['total1'] ...

The swap feature in drag-and-drop does not have a defined function

I am currently developing a to-do app that utilizes drag and drop functionality to reorder items in the list. However, I have encountered an issue where swapping elements works perfectly for the first five elements but throws errors when dealing with the s ...

Open a new HTML page with jQuery with a click event trigger on an iframe element

Hello everyone, I have a simple question as I am still learning about Jquery. I am attempting to load another .html page whenever a figure element is clicked on this .html page. However, the script tag is not working as expected. Can someone please assist ...