What is the best way to ensure two flexbox items that are overlapping are positioned side by side in a single row, and still maintain responsive display when transitioning to a mobile screen

Currently, I am immersed in creating a compact set of HTML CSS codes to exhibit two overlapped contents (images) next to each other in a single row. My goal is to stack them vertically when the width is reduced for mobile display. Despite implementing the following HTML and CSS codes, I am unable to achieve the desired outcome.

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Test HTML CSS Basic</title>

    <!-- Bootstrap 5.1.0 -->
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5944544f494f45514651454746485b6f48504d">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <!-- CSS Stylesheet -->
    <link rel="stylesheet" href="styles.css">
    <!-- Bootstrap Script -->
    <script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc" crossorigin="anonymous"></script>
  </head>
  <body>
    <main class="container my-4" role="main">      
      <div class="row text-center cc-title-block">
        <div class="col">
          <h1 class="head-title">Exploring the Overlap of Two Contents</h1>
        </div>
      </div>
      
      <div class="row">
         <div class="col-sm-12">
            <div class="cc-iat">
              <div class="cc-iat-image">
                <a href="https://www.google.com" class="d-block">
                  <picture>
                    <img src="img/spacex-launch.jpg" alt="" style="width:100%; height: auto;">
                  </picture>
                </a>
              </div>
              <div class="cc-iat-image-content">
                <h3 class="cc-iat-title">Self Landing Rocket</h3>
                <p class="cc-iat-text">
                  "Possibly the most amazing thing ever! We can customize this for you."
                </p>
                <a href="https://www.google.com" class="btn btn-light btn-lg"
                  style="padding-left: 3rem; padding-right: 3rem;">
                  More Details
                </a>
              </div>
            </div>
          </div>
      </div>      
    </main>
  </body>
</html>
main {
  display: block;
}

.my-4 {
  margin-bottom: 4.5rem !important;
  margin-top: 4.5rem !important;  
}

.container {
    width: 100%;
    padding-right: 17.5px;
    padding-left: 17.5px;
    margin-right: auto;
    margin-left: auto;
}

.row {
  display: flex;   
  margin-right: 17.5px;
  margin-left: 17.5px;
}


/* @media (min-width: 576px) */
.col-sm-12 {
  flex: 0 0 100%;
  max-width: 100%;
  position: relative;
  width: 100%;
  padding-right: 17.5px;
  padding-left: 17.5px;

}

.text-center {
  text-align: center !important;
}

.cc-title-block {
  margin-top: 115px;
  margin-bottom: 115px;  
}

.head-title {
  font-family: proxima-nova, sans-serif;
  font-size: 30px;
  font-weight: 300;
  line-height: 1.33;
  color: #000;
  text-align: center;  
}

.justify-content-center {
  justify-content: center !important;
}

.cc-iat {
  position: relative;
  display: flex;
  flex-wrap: wrap; 
  flex-direction: row;
  align-items: flex-end;
  margin-bottom: 100px;
}

.cc-iat-image {
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: 65%;
  width: 65%;
  min-width: 65%;
}

.d-block {
  display: block !important;
}

a, a:hover {
  text-decoration: none;
}

.cc-iat-image-content {
  flex-basis: 42%;
  flex-shrink: 0;
  flex-grow: 0;
  width: 42%;
  padding: 50px;
  color: #fff;
  position: relative;
  left: -140px;
  bottom: -70px;
  background-image: url('img/image-bg.png');
}

.cc-iat-title {
  font-family: proxima-nova, sans-serif;
  font-style: normal;
  font-weight: 800;
  font-size: 34px;
}

.cc-iat-text {
  font-weight: 300;
  font-size: 21px;
  line-height: 1.43;
  font-family: proxima-nova, sans-serif;
  font-style: normal;
}

.btn {
  text-transform: uppercase;
}

I utilized Bootstrap and designated my parent style as "flex-wrap: wrap;" while also establishing meta properties in the HTML head. The page appears like this at maximum width: https://i.sstatic.net/kAh08Nb8.jpg When narrowing the width, the "Self Landing Rocket" content should transition below the Rocket images for responsive viewing. However, I seem to be missing something crucial. Any insights on what might be causing this issue would be greatly appreciated. Thank you!

My expectation is that the flexbox items will appear stacked on top of each other when viewed on a mobile device.

Answer №1

Consider adjusting your media query in the following way:

/* Make these modifications to your current CSS inside a media query */

@media (max-width: 768px) {
  .cc-iat {
    flex-direction: column;
    align-items: flex-start; /* Ensure alignment at start */
}

    .cc-iat-image,
    .cc-iat-image-content {
        flex-basis: 100%;
        width: 100%;
        left: 0;
        bottom: 0; 
        margin: 0;
    }

    .cc-iat-image-content {
        margin-top: 20px;
    }
}

This adjustment ensures that when the screen width is 768px or less, the direction of the .cc-iat container switches to column, and its child elements .cc-iat-image and .cc-iat-image-content occupy 100% width, stacking them vertically.

I hope this clarification proves useful :)

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

``Is there a way to align content in the middle of the page while

Hey everyone, I need some help with centering the content on my webpage. The sidebar is always active and I want to make sure the main content is centered like the highlighted text. The issue is that it's currently centered based on the width of the e ...

Is there a way to use a media query on a div element to check for the presence of a scroll bar?

A div has been styled with overflow-y: auto, triggering the appearance of a vertical scroll bar when the content exceeds the height of the div. This causes the content to shift horizontally to accommodate the scroll bar, resulting in misalignment with the ...

Enhance the impact of "dialogs" to the fullest extent or minimize it to achieve the

How can I position the minimized dialog on the bottom of the div when appending dialogs next to each other in a FB messaging system? To achieve a Facebook messaging effect, the titlebar must go down when minimizing/maximizing the dialog. Perform the follo ...

What is the best way to manage font size on an HTML webpage?

In a scenario where I specify font-size = 25px along with a specific font-family, my intention is to display a character that is exactly 25px on the HTML page. However, the rendered character's size may be slightly different, such as 27px or another v ...

What is the best way to give precedence to non-auto capitalization in input fields on the Input Component from Material UI, especially when using iPads?

The issue I'm encountering is specific to Material UI and IPad, requiring a change in the component used from MUI. Currently, I am utilizing the Input component from Material UI. Upon clicking on the input, the IPad keyboard opens and automatically ...

Does anyone have tips on how to properly align a table within a page or div so that it appears centered?

I followed an online tutorial to create this table, but now I want to center it on the page and I'm not sure how to do that. Can someone help me out? Thank you in advance! http://jsbin.com/xiwayugu/8/ ...

Truncate a string in Kendo Grid without any white spaces

While using a Kendo-Grid for filtering, I've come across an issue where my cell does not display the full string if there is no white space. The problem can be seen in the image below: https://i.sstatic.net/0h1Sg.png For example, the string "https:/ ...

How can I determine if a user has reached the end of a non-scrollable div by utilizing HostListener and directives within Angular?

In my Angular project, I have designed a main layout using flex display with specific height and overflow settings. The main content div inside this layout has its own unique styling to ensure consistent appearance for any components inserted within it. By ...

Button inside table cell not showing Bootstrap tooltip

I've implemented a feature where each row in a table has a button that triggers a pop-up modal when clicked. I want these buttons to display tooltips when hovered over and when focused. Below is an example of the HTML structure: <link hr ...

Positioning close icon on Bootstrap 4 tab

I need help aligning a close icon for a BootstrapVue tab with Bootstrap 4.2 in the top right corner. Here is my code: <b-tab v-for="order in tabs" :key="order.id"> <template slot="title"> <span class="float-left">{{ order.nam ...

Is there a way to determine the number of Waypoints that are currently toggled simultaneously?

I have a collection of elements arranged in rows and I am applying Waypoints instances to them. <div class="row"> <div class="col-xs-6 col-sm-4"><div class="foo"></div></div> <div class="col-xs-6 col-sm-4"><div c ...

When the content of input fields (specified by type="number") is modified, the two-way binding property of angularjs is lost

My issue seems simple but is quite tricky to tackle. When accessing the index.php (locally using xampp), you will encounter a basic form. The form contains multiple elements and is a work in progress, hence the likelihood of bugs. However, there is one par ...

Tips for initiating a scrollable (overflow-y: scroll) div in the middle rather than the top while scrolling

I am working on a code where I have a scrollable div with overflow-y: scroll. However, the default scrolling starts at the top of my div when I want it to start in the middle. Can anyone suggest a solution for this issue? export function Portfolio({ chi ...

Setting default data in a data table that spans multiple pages is a useful feature that can

I am facing an issue with the default settings in the data table where I need the checkbox to be pre-checked on the controller's side. The problem arises when the checkbox is not staying checked after navigating to the second page and beyond. functi ...

Employees are unable to operate within an HTML page embedded in a WPF project

When running scripts on an HTML page, the page tends to freeze until the script completes. This is why I am considering using workers. However, I have encountered a problem: <html> <head> </head> <body> <button onclick="startWor ...

Unable to retrieve the sum total of all product items and display it in the designated text element

My product items are dynamically generated in a list. I have used the calculateItemTotal() method to determine the total for each item. Now, I need to sum up all these item totals and display the result in the total text field. However, instead of showing ...

Tips for matching variable content with button identification to display content within a variable for ThreeJS?

I'm currently working on a project using Three.js where I need to load multiple 3D models and store them in an array. Each model has a corresponding button with a unique ID that when clicked, should add that specific model to the scene and remove any ...

What key element is not included in this code?

I've been trying to create an interactive green circle that switches between red and green when clicked. Despite checking and rechecking my code, it's still not functioning correctly. I must be making a beginner mistake somewhere. Can anyone poin ...

Submenu alignment issue in RTL mode

I am currently utilizing bootstrap 4.4.1 for my template and I am attempting to implement a right-to-left (RTL) Nav bar for an Arabic website, ensuring the menu displays from right to left. In my code, the fifth item from the right has a submenu which is ...

Data attribute in jQuery not functioning properly

I'm attempting to identify a 'tr' element that has a matching data-title value and then change the data-qty value for that specific element. This is my current approach: var menu_type = data.type; switch (menu_type) { case 'breakfa ...