When a media query is activated, the Flex item mysteriously vanishes from

UPDATE I followed the suggestion in Ezra Siton's answer and changed the media query display to block, which successfully resolved the issue!

Within my html code, I have a parent column flexbox containing two children. The second child is itself another flexbox with four divs.

I have placed a canvas in each div with the intention of displaying some charts within them. The child flexbox is displayed as a 2x2 grid on larger screens, but on smaller screens such as tablets or smartphones, the flexbox decoration should change to no-wrap column:

.inst-data {
  padding: 20px;
  position: fixed;
  width: 90%;
  height: 90vh;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, 1);
  border-radius: 15px;
  border: 2px dotted orangered;
  z-index: 100;
}

.grid-container {
  display: flex;
  align-content: center;
  justify-content: center;
  width: 100%;
  height: 80vh;
  flex-wrap: wrap;
}

.grid-item {
  border-radius: 15px;
  margin: 10px;
  width: 45%;
  height: 45%;
  background-color: orchid;
}

@media (max-width: 800px) {
  .grid-container {
    overflow-y: auto;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-items: center;
    align-items: center;
  }
  .grid-item {
    width: 90%;
  }
}
<div class="inst-data shadow">
  <div class="d-flex flex-column justify-content-center align-items-center">
    <h2 class="text-center border-bottom my-2" style="height: 10%; width: 100%;">Title</h2>
    <div class="grid-container" id="ch-div">
      <canvas id="buy-chart" width="200" height="200" class="grid-item"></canvas>
      <canvas id="net-chart" width="200" height="200" class="grid-item"></canvas>
      <canvas id="stock-chart" width="200" height="200" class="grid-item"></canvas>
      <canvas id="price-chart" width="200" height="200" class="grid-item"></canvas>
    </div>
  </div>
</div>

During testing of the media query, the top canvas disappeared and became unreachable even with the scrollbar. What steps should I take to resolve this issue?

Answer №1

Initially, your code is considered unrefined (Overflow of ideas/tricks/flex properties leads to issues with a basic grid layout = unexpected glitches)

Have you thought about using a simple flexbox grid? https://css-tricks.com/dont-overthink-flexbox-grids/

Addressing the "chaos bug" - previously, the blue card (First column) extended beyond the screen due to these two lines:

  align-content: center;
  justify-content: center;

snippet

.grid-container {
  display: flex;
  align-content: center;
  justify-content: center;
  width: 100%;
  height: 80vh;
  border: 5px solid red;
  flex-wrap: wrap;
  overflow-y: auto;
}

.grid-item {
  border-radius: 15px;
  margin: 10px;
  width: 100%;
  height: 45%;
  background-color: orchid;
}
<h2>The blue box out of the screen</h2>
<div class="inst-data shadow">
  <div class="d-flex flex-column justify-content-center align-items-center">
    <h2 class="text-center border-bottom my-2" style="height: 10%; width: 100%;">Title</h2>
    <div class="grid-container" id="ch-div">
      <canvas id="buy-chart" style="background: blue;" width="200" height="200" class="grid-item">1</canvas>
      <canvas id="net-chart" width="200" height="200" class="grid-item">2</canvas>
      <canvas id="stock-chart" width="200" height="200" class="grid-item">3</canvas>
      <canvas id="price-chart" style="background: red;" width="200" height="200" class="grid-item">4</canvas>
    </div>
  </div>
</div>

Modify to top ==> resolve the problem (Consider utilizing a different code strategy):

  align-content: top;
  justify-content: center;

.grid-container {
  display: flex;
  align-content: top;
  justify-content: center;
  width: 100%;
  height: 100vh;
  border: 5px solid red;
  flex-wrap: wrap;
  overflow-y: auto;
}

.grid-item {
  border-radius: 15px;
  margin: 10px;
  width: 100%;
  height: 45%;
  background-color: orchid;
}
  <div class="inst-data shadow">
    <div class="d-flex flex-column justify-content-center align-items-center">
      <h2 class="text-center border-bottom my-2" style="height: 10%; width: 100%;">Title</h2>
      <div class="grid-container" id="ch-div">
        <canvas id="buy-chart" style="background: blue;" width="200" height="200" class="grid-item">1</canvas>
        <canvas id="net-chart" width="200" class="grid-item">2</canvas>
        <canvas id="stock-chart" width="200" class="grid-item">3</canvas>
        <canvas id="price-chart" style="background: red;" class="grid-item">4</canvas>
      </div>
    </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

Click-triggered CSS animations

Trying to achieve an effect with regular JS (not jQuery) by making images shake after they are clicked, but running into issues. HTML: <img id='s1_imgB' class="fragment"... onClick="wrongAnswer()"... JS: function wrongAnswer(){ docume ...

Internet Explorer experiencing off-screen menu loading issue

Currently, I am in the process of developing a website for my sister but facing challenges with cross-browser compatibility. The menu appears off-screen when using Internet Explorer, while it looks fine on Chrome. Despite various attempts to adjust alignme ...

What causes Safari to display a black border around resized videos?

I've come across a strange issue while using Safari. Whenever I try to play an MP4 video with dimensions of 1920 * 1080, and resize the Safari window to a width of 937px, something strange happens: https://i.stack.imgur.com/oI50i.png A black border ...

How can I ensure that my boxes are aligned in a straight line?

I'm having trouble with applying the style="display: inline-block" to each div. Can someone please assist me? https://i.sstatic.net/Gi75l.png Below is my HTML code: <div class="hobby" style="display: inline-block"> <di ...

Incorporate a background image with the JavaScript CSS property

I'm having trouble adding a background image using the Javascript CSS property in my code. When I directly add the 'url', it works fine. Could the issue be with the 'weatherImage' variable? Javascript var OpenWeatherKey = ' ...

The issue arises when attempting to render an SVG with JavaScript embedded inside using the img, object, or

Issue with the title ... please follow these steps: (view codes below) Create an svg + open it separately (name it keysaway.svg) Create html + open it individually When you observe, the svg displays a simple up and down animation but fails to work when l ...

Ways to verify the user's authentication status on the server end

Before displaying an HTML page, I need to verify user authentication. Here is a snippet from my server.js: const express = require('express'); var jquery = require('jquery'); var admin = require("firebase"); const app = expre ...

Refreshing a webpage to accurately display changes made in a CRUD application without the need for a hard reset

My to-do app is almost fully functional - I can create items, mark them as completed, and delete them using a delete button. However, there's one issue: when I delete an item, the page doesn't update in real-time. I have to manually refresh the p ...

Methods for passing JavaScript variables to PHP

I have encountered this problem on Stack Overflow before, but I couldn't find a solution that worked for me. I am using Codeigniter and have a form where users can rate a product. What I need to achieve is to insert the user's rating into the dat ...

Looking for assistance in turning this curl script into one that can handle multiple links

Below is a script that has the ability to download files from a given URL. However, I am looking for a way to include multiple links for downloading files. Instead of having a button to add another input box for URLs, I want users to be able to input three ...

event triggered when the values of dynamically added rows change

I am trying to calculate the product of two input fields and display it without submitting the form. I have incorporated an array and included functionality to add/delete rows, with an onchange function to calculate the product. This solution works well ...

Issue with flex item not expanding to fill entire height within flex container

My markup contains the following code: .container { display: flex; width: 500px; } .col1 { background: red; height: 100%; width: 50px; } .col2 { background: blue; flex: 1; height: 200px; } <div class="container"> <div class= ...

What is the best way to showcase the keys of a Python dictionary in an HTML table with the values of each key displayed as a row under its corresponding table header?

I am currently involved in a django project where I perform data analysis using the pandas library and would like to showcase the data (which has been converted into a dictionary) as an HTML table. This is the dictionary I wish to present: my_dict = { ...

Including a hyperlink button within a Kendo Grid cell that is editable

I have a grid featuring a column for editable phone numbers. I want to include a small icon that users can click on to trigger a "tel: link" which will prompt their phone hardware to act accordingly. For a demo, check out this link -> Objective: Clic ...

Ensuring the validity of an HTML form by including onClick functionalities for additional form elements

I am working on a form that I put together using various pieces of code that I found online. My knowledge of HTML and JavaScript is very basic. The form includes a button that, when clicked, will add another set of the same form fields. Initially, I added ...

mobile-exclusive wordpress text area problem

There seems to be a padding issue that is only affecting mobile devices. https://i.stack.imgur.com/2Tbuc.png The cause of this problem has been somewhat identified, but the solution isn't clear without impacting the preview on computers. Here is th ...

Utilizing AngularJS to include information into JSON-LD

As a newcomer to AngularJS, I find myself stuck in one of my projects. My goal is to convert the user-entered data in a form into the format: << "schema:data" >> and then push and display it within the @graph of the json-ld. Here are my HTML an ...

What method can I use to ensure that the sidebar stays fixed at a particular div as the user continues to scroll down the

Is there a way to automatically fix the sidebar once the user scrolls down and hits the top of the .Section2? Currently, I have to manually enter a threshold number which can be problematic due to varying positions across browsers and systems. Fiddle htt ...

How can I save the complete CSS file using Firebug FireDiff?

While making CSS modifications with Firebug and FireDiff, I've encountered a few mishaps where I mistakenly saved a diff instead of the complete snapshot. This resulted in uploading only one or two changes to my web server and overwriting the entire C ...

Adding an id to a ul tag using JavaScript

I am trying to dynamically add an ID called "myMenu" using JavaScript to a ul element for a search filter. Unfortunately, I am unable to directly access the ul tag to change it, so I need to do it via JavaScript. As I am new to JavaScript, I am looking t ...