Utilize the entire width for header elements

I have implemented four header elements: home, about, portfolio, and contact. I want these elements to stretch across the entire navigation bar with each taking up 25% of the space. How can I achieve this? Additionally, I specified that the home element should always be displayed in red, but it appears wider than the navigation bar itself, which I would like to address as well.

Code:

body {
    background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url('tape-measure.jpg');
    background-size: cover;
    height: 1000px;
    color: #000305;
    font-size: 100%;
    font-family: 'Coming Soon', 'Lucida Sans Unicode', cursive;
    line-height: 1.5;
}

a {
    text-decoration: none;
}

a:link, a:visited {
    color: #CF5C3F;
}

a:hover, a:active {
    background-color: #CF5C3F;
    color: #fff;
}

.mainHeader {
    width: 90%;
    margin: 0 auto;
}


.mainHeader img.Logo {
    position: absolute;
    right: 5%;
    top: 54%;
    width: 20%;
    height: auto;
}

.mainHeader img.Margrit {
    position: absolute;
    right: 5%;
    top: 25%;
    width: 20%;
    height: auto;
}


.mainHeader nav {
    width: 100%;
    background-color: #9cb34f;
    height: 20px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
}

.mainHeader nav ul {
    list-style: none;
}

.mainHeader nav ul li {
    text-align: center;
    display: inline-block;
    
}


.mainHeader nav a:link, .mainHeader nav a:visited {
    color: #fff;
    
    
}

.mainHeader nav a:hover, .mainHeader nav a:active,
.mainHeader nav .active a:link, mainHeader nav .active a:visited {
    background-color: #CF5C3F;
    text-shadow: none;
    
}


.mainHeader nav ul li a {
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    
    
}


.mainHeader p {
    
}


.mainHeader p.inWork {
    position: absolute;
    top: 45%;
    left: 5%;
    font-size: 150%;
}



.mainFooter {
    position: absolute;
    bottom: 3%;
    width: 90%;
    left: 5%;
    right: 5%;
    height: 20px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    background-color: #9cb34f;
display: table;

}


.mainFooter p {
    
    color: #fff;
    display: table-cell;
    vertical-align: middle;
}
<!DOCTYPE html>
<html lang="de">
    <head>
<title>Couture Anni</title>
<meta charset="utf-8">

        <link rel="stylesheet" type="text/css" href="style.css">
        <link href="https://fonts.googleapis.com/css?family=Coming+Soon" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
<header class="mainHeader">
<img class="Logo" src="Content_variation_800_e.png" alt="Logo">
<img class="Margrit" src="IMG_5347_small.jpg" alt="Annamaria Hofstetter">

<nav>

<ul>
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Portfolio</a></li>
                    <li><a href="#">Kontakt</a></li>
                </ul>

    </nav>


<p class="inWork"><strong>This page is under<br>construction.<br> Coming soon!</strong></p>
</header>


        
        <footer class="mainFooter">
  
    <p>Copyright © <a href="#" title="couture-anni">couture-anni.ch</a></p>
  
</footer>
        
    </body>


</html>

This is the current appearance of the design:

https://i.sstatic.net/PzqL7.png

Answer №1

Changes have been made in the CSS below. You can further customize these to adjust the header width and color scheme on the link.

  1. .mainHeader width has been changed to 100%.

    .mainHeader {
        width: 100%;
        margin: 0 auto;
    }

  2. The width has been adjusted to 23%. As mentioned in Gabriel's answer, this 22% or 23% is due to margins or paddings applied, causing them to move to the next line at 25% width.

    .mainHeader nav ul li {
        text-align: center;
        display: inline-block;
        width:23%;
      }

  3. The code snippet below has been modified

    .mainHeader nav a:hover, .mainHeader nav a:active, 
    .mainHeader nav .active a:link, mainHeader nav .active a:visited 
    {
      background-color: #CF5C3F;
      text-shadow: none;
    }
    

To eliminate the default red box displayed over an active link.

.mainHeader nav a:hover, mainHeader nav .active a:visited {
  background-color: #CF5C3F;
  text-shadow: none;
 }

EDIT: To center all 4 elements together, follow these steps.

.mainHeader nav ul {
    list-style: none;text-align: center;
}

.mainHeader nav ul li {

    display: inline-block;
    width:12%;
}

body {
    background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url('tape-measure.jpg');
    background-size: cover;
    height: 1000px;
    color: #000305;
    font-size: 100%;
    font-family: 'Coming Soon', 'Lucida Sans Unicode', cursive;
    line-height: 1.5;
}

a {
    text-decoration: none;
}

a:link, a:visited {
    color: #CF5C3F;
}

a:hover, a:active {
    background-color: #CF5C3F;
    color: #fff;
}

.mainHeader {
    width: 100%;
    margin: 0 auto;
}


.mainHeader img.Logo {
    position: absolute;
    right: 5%;
    top: 54%;
    width: 20%;
    height: auto;
}

.mainHeader img.Margrit {
    position: absolute;
    right: 5%;
    top: 25%;
    width: 20%;
    height: auto;
}


.mainHeader nav {
    width: 100%;
    background-color: #9cb34f;
    height: 20px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
}

.mainHeader nav ul {
    list-style: none;
}

.mainHeader nav ul li {
    text-align: center;
    display: inline-block;
    border:1px solid red;
    width:23%;
}


.mainHeader nav a:link, .mainHeader nav a:visited {
    color: #fff;
    
    
}

.mainHeader nav a:hover, mainHeader nav .active a:visited {
    background-color: #CF5C3F;
    text-shadow: none;
    
}


.mainHeader nav ul li a {
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    
    
}


.mainHeader p {
    
}


.mainHeader p.inBearbeitung {
    position: absolute;
    top: 45%;
    left: 5%;
    font-size: 150%;
}



.mainFooter {
    position: absolute;
    bottom: 3%;
    width: 90%;
    left: 5%;
    right: 5%;
    height: 20px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    background-color: #9cb34f;
display: table;

}


.mainFooter p {
    
    color: #fff;
    display: table-cell;
    vertical-align: middle;
}
<!DOCTYPE html>
<html lang="de">
    <head>
<title>Couture Anni</title>
<meta charset="utf-8">

        <link rel="stylesheet" type="text/css" href="style.css">
        <link href="https://fonts.googleapis.com/css?family=Coming+Soon" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
<header class="mainHeader">
<img class="Logo" src="Content_variation_800_e.png" alt="Logo">
<img class="Margrit" src="IMG_5347_small.jpg" alt="Annamaria Hofstetter">

<nav>

<ul>
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Portfolio</a></li>
                    <li><a href="#">Kontakt</a></li>
                </ul>

    </nav>


<p class="inBearbeitung"><strong>Diese Seite ist in<br>Bearbeitung.<br> Bis demnächst!</strong></p>
</header>


        
        <footer class="mainFooter">
  
    <p>Copyright © <a href="#" title="couture-anni">couture-anni.ch</a></p>
  
</footer>
        
    </body>


</html>

https://i.sstatic.net/Q2DMI.jpg

Answer №2

Follow these guidelines to steer you in the right direction:

.mainHeader nav ul li {
  width: 25%;
}

.mainHeader nav ul li a {
  display: block;
}

NOTE: Consider adjusting the spacing between the menu items by setting a percentage like 22% for a cohesive look.

Trust this advice proves beneficial!

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

I am experiencing difficulty closing my Bootstrap 5 nav bar once it has been clicked in mobile view

Having an issue with my Bootstrap 5 navbar in mobile view - it can be clicked to open, but won't close when clicked again. Currently using Bootstrap v5.0. If anyone has insight into why this might be happening, please let me know! <link href ...

Error: The value being evaluated in document.getElementById(x).style is not an object and is not supported

Desired Outcome for my Javascript: I am working with a div that includes an "onmouseover='getPosition(x)'" attribute which can be dynamically added and removed through my javascript by clicking a specific button. The function 'getPosition() ...

Adding flair to a fresh element and then removing it upon its inception

I'm working with a JavaScript code that creates a new element when a button is clicked. I have a question about it. Here's the JavaScript code snippet: var comment = document.querySelector("#AddComment"); var req = new XMLHttpRequest(); if(comm ...

Receiving an inaccurate value from the input with type='number' attribute

There is an input field where users can enter a string, and I need to capture the value entered into it. $("#test").on("change", function(){ console.log($(this).val()); }) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery ...

The HR tag does not display anything following the mailing

Struggling to design a newsletter template with different lines for each product using the HR tag. However, none of my attempts with divs, tables, or CSS properties have been successful. The template looks fine in Chrome, but when sent to the provider, th ...

Ways to emphasize the four edges of a table cell using border-collapse collapse

I am trying to emphasize the borders of cells with the class active. The challenge arises from the fact that the table's border-collapse property is set to collapse, causing the top and left borders of cells (except for the leftmost and top row cells ...

Different methods to retrieve content within a div that is located on a separate, included page

It's a bit tricky to come up with a title for this topic, but essentially, I'm trying to figure out how to get content from an included page into a specific div using HTML and PHP. Let me break it down: In the header.php file, we have the follo ...

Is there a way to detect if JavaScript is disabled using a unique CSS selector?

Does anyone know of a CSS selector that can be used when JavaScript is disabled? I'm not referring to the noscript tag, but specifically in CSS. ...

Split a column into two at the md breakpoint with Bootstrap

I am looking to rearrange the cards in a specific order on breakpoint -md. Please refer to the image below for reference. https://i.sstatic.net/UXABN.png The goal is clarified in the image above. I would prefer if this can be achieved using bootstrap and ...

Submitting values in URI through an HTML form

Hey there, I'm really in need of some assistance with this issue that's been driving me crazy... :) So, I'm working on a project using Symfony2 and AngularJS: <form class="navbar-form navbar-right" role="form" action="" method="post"> ...

Issue with Vuetify carousel not resizing to fit the entire width and height

I am looking to create a carousel that spans the entire width and height of a viewport in my Vue.js 2 project using Vuetify. Below is the code I have so far: <head> <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500, ...

Responsive design is achieved through the use of CSS media queries targeting the

Could someone please clarify the meaning of the following value? Does it indicate that the output device must support exactly 256 colors, or is it acceptable for the output device to have 256 colors or fewer, or does the device need to support 256 colors ...

Tips for adjusting font size automatically to fit within its parent container every time

On my blog, the post titles are displayed in a div with a video playing in the background. The length of the title varies, ranging from 2 words to over 20 words. When the title is too long, it may overflow from its parent container where the video is playi ...

Is there a way to determine the orientation of an image in React-Native, whether it is horizontal or vertical

When working with react-native, I aim to utilize the 'contain' feature for vertical images and the 'stretch' feature for horizontal images. What would be the best way to determine the orientation of an image as either horizontal or vert ...

I'm having trouble with my code - I suspect the issue might be related to the header() function

When I execute the following code, it resets the form and keeps me on the same page. I am aiming for it to redirect to home.php after a successful login. I believe there is an issue with my header() I have experimented with different approaches and have ...

Looking for straightforward tips on parsing CSS code

Seeking advice on how to extract rules and property/values from a .css file or <style></style>. I am not in need of an elaborate parser, as the validity of selector text, property name, or value is not important. My main focus is ensuring that ...

OutOfMemoryError caused by Bitmap

I'm experiencing an issue with this error. I created a favicon parser from URLs using the following code: public class FavIconParser { public static String extractUrl(String url) { StringBuffer sb = new StringBuffer(); Pattern p = Pattern.co ...

Using jQuery to dynamically insert variables into CSS styles

I am attempting to use jQuery 3 to modify the CSS of a class and change the background color. My first step is converting an rgba color code (properties.color) to a hexadecimal code, which is functioning correctly. Next, I try to change the background co ...

Adjusting the size of <nav> element to accommodate its child elements

I've exhausted all possible CSS solutions today in an attempt to make my parent nav tag home-main-nav-menu resize based on its children and grandchildren, but it just won't cooperate. If anyone could provide a clear explanation on how to solve th ...

Ways to display the ChaptersButton in Videojs-Player

I'm trying to incorporate videojs (version 8.10.0) into my project and provide viewers with a way to select chapters within the video. According to the official documentation, it seems that simply including a track element linking to a VTT file within ...