Can someone assist me in properly spacing out my divs for three distinct media queries?

Here is the link to the CodePen containing the code: https://codepen.io/gregelious/pen/zYmLGex

The CodePen features a restaurant menu with 3 categories (divs) displayed as separate boxes.

(For detailed instructions, visit: https://github.com/jhu-ep-coursera/fullstack-course4/blob/master/assignments/assignment2/Assignment-2.md)

The assignment included the following guidelines:

  • When the width is greater than or equal to 992, each box should occupy one-third of the screen's width
  • For widths between 768 and 991, the first two boxes should take up 50% of the screen width, while the third box occupies 100% on a new line
  • For widths less than 768, each box should span the entire width, resulting in three separate lines

I assigned the divs IDs "first," "second," and "third," and provided the following CSS:

@media (min-width: 992) {
  div {
    width: 33.33%;
  }
}

@media (min-width: 768) and (max-width: 991) {
  #first, #second {
    width: 50%;
  }
  #third {
    width: 100%;
  }
}

Despite my efforts, the sizes of the divs are not adjusting when I resize the browser window, and I'm struggling to find a solution. This assignment was part of a Coursera course, and despite reviewing relevant materials, I haven't been able to progress.

Answer №1

If you want to effectively control the flex layout, my recommendation would be to utilize a container div. Check out the following demo for a visual representation:

Ensure that your layout functions properly by adjusting the flex properties. Remember to assign units (e.g., px) to the min-width and max-width properties (e.g., min-width: 768px)

Start building your layout from smaller screens to larger screens (following a mobile-first approach) and utilize only @media (min-width) CSS queries. Keep in mind that larger media queries take precedence over smaller queries if set.

Take a look at this operational demo:

body {
  margin: 0;
  padding: 0;
}

h1 {
  text-align: center;
}

div {
  float: left;
}

section {
  background-color: gray;
  border: 1px solid black;
  margin: 10px;
}

section h2 {
  background-color: blue;
  border: 1px solid black;
  margin-top: 0px;
  padding-top: 0px;
  padding-bottom: 2px;
  padding-right: 30px;
  padding-left: 30px;
  display: inline;

  float: right;
}

section p {
  clear: right;
  padding: 0px 10px 10px 10px;
}

/** added code */
.container {
  display: flex;
  flex-wrap: wrap;
}

.menu {
  width: 100%;
}

@media (min-width: 768px) {
  .menu {
    width: 50%;
  }
  
  .flow {
    width: 100%;
  }
}

@media (min-width: 992px) {
  .menu, .flow {
    width: 33.333%;
  }
}
<h1>Our Menu</h1>

<div class="container">
  <div id="first" class="menu">
     <!-- content here -->
  </div>

  <div id="second" class="menu">
      <!-- content here -->
  </div>

  <div id="third" class="menu flow">
      <!-- content here -->
  </div>
</div>

Answer №2

Make sure to add 'px' at the end of your min and max width values.

@media (min-width: 768px) and (max-width: 991px) {}

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 method for capturing a select menu using form submission?

I've been working on a website with Bootstrap 5 and hosting it through GitHub pages. Additionally, I integrated a form on the site using Bootstrap 5 and utilized Formsubmit to send form submissions to my email address. However, I encountered an issue ...

This React.Js application hosted on Heroku is optimized for use on Chrome Desktop browsers

Just finished completing a React project that retrieves News Articles and presents them. Recently launched it on Heroku, but I'm encountering an issue where only Chrome on Desktop seems to be able to run it. Both Safari and Firefox are showing the sa ...

The parent DIV has a height of 0px, yet the child element is still visible

As I explored options for creating a navbar drop down menu with an accordion effect, I stumbled upon this informative YouTube video. The tutorial demonstrated the use of a custom component and manipulation of the max-height property of the card-content ele ...

Is it advisable to incorporate CSS variables in place of traditional HTML color names?

As an illustration, I frequently utilize the color white in my CSS code. :root { --color-white: #fff; } I am curious if it is beneficial to define colors such as 'black' and 'white' as variables or if I should stick to using the def ...

Steps to center an HTML canvas on the screen

I attempted to center my canvas horizontally using <div style="text-align:center;">, but the y axis remains unchanged. How can I position the HTML canvas in the center of both the x and y axes? Any suggestions on how to achieve this alignm ...

Using a personalized function in JQuery

I need to execute a JavaScript function I created after a JQuery event has been triggered. The function in question is called scrambleDot, and I defined it previously like so:var scrambleDot = new function() { //my code }. Here's the attempted implem ...

What is the best way to create a fixed positioning for a div within a Material-UI table container?

When using the Material UI Table, I encountered an issue with applying the sticky property. The table itself works fine, but I also have a button inside the TableContainer that should be sticky alongside the table head. I attempted to achieve this by using ...

When two divs are activated, the third div goes into invisible mode

I attempted this code, but it doesn't appear to be functioning correctly. if(div1)&& (div2)==display:none { div3=display:none; } else { div3=display:block; } ...

Sending Data to a Component in AngularJS

I need help passing a value from one component to another. Location List <div uib-accordion-group class="panel-default" heading="{{location.name}}" ng-repeat="location in $ctrl.locations"> <p>This is from location list: {{location.id}}</ ...

Using AJAX/JQuery along with PHP to instantly send notifications without refreshing the page for a contact form

Website URL: This website was created by following two separate tutorials, one for PHP and the other for AJAX. The main objective was to develop a contact form that validates input fields for errors. If no errors are found, a success message is displayed ...

IE Browser Exposes Flawed Design in JSP Coding

I have a pair of JSP files. The first one features a table, while the second one contains rows (<tr>) that are meant to be added to the table. Within the second JSP file, I initialize the same action, followed by the <tr> element that should b ...

Adding a gradient to enhance an SVG chart

Hey there! I'm currently experimenting with Plotly to create some awesome charts, and I've been trying to figure out how to give my area-charts a gradient instead of the usual fill with opacity. This is how I typically build my graph: Plotly.ne ...

Hyperlinks both inside and outside of List elements are unresponsive

Struggling to figure out how to create an unordered list where each li element has its own background image (no text, just image). Having difficulty getting the link to work in Firefox - it works in Safari but not in Firefox. The image changes on hover in ...

A guide on dynamically adjusting text size based on viewport width and height in React with TailwindCSS

Utilizing React and Tailwind, I am working towards creating two overlapping rectangles with text. The top rectangle's text should be aligned to the left, while the bottom rectangle's text should be aligned to the right. Regardless of window size ...

Uh oh! AngularJS just threw an error: Uncaught TypeError - angular.service is not a function. It happened in taskService.js

I've run into some issues with AngularJS, even though I have imported all the necessary libraries. Specifically, I am encountering the following errors: Uncaught TypeError: angular.service is not a function at taskService.js:2 taskFactory. ...

Define the term "Parse Error" as it pertains to CSS

i'm encountering an issue while attempting to validate my CSS. Despite trying to validate using direct input (copy and paste), the error persists. In my pursuit to find a solution, I researched on Google only to discover that it could be related to sp ...

jQuery replacing spaces in id names

I'm encountering an issue with the following HTML and jQuery code. The problem seems to be related to the space in the ID name. HTML <span id="plus one">Plus One</span> jQuery $("#plus one").hide(); Since I'm unable to change the ...

Using jQuery or JavaScript to hide a div if the image inside it is unable to load

There is a div called 'userFeatureProductImage' that is duplicated. Sometimes a broken image may appear, so I would like to hide the div as a precautionary measure. How can I achieve this? ...

An issue occurred with the DOMException while trying to execute the 'setAttribute' function on the 'Element': '{{' is an invalid attribute identifier

Currently, the code is under development and some methods are still empty while the *ngIf directives may not be correct in terms of logic. However, even with these issues, I encountered the same error without bothering to remove them at this stage. While ...

Despite using the 'img-fluid' class, the image is still not fully responsive

Despite using the img-fluid class in my img tag, I am facing an issue with image responsiveness. The image is responsive on smaller screens but loses its responsiveness after a certain screen size. I have researched solutions on How to make an image respo ...