Tips for adjusting UI with Media queries based on screen dimensions

Looking for a better solution

I currently have 5 sets of items.

  1. Button
  2. Text
  3. Dropdown
  4. Pagination
  5. Icon

For larger screen sizes, specifically @media (min-width: 990px) {}

I would like all items to be in a single row, like so:

[button][Text][Dropdown][Pagination][Icon]

For medium screen sizes, i.e. @media (max-width: 990px) {}

I prefer all items to be in 3 rows, as shown below:

[button]
[Text][Dropdown]
[Pagination][Icon]

For smaller screen sizes, i.e. @media (max-width: 575px) {}

I want all items to be in 4 rows, laid out as follows:

[button]
[Text]
[Dropdown]
[Pagination][Icon]

I have attempted this in the provided code sandbox. Can someone offer a more effective solution?

Thank you.

@media only screen and (min-width: 990px) {
  .items {
    display: flex;
    flex-direction: row;
    margin-top:30px;
  }
  .item23 {
    display: flex;
    flex-direction: row;
  }
}

@media only screen and (max-width: 990px) {
  .items {
    display: flex;
    flex-direction: column;
    margin-top:30px;
  }
  .item23 {
    display: flex;
    flex-direction: row;
    margin-top:30px;
  }
}

@media only screen and (max-width: 575px) {
  .items {
    display: flex;
    flex-direction: column;
    margin-top:30px;
  }
  .item23 {
     display: flex;
    flex-direction: column;
    margin-top:30px;
  }
}
<div class="container">
   <div class="items">
      <div class="item1">
         <button> button 1 </button>
         <button> button 2 </button>
         <button> button 3 </button>
         <button> button 4 </button>
         <button> button 5 </button>
      </div>
      <div class="item23">
         <div class="item2">
            <span> I am a Text! </span>
         </div>
         <div class="item3">
            <div class="btn-group">
               <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
               Choose letter <span class="caret"></span>
               </button>
               <ul class="dropdown-menu">
                  <li><span>A</span></li>
                  <li><a href="#">Another action</a></li>
                  <li><a href="#">Something else here</a></li>
                  <li><a href="#">Separated link</a></li>
               </ul>
            </div>
         </div>
      </div>
     <div class="item4">
       <nav aria-label="Page navigation example">
        <ul class="pagination">
          <li class="page-item"><a class="page-link" href="#">Previous</a></li>
          <li class="page-item"><a class="page-link" href="#">1</a></li>
          <li class="page-item"><a class="page-link" href="#">2</a></li>
          <li class="page-item"><a class="page-link" href="#">3</a></li>
          <li class="page-item"><a class="page-link" href="#">Next</a></li>
        </ul>
      </nav>
     </div>
     <div class="item5">
       <i class="bi bi-gear"></i>
     <div>
   </div>
</div>

Ref: https://codepen.io/belt-basya/pen/vYRXBzE

Answer №1

If you're looking for more control over your layout, CSS grid might be the solution for you.

Below is a basic example using grid-template-areas. You'll need to adjust the widths of the items to fit your specific design.

.items {
  display: grid;
  grid-template-areas: 'Button Text Dropdown Pagination Icon';
  margin-top: 30px;
}

.item1 {
  grid-area: Button;
  background: red;
}

.item2 {
  grid-area: Text;
  background: green;
}

.item3 {
  grid-area: Dropdown;
  background: blue;
}

.item4 {
  grid-area: Pagination;
  background: cyan;
}

.item5 {
  grid-area: Icon;
  background: yellow;
}

@media only screen and (max-width: 990px) {
  .items {
    grid-template-areas: 'Button Button' 'Text Dropdown' 'Pagination Icon';
  }
}

@media only screen and (max-width: 575px) {
  .items {
    grid-template-areas: 'Button Button' 'Text Text' 'Dropdown Dropdown' 'Pagination Icon';
  }
}
<div class="container">
  <div class="items">
    <div class="item1">
      <button> button 1 </button>
      <button> button 2 </button>
      <button> button 3 </button>
      <button> button 4 </button>
      <button> button 5 </button>
    </div>
    <div class="item2">
      <span> I am a Text! </span>
    </div>
    <div class="item3">
      <div class="btn-group">
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
               Choose letter <span class="caret"></span>
               </button>
        <ul class="dropdown-menu">
          <li><span>A</span></li>
          <li><a href="#">Another action</a></li>
          <li><a href="#">Something else here</a></li>
          <li><a href="#">Separated link</a></li>
        </ul>
      </div>
    </div>
    <div class="item4">
      <nav aria-label="Page navigation example">
        <ul class="pagination">
          <li class="page-item"><a class="page-link" href="#">Previous</a></li>
          <li class="page-item"><a class="page-link" href="#">1</a></li>
          <li class="page-item"><a class="page-link" href="#">2</a></li>
          <li class="page-item"><a class="page-link" href="#">3</a></li>
          <li class="page-item"><a class="page-link" href="#">Next</a></li>
        </ul>
      </nav>
    </div>
    <div class="item5">
      <i class="bi bi-gear">I</i>
    </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

Can you explain the concept of "cascading" within CSS?

Can someone kindly explain the specific definition of "Cascading" in Cascading Style Sheets (CSS)? I have heard conflicting perspectives on this topic, so I am seeking clarification here. Any examples to illustrate would be greatly appreciated. ...

The ngOnInit function is not triggered upon instantiation of an Injectable class

What could be causing the ngOnInit() method not to be called upon resolution of an Injectable class? Code import {Injectable, OnInit} from 'angular2/core'; import { RestApiService, RestRequest } from './rest-api.service'; @Injectable ...

Understanding the proper way to enclose JSX components and exhibit JSON based on user input

I'm currently working on a university administration website using React that needs to support multiple languages. I have successfully built the Login page, which you can see here: https://i.stack.imgur.com/cIiaU.png Now, my next task is to allow us ...

Search MongoDB to find, update, and validate any empty fields in the body

I'm currently working on updating a user's profile information, but I'm facing a problem with checking for empty values before proceeding with the update. The code I've written so far is not displaying error messages and is preventing m ...

"Designing with CSS: The Art of Styling

Recently, I came across a styling issue with a text area. After adding the following code to the view file of a Yii Application and the corresponding styling code to the CSS file, I noticed that the border of the text area remained unchanged when an error ...

I want to add an LI element to a UL by utilizing jQuery in forms.js

I have several forms on my webpage and I am utilizing jQuery form.js to save each comment that users post. After saving the comment successfully, I want to append it to the UL tag. Although the saving part is functioning properly, I am encountering difficu ...

Displaying a child component as a standalone page rather than integrating it within the parent component's body

I'm currently working on implementing nested navigation in my website, but I am facing challenges with loading the child component without the need to include a router-outlet in the parent component. This setup is causing the child component's co ...

What exactly does a 'hoisted manifest' mean when it comes to using Yarn?

Encountering an issue while attempting to install a package using yarn, I am receiving the error message: expected hoisted manifest for \"myPackage#@material-ui/core#react-dom\" However, the concept of a 'hoisted manifest' is not entir ...

What is the best way to reposition the caret within an <input type="number"> element?

How can I make the caret move when an input is clicked? The code I found works with text, but not with number. Also, is there a way to pass the length without specifying a value (e.g. 55) in the parameters? HTML <input type="number" name="cost" value= ...

Chaining inheritance through Object.create

Recently, I decided to experiment with Object.create() instead of using new. How can I achieve multiple inheritance in JavaScript, for example classA -> classA's parent -> classA's parent's parent, and so on? For instance: var test = ...

Vue is actively eliminating a background image through the use of the style attribute

There appears to be an issue where Vue is removing both the style attribute and the background image from this tag. <section style="background: url(images/banners/profiles/profile-banner-1.png);" class="profile-banner flex items-end md:items-end md:j ...

Why does the Next.js GET index request keep fetching multiple times instead of just once?

Currently, I am encountering an issue while working on a tutorial app with Next.js. One of my components is not rendering due to what seems like multiple executions of a simple GET request for an index page. The problem perplexes me, and I need some assist ...

Access Information within live tabs

When attempting to click on any tabs in order to retrieve their respective data, I am successfully able to do so. However, I'm only able to retrieve the data of one row at a time. Can anyone provide assistance with this issue? <div class="col-lg-1 ...

What is the best method for converting a string picture URL into an image and showcasing it in an image tag?

I have a URL for an image that looks like this: C:\inetpub\MaujApp\OMSAPI\ManualReceivingImages\WhatsApp Image 2021-03-24 at 12.07.41 PM.jpeg My goal is to convert the original image and display it to the user using jQuery. I woul ...

Utilizing a backup system to store environment variables within a configuration file

Currently, I am utilizing my environment variables by directly referencing process.env.NODE_ENV throughout my application. While this method works, it is becoming challenging to manage and keep track of. Therefore, I would like to consolidate all these var ...

Using jQuery to Extract HTML Content from a JSON Object

I'm completely lost on what I'm doing incorrectly, but the JSON string I have looks like this: jsonp443489({"content":"<!DOCTYPE html><html><head><title>Title</title></head><body><p>Hello World< ...

Tips for displaying a pop-up when pressing a link:

I've spent a considerable amount of time on this project without making much progress. However, I came across a tutorial with a beautiful menu design that I decided to replicate. Here is the code: HTML: <!DOCTYPE html> <html> <head> ...

Tips for implementing an HTML modal with AngularJS binding for a pop up effect

As a beginner in AngularJS, I am facing a challenge. I have an HTML page that I want to display as a pop-up in another HTML page (both pages have content loaded from controllers). Using the router works fine for moving between pages, but now I want the s ...

Move the footer down to the bottom of the page

I'm struggling to create a footer that consistently stays at the bottom of the page, regardless of the amount of content on the page. I've tried following various tutorials but haven't had much success. I'm starting to think I must be d ...

Firefox experiencing issues with the onchange event

Here in this block of code, I have two dropdown lists: one for department and the other for section name. Based on the selected department, I dynamically change the options available for the section name dropdown list and then populate the values from both ...