What method do you use to determine the percentage-based width of a container in relation to an element that is not its immediate parent?

My header consists of a logo and navigation with 4 links. I want to align these elements next to each other at the top, which is working fine. However, I am struggling to make them appear at an equal distance from each other.

To position all these elements on top next to each other, I am using float:left. I am also using the CSS property width to ensure that they each occupy 20% of the total page width.

<body>
    <header>
      <a href="./index.html" class="logo"><img src="mylogo.png" style="width:42px;height:42px"></a>
      <nav>
        <a href="./index.html" class="welcome active">Welcome</a>
        <a href="./about.html" class="about">About</a>
        <a href="./artwork.html" class="artwork">Art Work</a>
        <a href="./events.html" class="events">Events</a>
       </nav>
     </header>
     <h1>Title of the page</h1>
</body>
* {
    box-sizing: border-box;
    padding: 0px;
    margin: 0px;
}

header {
    width: 100%;
}

.logo {
    float: left;
    width: 20%;
}

nav {
    float: left;
}

nav a {
    width: 20%;
}

While there is some space between the logo and the links, the links do not align along the top at an equal distance; they seem stuck together. It seems like their width is relative to the nav element, which is not taking up the full 100% due to the presence of the logo. I am unsure how to set the size of these 'a' elements relative to the header, which I have fixed to be 100% of the page's width.

Answer №1

I have come up with a solution for you by tweaking your CSS code and implementing the flex-box technique for alignment instead of using floats. I also changed the header to appear black for better visibility, but this can be adjusted in the CSS file. I hope this solution proves helpful to you.

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

header {
    height: 10vw;
    line-height: 10vw;
    width: 100%;
    background-color: #000;
}

.logo img {
    vertical-align: middle;
}

a {
    display: inline-block;
    height: inherit;
    width: 20%;
    text-align: center;
    line-height: inherit;
    vertical-align: bottom;
    transition: all .33s ease-in-out;
}

a:hover {
    background-color: white;
}
<body>
    <header>
      <a href="./index.html" class="logo"><img src="mylogo.png" style="width:42px;height:42px"></a><!-- --><a href="./index.html" class="welcome active">Welcome</a><!-- --><a href="./about.html" class="about">About</a><!-- --><a href="./artwork.html" class="artwork">Art Work</a><!-- --><a href="./events.html" class="events">Events</a>
       
     </header>
     <h1>Title of the page</h1>
</body>

Answer №2

The CSS solution that I discovered was as follows:

header {
    width: 100%;
    display: inline-block;
}

.logo {
    float: left;
    width: 20%;
}

header nav {
    width: 80%;
    float: left;
}

header nav a {
    width: 20%;
    float: left;
    text-align: center;
}

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

With two divs side by side, the second div is covering part of the first div when viewed on a mobile device

Recently, I encountered an issue with the mobile version of my website while using a modified classic theme for Prestashop 1.7.6. If you'd like to take a look, here is the link to the site. Within the footer section, there are two consecutive divs, e ...

The background refuses to cooperate, soaring upward only to be abruptly sliced in two

I am in the process of creating a website login window with an image background, but I'm experiencing issues where the image is being cut in half and moved up. Here is the code snippet I am currently using: .loginbody { background: url(img/LoginB ...

svg contained within a resizable div

I am trying to insert an svg into the .container div that I created using this code snippet. The goal is to make the svg fit perfectly within the dimensions of the .container div, while also scaling proportionally with the page as it is resized: <html& ...

What is the best way to create transitions for Placeholder and Label elements?

I need help understanding how to animate a label/placeholder transition up and out of its placeholder position into a label state, and back again. Can anyone provide an example or tutorial on this? Here is an example I found: ...

Navigation bar with a divider line along with an accompanying image

How can I position the image behind the navbar without any white line in between them? I've tried adjusting the positioning, z-index, and margin, but nothing seems to work. The website is built with Angular 6 and Bootstrap, and I know it's not th ...

Are there any universal methods for enlarging the size of a checkbox without altering the HTML structure?

Is it possible to adjust the size of a <input type="checkbox"> in a way that works across all browsers without changing the HTML markup? I attempted to modify the height and width using CSS, but encountered issues such as pixelation in Firefox and o ...

What is the best method for placing an element above all other elements on a page, regardless of the layout or styles being used?

I want to create a sticky button that remains fixed on the page regardless of its content and styles. The button should always be displayed on top of other elements, without relying on specific z-index values or pre-existing structures. This solution must ...

What is the best way to target multiple instances of a classname selector in TestCafe?

Having trouble defining a selector in TestCafe to click the YES button shown in this image https://i.sstatic.net/cJ78J.png I am able to locate and click the first Yes button using: .click(Selector('.c-segmented-control__label').withExactTex ...

The Openshift Node.js application is proficient in serving JavaScript files, however, it faces

I am facing an issue with my Node.js application running on Express where I am unable to load the CSS file from the stylesheets directory. Whenever I try to request the css, I receive a 503 error. Surprisingly, all my javascript files load without any issu ...

Tips for effectively drying up sass mixin code using before and after blocks

Here is the SCSS code I currently have: @if $position == bottom { &:after { height: $triangle-width; width: $triangle-width; content:""; position:absolute; margin-top: -$triangle-width/2 -$stroke-width; } } @if ...

Centering an image and text both vertically and horizontally within a container div

I am currently working on creating a series of thumbnail-style divs with fixed dimensions. These divs will contain an image and a title text, both of which need to be centered vertically and horizontally within the container div. While I have successfully ...

Styling Vue JS Components with CSS Binding

I've been trying to apply CSS styling to vuejs tags without success. I'm struggling to get it to work with both regular styling and conditional binding. I've exhausted all the solutions on stackoverflow, but nothing seems to be working for m ...

Issues with the alignment of card-footer in Bootstrap 5 card components

Looking to enhance the website for the electronics store I am employed at, I have encountered a challenge while constructing cards using Bootstrap 5. Initially, the cards varied in height until I managed to resolve the issue by setting the card height to 1 ...

How can I change a textarea to uppercase?

I'm looking to create a textarea that automatically types in all capital letters, regardless of the user's input or whether they are holding down the shift key. I want it to accept any input and convert it to uppercase on the fly without showing ...

Instructions on enabling CSS suggestions in a Django HTML file within Visual Studio Code

Similar clues can be found in html files https://i.sstatic.net/DXQhW.png I am looking to access them in django-html as well. Is there a way to activate this feature? I have installed a vs code extension and enabled emmet support for django-html using th ...

Arranging circles on top of each other creates a dark line along the edges with rounded

I find myself entangled in quite the enigmatic venture. My latest project involves crafting a mouse that doubles as a 'torch / searchlight'. Whenever there is a hover action on any text (inline elements, buttons, and so forth), its color transit ...

Is there a way to showcase a block of Python code using a combination of HTML, CSS, and Javascript to enhance its

On my website, I want to display code blocks similar to how StackOverflow does it. The code block should be properly colored, formatted, and spaced out for better readability. All the code blocks on my site will be in python. def func(A): result = 0 ...

CSS: Experimenting with creating a hover effect that lifts the box upwards

Hey there! I'm currently working on adding a red hover box for my Menu. The issue I am facing is that the hover box is displaying below the menu line. I am attempting to adjust the hover box to move a bit higher or up. You can view the image here: ht ...

jQuery - Enhancing User Experience with Dynamic Screen Updates

Is there a way to update the screen height when resizing or zooming the screen? Whenever I zoom the screen, the arrows break. I'm also curious if the method I'm using to display images on the screen is effective. It's supposed to be a paral ...

Best practice for showcasing an MVC Form with the use of Bootstrap

I am facing an issue with my form layout while trying to capture user input. The use of Bootstrap is creating two columns for text boxes and their labels, but the alignment goes off track with the last EditorFor element. I'm questioning if I am misusi ...