Tips for choosing the parent's parent and applying a width of 100%

I am currently facing some challenges while creating a simple navigation bar from scratch. I am struggling with setting specific widths, especially wanting .navbarDropBtn to align its width with .navbarMain. However, it seems to only match the width of the text. I have experimented with changing the order in which they appear in the HTML, but I require .navbarText to be positioned on top due to the smooth opacity transition I am implementing for gradient transitions.

My main query is how can I set the width to be 100% of a parent's parent element. I prefer not to use fixed pixels (px) as it may lead to issues across different screen sizes, and even setting a specific value such as 120% might pose similar problems.

    .navbarBody {
        width:100%;
        height: 46px;
        position: sticky;
    }
    .navbarButton, .navbarBody {
        background: linear-gradient(0deg, #2a2a2a, #4a4a4a);
        z-index: 100;
    }
    .navbarButton, .navbarMain {
        float:right;
        text-align:center;
        padding: 14px 16px;
        color: #fff;
        position:relative;
    }
    .navbarButton:hover, .navbarMain:hover {
        cursor: pointer;
        color:black;
        transition: .25s;
    }
    .navbarText {
        width:100%;
        height:100%;
        position: relative;
    }
    .navbarButton:hover .navbarText {
        color:black;
        }
    .navbarMain {
        background: linear-gradient(0deg, #296a29, #4aca4a);
    }
    .navbarDropBtn {
        width: 100%;
        height: 100%;
background-color: white; /*example only */
    }
/*hover effect*/
.navbarButton::before, .navbarMain::before {
    content: "";
    opacity:0;
    background: linear-gradient(0deg, #999, #fff);
    top:0px;
    left:0px;
    position: absolute;
    width:100%;
    height:100%;
}
.navbarMain::before {
    background: linear-gradient(0deg, #3a8a3a, #aafaaa);
}
.navbarButton:hover::before, .navbarMain:hover::before {
    opacity:1;
    transition:opacity .25s, content .05s;
}





 <div class="navbarBody">
                <div class="navbarMain" onclick="dropBtn()">
                    <div class="navbarText">
                        <div class="navbarDropBtn">
                Positioned at far right
                        </div>
                    </div>
                </div>
                <div class="navbarButton">
                    <div class="navbarText">
                Test 1
                    </div>
                </div>
                <div class="navbarButton">
                    <div class="navbarText">
                Test 1 1
                    </div>
                </div>
            </div>

Answer №1

The reason for this discrepancy is due to the padding added to navbarMain. If you eliminate the paddings, you'll notice that navbarDropBtn will match the width of navbarMain. Consider adding paddings like this:

 .navbarMain {
     padding: 14px 0;
 }

By implementing this, there will be no paddings on the right and left sides, resulting in navbarDropBtn being as wide as navbarMain.

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

When previewing a card on Twitter's validator tool, the image displays properly. However, when attempting to share the image on

Below is the code that I have written on my HTML page: <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@name" /> <meta name="twitter:title" content="Twitter - image share" /> <meta name="twi ...

Unable to insert menu links into the container

Currently, I have a logo and image slider placed next to each other in a container. However, I am facing difficulty in adding 3 menu links horizontally under the logo. Every time I try to do so, the links end up going above the image slider, causing it to ...

What is the best way to use JavaScript to conceal a section of a form div?

After receiving some code from a certain platform and implementing it to hide part of my form div element, I noticed that the element hides and unhides quickly when running in a browser. This rapid hiding and showing behavior occurs when clicking on the bu ...

Assigning a price to a list of pizza options within a dropdown menu

//The goal is to assign a numerical price to each pizza in the array. We must maintain the structure of the array within the select form. Next, we need to display it in another PHP file, how do we retrieve the values? <fieldset> ...

Maintaining Scene Integrity in THREE.JS: Tips for Handling Window Resizing

My layout includes a div with a scene that looks great initially; however, as soon as I start moving or resizing the window, the scene overflows the boundaries of the div and goes haywire, almost filling the entire window. Are there any techniques or solu ...

How can I exclude specific lines from an XML file using filters?

Let's say I have the following data: <div class="info"><p><b>Orange</b>, <b>One</b>, ... <div class="info"><p><b>Blue</b>, <b>Two</b>, ... <div class="info"><p><b& ...

tips for displaying a label and text side by side on a single line

Is there a way to keep my label and text on the same line by adjusting the CSS? I've tried using float based on suggestions from other posts, but they still end up on different lines. .indentColumn { width: 71px; padding-top: 5%; } .labelColumn ...

How can I showcase the index of `<tr>` and `<td>` elements in a dynamically generated table

How can I print the index of table rows and data on click in javascript? <html> <head> <title>Table Creation</title> <script language="javascript" type="text/javascript"> function createTable() { ...

Troubleshooting the Missing Border Issue in Tailwind Form Fieldset [Code Snippet Included]

I am developing a basic react application with the help of Tailwind css. Scenario 1: Functioning correctly without tailwind: https://codesandbox.io/s/bold-agnesi-y70ue In this example, the tailwind library is not utilized. The simple react app displays ...

JavaScript Error: Unable to determine the value of a null property

Is there a way to validate the user's input in real-time on an HTML form? Take a look at the following HTML code: <div class="col-md-2"> <input class="form-control" name="nbequipement" id="nbequipement" placeholder="" type="text"> ...

I'm looking to customize the background color and font size for an accordion in Bootstrap 4 with Vue.js. Can anyone provide guidance

Hello, I am currently using an accordion with Bootstrap 4 and Vue.js. Here is the code snippet: <div class="faq_accordion" role="tablist"> <b-card no-body class="mb-1"> <b-card-header header-tag=" ...

Transferring an HTML file to a destination stream

Currently, I'm in the process of constructing a proxy server. One of the tasks I'm tackling involves blocking specific URLs. I have developed a simple HTML page that is supposed to display whenever a blocked URL is requested, but unfortunately, ...

Managing input jQuery with special characters such as 'ä', 'ö', 'ü' poses a unique challenge

Hey there, I'm running into a bit of trouble with my code and I can't figure out why it's not working. Here's a brief overview: I created my own auto-suggest feature similar to jQuery UI autosuggest. Unfortunately, I'm unable t ...

Switch Between More and Less Text - Implement Smooth Transition on Shopify Using Javascript

I recently implemented a More/Less toggle button using resources from this website. The functionality is there, but I now want to add a smooth transition effect. When the user clicks on "read more," I would like the hidden content to gradually appear, and ...

Guide on navigating to a specific section on a different page using scrolling

Adding a link for a "read more" button is simple. Just include the href attribute like this: <a href="about.html#post2">readmore</a> In my index.html page, I have implemented Bootstrap 3 with scroll animations for navbar sections. When clicki ...

Insufficient width of images in the bootstrap carousel

Is there a different solution to this problem that hasn't been mentioned in the other posts? I've tried all the top solutions from those posts, but my issue still remains. In particular, the "example" code and "Stock" bootstrap carousel appear t ...

Shade the entire td column in different colors depending on the characteristics of th

I am working with a table and here is the code for it: <table> <thead> <tr> <th style="width: 40%; text-align: center; vertical-align: center;">Nr.<br> crt.</th> <th style="font-weight: bold; wi ...

Horizontal centering using Bootstrap 4 media

How can I horizontally center a media element? Here is the code for my media: <div class="container"> <div class="row"> <div class="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"> <div class="media"> ...

How can I place this ribbon on top of an image in an HTML email to ensure it displays correctly on email clients such as Outlook?

As I delve into my research, it's becoming apparent that achieving this result might not be feasible across all email clients. However, I'm curious to know if there have been any recent developments in the email world that would allow me to repli ...

What specific features does CSS3 offer in terms of background-size properties?

Let's delve into my knowledge: $, like in font-size: 14$; em, as in margin: 2em; What other units are out there? ...