Formatting text over an image

The challenge I am facing involves having an image and text on it in two separate divs with a background color. The length of the text in one div is long while the text in the second div is short, but I want to ensure that the width of both divs are equal. Below is my current code snippet:

.banner{
  position: relative;
}
.banner img{
  width: 100%;
}
.image-text{
  position: absolute;
  top: 40%;
  left: 8%;
}
.image-text h2{
  font-size: 22px;
  font-weight: bolder;
  color: #fff;
  margin: 0px;
}
.upper-text{
  background: #2b96c3;
  padding: 5px 10px;
}
.lower-text{
  background: #0d729c;
  padding: 5px 10px;
}
<section>
  <div class="banner">
    <img src="https://i.imgur.com/xTeFbiv.jpg">
    <div class="image-text">
      <div class="upper-text">
        <h2>Excellent Successfull</h2>
      </div>
      <div class="lower-text">
        <h2>Sustainable</h2>
      </div>
    </div>
  </div>
</section>

I am aiming for a desired output similar to the attached picture referenced here:

https://i.sstatic.net/MxDYC.jpg.

Despite trying various techniques, I am still unable to achieve the desired result.

Answer №1

Adjust the second div's display style to inline-block to prevent it from spanning 100% width. Here is a sample code snippet:

.banner{
  position: relative;
}
.banner img{
  width: 100%;
}
.image-text{
  position: absolute;
  top: 40%;
  left: 8%;
}
.image-text h2{
  font-size: 22px;
  font-weight: bolder;
  color: #fff;
  margin: 0px;
}
.upper-text{
  background: #2b96c3;
  padding: 5px 10px;
}
.lower-text{
  background: #0d729c;
  padding: 5px 10px;
  display: inline-block;
}
<section>
  <div class="banner">
    <img src="https://i.imgur.com/xTeFbiv.jpg">
    <div class="image-text">
      <div class="upper-text">
        <h2>Excellent Successfull</h2>
      </div>
      <div class="lower-text">
        <h2>Sustainable</h2>
      </div>
    </div>
  </div>
</section>

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

The functionality of "Overflow-scroll true" does not seem to be operational within an iOS device when using an Ionic application

Currently, I am working on an Ionic application and focusing on improving its performance. During my research process, I have come across various suggestions primarily related to scrolling functionality. It is highly recommended to use overflow-scroll="tru ...

Struggling to extract the values from a KendoDropDown using Selenium

When using the selenium web driver, we are trying to retrieve a set of option values such as Last 30 days, Last 60 days, etc. Attempts have been made to locate these elements using a CSS selector. var timeperiodcss = "span.k-widget.k-dropdown.k-header se ...

Can one open a unique custom pop-up before the window is about to close?

Seeking a way to create a pop-up confirmation dialog for users when they try to log out or stay on the page while logged in. I attempted using the code below, but haven't been able to find the correct solution: window.onbeforeunload = function (e) { ...

Using JavaScript to apply styling on images with overlays

I am currently facing an issue with placing an overlay on top of a background image. Despite my efforts, I am unable to get the background color to appear on top of the image. Any helpful suggestions on how to resolve this would be greatly appreciated. M ...

A guide to sending HTML emails with the Linux command line

I am in search of a solution to send emails in HTML format using only the Linux command line and the "mail" command. So far, I have tried the following: echo "To: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99f8fdfdebfceae ...

What is the best way to conceal an HTML element on a particular page that is already styled as (visibility: visible), but only if its child has a specific class assigned to it?

I have a grid of content and posts displayed on multiple webpages. Users can select specific items from the grid to navigate to more detailed information on another page. Each webpage has its own grid which I want to filter based on a specific class. The ...

How can a JavaScript function be triggered by Flask without relying on any requests from the client-side?

I'm in the process of setting up a GUI server using Flask. The challenge I'm facing is integrating an API that triggers a function whenever there's a change in a specific Sqlite3 database. My goal is to dynamically update a table on the HTML ...

Firefox and Internet Explorer have a tendency to break lines at very specific pixel measurements

Hey, I have a situation that goes like this: I have 4 objects with dimensions of exactly 76x76px placed within a 320px container. This setup seems to work well in Chrome as seen above. However, in Firefox and IE9, it looks like this: even though the < ...

Altering Hues with a Click

One thing I wanted to achieve was changing the color of a hyperlink once it's clicked. I managed to make it work by using the code below: var current = "home"; function home() { current = "home"; update2(); } function comp() { current ...

Unable to replicate the exact Bootstrap template found on their website

Attempting to replicate a template from bootstrap.com, but encountering issues with the copied version. The navigation bar is turning white instead of remaining black, and I'm unable to change the color. Additionally, facing problems with the toggle ...

Displaying dynamic content in JavaScript using Galleria module

Hello and thank you for taking the time to check out my question. I've encountered a little issue that I can't seem to solve by myself. I'm hoping someone could lend me a hand. On my website, there is a dynamic field filled with a code from ...

Having trouble setting a value as a variable? It seems like the selection process is not functioning properly

My Hangman game has different topics such as cities and animals. When a user selects a topic, the outcome should be a random item from that specific topic. For example: London for cities or Zebra for animals. Currently, I am only generating a random lett ...

jQuery insert custom text into HTML and eliminate malfunctioning elements

Using jQuery, I have created a form where the entered text is appended to certain elements. This functionality works correctly. Additionally, I have included a remove link with each added element. When someone clicks on the remove button, it should remove ...

Embedding links in v-bind:href

I inserted a dynamic link inside the <header></header> section using the method mentioned here: Vue JS and appending a variable to end of a URL Below are the relevant codes: <tr class="notice-row" v-for="(myCase, id) in case ...

Tips for comparing two arrays and displaying matching elements in separate columns

I am faced with the challenge of working with two arrays. The first array contains a list of all dates, while the second array holds the dates when a person is present. My objective is to generate a table that displays all the dates in the first row, and ...

The Alchemy feature on hover is not functioning

I am currently using alchemy.js to display a graph, but I am encountering issues with showing the "onMouseOver" caption of the graph's node. The console is displaying some errors which you can see here. Here is the code snippet: <html> < ...

Set the CSS table to have a width of 100% and ensure that the table data cells have hidden

My table has a 100% width, and some of the td elements have overflow hidden. I want to make their text appear as ellipsis when it's too long. I can achieve this with fixed td sizes, but I need them to be relative to the content. This question was fi ...

Navigating within Vue.js: Utilizing vue-router for seamlessly redirecting to targeted sections within a view identified by an ID

I am in the process of using a Home view and a Blog view. My goal is to utilize a <router-link> to transition from the Home view to a particular section within the Blog view. The specific section I am referencing appears as follows: <section id=&q ...

Tips for minimizing the arrow size in the infowindow on a Google Maps interface

As a newcomer to customizing Google Maps, I am looking to decrease the size of the arrow in the infowindow and position it in the bottom left corner for a better appearance. I am struggling to figure out how to adjust the height and width of the arrow and ...

The overflow scroll feature is activated, causing the background to appear greyed out, yet the

I've been struggling for hours to achieve this layout: Check out the code example here html, body { height: 100%; overflow-y: hidden; overflow-x: hidden; margin: 0px; padding: 0px; } .MyNavbar { height: 30px; background-color: red; } ...