Unable to place text inside an image using Bootstrap

My issue pertains to text placement on images, which is behaving oddly in my code. The problem seems to stem from a hover system I have implemented, where the text only displays correctly when triggered by a hover event. My code snippet below addresses the specific styling concerns:

/* Styling for the image and text container */

* {
  margin: 0;
  padding: 0;
}


/* Styling for the text within the images" */

.text-block {
  position: absolute;
  top: 5%;
  left: 5%;
  color: black;
}

/* Styling for all four images */

.img {
  width: 100%;
  border-radius: 30px;
  filter: drop-shadow(0 0 0.5rem grey) opacity(60%);
}

.zoom {
  margin: 10px;
  transition: transform .5s;
}

.zoom:hover {
  transform: scale(1.1);
  z-index: 1;
}
<!-- Bootstrap-5 CDN -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="76141919020502041706364358465844">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10727f7f64636462716050253e203e22">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<!-- Body -->
<div class="row g-0">
  <!-- "Applications" square -->
  <div class="col-lg-6 col-sm-12">
    <div class="zoom">
      <a href="./candidatures.php">
        <img src="https://via.placeholder.com/1920x1080.png" class="img" alt="meeting">
        <div class="text-block">
          <h4 class="big">Your applications</h4>
          <p class="medium">Keep track of your applications here.</p>
        </div>
      </a>
    </div>
  </div>
  <!-- "Offers" square -->
  <div class="col-lg-6 col-sm-12">
    <div class="zoom">
      <a href="./offers.php">
        <img src="https://via.placeholder.com/1920x1080.png" class="img" alt="shake">
        <div class="text-block">
          <h4 class="big">Current offers</h4>
          <p class="medium">Check out the latest offers here.</p>
        </div>
      </a>
    </div>
  </div>
</div>
<div class="row g-0">
  <div class="col-lg-6 col-sm-12">
    <!-- "Companies" square -->
    <div class="zoom">
      <a href="./companies.php">
        <img src="https://via.placeholder.com/1920x1080.png" class="img" alt="corporations">
        <div class="text-block">
          <h4 class="big">Companies</h4>
          <p class="medium">Explore various companies here.</p>
        </div>
      </a>
    </div>
  </div>
  <!-- "Personal Information" square -->
  <div class="col-lg-6 col-sm-12">
    <div class="zoom">
      <a href="./companies.php">
        <img src="https://via.placeholder.com/1920x1080.png" class="img" alt="paper">
        <div class="text-block">
          <h4 class="big">Personal Information</h4>
          <p class="medium">Update your personal information here.</p>
        </div>
      </a>
    </div>
  </div>
</div>

Answer №1

The issue at hand is that when you use position: absolute, the element will align itself to the nearest relative parent. In this scenario, since you haven't specified any other element as the relative parent, it defaults to the body.

To solve this problem, simply add position: relative to the immediate parent of the .text-block class like so:

.zoom a { position: relative; }

.zoom a {
  position: relative;
}
/* Additional CSS Goes Here */

<!-- Bootstrap-5 CDN Links -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<!-- Your HTML Content with Grid Layout -->

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

Determining User Consent: A Straightforward Guide to Identifying Previously Accepted Cookie Consent

I've been tasked with adding a cookie to a website. The site only tracks the number of visits, so there is no user-specific data available for cookie assignment or acknowledgment tracking. Is it possible to show the cookie notification only to users ...

"Trouble with Express.js: CSS isn't loading, yet HTML displays

Click here to view folders and files server.js is the code I have created so far //importing packages const express = require('express'); const admin = require('firebase-admin'); const bcrypt = require('bcrypt'); const path = ...

Utilizing jQuery to dynamically pass parameters to the YouTube API

When making a request to the YouTube API, I am structuring it as follows: $.get("https://www.googleapis.com/youtube/v3/channels", { part: "contentDetails", id: "somestring", key: "MY-API-KEY" } /*, ...*/ ) A hidden field contains the value id ...

Why is URL Hash Navigation not functioning when linking to a different page's slide on the carousel?

Why isn't the #tag link being recognized? Even though the data-hash items are visible in the URL window, the script doesn't seem to pick them up. The standard script used on the carousel page is as follows: $(document).ready(function() { $( ...

Exploring the potential of overflow within a container with a percentage-based

I am attempting to design a webpage that fills the entire screen height without scroll bars, yet contains a scrollable container within it. Despite trying various approaches with percentage heights, I have not been successful as the percentages do not beha ...

Guide on how to incorporate an external javascript file into an HTML button

I am currently working on an exercise through Udacity that involves creating an HTML form with JavaScript functions. The task is to input text into the form, submit it, and have it displayed on the webpage within a blue square. This exercise is designed to ...

Add a CSS class to an innerHTML element just a single time

I currently have 2 files available: data.php page.php The data.php file is responsible for fetching a row from a SQL database and sending it to the page.php file. Within the page.php file, there is a JavaScript script that receives this row through AJAX ...

Tips on targeting specific HTML content

Looking for assistance. I'm trying to extract HTML content from test.php <p> This is the content of a paragraph or article on a web page</p> <p>[albumname=Mount Climbing]</p> I want to process those [albumname] tags into thum ...

Restricting the output from BeautifulSoup

After spending some time working with BeautifulSoup and Selenium, I have encountered a problem that has me stumped. I am trying to extract the HTML from the first 6 rows of a table, but these rows do not have any shared class or ID. Here is the structure ...

Fixed Div Columns on the Far Left

My goal is to create a left-floating div that remains fixed in place while allowing another div to float next to it, creating two columns that stay intact when the page is scrolled. After researching, it seems like fixed position and floating elements don ...

Vue.js V-for not rendering any content

I am currently working on populating an array with objects retrieved from an API. The objects are being fetched successfully, but I am facing issues when trying to display them using v-for in the array. Below are my data variables: data() { return { ...

magnetic container: stationary container nested within absolutely positioned container

I recently created a page that can be viewed here: [LINK] This page is set up to scroll horizontally, resulting in a row of divs with black borders. However, I am facing an issue with the smaller divs inside (red ones). I want them to stay within the par ...

Get rid of the box-shadow on the Vuetify element

I currently have a special-table component that includes a box shadow when the row is expanded https://i.stack.imgur.com/8zgjp.png I am aiming for the removal of the box-shadow effect. After inspecting the console-style tab, I identified https://i.stac ...

Adding quotes is optional when using the append function

We are retrieving product options from the displayed html using variables PRODUCT_CUSTOM_1_ and PRODUCT_NAME_. Some products have quotations like " and '. However, when we post these strings elsewhere, they get cut off at the ". We need to either remo ...

Adjusting my menu layout causes my header image to be partially covered

I've been trying to make my menu fixed at the top of my website, but it keeps overlapping into my header image and doesn't look right. I've experimented with different solutions, but nothing seems to work. Here's the CSS code I used th ...

What are some methods for customizing the calendar icon displayed in an input field with type date?

Is there a way to customize the calendar logo without relying on jquery? The desired appearance should resemble the image shown below: https://i.sstatic.net/G06iZ.png input { width: 50%; padding: 8px; border-radius: 10px; ...

Having trouble with adding data from an array of objects to an HTML element using jQuery's each method

I am currently facing an issue with looping through an array of objects using $.each in jQuery and trying to append the values to an <li>. Here is the relevant jQuery code: $(".sidebar").empty().append("<div>" + "<h5>Student Info</ ...

Is it possible to adjust the ul width to match its parent's width using CSS?

Whenever I type in a search, a list is displayed. The issue arises on small devices where the width of the list exceeds that of the search input or its parent container, which should not happen. On small devices, it should always look like this: Example L ...

Resizing the Vue page to fit the viewport and using Flexbox to anchor the footer to the bottom

My Vue.js page structure consists of a navigation bar, main body content, and a footer: <template> <div class="flex_container"> <div class="container_navigation"> <nav-bar /> </div> < ...

Using Background Images in Emails with HTML

Struggling to add a background image with overlaid text? I've attempted multiple methods without success. Can someone provide a foolproof solution for achieving this? Thank you. ...