How can I position a div element beneath an img-fluid using Bootstrap?

Looking for a way to ensure that an image is displayed responsively with a div-element positioned underneath it in Bootstrap. Currently, the image can overlap the div-element. How can I resolve this issue?

<div class="col-sm-12 col-md-6 pt-5 pl-lg-5">
    <div class="row">
        <div id="container">
            <img id="image" class="img-fluid" src="~/images/myImage.png" />
        </div>
    </div>
    <div class="row">
        <div class="py-3 py-lg-4">
            This text should always appear below the image.
        </div>
    </div>
</div>

CSS:

#container {
  height: 245px;
  width: 310px;
  position: relative;
  width: 100%;
}

#image {
  position: absolute;
  left: 0;
  top: 0;
}

Your assistance is greatly appreciated!

Answer №1

Upon reviewing your code example, it appears there are several issues that need to be addressed. Let's break it down step by step:

  1. The Bootstrap documentation for .img-fluid states that the image should have a max-width of 100% and height set to auto. Adding a max-height of 100% to the #image element will prevent it from overflowing the specified height of 245px.
  2. You initially set a width of 310px but then overwrite it with width: 100% in the following lines.
  3. While you use the .row class, it seems you forgot to include columns (.col) within it. Refer to the Bootstrap Grid system for guidance on structuring your layout.
  4. The CSS styling for #image is unclear, but it should be inherent given the context.

In order to achieve the desired result, consider revising your code as shown below:

#container {
  height: 245px;
  width: 310px;
  position: relative;
}

#image {
  max-height: 100%;
}
<div class="col-sm-12 col-md-6 pt-5 pl-lg-5">
  <div class="row">
    <div class="col">
      <div id="container">
        <img id="image" class="img-fluid" src="https://via.placeholder.com/450" />
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="py-3 py-lg-4">
        This text should always be displayed under the image
      </div>
    </div>
  </div>
</div>

To visualize the changes, you can view this demo on JSFiddle.

Below are some screenshots showcasing the layout with different image sizes:

Image size: 450px x 450px - View image

Image size: 450px x 800px - View image

Image size: 800px x 450px - View image

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 CSS issues on Internet Explorer 8

I am experiencing issues only with Internet Explorer 8. As a newcomer, I am unsure why the link I added to my website is not working properly: Website When you click "Candidaturas" in IE8, you will notice that the text is broken and the styles are not di ...

Performance issues with jquery addClass and removeClass functions observed in Internet Explorer 11

I am currently working on an application that monitors nodes within a cluster, and I have created a visual state example to demonstrate this. Each small box in the grid represents a node, and when hovering over a node, the rest of the nodes in that particu ...

Creating a default homepage across various HTML files using Google Apps Script and deploying it as a WebApp

Is there a way to set a default main page on multiple HTML and GS files in Google AppScript? I plan to publish it as a Web App. Have you attempted using the doGet function? ...

The jQuery function may encounter issues when trying to target elements within an appended HTML form

I am encountering a couple of issues. I have 2 separate JQuery functions, one for appending content and the other for displaying the selected file's name. FUNCTION 1 <script> jQuery( document ).ready(function( $ ) { $("input[name ...

Using Node JS and Express to deliver a static HTML page with an embedded image

I have successfully set up a system where I can serve static HTML pages using express. I have also installed "ejs" to render the page with data from local variables in my .js file. The only issue I am facing is displaying a small logo in the corner of the ...

Angular 10 carousel malfunction: unable to scroll images as intended

Recently, I attempted to integrate a bootstrap carousel into my angular web page. I copied the source code directly from the bootstrap site, so I assumed everything was configured correctly... Despite this, the carousel does not slide as expected. Additio ...

Angular is throwing an error message stating that it cannot find a differ that supports an object of type 'object'. NgFor only supports binding to iterables like Arrays

I am currently in the process of developing an Angular application that displays a list of projects and users from a PostgreSQL database. However, I have encountered difficulties when trying to display the list of users in HTML. The issue seems to be that ...

What could be the reason behind the occurrence of an error after deleting certain lines of code

The code below is functioning correctly. obj = { go: function() { alert(this) } } obj.go(); // object (obj.go)(); // object (a = obj.go)(); // window (0 || obj.go)(); // window However, an error arises when I comment out the first two lines. obj ...

Deactivate rounding numbers in jQuery AJAX

When using jQuery to send numbers, I noticed that the decimals are not saved correctly. For example, 2.5 is saved as 3 and 17.5 is saved as 18. $("#sendnomreform").on('submit',(function(e) { e.preventDefault(); $('#loading').s ...

Is there a way to display an image onto a bootstrap carousel using PHP?

I have been attempting to dynamically update the images within a bootstrap carousel based on the image URL stored in a MySQL database. I successfully displayed the first image ($value1) using a foreach loop, but encountered issues with displaying subsequen ...

Set the div to have a position of absolute, disregarding any other parent divs that may also have position absolute set

Is there a way to bring an outsider class <div> to the front of all others, even when all <div> elements are set as position: absolute;? How can the .free-object, which is inside .left, overlap both .left and .right? I have tried using z-ind ...

Notification box appearing at the lower part of the display

I have recently been given the task of updating my company's JavaScript alerts with more customizable bootbox alerts. After playing around with it, I managed to make an alert appear when a button is clicked, but the alert keeps showing up at the botto ...

How can I enhance Parallax scrolling performance on Chrome?

I recently completed a tutorial from the following link: After customizing it by changing the image to something different than clouds, I noticed that my version of the website was experiencing lag issues in Chrome but worked fine in Firefox. However, whe ...

using jquery, how can you send multiple parameters in an ajax request

Hello and welcome! I'm having trouble passing parameters through an ajax URL. I am attempting to send multiple data using the jQuery $.ajax method to my PHP script, but I can only pass a single data item when concatenating multiple data entries toget ...

Issues with basic internal linking functionality

I'm looking to set up a "Table of Contents" that connects to different sections of a lengthy page and I thought it would be simple... This was my attempt: <ul> <li><a href="#features">Features</a></li> </ul> ...

Retrieving inner text using HTML Agility Pack

I need assistance with parsing a website I have found here: https://i.sstatic.net/GdFN1.png My goal is to extract information from specific fields that have IDs and classnames: label = node.SelectSingleNode( ".//h3[@c ...

Issues with the proper display of Bootstrap 4 in Django are causing problems

I am currently in the process of setting up Django to work with Bootstrap. Despite my efforts, I can't seem to get it functioning correctly and I'm unsure of where I may be making a mistake. Initially, I am just trying to display a panel. You can ...

Transform pixel padding into percentage ratios

I've been searching through various discussions to find a solution, but none of them seem to fit my particular scenario. My issue involves a hyperlink with absolute positioning inside a div with relative positioning. The padding and margins are curre ...

Does Courier New font have any downsides?

Is it possible to highlight text in a flex textarea using htmlText when certain font tags are not supported? I am looking for a font that is the opposite of Courier New so that when embedded in Flash and written with a specific color, I can achieve a hig ...

Changing the Parent div style based on checked radio input in Reactjs

As a Reactjs beginner, I am looking for assistance in changing the background color of a faculty item (.faculty) to red when the respective radio input is checked. This should be achieved using the onChange method in Reactjs. Thank you in advance for you ...