Ways to ensure all tabs on the Nav bar are evenly distributed across the width of the screen

Hello! I am in the process of creating my first website, and it's going to be a D&D character randomizer. Currently, I'm working on the Nav-bar section and I have four bars that I want to fit across the screen equally, regardless of the screen size. If anyone has expertise in this area, I would greatly appreciate your advice on what modifications should be made to my existing code. I've shared all the CSS and HTML required to replicate the issue. Thank you in advance!

/* Menu CSS */
#Menu {
    font-size: 25px; /* Font size set to 25 px */
   }
   
   /* Background CSS for the Menu */
   #Menu ul {
     list-style-type: none; /* Bullets removed */
     overflow: hidden; /* Scrollbar disabled */
     background-color: rgb(77, 39, 21); /* Menu background color */
     margin-bottom: 0px; /* 0 pixels bottom margin */
     margin-top: 0px; /* 0 pixels top margin */
     padding: 0;
   }
   
   /* Edges of the Menu CSS*/
   #Menu li {
     float: left; /* Floats Menu items to the Left */
     border-right: 1px solid rgb(104, 99, 99); /* 1px left border line separating blocks */
     border-left: 1px solid rgb(104, 99, 99); /* 1px right border line separating blocks */
   
   }
   
   /* Tab styling for Menu CSS */
   #Menu li a {
     display: block; /* Block level display */
     color: rgb(187, 180, 180); /* Text color */
     text-align: center; /* Aligns text in the middle of the block */ 
     padding: 16px 177px; /* Padding between blocks */
     text-decoration: none; /* No underline for links */
   }
<link rel="stylesheet" type="text/css" href= "StylesheetQ.css">

<div id="Menu">
    <ul>
       <li><a class="active" href="Main Page.html">Home</li></a></li>
       <li><a href="About.html">About</li></a></li>
       <li><a href="Contacts.html">Contacts</li></a></li>
       <li><a href="Testimonials.html">Testimonials</li></a></li>
   </ul>
</div>

Answer №1

You'll find the solution you need in this code snippet.

Utilizing Flexbox, a powerful layout tool that can be explored further at: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

The key modifications include adding display: flex to the ul element and assigning flex: 1 to the li elements.

flex: 1 shorthand for flex-grow: 1, ensures equal distribution of remaining space among children within the container.

Incorporating Flexbox eliminates the necessity of using float and excessive horizontal padding on the a elements.

/* CSS for the Menu */
#Menu {
    font-size: 25px; /* Set Font size to 25 px */
   }
   
   /* CSS for the Menu background */
   #Menu ul {
     list-style-type: none; /* No Bullets */
     overflow: hidden; /* Disable Scroll Wheel */
     background-color: rgb(77, 39, 21); /* Background color of Menu */
     margin-bottom: 0; /* Bottom Margin set to 0 */
     margin-top: 0; /* Top Margin set to 0 */
     padding: 0;
     display: flex; /* Apply flexbox layout to the list */
   }
   
   /* CSS for Menu Edges*/
   #Menu li {
     border-right: 1px solid rgb(104, 99, 99); /* 1px left border line separating blocks */
     border-left: 1px solid rgb(104, 99, 99); /* 1px right border line separating blocks */
     flex: 1; /* Float is not required with Flexbox */
   
   }
   
   /* CSS for Menu Tabs */
   #Menu li a {
     display: block; /* Block-level display */
     color: rgb(187, 180, 180); /* Text color */
     text-align: center; /* Center align text */ 
     padding: 16px; /* Reduced horizontal padding */
     text-decoration: none; /* Remove underlines from links */
   }
<link rel="stylesheet" type="text/css" href= "StylesheetQ.css">

<div id="Menu">
    <ul>
       <li><a class="active" href="Main Page.html">Home</li></a></li>
       <li><a href="About.html">About</li></a></li>
       <li><a href="Contacts.html">Contacts</li></a></li>
       <li><a href="Testimonials.html">Testimonials</li></a></li>
   </ul>
</div>

Answer №2

Below is the modified code along with some comments to help you understand the changes made and improvements suggested.

If you consider using CSS Grid, it may greatly reduce the redundancy in your code.

Check out the cleaned-up version of HTML/CSS Style:

*{
    margin:0;  
    padding:0;
    box-sizing:border-box;
   }

#Menu {
    width:100%;
   }
   
#Menu ul {
    font-size: 100%; 
    list-style-type: none; 
   }
    
#Menu li {
     display: inline-block; 
     width: calc(100%/4); 
     text-align: center;
     padding: 16px 0px;
     float: left; 
     border-right: 1px solid rgb(104, 99, 99); 
     background-color: rgb(77, 39, 21); 
   }    
    
#Menu li:nth-child(1){ 
     border-left: 5px solid red; 
   }
   
#Menu a {
     color: rgb(187, 180, 180); 
     text-decoration: none;
     font-size: 100%;
   }

<!--end snippet-->

<!-- begin snippet: js hide: false console: true babel: false -->
<link rel="stylesheet" type="text/css" href= "StylesheetQ.css">

<div id="Menu">
    <ul>
       <li><a class="active" href="Main Page.html">Home</li></a></li>
       <li><a href="About.html">About</li></a></li>
       <li><a href="Contacts.html">Contacts</li></a></li>
       <li><a href="Testimonials.html">Testimonials</li></a></li>
   </ul>
</div>

Cleaned-up version of HTML/CSS with comments:

*{
margin:0;   /* zero-out MARGINS and PADDING */
padding:0;
box-sizing:border-box; /* helps keep dimensions of any block elements when they resize */
}

#Menu {
    /* don't target the Menu as a whole
    unless you want ALL the children to 
    have the same traits you intend for 
    your menu items (e.g. if you want menu
    items to be 25px) */
    
    width:100%; /* assuming you want it to expand the entire width of the window*/
    
 /* width:800px; <------ use this method if you want a FIXED size */
   }
   
   /* CSS for the Menu background */
   #Menu ul {
    font-size: 100%; /* Font size of 25 px */
    list-style-type: none; 
    }
   
   /* CSS for Menu Edges*/
   #Menu li {
     display: inline-block; /*INLINE-BLOCK */
     width: calc(100%/4); /* 100% width DIVIDED by No. of Elements */
     text-align: center;
     float: left; /* Menu is floated to the Left */
     border-right: 1px solid rgb(104, 99, 99); /* right border of other menu items. */
     background-color: rgb(77, 39, 21); 
     padding: 16px 0px; /* the padding is added to each element */
    }
    
#Menu li:nth-child(1){ /* left border of FIRST menu item. */
     border-left: 5px solid red;
    }
   
#Menu a {
     color: rgb(187, 180, 180);
     text-decoration: none;
     font-size: 100%;
   }
<link rel="stylesheet" type="text/css" href= "StylesheetQ.css">

<div id="Menu">
    <ul>
       <li><a class="active" href="Main Page.html">Home</li></a></li>
       <li><a href="About.html">About</li></a></li>
       <li><a href="Contacts.html">Contacts</li></a></li>
       <li><a href="Testimonials.html">Testimonials</li></a></li>
   </ul>
</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

What are some ways to customize the appearance of React Semantic components?

Is there a way to apply CSS for react semantic UI when using create react app? I have installed semantic-ui-react and included the CSS CDN: loginForm.js: import React from "react"; import { Button, Form, Header } from "semantic-ui-react"; import styles f ...

Bootstrap requires Visual Studio Code live-server plugin to function properly

I have been utilizing the Visual Studio Code IDE for my coding projects, along with a helpful plugin called "live server" that allows for quick checks of code changes. Everything was running smoothly with Bootstrap until I encountered an issue. When I att ...

tips for passing value to the date field using proctractor

This is the HTML code I am working with: <input id="filter-datepicker" type="daterange" ranges="ranges" class="form-control date-picker" placeholder="Select Date Range" name="sensorDetails.date" ng-model="myDateRange" ranges="ranges" requi ...

The background video on the website is having trouble resizing properly to fit the screen dimensions

I'm new to coding and I'm attempting to incorporate a background video on the homepage. The issue arises when I resize my window or view it on a mobile device, as the video fails to adjust to the window size, causing side scrolling that reveals t ...

Is there a way to effectively overwrite CSS styles when utilizing @extend with another class in SCSS?

Here is some SCSS code I am working with: .a { height: 100px; width: 100px; } .b { @extend .a; height: 200px; } After compiling, the CSS appears as follows: .a, .b { height: 100px; width: 100px; } .b { height: 200px; } Whe ...

Maintaining text color while adding a color overlay to a Bootstrap Jumbotron

After searching extensively, I couldn't find any mention of this particular issue. According to the Bootstrap 4.2 documentation on the Jumbotron, I constructed my div with an inline background image. However, when I attempted to apply an overlay on to ...

Creating a table with a static first column and vertical text positioned to the left of the fixed column

To create a table with the first column fixed, refer to this fiddle link: http://jsfiddle.net/Yw679/6/. You also need a vertical text to be positioned to the left of the fixed column in a way that it remains fixed like the first column. The disparities be ...

What is the best way to calculate the time elapsed between two consecutive double clicks using jQuery?

I am trying to calculate the time difference between two clicks on a single button. Here is my code snippet: <a href="#">click here</a> My current JavaScript code to capture the time of each click is as follows: var clickedTime = '&apos ...

Is there a way to overlap shapes (transform) using ::before and ::after in CSS? I'm facing an issue where Edge is not showing the shape on top

I designed two diagonal shapes to overlay a container with a background image using the pseudo elements ::before and ::after. While this setup functions correctly in Firefox and Chrome, it is not working as expected in Edge. What could be causing this di ...

Connecting the search results page with the specific details page

Currently, I am developing a results page for my website and require assistance with linking each restaurant to a detail.php page. The project involves showcasing all the restaurants in San Francisco along with their health inspection scores, address, and ...

Attempting to adjust table size in MPDF to fill the available height

I'm currently utilizing MPDF in order to create a PDF document from an HTML page that includes a table. My goal is to have the table expand to fill up the remaining space on the page. Here is the specific table I am working with: I want the final el ...

Modifying the default actions of Material UI components: A step-by-step guide

In my project, I am using materialUI to showcase an Expansion Panel. Here is the code snippet: import React from 'react' import ExpansionPanel from '@material-ui/core/ExpansionPanel'; import ExpansionPanelSummary from '@material-u ...

Tips for correctly cloning a table row:

Currently, I am engaged with a Django project that involves incorporating a form within a table structure. <table name="mytable" id="table_purchase" role="grid"> <thead> <tr> <th class="text-center" hidden>No</th& ...

The width of an HTML input and button elements do not match

Currently, I am facing an unusual issue where my input and button tags seem to have the same width assigned to them (width: 341.5px; calculated from $(window).width() / 4) in the code, but visually they appear to be of different widths. Here is a visual re ...

Tips for resizing a larger image to display only a specific portion in CSS (and incorporating JS if needed)

I need to resize an image that measures 1024x1024 and is segmented into 4 quadrants: My goal is to reduce the size of this image so that quadrant 2 is 256x256 while masking out or hiding the remaining 3 quadrants, displaying only the desired section on th ...

Using Rails and HAML to incorporate looping HTML5 video tags

I have a question regarding how I'm using Rails video_tag to display a video: = video_tag("SMR_video.mp4", :controls => false, :autobuffer => true, :autoplay => true, :loop => true, :id => "hero-video") After implementing the above co ...

Conceal the div by clicking outside of it

Is there a way to conceal the hidden div with the "hidden" class? I'd like for it to slide out when the user clicks outside of the hidden div. HTML <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.c ...

Slide the next section over the current section using full-page JavaScript

I'm currently developing a website utilizing the FullPage.JS script found at this link . My goal is to have the next section slide over the previous one, similar to what is demonstrated in this example. I've attempted setting the position to fix ...

What could be causing my divs to overlap?

I recently started coding and I'm facing an issue with my <header> and <section> divs overlapping each other. I was under the impression that being block elements, <section> should be displayed below <header>. Any suggestions o ...

Need Some Help Reversing the Direction of This CSS Mount Animation?

Exploring this fiddle, I encountered a small div showcasing an opacity animation. The animation smoothly executes when the state transitions to true upon mounting the component, causing the div to fade in beautifully. However, I faced a challenge as the an ...