Adjusting the size of a horizontal menu for different devices

Is there a way to make the navigation menu responsive for different screen sizes, such as iPad and iPhone? The menu looks great on desktop, but when viewed on smaller devices, it is too wide and causes the main content of the page to appear smaller and off-center.

CSS

#menu, #menu ul {
    margin: 0 auto;
    padding: 0;
    background-color: #FFFFFF;
}

#menu {
    display: table;
    width: 100%;
    list-style: none;
    position: relative;
    top: -30px;
    text-align: center;
    -webkit-box-shadow: 0px 3px 5px 0px rgba(50, 50, 50, 0.75);
    -moz-box-shadow: 0px 3px 5px 0px rgba(50, 50, 50, 0.75);
    box-shadow: 0px 3px 5px 0px rgba(50, 50, 50, 0.75);
    font-size: 18px;
    height: 20px;
    z-index: 101;
}

#menu.fixed {
    position: fixed;
    top: 0;
    width: 100%;
}

#menu li {
    display: table-cell;
    list-style: none;
    padding-right: 50px;
    left: 50px;
    vertical-align: middle;
}

#menu > li:hover > ul {
    background: #FFF;
    display: block;
    left: 0;
    width: 100%;
    -webkit-box-shadow: 0px 3px 2px 0px rgba(50, 50, 50, 0.75);
    -moz-box-shadow: 0px 3px 2px 0px rgba(50, 50, 50, 0.75);
    box-shadow: 0px 3px 2px 0px rgba(50, 50, 50, 0.75);
    border-top: thin dotted #999;
    top: 32px;
}

#menu > li > ul {
    display: none;
    position: absolute;
    text-align: center;
}

#menu li a {
    display: block;
    padding: 2px 10px;
    text-decoration: none;
    font-weight: lighter;
    white-space: nowrap;
    color: #333;
}

#menu li a:hover {
    color: #CCCCCC;
    font-size: 18px;
    vertical-align: middle;
}

#menu li ul li {
    display: inline-block;
    float: none;
}

HTML

<div id="menu">
 <ul>
  <li><a href="#">Milk</a></li>
  <li><a href="#">Eggs</a></li>
  <li><a href="#">Cheese</a></li>
  <li><a href="#">Vegetables</a></li>
  <li><a href="#">Fruit</a></li>
 </ul>
</div>

Answer №1

Utilizing css Media Queries is essential for responsive web design.

When you implement @media (min-width: 768px){}, you are instructing the browser to apply certain styles to windows wider than 768px. This approach follows the mobile-first philosophy, which prioritizes designing for smaller screens first. If unfamiliar with this concept, I recommend researching it online.

Simply changing display: table-cell; to display: table-row for mobile devices will stack items vertically instead of horizontally.

I have created a demonstration incorporating the techniques mentioned above, complete with an HTML button and some jQuery functionality.

To view the demo, click here

Answer №2

Give this approach a shot: CLICK HERE

CSS:

@media handheld, only screen and (max-width: 768px) {
    #menu li {
        clear:both;
        float:none;
        display:block;
        background-color:#f0f0f0;
        margin-bottom:2px;
    }
}

Adjust the max-width in the media query based on your device requirements

Answer №3

Optimizing your website for different devices is crucial in today's digital world. Using media queries, you can customize the appearance of your menu bar based on the dimensions of various devices. Simply identify the dimensions you want to target and create specific rules within your media query to adjust the styling accordingly.

::Sources::

http://www.w3schools.com/css/css_mediatypes.asp

http://www.youtube.com/watch?v=aPtA4nYkvDw - Khoury's channel and website provide valuable insights into optimizing websites for different devices.

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

Having Trouble Setting the Root View Controller Programmatically in iOS 13 with Swift?

The code snippet in my AppDelegate.swift file is meant to set up the root view controller for my iOS application, following the Target structure (defined under General tab). However, it does not seem to be working as expected. I am using Xcode 11, Swift 5. ...

Limit the size of images with CSS properties like max-height or max-width

What is the preferred method for restricting the size of an image and why? While max-width can be used to restrict the size of an element, this article recommends using it specifically for images within containers. If you have a web application with a ve ...

Guide to adjusting the text size of an individual index within a UISegmentedControl?

If you want to modify the font size of a specific index within a UISegmentedControl, you can achieve this by adjusting the attributes for that particular segment. Additionally, if the text for a segment is too long and does not fit within the allocated spa ...

Changing the orientation of a dropdown menu from horizontal to vertical

Hello, I am seeking assistance to transform the top dropdown menu layout into a vertical one similar to the menu on the left side. Any help would be greatly appreciated as I am running out of ideas! Thank you in advance! @charset "utf-8"; /* CSS Docum ...

Shut the offcanvas using a button

I am having trouble closing the offcanvas using the close button inside the offcanvas itself. Currently, I can only close it with the button that I used to open it. What mistake did I make? Please refer to the code snippet below. (function mainScript( ...

Can PHP retrieve data when the form submit function is overridden with AJAX?

I have customized the .submit function of a form on this webpage. It is being loaded inside the #mainContent in an "index.php" and I want the Submit button to only replace this #mainContent. My goal is to retrieve data from this form and send it to a .php ...

The background color should only cover a specific percentage of the element

Here is the current structure I have: <li class="myclass" ng-class="myAngularClass"> <div> div1 <div> div2 </div> </div> </li> The li element has dynamic dimensions and width. The class "myAngularClass" i ...

The useState function does not trigger a re-render

I'm currently facing an issue with the useState hook where the state gets updated, but the changes are not reflected until I manually refresh the app. To provide some context, I initialize an array called sampleFriends containing objects with fields " ...

animate the alignment of right-aligned text to move to the right side and realign to the left

I need help with a small menu-list that should expand when the mouse is nearby. By default, the menu is aligned to the right, but on hover, every other item shifts to the left to accommodate the increased height (check out the JSFiddle). ul { font-siz ...

How to incorporate spacing within table rows using HTML

I am trying to create a table with rows starting from different positions like this 12345 678910 This is my code <table class="center"> <tr style="padding-right: 22%"> <td>1</td> <td>2</td> ...

Not able to get CSS animations to function on Mac computers

I am encountering an issue with Mac/Safari where two CSS animations are not working properly upon refreshing the screen. The div should perform a right scroll left animation. The image is not resizing as intended. For the scrolling animation, I have imp ...

When utilizing the 'card-deck' class, the top and bottom cards align perfectly

I am currently working with the card-deck class in bootstrap 4.5.0. The issue I am encountering is that when new cards are added below the previous card, they overlap without any separation. Code: <!Doctype html> <html lang="en"> <he ...

Unexpected changes in UIWebView size occur when loading a webpage from the internet

When loading a page from the internet, the content size of the UIWebView in StoryBoard changes and I'm not sure what the problem is. https://i.sstatic.net/oauyY.png On the iPhone, the content size is different from the UIWebView size. Could I have d ...

Prevent dragging functionality after being dropped onto a droppable area

I am currently working on a drag and drop project and have encountered a minor issue. My goal is to disable the draggable item after it has been dropped onto the droppable area. I have created a function called disableDrag, but I am receiving an error in t ...

Ways to resolve the issue of undefined $route.current in AngularJS

I have recently started using AngularJS and I am encountering an error "TypeError: $route.current is undefined". Please refer to the code snippet below: var sampleApp = angular.module('sampleApp', ['ngRoute']); sampleApp.config([&ap ...

Insert a hyperlink into a button through Javascript by modifying the window

For my text adventure game, I need one of the option buttons to act as a hyperlink that directs players to another webpage. Currently, my code looks like this: <article> <div id="text"></article> </div> <input id=" ...

When an HTML element such as a div includes multiple classes, it can impact the render tree and ultimately affect the performance of the webpage

<div class="a b c d e f g h i j"> </div> In order to display a web page, the browser parses the data to create both the DOM and CSSOM trees. These two trees are then merged into a render tree, which is used to determine the layout of each visi ...

Create a button that dynamically adds an image and text using code

I am looking to create a button with both an image and the text "View Details" programmatically. The button should be red in color, with the image on the left side and the text on the right side. UIImage *img = [UIImage imageNamed:@"image.png"]; [aButton ...

What is the best way to position four circles within a square container using CSS?

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> .circles { height: 150px; width: 150px; background-color: #bbb; border-radius: 50%; displ ...

Clashing CSS Styles: Font Size Edition

Can someone explain how CSS font sizes work? I set a specific line to be 200% font size, but changing the body font size affected it. Shouldn't the line maintain its specified size? When the body font size is changed from 12pt to 8pt, the line "There ...