The div vanishes once the media query changes the flex-direction to column, likely due to the flex property

My goal was to create overlapping content using flex-box. When viewed on a large screen, the flex-direction is set to row, but for smaller screens, I wanted it to change to column.

body {
  min-height: 100vh;
  background-color: #d6bd9e;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  width: 90%;
  max-width: 960px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.left {
  border: 2px solid green;
  flex: 1 40%;
  width: 40%;
  height: 30rem;
  background: url("https://images.unsplash.com/photo-1512641406448-6574e777bec6?ixlib=rb-   1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHw%3D&w=1000&q=80") no-repeat;
  background-size: cover;
  border-radius: 8px;
}

.right {
  min-height: 20rem;
  flex: 1 50%;
  background-color: #4d6d52;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1.5em;
  border-radius: 8px;
  margin-left: -4em;
}

@media(max-width: 500px) {
  .container {
    width: 100%;
    flex-direction: column;
  }
  .left {
    width: 90%;
  }
  .right {
    min-height: 14rem;
    margin: 0;
    margin-top: -6em;
    background: transparent;
  }
}
<div class="container">
	<div class="left"></div>
	<div class="right">
	  <h1 class="heading">
	    This is a heading!
	  </h1>
	  <p class="lorem">
	    Lorem ipsum dolor sit amet consectetur adipisicing elit. Molestiae iste nihil, quasi eos aspernatur doloribus quos consequatur animi. In, nemo!
	  </p>
	</div>
</div>

I changed the background of .right to transparent so that I could still see my div (I can see the green border), but the background image appears to be missing. I suspect the issue lies with the flex: 1 40%; property that I set in .left initially because when I remove this flex property, everything seems to work fine. However, my question remains: Why does the .left div collapse when I switch the flex-direction? After some research, the only solution I found suggested setting a height for the div, but as evident from my code, my .left element already has a specified height. I am confident that the problem is related to the flex property of my .left element.

Answer №1

It seems that the background image would collapse at the media query width, but not entirely disappear because of the green border.

To fix this issue, I added a height property to both the container in the media code and the original container selector. Now, the image remains visible without disappearing. Also, remember to include the meta tag in your HTML head for proper display! Hopefully, this solution helps!

HTML

<meta name="viewport" content="width=device-width, initial-scale=1">

CSS

body{
  min-height: 100vh;
  background-color:#d6bd9e;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container{
  width: 90%;
  height: 100vh;
  max-width: 960px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.left{
  border: 2px solid green;
  flex: 1 40%;
  width: 40%;
  height: 30rem;
  background: url("https://images.unsplash.com/photo-1512641406448-6574e777bec6?ixlib=rb-   1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHw%3D&w=1000&q=80") no-repeat;
  background-size: cover;
  border-radius: 8px;
}

.right{
  min-height: 20rem;
  flex: 1 50%;
  background-color: #4d6d52;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1.5em;
  border-radius: 8px;
  margin-left: -4em;
}

@media all and (max-width: 500px){
  .container{
    width: 100%;
    flex-direction: column;
    height: 80vh;
  }

  .left{
    width: 90%;
  }   

  .right{
    width: 75%;
    min-height: 14rem;
    margin: 0;
    margin-top: -6em;
  }
}

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

Adding an <a> tag to Flickity: Step by step guide

Is there a way to incorporate tags into a Flickity image slider without affecting its functionality? Here's the code snippet I have for the slider: <div class="carousel" data-flickity='{ "imagesLoaded": true, "percentPosition": false, "auto ...

Improve the performance of my website and resolve the zooming problem

Trying my hand at teaching myself jquery and creating a decent layout. I believe what I have done is acceptable, but I still have some questions. This is my first attempt at jquery without copying someone else's work. I've aimed to keep it relev ...

Exploring the possibilities in Bootstrap 5.3: Modifying the maximum width of an individual tooltip

Is there a way to modify the maximum width of a specific Bootstrap Tooltip without affecting the others? I do not utilize Sass or SCSS, and have attempted various methods outlined in the documentation: tooltip-max-width="300px" bs-tooltip-max-wid ...

Conceal excessive content on elements set in a stationary position

How can we prevent overflow of a fixed div within a container? Initially, I attempted nesting fixed elements inside each other, but that did not solve the issue. The only solution that comes to mind is using "inverted" masks: other fixed divs that hide eve ...

Having difficulty selecting a nested ul with CSS, I'm currently exploring different options

For the website I am working on, I have a cms system that is responsible for rendering menus. My current task involves selecting the children of the parent menu items, and here is how the code is being generated: <div class="menu"> <ul> ...

What is the most effective method for sharing features while maintaining unique aesthetics?

In the process of developing a Next.js website, I've encountered a challenge: I have 3 forms that function in similar ways but have different styles. Instead of duplicating code for each form, I'm looking for a way to build the form structure on ...

Creating a Welcome Page for my React Application with IndexRoute

I am in the process of setting up a landing page that will then redirect to my main home page using React. I have created a component for the landing page named Splash. I am attempting to utilize IndexRoute to make sure my app initially displays Splash as ...

Sorting items using jQuery filter

I am working with two sortable div containers that are connected using connectWith. Both containers contain draggable items that can be moved as desired. These items have specific classes such as group1 and group2. Let's refer to the containers as con ...

Using jQuery to Generate an Automatically Updating Timer for Slideshow - Pausing on Mouse Click

My first slideshow with jQuery is up and running, but it's very basic and hard coded. I want to add an automatic scrolling effect every 8 seconds to the next item before looping back to the beginning. I have an idea on how to achieve this, but I want ...

Transitioning with a CSS cubic-bezier function results in a noticeable jump of the content

My transition in an accordion list is using cubic-bezier(0.68, -0.55, 0.265, 1.55), but the issue I am facing is that the content below is also 'jumping'. Does anyone have suggestions on how to create space below my accordion (ul list) so that w ...

Notification (jQuery) failing to display user messages

Is there a way to retrieve the post title and display it in a notification? The notification's title is extracted from a form and shown in the notification. When the user clicks the submit button, the system captures the duration and title, displaying ...

Picture is not displaying properly post-rotation

Is there a way to rotate an image by 90 degrees and display it in a Card Component without it covering the text? Currently, I am utilizing Element.io, but encountering issues with the rotated image overlapping the text. The image currently has the followi ...

HTML file unable to connect with CSS stylesheet

I'm facing an issue where my CSS stylesheet and HTML files aren't linking properly. Here is the code snippet from my HTML file: <head> <title>Website Sample</title> <meta charset="UTF-8"> <meta http-equiv= ...

Tips for using the foreach loop to print out multiple arrays

I am working with two arrays in PHP: post_titles and posts. I need to figure out how to print them sequentially using a foreach loop. Printing one array at a time is not an issue, as shown below: <?php foreach ($titles as $row) { ?> <?php ec ...

Is Object Cloning the Solution for Creating Multiple Views in THREE.js with Multiple Canvasses? Considerations to Keep in Mind

While seeking advice on object picking from small three.js viewport, I learned a technique for mouse-picking objects from a smaller canvas. Now, I am looking to showcase N different views of the same scene while still being able to perform object picking. ...

Tips for fixing the issue of the "nativeElement" error message that appears as "Cannot read properties of undefined" while using a scrollable Primeng table

I'm encountering an issue with a p-table where 'scrollable' is set to 'true' and 'scrollHeight' is set to '288px'. The problem occurs when the table has insufficient data, resulting in a height less than 288px. ...

Spotlight the content of the file obtained from GitHub

I'm having trouble with syntax highlighting for code fetched from GitHub. fetch("https://raw.githubusercontent.com/Ayanmullick/test/master/AutomationAcc/test1.ps1") .then(response => response.text()) .then(data => document.getElem ...

Dilemma in CSS: The debate between using padding shorthand or writing out the padding

Thanks for stopping by! Today, I ran into a peculiar CSS issue that I need help with. Take a look at the snippet below: /* padding: 0.5rem,2rem,0.5rem,2rem; this line is not working*/ /* while these do:*/ padding-top: 0.5rem; padding-bottom: 0.5rem; paddin ...

Tips for creating responsive scrollable columns in an HTML table with Bootstrap

I'm not a professional web designer. I attempted to create a web layout with scrollable columns containing data in anchor tags that are loaded dynamically. To achieve this, I created an HTML table structure along with a stylesheet. Here is the source ...

Struggling to adjust the carousel selector to display images in the center, not off to the left

Encountering an issue with adjusting the carousel selector to showcase images in the center instead of on the left. The slides are functioning correctly, but the showcase item is not aligning properly. Here is a preview, thank you. <script src="htt ...