How to Create a Responsive Overlapping Effect for Two Divs in Bootstrap 4

As someone who specializes in backend work, I am currently facing some challenges with a project that utilizes Bootstrap 4.

Our goal is to replicate the layout demonstrated in this link: https://codepen.io/mediumandmessage/pen/xVeXop

(please note that the example and code provided are based on an original Bootstrap 3 design rather than Bootstrap 4)

Here is the HTML structure:

.somesection {margin-top:50px!important;}
body {
  font-size:17px;
  font-family:Helvetica Neue;
}
img {max-width:100%;}

.overlay-text {
  font-size:3.5vw;
  float:right;
  width:65%; /*important*/
  bottom:21vw; /*important*/
  padding: 25px;
  background:#f7f7f7;
}
<div class="container somesection">
  <div class="row col-xs-6">  
     <img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&auto=compress&dpr=1&crop=entropy&fit=crop&w=1087&h=725&q=80">
  </div>
  <div class="col-xs-3 col-sm-offset-4 overlay-text">
      This is some text that should partially overlay.
  </div>
</div>

However, trying to implement this layout in Bootstrap 4 has proven to be challenging. The text ends up displaying horizontally below the image and the divs fail to stack responsively.

I have experimented with different positioning techniques such as absolute and relative, but achieving clean and responsive execution became complex. I am seeking advice or insights from those experienced in pure Bootstrap 4 implementation...

If anyone can offer guidance or expertise on this matter, it would be greatly appreciated!

Answer №1

If you want to enhance your overlay column, consider adding a transform (remember to adjust this for smaller screens with a media query).

It's important to note that in the HTML provided below, I have made adjustments to ensure compatibility with Bootstrap 4. Remember that columns should be nested inside a row and the -xs class may no longer exist.

.overlay-text {
  /* these two are needed - the align self makes the column not stretch the whole height of the image column and the translate moves the column over the image */
  align-self: flex-start;
  transform: translateX(-20%);

  /* the following are for example only */
  background-color: #ffffff;
  padding:20px;
}
<div class="container somesection">
  <div class="row">
    <div class="col col-sm-6">
      <img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&amp;auto=compress&amp;dpr=1&amp;crop=entropy&amp;fit=crop&amp;w=1087&amp;h=725&amp;q=80" class="w-100">
    </div>
    <div class="col col-sm-3 col-sm-offset-4 overlay-text">
      This is some text that should partially overlay.
    </div>
  </div>
</div>

Example bootply

Answer №2

To improve the layout of your website, simply include position:relative; in the .overlay-text class You can also customize the value of bottom

.somesection {margin-top:50px!important;}
body {
  font-size:17px;
  font-family:Helvetica Neue;
}
img {max-width:100%;}

.overlay-text {
  font-size:3.5vw;
  float:right;
  width:65%; /*important*/
  bottom:21vw; /*important*/
  padding: 25px;
  background:#f7f7f7;
  position:relative;
}
<div class="container somesection">
  <div class="row col-xs-6">  
     <img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&auto=compress&dpr=1&crop=entropy&fit=crop&w=1087&h=725&q=80">
  </div>
  <div class="col-xs-3 col-sm-offset-4 overlay-text">
      This is some text that should partially overlay.
  </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

Issues with Bootstrap loading correctly within a Ruby on Rails application

What steps should I take to successfully load Bootstrap 4 on my web app? Here is the contents of my Gemfile: gem 'rails', '~> 5.1.5' gem 'devise' gem 'bootstrap-sass', '~> 3.3.7' gem 'sass-rail ...

Inheritance in SASS - excluding the parent class

Can I apply this syntax to inherit a class in SASS? Code .message { border: 1px solid #ccc; padding: 10px; color: #333; } .success { @extend .message; border-color: green; } Output .message, .success, .error, .warning { border: 1px solid # ...

Issue with dropdown selection not functioning properly in Bootstrap 4.1.0 release

When I click on the select options, I want a dropdown with icons to appear. Requirement: The code needs to be updated for BootStrap version 4.1.0. Currently, clicking on the select dropdown does not display anything. Actual Output: This code works perfec ...

Challenges arise when transferring data retrieved from a table into a bootstrap modal window

I have been attempting to transfer values from a table into a modal. Initially, I successfully displayed the <td> values in an alert when a button was clicked on a specific row. Now, I am aiming to take it a step further by having these <td> va ...

Switching the navigation menu using jQuery

I am looking to create a mobile menu that opens a list of options with a toggle feature. I want the menu list to appear when the toggle is clicked, and I also want to disable scrolling for the body. When the toggle menu is clicked again, the list should c ...

Ways to update the color of the mat-dialog-title using the material theme

Currently, I am utilizing the Angular Material Dialog and have been attempting to dynamically change the title color of the dialog based on the material theme. <h1 mat-dialog-title color="primary">{{ title }}</h1> Even though setting ...

Using GSAP to merge texts from left to right

I'm curious about the animation technique used by the developer to make his name come together from right to left. I'd love to try implementing it myself to improve my CSS skills. It looks like GSAP ScrollTrigger was involved in this animation, ...

Two variations of identical descriptions within a single div container

Is there a way for me to use an item, specifically an img, within the same div class but with different definitions? For example: section .row img { margin: 0 0 30px; width: 100%; border: 4px solid #18a00e; } section .row img { margin: 0 ...

Lightbox2 is looking to reposition the caption to the right of the image

Can anyone help me understand how to move the caption, found in data-title, from underneath an image to the right of the image? I'm not very familiar with HTML/CSS, but it looks like the image is enclosed within a div called .lb-outerContainer, and t ...

Struggling to properly position navigation bar items in a Bootstrap website

I'm struggling to align the bar items on a new site that I've built. I've tried placing two bars at the top to mimic the layout of this site: . I'd like it to look similar to this site: , where everything is nicely centered and mobile-f ...

Shifting columns in Bootstrap version 4

What it used to look like: <div class="row"> <div class="col-sm-2 col-sm-offset-3"> ... </div> </div> This is what it looks like now: <div class="row"> <div class="col-xs-2 offset-xs-3"> ... </div> </div& ...

Include an arrow in a personalized horizontal scroll bar using Angular and HTML

After styling a horizontal scrollbar for my chartjs graph, I attempted to add arrows using material icons like <mat-icon>keyboard_arrow_left</mat-icon>. However, I am struggling to align them next to the left and right of the scrollbar. Can any ...

How a child element in CSS can create a margin that spans the entire width of its parent

Constructing an overlay with a stylized container for text, but encountering an issue where the container is creating a margin that, when combined with the normal width of the elements, occupies the entire width of the parent element. Chrome dev tools indi ...

I seem to be having a problem with CSS and Flexbox sizing. Does anyone have an explanation for what might be

Hey there! I'm currently in the process of building a Netflix clone, and everything seems to be working well except for one issue with the layout. No matter how I adjust the width of the div containing the images, they just won't change size. I&a ...

Compact NiceScroll Scroller within a narrower div compared to the width of the scrolled div

I am currently working on a table layout where I have fixed two columns on the left, similar to what is shown in this example. To enhance the scrolling experience, I am using the nicescroll plugin. However, I have encountered an issue where the plugin be ...

A comprehensive guide to effectively formatting Recharts in React: addressing alignment and size management!

While attempting to style two graphs as floating cards, I am encountering difficulties in controlling the sizing and centering of the graphs. Specifically, I am having trouble with the pie chart in this example. To address this issue, I am passing paramete ...

Long content within an editable div in a table causes the div to exceed its percentage-width

Recently, I encountered an issue while using Chrome. Inside a table, there is an editable DIV with a width defined in percentage. The CSS for the DIV looks like this: div#editable { width:20%; white-space:nowrap; overflow:hidden; borde ...

The initial click event for the input element in Jquery is not functioning correctly

I found a jQuery date selector online and the script looked something like this... <script type="text/javascript> $(document).ready(function () { $("#date3").click(function() { $("#date3").scroller({ preset: 'datetime' }); wheels = []; whe ...

Maintaining aspect ratio while keeping images the same height and responsive

I have encountered a challenging issue that remains unresolved despite my extensive search for an answer. I have not found a solution in the "Questions that may already have your answer" section, nor have I discovered one elsewhere on the web. Here is the ...

Tips for altering CSS using the `:hover` pseudo-class

CSS Style for Navbar .navbar li { color: #B7B7B7; font-family: 'Montserrat', sans-serif; font-size: 14px; min-width: 70px; padding: 22px 16px 18px; } .navbar #menumain ul { width: 120%; } .navbar ul ul { display: none; } <div ...