What is the best way to position a container div over another container using Bootstrap or CSS?

https://i.sstatic.net/q1qGi.png


I'm working on a Bootstrap 4 layout where container B needs to overlay part of container A. I want to achieve a design where container B appears on top of container A. Any suggestions or references on how to achieve this layout would be greatly appreciated.

  <!-- container A -->
        <div class="container-fluid">
         <div class="row text-center">
            <div class="col-12">
              <h4>HOW IT WORKS</h4>
            </div>
          </div>
        
          <div class="row text-center">
            <div class="offset-md-2 col-md-2 px-0">
                <div class="card">
                    <div class="card-header">
                      card 1 title
                    </div>
                    <div class="card-body">
                       card 1 body
              
                    </div>
                  </div>
            </div>
            <div class="offset-md-1 col-md-2">
              <div class="card">
                    <div class="card-header">
                      card 2 header
                    </div>
                    <div class="card-body">
                        card 2 body
                    </div>
                  </div>
            </div>
            <div class="offset-md-1 col-md-2">
                <div class="card">
                    <div class="card-header">
                      card 3 header
                    </div>
                    <div class="card-body">
                      card 4 body
                    </div>
                  </div>
            </div>

          </div>
        </div>

        <!-- container B -->

        <div class="container rounded-border">
          <div class="row">
            <div class="col-12 text-center">
              <h4 class="display-4">What is Lorem Ipsum</h4>
              <p>
              ustry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker includin
              </p>
            </div>
           
          </div>
        </div>

Answer №1

One way to achieve the desired layout is by utilizing pure CSS, specifically by positioning an element absolutely within a relatively positioned container. By setting the values for the top and left properties in the CSS, you can control the placement of Container B in relation to its parent div.

#outerContainer{
  position: relative;
}
#containerA{ 
    height: 100px;
    width: 500px;
    border: solid 1px gray; 
}
#containerB{
  position: absolute;
  top: 50px;
  left: 100px;
  height: 100px;
  width: 300px;
  z-index: 1;
  background-color: maroon;
  color: white;
  border: solid 1px gray;
}
#rest-of-page{
  height: 100px;
  width: 500px;
  border: solid 1px gray;
}
<div id="outerContainer">
  <div id="containerA">Container A</div>
  <div id="containerB">Container B</div>
  <div id="rest-of-page">(Rest of Page)</div>
</div>

Answer №2

To achieve the desired layout, apply absolute positioning to container B to determine its exact position. Next, use z-index on container B and assign it a value of 1 to ensure it appears on top of element A.

For more information on z-index, you can visit: https://www.w3schools.com/cssref/pr_pos_z-index.asp

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

What is the best way to enclose text within a border on a button?

I am trying to create a button with a colored border around its text. Here is the code for my button: <input id="btnexit" class="exitbutton" type="button" value="Exit" name="btnExit"></input> Although I have already added CSS styles for the ...

Attempting to send an AJAX request to a different server (not functioning with the provided example)

UPDATE - Issue Resolved. Many thanks to everyone who provided input. After conducting extensive research, I discovered that instead of CORS, I needed to focus on JSONP all along. I have read several tutorials and believe I now have a good grasp of the con ...

How can we use JavaScript to add a class to the <html> element?

What is the method to apply a class to the root element <html> in JavaScript? ...

"Identifying the exact number of keys in a JSON object using

Is there a key in the JSON data that may or may not exist? How can I determine the total number of occurrences of this key using jQuery? JSON : jasonData = [{"test":"sa3"},{"test":"4s"},{"acf":"1s"},{"test":"6s"}]; I need assistance with implementing th ...

"Effortlessly transform a JSON string into a JSON Object with JavaScript - here's how

When I serialize my object using the ASP.net JavaScriptSerializer class and return it to the client side, how can I deserialize the string in JavaScript? ...

Retrieve the chosen option within the current page

Currently, I am working with a select button on my webpage: <form method="post"> <label> <select id="label" name="challengeX" style="margin-top:150px;"> <option selected value="0"> Global Statistics </opt ...

What could be causing the submit action to not work when using PHP's isset function?

This is an HTML registration form with a PHP action triggered by the submit button. <html> <head> <title>Registration Form</title> </head> <body> <h1 align="center"> REGISTRATION FORM </h1> <form ...

Having issues with IE8 and SmoothGallery script errors

I'm currently utilizing the SmoothGallery plugin created by Jon Designs to enhance the website of one of my clients. However, there seems to be a minor issue in Internet Explorer 8 when attempting to navigate to the next image. Interestingly enough, a ...

Adding a div to the preceding div based on matching IDs in AngularJS

Here is a representation of my layout in HTML: <div ng-repeat="message in messages"> <div ng-class="{'messages messages--sent': userId == message.id, 'messages messages--received': userId != me ...

What are some ways to troubleshoot a dropdown box toggle switch in a website header?

<li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">Contacts <span class="caret"&g ...

Add new data to an existing array in Angular 7 without overwriting it

After clicking a button, I'm retrieving data from the WordPress API: <a (click)="initGetComments()">Get comments</a> This is the code snippet: export class AppComponent { commentsResults: CommentsItem[] = []; ...

Definition of Stencil Component Method

I'm encountering an issue while developing a stencil.js web component. The error I'm facing is: (index):28 Uncaught TypeError: comp.hideDataPanel is not a function at HTMLDocument. ((index):28) My goal is to integrate my stencil component i ...

"Encountering issues while upgrading Polymer project version from 0.5 to 1.0

I am in the process of upgrading my project from polymer .5 to polymer 1.0. After installing the new version of the polymer library, iron element, and paper element, I encountered the following error: polymer-micro.html:63 Uncaught TypeError: prototype ...

Find the difference between the sum of diagonals in a 2D matrix using JavaScript

I'm currently practicing on hackerrank and came across a challenge involving a two-dimensional matrix. Unfortunately, I encountered an error in my code implementation. 11 2 4 4 5 6 10 8 -12 The task at hand is to calculate the sum along the primary ...

Executing an on-click event on a button in PHP

I've been developing a learning site that involves teachers, students, and different subjects. To organize the registration process, I decided to separate the teacher and student registration pages since they input data into different tables in the da ...

Ways to incorporate color gradients into the corners of CSS cards

Below is an example of a basic bootstrap card: Example 1: https://i.stack.imgur.com/DK2Sz.png I attempted to draw blue and green colors side by side, but was only successful in drawing blue as shown below: Example 2: https://i.stack.imgur.com/CcSzP.png ...

Create dynamic animations on HTML text using CSS

Looking to add some flair to your website with a text animation? How about making the text automatically glide from one side to the other like a ticker display? Any suggestions for simple CSS code to achieve this would be greatly appreciated! Thanks in ad ...

Implementing automatic line breaks in Bootstrap

When setting the "overflow scroll able" option, I want it to only allow scrolling in the y direction and if x content overflows, a line break should occur. I tried applying 'white-space', but it didn't work as expected. <ul class="s ...

Leveraging font awesome ttf in conjunction with scss

I am currently attempting to incorporate Font Awesome with SCSS. @font-face { font-family: 'awesome'; src: url('../../fonts/font-awesome/fa-light-300.ttf') format('ttf'); } Although the path appears to be correct, I am ...

Whenever I create the code using javascript, padding seems to always show up

I have a basic stacked row layout that displays an upper box and lower box without any padding. <span id="route" class="small"> <div class="row" style="border-style:solid;"> <div class="col-md-12 col-12"> <p>UpperBox</p& ...