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

Tips for adjusting the font size according to the varying number of characters entered

I am currently facing a challenge with adjusting the font size based on the dynamic number of characters. For example, I have a set of characters with a font-size of 40px, and as more characters are added, the font size should decrease to fit within the av ...

AJAX response for form validation

I need to validate my contact form when the submit button is clicked. If all fields are valid, I want to display a Processing message using AJAX, followed by a success message with the entered name. The content of my Form is: <form onsubmit="return va ...

What is the best way to modify tabulator styles?

Is there a way to update the column header in Tabulator using inline style when trying to avoid ellipsis as the column size is reduced? <VueTabulator ref="tabulator" v-model="receipts" :options="options" style="white ...

Adding a slight 1px right margin between an HTML table and its enclosing div in Bootstrap 3

Help! I'm trying to troubleshoot some HTML issues and can't figure out where this pesky 1px gap between my table and the wrapping div is coming from... Just for reference, I am using Bootstrap. I suspect that it could be a glitch in the Bootst ...

Created a notification for when the user clicks the back button, but now looking to remove it upon form submission

My survey is lengthy and I don't want users to accidentally lose their data by clicking the back button. Currently, I have an alert that warns them about potential data loss, but the alert also pops up when they submit the form. Is there a way to disa ...

What causes the container's height to remain unchanged despite changes in the height of its image content?

When I set the height of an image to 50%, its container's height remains the same instead of automatically adjusting. HTML: <section class="img-show"> <div class="img-container"> <ul> <li cla ...

What is the best way to reset the 3rd dynamic dropdown menu once a new selection is made in the 1st dropdown menu?

I have been working on creating a dynamic dropdown using Jquery ajax and json in js. Almost everything is working as expected, except for one issue that I am facing. The problem arises when I select an option in dropdown A, which then loads items into drop ...

Styling a clock with rounded corners using CSS: Border-radius trick

Attempting to craft a analog 12-hour clock using basic CSS properties, I initiated the process by forming a square div of the size 500px. Applying a border-radius of 250px resulted in an aesthetically pleasing circle design. Following this step, I incorpor ...

Using data from an API, I am implementing JavaScript validation for my dropdown select menu

Using an API, I am able to access information about the city's subway stations through a select option. Currently, I can only display details about one station (Balard). However, I would like to be able to display information about other stations tha ...

Ways to halt a CSS animation when it reaches the screen boundary

I put together this demo: By clicking, a red box falls down. The issue arises when trying to determine the screen size using only CSS. In my demo, I set the box to fall for 1000px regardless of the actual screen height. Here is the keyframe code snippet ...

How to effectively manage multiple stylesheet links in React Native Expo development

Hello, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS/JS along with the basics of React. As a beginner developer, my current project involves creating a Study planner app for myself using React Native ...

What is the technical process behind conducting A/B testing at Optimizely?

I'm currently experimenting with Google Analytics and Content Experiments for A/B testing on my website, but I'm encountering some challenges in making it seamless. To utilize the Google API properly, a few steps need to be taken. Firstly, I nee ...

Setting the height of a Bootstrap radio button

My Bootstrap radio buttons are set to a width of 200px. However, when the text inside the button exceeds this width, it wraps onto two lines leading to changes in button height. How can I ensure that all other buttons displayed scale to the same height? h ...

Disappearing input field in DateTimePicker Bootstrap when blurred

Currently, I am utilizing the Bootstrap DateTimePicker plugin to enable users to select a specific date and time. The plugin functions well with one minor issue - whenever the user clicks outside or loses focus on the calendar box, both the box itself and ...

Is there a way to convert a URL into a clickable link and retrieve the YouTube code embedded within

I have retrieved a string from the database using PHP and I want to echo it. Within this string, there is a YouTube link that I would like to make clickable, as well as extract the YouTube code at the end of the link. How can I achieve this? For example: ...

Using the flexslider to override CSS styles in an <li> element

I have implemented the flexslider by whoothemes on my responsive website to showcase tiles. However, I am facing an issue with setting a specific width for the tiles on various devices. Upon inspecting with Chrome, I see this code: <li style="width: 21 ...

issues arising from a growing css division

I am facing an issue where the tiles on my webpage expand on hover, but some of them are partially covered by neighboring tiles. How can I resolve this problem? Here is the CSS code snippet: .tile { position: absolute; width: 86px; background-color ...

The box-shadow feature seems to be disabled or ineffective when trying to send HTML content via

I am having a unique approach to sending emails using html. To combat the issue of image blocking by email providers, I have resorted to utilizing box-shadow to create the images I require. The strange thing is that it appears perfectly fine in the browser ...

Activate animation while scrolling the page

I am using a progress bar with Bootstrap and HTML. Below is the code snippet: $(".progress-bar").each(function () { var progressBar = $(this); progressBar.animate({ width: progressBar.data('width') + '%' }, 1500); }); <body> & ...

What is the simplest method for finding the position of a child element in relation to its parent?

My HTML markup is structured similarly to this: <div id="parentElement"> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div ...