Expand the width of navbar buttons to match the width of a container

Currently, I am immersing myself in learning navigation bars and dropdowns using only HTML & CSS. However, I have run into a problem where I am unable to fill the buttons to the length of a container. I attempted to utilize flexbox but unfortunately, that did not yield the desired outcome. Here is the code snippet:

body {
    background-color: #D4D4D4;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    line-height: 26px;
    color: #666;
    margin: 0;
}

.reset {
    clear: both;
}

.navigation {
    list-style: none;
    padding: 0;
    margin: 0;
    background-color: rgb(58, 58, 58);
    border-top: 3px solid #FFF;
    box-shadow: 0px 11px 21px 0px rgba(0,0,0,0.59);
}

.navigation li {
    float: left;
}

.navigation li:hover {
    background-color: #222;
}

.navigation li:first-child {
    border-radius: 5px 5px 0 0;
    -webkit-border-radius: 5px 5px 0 0;
    -moz-border-radius: 5px 5px 0 0;
    -ms-border-radius: 5px 5px 0 0;
    -o-border-radius: 5px 5px 0 0;
}

.navigation li a {
    display: block;
    padding: 0 20px;
    text-decoration: none;
    line-height: 40px;
    color: #FFF;
}

.navigation ul {
    display: none;
    position: absolute;
    list-style: none;
    margin-left: -3px;
    padding: 0;
    overflow: hidden;
}

.navigation ul li {
    float: none;
}

.navigation li:hover > ul li:hover {
    border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    -moz-border-radius: 0 0 5px 5px;
    -ms-border-radius: 0 0 5px 5px;
    -o-border-radius: 0 0 5px 5px;
}

.navigation li li a:hover {
    background-color: #000;
}

.navigation ul li:last-child a,
.navigation ul li:last-child a:hover {
    border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    -moz-border-radius: 0 0 5px 5px;
    -ms-border-radius: 0 0 5px 5px;
    -o-border-radius: 0 0 5px 5px;
}

.navigation li:hover {
    border-bottom: 2px solid dodgerblue;
}

.navigation li {
    border-bottom: 2px solid transparent;
}

@media screen and (min-width: 1281px) {
    .submenu::after {
        content: " +";
    }
    
    .navigation li:hover .submenu::after {
        content: " –";
    }

    .navigation li:hover > ul {
        display: block;
        background-color: #222;
        border: 3px solid #FFF;
        border-top: 0;
        border-radius: 0 0 8px 8px;
        -webkit-border-radius: 0 0 8px 8px;
        -moz-border-radius: 0 0 8px 8px;
        -ms-border-radius: 0 0 8px 8px;
        -o-border-radius: 0 0 8px 8px;
        box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.25);
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./css/navbar.css">
    <title>My Website</title>
</head>
<body>
    <div id="container">
        <!-- HEADER -->
        <header>
            <ul class="navigation">
                <li><a href="./index.html">HOME</a></li>
                <li><a class="submenu" href="#">DROPDOWN 1</a>
                    <ul>
                        <li><a href="#">PLACEHOLDER 1</a></li>
                        <li><a href="#">PLACEHOLDER 2</a></li>
                        <li><a href="#">PLACEHOLDER 3</a></li>
                    </ul>
                </li>
                <li><a class="submenu" href="#">DROPDOWN 2</a>
                    <ul>
                        <li><a href="#">PLACEHOLDER 1</a></li>
                        <li><a href="#">PLACEHOLDER 2</a></li>
                        <li><a href="#">PLACEHOLDER 3</a></li>
                    </ul>
                </li>
                <li><a href="#">LINK 1</a></li>
                <li><a href="#">LINK 2</a></li>
                <li><a href="#">LINK 3</a></li>
                <div class="reset"></div>
            </ul>
        </header>
    </div>
</body>
</html>

Answer №1

Give this code a shot:

body {
    background-color: #D4D4D4;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 14px;
    line-height: 26px;
    color: #666;
    margin: 0;
}

.reset {
    clear: both;
}

.navigation {
    list-style: none;
    padding: 0;
    margin: 0;
    background-color: rgb(58, 58, 58);
    border-top: 3px solid #FFF;
    box-shadow: 0px 11px 21px 0px rgba(0,0,0,0.59);
    display: flex;
}

.navigation li {
    flex: 1 0 auto;
}

.navigation li:hover {
    background-color: #222;
}

.navigation li:first-child {
    border-radius: 5px 5px 0 0;
    -webkit-border-radius: 5px 5px 0 0;
    -moz-border-radius: 5px 5px 0 0;
    -ms-border-radius: 5px 5px 0 0;
    -o-border-radius: 5px 5px 0 0;
}

.navigation li a {
    display: block;
    padding: 0 5px;
    text-decoration: none;
    line-height: 40px;
    color: #FFF;
}

.navigation ul {
    display: none;
    position: absolute;
    list-style: none;
    margin-left: -3px;
    padding: 0;
    overflow: hidden;
}

.navigation ul li {
    float: none;
}

.navigation li:hover > ul li:hover {
    border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    -moz-border-radius: 0 0 5px 5px;
    -ms-border-radius: 0 0 5px 5px;
    -o-border-radius: 0 0 5px 5px;
}

.navigation li li a:hover {
    background-color: #000;
}

.navigation ul li:last-child a,
.navigation ul li:last-child a:hover {
    border-radius: 0 0 5px 5px;
    -webkit-border-radius: 0 0 5px 5px;
    -moz-border-radius: 0 0 5px 5px;
    -ms-border-radius: 0 0 5px 5px;
    -o-border-radius: 0 0 5px 5px;
}

.navigation li:hover {
    border-bottom: 2px solid dodgerblue;
}

.navigation li {
    border-bottom: 2px solid transparent;
}

@media screen and (min-width: 1281px) {
    .submenu::after {
        content: " +";
    }

    .navigation li:hover .submenu::after {
        content: " –";
    }

    .navigation li:hover > ul {
        display: block;
        background-color: #222;
        border: 3px solid #FFF;
        border-top: 0;
        border-radius: 0 0 8px 8px;
        -webkit-border-radius: 0 0 8px 8px;
        -moz-border-radius: 0 0 8px 8px;
        -ms-border-radius: 0 0 8px 8px;
        -o-border-radius: 0 0 8px 8px;
        box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.25);
    }
}
<div id="container">
        <!-- HEADER -->
        <header>
            <ul class="navigation">
                <li><a href="./index.html">HOME</a></li>
                <li><a class="submenu" href="#">DROPDOWN 1</a>
                    <ul>
                        <li><a href="#">PLACEHOLDER 1</a></li>
                        <li><a href="#">PLACEHOLDER 2</a></li>
                        <li><a href="#">PLACEHOLDER 3</a></li>
                    </ul>
                </li>
                <li><a class="submenu" href="#">DROPDOWN 2</a>
                    <ul>
                        <li><a href="#">PLACEHOLDER 1</a></li>
                        <li><a href="#">PLACEHOLDER 2</a></li>
                        <li><a href="#">PLACEHOLDER 3</a></li>
                    </ul>
                </li>
                <li><a href="#">LINK 1</a></li>
                <li><a href="#">LINK 2</a></li>
                <li><a href="#">LINK 3</a></li>
                <div class="reset"></div>
            </ul>
        </header>
    </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

Is the table scrollable within the tbody tag?

Is it possible to create a table with a scrollbar on the right side using only CSS and without any plugins like jQuery? Additionally, I want the table header to remain fixed in place. What steps do I need to take to achieve this functionality? ...

Is there a way to change an HTML document into a Word file?

I am looking for recommendations on libraries that can help me save HTML documents in memory as Word .DOC files. Any suggestions for both closed and open source options are welcome. Additionally, could someone provide links to these libraries based on the ...

What is the best method to ensure that a span element's width is maximized through CSS

Here is an example to consider: (live demo) Sample HTML: <div> <p> <strong>Stack</strong> <span>Overflow</span> </p> </div> CSS: p { background-color: #aaa; } span { backgr ...

What is the best way to send a POST parameter using JavaScript?

Attempting to submit a variable and its name through a form, I had to change the button type to "button" instead of "submit" for additional validation purposes. Here is the updated button: <button type="button" onclick="subForm()" name="del" id="delet ...

Using HTML and JavaScript to verify email addresses

I have been working on an "Email Validation" code, but it seems to be malfunctioning. I can't figure out why. Would you mind taking a look at it? Thank you. I suspect that the issue lies with this line document.getElementById("custEmail").onchange = ...

Is there a way for me to execute a function multiple times in a continuous manner?

I am attempting to create a blinking box by calling a function within itself. The function I have is as follows: $(document).ready(function(){ $("button").click(function(){ $("#div1").fadeToggle("slow"); }); }); <script src="https://a ...

Hover effect on a single cell in a Bootstrap table

As I was working on creating a calendar design, I found myself pondering how to incorporate a hover effect using Bootstrap 4 for the cells. Here is a preview of the current calendar design I am aiming to achieve a hover effect similar to Bootstrap's ...

Ways to evenly distribute children in a row using CSS

https://i.stack.imgur.com/HmziN.png The image above showcases the desired effect I am aiming for. It is important to note that the width of the container div may vary. The child elements should have consistent spacing between each other as well as the le ...

Is there a method for redirecting my page to a specific href link without triggering a page reload?

This is my HTML code. <a href="http://127.1.1.0:8001/gembead/emstones.html?car=36">Car</a> I am trying to redirect to a specific page with parameters without fully reloading the current page. Is there a way to achieve this task? I believe the ...

Encountering a React JS error while compiling projects

Every time I attempt to compile my project, everything goes smoothly until it reaches the database call, where I encounter this error: ./node_modules/msnodesqlv8/lib/sqlserver.native.js module not found: Error: Can't resolve 'fs' ./node_mo ...

Issue with Bootstrap 4's vertical alignment center in flex layout not functioning as expected

Im using Bootstrap 4 to create a responsive layout with 3 columns that adjust order, width, and horizontal alignment based on screen size. Everything is functioning correctly except for the horizontal center alignment of the images on screens smaller than ...

Animating positional changes with translate percentages in Android's native browser version 4.x

Lately, I've been developing a compact web application that incorporates the well-liked sidebar interaction design. In applying CSS3 animations to shift the sidebar into view, the animation glides smoothly but halts at the correct spot in the native b ...

Utilize AJAX to securely transmit solely $_POST variables to PHP, omitting any HTML responses

Seeking to solely transfer $_POST's variables (variables from the $_POST array) into a PHP file via AJAX call without displaying any success messages like alert('ok, it's fine') or $('blah').text('done it!'). My goal ...

Fizzy beverage with right alignment

Why are the sprites in my #tbr div aligning to the left even though its align is set to right? Any suggestions? Regular text, links, and images seem to be working correctly (aligned to the right with a 20px right margin as intended). HTML: <div id="t ...

Identify all unticked checkboxes using jQuery

Looking for help with checkboxes: <input type="checkbox" name="answer" id="id_1' value="1" /> <input type="checkbox" name="answer" id="id_2' value="2" /> ... <in ...

Insert JavaScript code into the head of the document (including the complete script code)

I am looking to insert a script in the head section of a website so that the target HTML appears as follows: <head><script type="text/javascript">*some code...*</script></head>. This particular script works perfectly: var head = d ...

Navigating to the following webpage with Selenium using Python

url_main = "https://comic.naver.com/webtoon/detail.nhn?titleId=131385&no=292" This particular website features a comment pagination at the bottom of the page. Upon inspecting the page, I noticed that the buttons were structured like this: &l ...

What is the method for accessing a selector within a foreach loop?

Whenever a user clicks on a date in the jquery datepicker, two ajax calls are triggered. The goal is for the second ajax call to populate the response/data into a paragraph with the class spots within the first ajax call, displaying available spots for th ...

Deactivate the button upon submission and reactivate it once the submission is complete

Recently, I've implemented a script that successfully sends data to the server using ajax. However, I now have a new requirement - I need to disable the button during submission and then re-enable it once the process is complete. $(document).ready(fu ...

What is the best way to alter the color of the "checkmark" in an HTML checkbox using CSS?

Is it possible to change the color of the check mark in an HTML checkbox? Currently, on Windows XP using Firefox, Chrome, or IE, the check mark appears green. I am interested in changing the check mark within an INPUT type="checkbox" to be orange. ...