Dynamic text overlay on a versatile image

With the help of bootstrap, I've created a mobile-responsive image with overlay text and a button that should be centered on the image. However, the text doesn't stay centered when the screen size is reduced. What it's supposed to look like: https://i.sstatic.net/S8k8p.jpg

What it looks like in responsive mode: https://i.sstatic.net/gZTJN.jpg

Here's the HTML code:

 <div id="container-a" class="img-content">
        <img src="../img/file.large.jpg" class="img-fluid" alt="workout world img" width="1440" height="600">
            <span class="text-box-a container px-4 text-center z-2 align-items-center">
            <div class="text-box-b">
                <h1 class="main-text f-30 f-sm-50 f-md-75 f-xl-75 text-center font-weight-bold animated fadeInUpSmooth" style="font-size:6vw;">Gains are simply one click away!</h1>
                <button type="button" class="btn btn-info" style="font-size:3vw;">Ready?</button>
            </div>
            </span>
 </div>

Here's the CSS code:

#container-a img {
  position: relative;
  display: block;
  z-index: 1;
  top: 0px;
  left: 0px; 
  padding-bottom: 20px;
}
.img-content {
  display: inline;
}

@media screen and (min-width: 601px) {
 .text-box-a {
   font-size: 80px;
 }
}
  @media screen and (max-width: 600px) {
.text-box-a {
  font-size: 30px;
 }
}

.text-box-a {
  z-index: 20; 
  top: 50%; 
  left: 50%;
  overflow: hidden;
  position: absolute;
  align-items: center !important;
  text-align: center !important;
  transform: translate(-50%, -50%); 
  text-shadow: 2px 2px #585858;
}
.text-box-b {
  background: rgb(204, 204, 204);
  background: rgba(204, 204,204, .1);  
}

.img-content .main-text {
   z-index: 2;
}

Answer №1

Your issue lies with the use of .text-box-a with a % measurement for the top property. Additionally, the purpose of the overflow property on .text-box-a is unclear without a specified width or height.

I recommend utilizing the banner image as a background-image:url(); rather than an img tag.

<div class="container">
<div class="text-box-a"></div>
</div>

.container{
display:flex;
justify-content: center
width:100%;
background-image:url(image.jpg);
background-size:contain;
height:/*height of banner img*/
}
.text-box-a{
padding:;
;
}

Consider formatting your top banner this way. Utilize padding to position the elements within the banner image and apply display:flex; and justify-content:center; for item alignment.

There are multiple solutions to address your problem, but these are the methods I typically employ.

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 JavaScript, you can manipulate the position of a line on a canvas to

I want to create a feature where users can manipulate lines on a canvas by skewing them. This means they would be able to drag one end point of the line to a desired point on the same x-axis using JavaScript and HTML5 canvas. Can someone please provide g ...

The HTML anchor tag paired with the italic tag is a powerful combination for

Here is some example code: <a href="#"> <i class="some-bg" /> Some Text </a> Along with some Javascript: $("a").bind("touchstart", function (e) { e.preventDefault(); console.log("Tag: " + e.target); console.log("Tag Nam ...

What causes an absolutely positioned element to be positioned by its sibling rather than at the top corner of the page?

Can someone explain to me why my absolutely positioned element is displaying after my child_static div? I'm under the impression that absolutely positioned elements are removed from the normal flow. So why isn't child_absolute covering the child_ ...

Struggling with aligning images in the center of a square container

Currently revamping my photography website and working on a simple slider to showcase some of my photos. Struggling with aligning the images in divs with padding all around while keeping them centered. I want to replicate the look of my photos on Instagram ...

"Restricting the height of a DIV container based on the size

Struggling with CSS as I try to get my #wrapper DIV to adjust its size based on its contents. I've experimented with 100% height and min-height, but nothing seems to work. Could it be related to relative versus absolute positioning? Here's a snip ...

Content not aligned in the center of the page on Internet Explorer

Recently, I've taken on the responsibility of managing the content for this website after it was passed down from the previous developer. Each page's content is contained within a div element with the class "ct", which has auto margins applied t ...

Using jQuery to modify CSS attributes is proving to be more challenging than I anticipated

I am attempting to modify a CSS attribute using jQuery. I have used the following code: wrapperInner.css('overflow', 'visible'); However, the value remains unchanged. When I use an alert alert(wrapperInner.css('overflow')), ...

Formula for full width skew in three adjacent divs using SkewX() function

.container { height: 500px; width: 500px; position: relative; background: black; } .box { transform: skewX(20deg); width: 33%; height: 100%; position: absolute; } .first-box { background: purple; left: 0; } .second-box { background ...

Transitioning from feelings to sewing: incorporate a duo of properties within variations and breakpoints

I was thinking about how to convert this styled-emotion code into stitches. Essentially, I need to handle the onlyShowOnDesktop and breakpoint props. The definition of breakpoint is as follows: const breakpoint = isTopNavigationBreakPoint ? theme.defaultB ...

Show data in a popup using jQuery DataTables and loading content asynchronously via Ajax

I am attempting to display a list in a popup based on an Ajax request. Prior to the Ajax call, the list is contained within the popup. However, after the Ajax request, the list remains on the page instead of inside the popup, and the old list still appears ...

"An error has occurred in the file system, make sure to handle it when

I am encountering an issue with submitting a form using jQuery's ajaxSubmit() function. The problem arises when the file selected in the form is renamed, deleted, or made inaccessible before submission, leading to potential discrepancies in whether th ...

Text that sparkles: one part in italics, one part not?

Within my shiny app, I have a textOutput component called "acronym" where I intend to display some text with half non-italicized and half italicized. My attempted approach was as follows: output$acronym_1 <- renderText(paste("SID SIDE:", tags$em("Side ...

Guide to creating a versatile bootstrap modal window in Vue.js

I need some assistance with creating a bootstrap modal window that provides a warning to the user before they take an action. The issue I am facing is that when I enter text into a field, close the modal, and then reopen it, the previous values are still t ...

Is there a way for me to design a button that includes an image?

I'm looking to create a button on my webpage using an image that will link to other pages. I want the flexibility to use both small and large text on this button. The specific image I want to use is: So far, I've attempted some coding but haven ...

How to overlay text onto an image using CSS

Is there a more efficient way to overlay text on an image without using position: absolute;? Dealing with position: absolute; for different screen sizes isn't the ideal solution for me. I've searched for alternatives, but all I keep finding is th ...

Is there still a need to preload dynamic images in the background?

Imagine you have HTML code that includes an image with a placeholder: <img class="lazy-load" src="http://via.placeholder.com/150/?text=placeholder" data-src="http://via.placeholder.com/150/?text=real%20image"/> At some stage, you want to implem ...

Troubleshooting a malfunctioning PHP script that verifies user ranks

I wrote a PHP script to determine the user's rank and grant access to view specific content based on that rank. Unfortunately, the script is not functioning as intended. Regardless of the user's rank, the content is still displayed. Here is the ...

Conceal the div on larger screens and display it on mobile devices

I have a fixed navigation bar at the top of my website. By default, it is always visible on all pages, but I want to hide it on desktop screens and display it only on mobile devices. Here is what I have implemented: @media (min-width: 992px) { #sp-he ...

Vue - making one element's width match another element's width

Trying to dynamically adjust the innermost element's width to match the outermost element's width with Vue: <div id="banner-container" class="row"> <div class="col-xs-12"> <div class="card mb-2"> <div ...

Boxes that expand to full width and appear to float on

I am dealing with a situation where I have a container that holds multiple child elements such as boxes or sections. <section> <div>Box 1</div> <div>Box 2</div> <div>Box 3</div> <div>Box 4< ...