Creating overlapping layouts with absolute positioning in Bootstrap leads to a visually appealing

I'm currently working on a layout that involves absolute positioning elements, similar to the one shown in this example:

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

It seems like the elements are overlapping and not clearing properly. I've been looking for a simple solution to fix this issue, but haven't been successful in resolving it.

.main-block .main-wrapper {
  margin-bottom: 4rem;
}

.main-block .main-wrapper:nth-child(odd) .main-box {
  height: 100%;
}

.main-block .main-wrapper:nth-child(odd) .main-box .image-container {
  width: 440px;
}

.main-block .main-wrapper:nth-child(odd) .main-box .text-container {
  background-color: #8EFFFF;
  position: absolute;
  width: 720px;
  top: 3rem;
  z-index: -1;
  right: 15px;
  padding: 4em 5em;
}

.main-block .main-wrapper:nth-child(even) .main-box {
  height: 100%;
}

.main-block .main-wrapper:nth-child(even) .main-box .image-container {
  width: 440px;
  position: absolute;
  right: 15px;
}

.main-block .main-wrapper:nth-child(even) .main-box .text-container {
  background-color: #8EFFFF;
  position: relative;
  width: 720px;
  z-index: -1;
  padding: 4em 5em;
  top: 3rem;
}
<body>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bf9f4f4efe8efe9faebdbafb5adb5aa">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">

  <main role="main" id="content" class="site-content">
    <section class="py-5 main-block">
      <div class="container">
        <div class="row">
          <div class="col-lg">
            <h2 class="text-blue-dark mb-5">Creating a Special Layout</h2>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-12 main-wrapper">
            <div class="main-box">
              <div class="image-container"><img src="https://dummyimage.com/440x440/6b59b3/fff"></div>
              <div class="text-container">
                <h5 class="font-weight-bold">Lorem Ipsum Layout</h5>
                <div class="blurb mb-2">
                  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
                    aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                  <p>Dui id ornare arcu odio ut sem nulla pharetra. Ullamcorper morbi tincidunt ornare massa eget egestas purus. Sit amet aliquam id diam maecenas ultricies mi eget. Pellentesque dignissim enim sit amet. Mollis nunc sed id semper. Integer
                    malesuada nunc vel risus. Tincidunt tortor aliquam nulla facilisi cras fermentum odio. Nisi est sit amet facilisis magna etiam tempor orci. Nulla facilisi cras fermentum odio eu. At quis risus sed vulputate odio ut enim blandit. Ipsum
                    suspendisse ultrices gravida dictum fusce ut. Facilisi cras fermentum odio eu feugiat. Vestibulum rhoncus est pellentesque elit ullamcorper. Et malesuada fames ac turpis egestas sed tempus urna et. Scelerisque purus semper eget duis
                    at tellus at urna condimentum.</p>
                  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
                    aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
                    aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.p>
                </div>
              </div>
            </div>
          </div>
          <div class="col-lg-12 main-wrapper">
            <div class="main-box">
              <div class="image-container"><img src="https://dummyimage.com/440x440/76b359/fff"></div>
              <div class="text-container">
                <h5 class="font-weight-bold">Special Design</h5>
                <div class="blurb mb-2">
                  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
                    aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                  <p>Dui id ornare arcu odio ut sem nulla pharetra. Ullamcorper morbi tincidunt ornare massa eget egestas purus. Sit amet aliquam id diam maecenas ultricies mi eget. Pellentesque dignissim enim sit amet. Mollis nunc sed id semper. Integer
                    malesuada nunc vel risus. Tincidunt tortor aliquam nulla facilisi cras fermentum odio. Nisi est sit amet facilisis magna etiam tempor orci. Nulla facilisi cras fermentum odio eu. At quis risus sed vulputate odio ut enim blandit. Ipsum
                    suspendisse ultrices gravida dictum fusce ut. Facilisi cras fermentum odio eu feugiat. Vestibulum rhoncus est pellentesque elit ullamcorper. Et malesuada fames ac turpis egestas sed tempus urna et. Scelerisque purus semper eget duis
                    at tellus at urna condimentum.</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </main>
</body>

Answer №1

Avoid using absolute positioning in this scenario as it can complicate layouts. Instead, consider utilizing standard Bootstrap columns and adjusting elements with negative margins.

.main-block .main-wrapper {
  margin-top: 3rem;
}

.main-block .image-container {
  width: calc(100% + 2rem);
}

.main-block .main-wrapper:nth-child(odd) .image-container {
  margin-left: -2rem;
}

.main-block .image-container img {
  margin-top: -2rem;
}

.main-block .main-wrapper .text-container {
  background-color: #8EFFFF;
  padding: 4rem 5rem;
  z-index: -1;
}

.main-block .main-wrapper:nth-child(odd) .text-container {
  margin-right: -2rem;
}

.main-block .main-wrapper:nth-child(even) .text-container {
  margin-left: -2rem;}< / code> </pre>
<pre class="highlight"><code> <body>
  <link rel = "stylesheet" href = "https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="086a67677c7b7c7a6978483c263e2639">[email protected] </a> / dist / css / bootstrap.min.css" integrity = "sha384 -zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG / eSrtxUkn" crossorigin = "anonymous">

  <main role = "main" id = "content" class = "site-content">
    <section class = "container py-5 main-block">
      <div class = "row">
        <div class = "col">
          <h2 class = "text-blue-dark mb-5"> Course of Lecture < / h2>
        </div>
      </div>

      <div class = "row main-wrapper">
        <div class = "col-4">
          <div class = "image-container">
            <img class = "img-fluid" src = "https://dummyimage.com/440x440/6b59b3/fff">
          </div>
        </div>

        <div class = "col-8 text-container">
          <h5 class = "font-weight-bold"> Lorem ipsum dolor sit amet < / h5>

          <div class = "blurb mb-2">
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. & lt; / p>
            <p> Dui id ornare arcu odio ut sem nulla pharetra. Ullamcorper morbi tincidunt ornare massa eget egestas purus. Suspendisse ultrices gravida dictum fusce ut. Facilisi cras fermentum odio eu feugiat. Et malesuada fames ac turpis egestas sed tempus urna et. Scelerisque purus semper eget duis at tellus at urna condimentum. & lt; / p>
          & lt; / div>
        < / div>
      & lt; / div>

      & lt; div class = "row main-wrapper" & gt;
        & lt; div class = "col-8 text-container" & gt;
          & lt; h5 class = "font-weight-bold" & gt; Sit amet aliquam & lt; / h5>

          <div class = "blurb mb-2">
            <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. & lt; / p>
            <p> Dui id ornare arcu odio ut sem nulla pharetra. Ullamcorper morbi tincidunt ornare massa eget egestas purus. Suspendisse ultrices gravida dictum fusce ut. Facilisi cras fermentum odio eu feugiat. Et malesuada fames ac turpis egestas sed tempus urna et. Scelerisque purus semper eget duis at tellus at urna condimentum. & lt; / p>
          & lt; / div>
        < / div>

        & lt; div class = "col-4" & gt;
          & lt; div class = "image-container" & gt;
            & lt; img class = "img-fluid" src = "https://dummyimage.com/440x440/76b359/fff">
          < / div>
        & lt; / div>
      & lt; / div>
    & lt; / section>
  & lt; / main>
& lt; / body>  

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

Tips for positioning a navigation bar in the center for various screen resolutions

After hours of searching, I still can't find a solution to my problem. I'm new to this, and on my main monitor, everything looks centered just fine. 1920x1080: View larger image But on my 1280x1024 monitor: View smaller image As you can see, i ...

In JavaScript, implement event listeners exclusively on the main container and its immediate child elements

Is there a way to target just the main container and its second child elements for an event? Specifically, targeting id="container" and all elements with class="secondChild" Currently, I have a listener attached to all elements inside ...

What causes discrepancies in CSS design across different browsers?

I have implemented the following CSS and HTML code to display an L shape before a span, but its appearance differs across different browsers. In Mozilla Firefox https://i.sstatic.net/1rCoJ.png and In Safari https://i.sstatic.net/YKiJ7.png .bulletInli ...

Is there a way to exclude the Unicode character from the first menu item while still utilizing it in the other items on my menu?

I have been working on customizing my menu by using the unicode character \25C6 to represent a black solid diamond as a spacer between labels. After searching for solutions on Stack Overflow, I attempted to implement the suggested method using the bef ...

Is it possible to toggle between thumbnails and a larger image in a jQuery gallery?

Hello, I am new to website development and I would like to create a jquery based photo gallery for my personal website. One feature that really catches my eye is this one (for example): The gallery has a large thumbnail grid where you can click on a thumb ...

Having trouble with a basic jQuery selector not functioning as expected

Having trouble parsing this HTML code: <tr id="a"> <td class="classA"> <span class="classB">Toronto</span> </td> <td class="classC"> <span class="classD">Winnipeg</span> </ ...

Getting rid of the empty spaces between the lines of cards in Bootstrap 4

I want to eliminate the vertical space between the cards in my layout. Essentially, I want the cards to maximize the available space. I am open to using a plugin if necessary, but I have not been able to find any relevant information online (maybe I used t ...

Activate unresponsive state when clicked

Struggling to implement a mobile responsive menu, my primary issue is the navigation buttons not toggling upon clicking. Here's what I have so far in terms of HTML and JQuery: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery. ...

The production build encountered an issue as it was anticipating 3 arguments, however, it only received

import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'elipsis' }) export class ElipsisPipe implements PipeTransform { transform(text, length, clamp) { text = text || ''; clamp = clamp || '...& ...

Having issues getting the single div to refresh with `.#div.load(scriptpage.php)` command

I've been attempting to update the content of a specific div (in-game chat) every X seconds or minutes using this simple code. The issue I'm facing is that the content never refreshes or loads the new page. I've searched through various cod ...

How to extract selected value from a dropdown menu in a React component

Is there a way for me to retrieve the chosen value from the dropdown menu? The select dropdown contains 12 options. My goal is to capture the selected value and then utilize it in handlecolumnchange to manipulate the number of columns added or removed. Des ...

Instructions for selecting an <option> within a <select> element using Python

I am trying to select a specific option by id, but I am encountering an issue when dealing with the elements above the select dropdown. <ul style="display: block; visibility: visible;"><li><a href="#" index="0" class="" onclick="s_objectID= ...

Implementing a Custom CSS Theme in JavaFX

Hey everyone, I stumbled upon this amazing JavaFX theme called Flatter on . I've been attempting to implement it for a couple of hours now, but I'm struggling with the process. Can someone provide me with a detailed guide on how to activate thi ...

I encountered an error while trying to synchronize my Redux state with the component state

Upon clicking the advanced sports search button, I need to display a drawer containing my API values. Currently, when mapping my Redux state with component state, an error is occurring stating "Actions must be plain objects. Use custom middleware for async ...

What is the most efficient way to load a PHP page once the webpage has completely finished loading?

I'm relatively inexperienced when it comes to PHP and JQuery, so please bear with me if my knowledge is limited. Currently, I'm facing an issue where loading a small HTML table that contains information queried from game servers (like shown in t ...

The alteration of the primary color to $theme-colors caused the button-primary's text color to switch from default white to black

$custom-colors:( "primary": #F58220 ); @import "../bootstrap/scss/bootstrap"; Despite specifying <button class="btn btn-primary w-100 text-center">Continue</button>, the text color turned black instead of the ...

Display and conceal specific table cells based on a user's search for a singular cell

I recently wrote a jQuery script to filter rows in a table based on user input. However, I encountered an issue with my current code when trying to search for a specific string within the table. Here's the scenario: a|b|c|d| e|f|g|h| i ...

What is the best approach to refreshing a particular div with the contents of a view file?

I have a block of code in PHP/Symfony that looks like this: <div id='content'> <?php include_partial( 'course/content.php', array("param1" => "1", "param2" => "2") ); ?> </div> Now, I am interested in updatin ...

Angular 2 Date Input failing to bind to date input value

Having an issue with setting up a form as the Date input in my HTML is not binding to the object's date value, even though I am using [(ngModel)] Here is the HTML code snippet: <input type='date' #myDate [(ngModel)]='demoUser.date& ...

Learn how to create a logarithmic scale graph using CanvasJS by fetching data from an AJAX call

window.onload = function() { var dataPoints = []; // fetching the json data from api via AJAX call. var X = []; var Y = []; var data = []; function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("applicatio ...