Adjusting the size of the jQuery window in Google Chrome

I'm encountering an issue with resizing a menu. The code I have works fine on all browsers except Chrome.

Describing this problem in words is tough, so I'll provide some screenshots to better illustrate the issue.

This is how my menu appears initially:

Upon resizing the browser window to <=605px, the jQuery code should remove the left float and align the items in a centered column. However, in Chrome, it looks like this:

Here's the code snippet:

HTML:

<div id="containerMenu">
    <div id="menu">
        <ul>
            <a href="index.html"><li>HOME</li></a>
            <li>SERVIZI</li>
            <a href="wedding.html"><li>WEDDING</li></a>
            <a href="food.html"><li>FOOD</li></a>
            <a href="contatti.html"><li>CONTATTI</li></a>
        </ul>
    </div>
</div>

CSS (relevant sections only):

#containerMenu {width:100%; text-align:center;}
#menu {display:inline-block}
#menu li {float: left;}

jQuery:

$(window).resize(function() 
{
    if($(window).innerWidth() <= 605) 
    { 
        $("li", $("#menu")).css("float","none")
    }
    else 
    {                   
        $("li", $("#menu")).css("float","left")
    }                   
})

I also have code that applies when the window is opened with a width <= 605, but it's essentially the same as above without the .resize() event.

Can anyone assist me in making this code compatible with Google Chrome?

Thanks, Stefano

Answer №1

To simplify handling this issue, instead of using jQuery, consider incorporating a media query in your CSS code. A sample implementation could look like the following:

/* Your standard CSS styles */
#containerMenu {
    width:100%;
    text-align:center;
}
#menu {
    display:inline-block
}
#menu li {
    float: left;
}

/* Media query */
@media (max-width: 605px) {
    #menu li {
        float: none;
    }
}

This approach ensures a smooth transition across various browsers. Essentially, you are instructing the browser to have the menu list items initially set to float: left. However, when the window size reaches a maximum width of 605px or less, the rule changes to float: none for the menu list items. This mirrors the functionality you would typically achieve with JavaScript.

Media queries serve as a fundamental component of Responsive Web Design.

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

Trigger a jQuery click event to open a new tab

On a public view of my site, there is a specific link that can only be accessed by authenticated users. When an anonymous user clicks on this link, they are prompted to log in through a popup modal. To keep track of the clicked link, I store its ID and inc ...

The CSS "ul" selector targets unordered lists in HTML

How can I target the ul elements highlighted below in my HTML markup without using a class or id for reference? Here is the relevant portion of my HTML code: <div id="mainmenu"> <ul class="menu"> <li class="item-472"> < ...

Replicating JavaScript functions with the power of Ajax

I'm facing an issue with Bootstrap modal windows on my page. The modals are opening and closing successfully, but the content inside them is fetched through AJAX as HTML. For example, there's a button in the modal: <button id="myBtn"> and ...

Is there a way to ensure dropdown links in a responsive menu can extend past the menu's height?

Recently, I have been working on creating a CSS responsive main menu by combining code snippets from different sources. Despite my efforts, I encountered a major issue in the form of dropdown menu items disappearing when they extend beyond the normal heigh ...

Properly align and position the background of the header image

I'm currently tackling an issue on my website where I need help aligning and positioning the background image in the main header section. My goal is to ensure that the image is fully visible in this area, even when viewed on mobile devices. Attached ...

Convert JSON data into HTML format

Can you assist me in creating the HTML DIV block for the response I received in JSON format? Here is the JSON format: [{"REL_NAME" : " 999999999","SO" : "","U_ID" : "105"}] Snippet: function ProcessData(data) { $('#accNo12').empty(); if (d ...

Enhancing game menus for various mobile screen sizes

I've noticed a consistent pattern in the games I download from the Google Play Store - they all have a background image at the menu with clickable buttons. When testing these games on different devices, I found that the backgrounds didn't stretch ...

I want to display events from my database table on their corresponding dates using JavaScript and jQuery. How can I achieve this?

Using the FullCalendar plugin, I attempted to achieve a specific functionality, but unfortunately fell short of my goal. Below is the snippet of my scripting code: $('#calendar').fullCalendar({ //theme: true, header: { ...

Retrieve GET ajax requests from a webpage by simply entering the URL

Here's an example from the API I'm currently working with: This particular page relies heavily on GET calls to load most of the data. Three main calls that catch my interest are: All these calls share a common feature, which is the numeric ID ...

Attempting to organize date and time down to the second

I'm currently working on sorting time with seconds included. While I am able to sort the minutes successfully, I'm facing difficulty in sorting the seconds. Despite trying various approaches and using dynamic data that needs to be sorted in desce ...

Uncovering the specific file housing a CSS definition

I'm currently navigating a complex webpage with numerous CSS files that were not authored by me. I am struggling to identify a CSS style that seems elusive, as it is not located in the most obvious places (such as the CSS files) and I have no idea whe ...

The floating label appears distorted when used with a datalist input in Bootstrap 5

How can I get the floating label to display correctly for datalists in Bootstrap 5? Currently, it looks like this... It's working fine without the form-floating class, but I want to use the floating form feature. <div class="form-floating" ...

Is there a way to automatically determine the text direction based on the language in the input text?

When posting in Google Plus (and other platforms), I noticed that when I type in Persian, which is a right-to-left language, the text direction changes automatically to rtl and text-alignment:right. However, when I switch to English, the direction switches ...

Is there a way to make my DIVS update their content dynamically like buttons do, without manually adding an onclick function in the HTML code?

I am currently customizing a WordPress theme that lacks the option for onclick events on div elements. However, I can assign classes and IDs to them. In my design, I have four spans in a row, and when each span is clicked, I intend for the corresponding p ...

An image spanning two columns on a page with three columns

I am currently utilizing Foundation 4 for my website design. I am attempting to create an image that spans over 2 columns in a 3-column layout using the Foundation grid system. Here is how I envision it: I am wondering if it is possible to achieve this by ...

Sending data to CSS file within Django

Can variables be passed in CSS files, similar to HTML files? For example: In views.py: def home(request): bgcolor = "#999" ... ... In the CSS file: body { background-color : {{bgcolor}}; } If it is indeed possible, could you please pro ...

Ways to update a div periodically with new data fetched from a file - Here's How!

I am looking to auto-refresh a specific div every few seconds on my index.php page below. <?php session_start(); if (! check_write()) { redirect('testlogin.php'); return; } if (file_exists('lmt.json')) { $lmt = json_de ...

Having problems saving data with JQuery.ajax

Is there a way to store data from a Textbox when the Button is clicked? I am currently using JQuery AJAX to accomplish this task, as shown below. Please note that these tags are placed inside the theme function. function theme_user_post_block($vars) { $t ...

Using jQuery, disregard elements that are not hidden

Currently, I am working with a jQuery validator and I am attempting to utilize the ignore: ':hidden:not feature for all controls that contain an ID with " selecting" in it. Here is my current code snippet: sb.AppendLine(@" ignore: ':hidden: ...

Refreshing a particular <div> on a webpage while making an AJAX request

I've encountered an issue that has left me stuck. The problem is that I need to refresh a specific div on my page that contains PHP script. Below is the JavaScript function causing trouble: function select_dayoff() { jQuery('#loader').c ...