Centered vertical border separating header sections

Could I easily create a vertical border in the center of my header to separate my logo/h1-text from my navbar?

I just started learning html and css last week, so forgive the messy code. I think I could clean it up a bit.

        header {
            height: 60px;
            width: 85%;
            /background-color: white;
            margin: 0 auto;
            /box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
            /border: 2px solid #333;
            left: 0;
            right: 0;
            /border-sizing: border-box;
            /-moz-box-sizing: border-box;
            /-webkit-box-sizing: border-box;
            z-index: 999999;
        }


        header #kage > *, header li {
            display: inline-block;
        }

        header li {
            padding: 0 8px 0 8px;
            float:left;
        }

        #kage {
            width: 99%;
            margin: 0 auto;
            height: 100%;
            line-height: 59px;
            text-align: center;
        }

        .button1:hover {
            background-color: #f2f2f2;
        }
        .button2:hover {
            background-color: #f2f2f2;
        }
        ... (more CSS)
    
    <header>
      <div id="kage">
      <a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
        <nav id="topnav">
            <ul class="menu">
                <a href="index.html"><li class="button1 active">Home</li></a>
                <a href="profil.html"><li class="button2">About</li></a>
                <a href="mdu.html"><li class="button3">MDU</li></a>
            </ul>
        </nav>
      </div>
    </header>

It looks like running the snippet in full screen is necessary to ensure the header doesn't break into two lines.

Answer №1

If you're looking to achieve this effect, there are several approaches you can take. One simple method is to apply a border-left style to your #topnav element.

#topnav {
            height: 100%;
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
            border-left: 2px solid rgb(0,0,0);
            margin: 0 0 0 40px;
        }

By adding a 2-pixel black border on the left side of your top nav and adjusting it with a left margin of 40 pixels, you can achieve the desired look.

        header {
            height: 60px;
            width: 85%;
            /background-color: white;
            margin: 0 auto;
            /box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
            /border: 2px solid #333;
            left: 0;
            right: 0;
            /border-sizing: border-box;
            /-moz-box-sizing: border-box;
            /-webkit-box-sizing: border-box;
            z-index: 999999;
        }


        header #kage > *, header li {
            display: inline-block;
        }

        header li {
            padding: 0 8px 0 8px;
            float:left;
        }

        #kage {
            width: 99%;
            margin: 0 auto;
            height: 100%;
            line-height: 59px;
            text-align: center;
        }

        .button1:hover {
            background-color: #f2f2f2;
        }
        .button2:hover {
            background-color: #f2f2f2;
        }
        .button3:hover {
            background-color: #f2f2f2;
        }

        .active {
            text-decoration: underline;
        }

        header a {
            text-decoration: none;
            color: #333;
            position: relative;
        }

        header h1 {
            margin: 0;
            float: left;
            height: 100%;
            text-shadow: 1px 2px lightgrey;
        }

        header h1:hover {
            color: #f2f2f2;
        }

        #topnav {
            height: 100%;
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
            border-left: 2px solid rgb(0,0,0);
            margin: 0 0 0 40px;
        }

        header ul {
            list-style-type: none;
            margin: 0;
        }
    <header>
      <div id="kage">
      <a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
        <nav id="topnav">
            <ul class="menu">
                <a href="index.html"><li class="button1 active">Home</li></a>
                <a href="profil.html"><li class="button2">About</li></a>
                <a href="mdu.html"><li class="button3">MDU</li></a>
            </ul>
        </nav>
      </div>
    </header>

Answer №2

If you're aiming for a specific layout, there's a way to simplify your HTML and CSS without sacrificing the design you want.

Start by using the header element as the parent container for your logo and navigation links.

Inside the header, create two child elements - h1 and nav, both set as inline-block elements.

To center the child elements within the header, adjust the positioning with text-align: center.

In case the header wraps on smaller screens, consider using white-space: nowrap to prevent this (based on your design preference).

You can fine-tune the spacing between links by adjusting margin, padding, and border values.

For the nav element, applying vertical-align: top is recommended to eliminate excess whitespace above or below the baseline.

Remember, if you specify a height for the header, ensure to also set a matching line-height for vertically centered inline child elements (if desired).

header {
  height: 60px;
  line-height: 60px;
  width: 85%;
  margin: 0 auto;
  border: 1px dotted blue; /* for demo only */
  text-align: center; /* optional? */
  white-space: nowrap; /* to prevent line from wrapping, optional */
}

header h1 {
  display: inline-block;
  border-right: 1px solid black;
  margin: 0;
  padding-left: 10px; /* adjust to taste */
  padding-right: 10px; /* add whitespace before vertical border */
  height: 100%;
}
header h1 a {
  text-shadow: 1px 2px lightgrey;
}
header h1:hover a {
  color: #f2f2f2;
}

header a {
  text-decoration: none;
  color: #333;
}
.button:hover {
  background-color: #f2f2f2;
}
.active {
  text-decoration: underline;
}
nav {
  display: inline-block;
  vertical-align: top;
  padding-left: 10px; /* whitespace after vertical border */
  font-weight: bold;
  font-size: 1.3em;
  border: 1px dotted gray; // for demo only 
}
nav a {
  margin: 0 20px;
}
<header>
  <h1><a href="index.html">H1 TEXT HEREEEEE</a></h1>
  <nav>
    <a href="index.html" class="button active">Home</a>
    <a href="profil.html" class="button">About</a>
    <a href="mdu.html" class="button">MDU</a>
  </nav>
</header>

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

Comparison of valueChanges between ReactiveForms in the dom and component级主动形

Is there a method to determine if the change in valueChanges for a FormControl was initiated by the dom or the component itself? I have a scenario where I need to execute stuff() when the user modifies the value, but I want to avoid executing it if the v ...

There seems to be an infinite amount of padding between the menu bar and content space on the right side

I am having trouble aligning the menu bar and content side spaces. The issue is that the right side of the menu bar and content space doesn't have any side space. I would like to add some space to them similar to the left-hand side. Below, you'll ...

Abandoned <li> and </li> elements

I've come across some HTML code where orphaned <li> and </li> tags are surrounding certain paragraphs without any corresponding opening or closing <ul> or <ol> tags. Is there a way to efficiently parse or mass find/replace ove ...

Scroll the page downward once the PHP script has loaded the content

Is it possible to create a script that automatically slides down content once the entire page has finished loading? Here's an example: Imagine clicking on a link such as index.php?category=1. After the page loads, you want it to smoothly slide down t ...

use javascript to color the image based on a specified number or percentage

Looking for a method to dynamically fill an image based on a percentage. Is there a dynamic approach or is it simpler to create images at specific intervals? I am working with Angular and need to set the percentage of a 1 to 5 rating system from AJAX. h ...

How to change the color of a child element using CSS inheritance

I am struggling with a css issue and need some help. .red { color: red !important; } .yellow { color: yellow; } In the context of my html document, I have the following structure: <div class="red"> red <span class=" ...

Conceal all elements until a search query is entered using the search bar

I want to create a search bar that hides all elements until the user actually searches for them, you can check out my JSfiddle for reference: `JSfiddle Link <div id="search"> <form> <input type="text" name="search" id="m ...

The jQuery animation concludes before its anticipated completion

I'm currently facing a small issue with a jQuery animation. The HTML code I have is as follows: <div id="menu"> <a id="menu-about" href="/">About...</a><br /> <a id="menu-ask" href="/">Ask me a question</a> ...

React Transition for Mui Menu

I'm having trouble implementing a Mui menu on my website and adding a transition effect from the right side. Here is the code I have tried: <Menu transitionDuration={1} open={Open} onClose={Close} MenuListProps={} className="nav"> ...

Using CSS transform to enhance Flash content

I am currently working on a web application that is contained within an iframe. The iframe has been scaled using -webkit-transform:scale(0.8), which is functioning well. However, I am encountering an issue with the flash audio recorder within the iframe no ...

Display a div on page scroll using jQuery

Check out this snippet of HTML code I have: HTML <div id="box1"></div> <div id="box2"></div> <div id="box3"></div> <div id="box4"></div> <div id="box5"></div> And here's a segment of C ...

Revise the color of the selected image

click here for image description I'd like the area's color to change when the mouse hovers over it. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA ...

Get rid of the gap below the line following the application of ::first-letter

Trying to create an information page with a question and its answer. Applied indentation and increased the size of the first letter in the paragraph. The issue is the extra space below the first line due to the ::before-letter pseudo-selector. Any ideas or ...

I wish to simply click on a browser link without the need to open a new tab

Is there a way to programmatically open a link or all links on a website in the same browser without manual clicks or keyboard commands? Essentially, I am looking to automate the process of opening a specific link within a website without the need for an ...

Intersecting interactions: Mouse events on flex boxes

I am facing a challenge in my React application where I have two panels with buttons displayed using flex properties. Below is a simplified version of the HTML/SCSS code that illustrates the setup: <div class="bottom-panel left"> <butt ...

How can I utilize a controller's method within a custom directive in AngularJS?

Within the javascript file, the code has been structured as follows: angular.module("amodule") .directive("bdirective",function(){ return{ scope: { testIsolated: '&test', }, restrict:'EA', repl ...

What might be the reason behind Chrome occasionally not updating the page when I resize the browser window?

Currently, I am in the process of developing a responsive design site prototype. Everything is going smoothly except for one peculiar issue that only seems to occur in Chrome. Sometimes, when expanding the window, the browser appears to get stuck between s ...

Having trouble getting the jQuery script to properly check file size in an HTML form before uploading?

I've created an HTML form with a jQuery script to verify the file size when the SAVE button is clicked. Despite my efforts, the check doesn't seem to be functioning properly in the HTML Form. Please assist me with this and thank you in advance ...

Auto-fill feature malfunctioning

Currently, I have implemented jQuery Autocomplete to retrieve data from a database and display it in the search box. Everything is working perfectly fine, except for one issue - when hovering over the results in the dropdown, instead of displaying the resu ...

Ways to implement a space above the navigation bar in Bootstrap 4 to accommodate text or buttons

I am trying to add a greeting message above the navigation bar, like: Welcome Sarah! Take a look at the image below to see what I currently have: https://i.sstatic.net/4v2Fd.png As shown in the image, I want to display "Welcome Sarah" above the Login | ...