When using CSS media queries and Javascript, the menu behaves differently on desktop compared to mobile devices. While it works well on

Encountering a difficulty with CSS media queries and a UL tag involving two menus (left and right) along with a search bar. The issue arises when the right menu goes off screen on desktop, even though it works correctly on mobile. When users activate the menus, they slide in from the edge of the screen as expected, but slide back when the search bar is clicked or tapped.

Seeking guidance on what might be going wrong in this setup. Any insights would be greatly appreciated.

Here’s an overview of the code:

HTML

<!DOCTYPE html>
<html>
<head>
<title> Title</title>
... (rest of HTML code)
</body>
</html>

CSS

@media screen and (max-width:450px) and (min-width:321px){
... (CSS code for different screen sizes)
}

@media screen and (max-width:1920px) and (min-width:1777px){
... (More CSS code for larger screens)
}

JavaScript

function max(){
... (JavaScript function for toggling right menu)
}
... (Other JavaScript functions for interactions)

Answer №1

Your CSS is on the right track, but consider separating some styles from the media queries for better organization.

It's recommended to start with general styles outside of media queries that apply to all screen sizes.

I made some modifications to your code in a CodePen here: https://codepen.io/vic3685/pen/xWdZzM

In the code, I adjusted certain elements at the top without altering your existing CSS drastically:

body, html {margin: 0; padding: 0; }
#container{width:100%; background:rgba(0,0,0,1); height:auto;color: white; }

/*Left menu */
#left-menu{
  color:white;
}

#left-list ul{  
    width:100%;
  list-style-type:none;          
  padding:0px  0px 3% 0px;  
  overflow:auto;

}
#left-list li{
  width:97%; height:auto; color:white; }

.dropdown-content{position:absolute; background:rgba(0,0,255,1);

overflow:auto; width:102%; height:auto; float:left; transition:0.2s; transform:translate(-100%, 0%); padding:0% 0% 20px 0%;}

@media (min-width: 451px) {
  #left-list {
    width:200px;
    position: absolute; left: 0; top: 50px; 
  } 
  #right-menu {
    width: 200px; 
    position: absolute; right: 0; top: 50px; 
    height:auto;  margin:0% 0% 0% 0%; padding:5% 0% 500% 0%; position:absolute; z-index:5;  background:rgba(0,255,0,1); transform:translate(105%, 0%); transition:0.3s; 
    transition:0.15s;
  }
  #left-list ul {padding-left: 30px; } 
  #left-menu, #find, .mob-menu {float: left; height: 50px;}
  .mob-menu {float:  right;}
  .mob-menu > a {display: block; text-align: right; }
  #container:after {
    content: ""; 
    display: block;
    clear: both; 
  }
}

My adjustments focused on: - Moving some styles outside of media queries. - Adding a new media query for screens wider than 450px where specific styling was missing before.

To position the menus at the sides, I utilized floats and cleared them using #container:after. Another option could be flexbox depending on browser support needed. Also, I introduced position:absolute to make menu content independent of surrounding elements.

You may need to fine-tune the design further, but this should provide a good starting point.

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

Nested dropdown menus in Vue.js with multiple levels of nesting

I am currently in the process of developing a Vue single page application and have encountered a challenge with a Vue element I am working on. var appCataloghi = new Vue({ el: '#appCataloghi', data: { cataloghi: oggettoCataloghi.catalog ...

Error Message: Unexpected Type Error with axios in Vue 3

Trying to implement axios in my Vue3 project for fetching APIs. Here is the code snippet from my component: export default { name: "Step2", data() { return { loading: true; }; }, mounted() { this.loading = false; }, ...

Avoiding line breaks when using an :after pseudo element

I am trying to style external links by adding a pseudo :after element right after the link itself, but I'm having trouble getting it to work on the same line. Here is the CSS code I have: a:not([href*="domain"])::after { content: url(data:image/svg+ ...

The ES6 alternative to require() when not using exports

When I utilize require(./filename), I am able to include and run the code within filename without any explicit export being defined inside filename. In ES6, what is the equivalent of this using import ? Appreciate it ...

How can I append a hash to a URL using JavaScript without causing the page to scroll?

Is it possible to add a hash to the URL without scrolling the page using JavaScript? I navigate to the page I scroll down I click on a link that adds a hash (for example: http://www.example.com/#test) The page should not scroll back to the top. What is ...

Setting a default value within an input tag: A step-by-step guide

const [userData, setUserData] = useState([]); const handleUserInfo = (id) => { fetch(`https://602e7c2c4410730017c50b9d.mockapi.io/users/${id}`) .then(res => res.json()) .then(data => setUserData(data)) } <inpu ...

Sleek CSS3 Slide Menu - Smooth Transition to Top when Sliding

I have been experimenting with a CSS-only slide menu. Essentially, I set the nav menu position to fixed with top: 0 and right:-200 using radio buttons for control. <input type="radio" id="nav-expand" name="nav" /> <input type="radio" id="nav-col ...

CSS gradient pointing arrow

Is there a way to create a CSS arrow with a gradient instead of a solid color? Below is the CSS I have been experimenting with: .breadcrumbDivider .arrow-right { width: 0; height: 0; border-top: 25px solid transparent; border-bottom: 25px solid ...

Executing a Long Press in JavaScript

My web application allows users to click on a field and have the text inside highlighted for copying. However, I have noticed that on Android devices, this action does not automatically trigger the copy context menu to appear. Instead, the user must manual ...

What is the proper way to notify a caller of a rejected call?

index.js async handleCallActions() { const tokenResponse = await this.generateTokenForChannel(this.agoraChannel); this.initializeAgoraSDK(tokenResponse.data.appID); this.joinRoomWithToken(tokenResponse.data.token, this.ag ...

Elevate when the mouse hovers over the button

My current task involves increasing the bottom-padding of a span when the mouse hovers over a button. Here is the button code: <button class="btn btn-success btn-circle" id="myBtn" title="Go to top"> <span id="move" class="fa fa-chevron-up"&g ...

CAUTION: Attempted to initialize angular multiple times...all because of jQuery...such a puzzling issue, isn

I am attempting to comprehend the situation at hand. The warning is clear, and I acknowledge that in my application, with the provided code and structure, the ng-view runs twice ('test' is logged twice in the console, indicating that angular is l ...

Can you include both a routerLink and a click event on the same anchor tag?

I am facing an issue with my li elements. When a user clicks on them, it should open a more detailed view in another component. However, I noticed that it takes TWO clicks to show the data I want to display. The first click opens the component with an em ...

The C# [WebMethod] will not trigger if the Content-Type "application/Json" is missing

After creating a C# WebMethod, I was able to successfully call it using Ajax, angular, and Postman when adding the header Content-Type: 'application/Json'. Here is an example of the HTTP request that worked: $http({ url: 'default.aspx/G ...

Is React the ideal choice for implementing a shared state subscription mechanism in your project?

Trying to determine if this falls under the "pub/sub" pattern or a variation of it. The goal is to establish a shared state where different components can subscribe to it and only receive updates when the state changes. const useForceUpdate = () => useR ...

FNS Date-Timezone Abbreviation

Is there a way to shorten the Australian Eastern Daylight Time abbreviation to just AEDT? When I use it currently, it displays as 11/11/2022 15:29:25 Australian Eastern Daylight Time. I would like it to show as 11/11/2022 15:29:25 AEDT import { formatInT ...

Resizing cell height in an HTML table is not supported by Angular 5

In my angular application, I've implemented a component that represents a basic HTML table. However, I'm facing an issue with reducing the height of cells within the table. It seems like my CSS styling is not affecting the display as desired. He ...

Using deconstruction in exporting as default

As I was diving into a new codebase, I stumbled upon this interesting setup: //index.js export { default } from './Tabs' export { default as Tab } from './Tab' //Tab.js export default class Tab extends Component { render() => &ap ...

What is the best way to pass a function along with its state to utilize useContext in React

Currently attempting to grasp the concept of using react hooks. I have implemented createContext and am looking to pass both state and a function to other components, but could use some guidance on how to achieve this. I'm also questioning if it is ev ...

Difficulty arises when trying to merge a dropdown menu with a horizontally scrolling menu

I am attempting to include a dropdown menu within a horizontally long menu. In order to achieve this, I have merged the script for displaying a scrollable menu with that of a dropdown menu. However, in doing so, the dropdown menu does not pop out from the ...