Place anchor tags in the middle of a navigation bar div

Having some trouble aligning the items in my navigation bar. I plan to include some svg icons in the nav bar later on, but for now, I can't seem to get the alignment right. Any ideas?

It's probably a simple fix, but I've tried various methods like vertical align and others, but nothing seems to work as intended.

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

/*
 Navigation
*/

.topnav {
    overflow: hidden;
    background: none !important;
}

.topnav button {
    border: none;
    cursor: pointer;
}

.topnav a {
    color: #0f1;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.floatLeft {
    float: left;
}

.floatRight {
    float: right;
}

.topnav a:hover {
    color: #000;
}

.topnav a.active {
    color: #000;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Grundfos Metrics</title>
    <link href="https://fonts.googleapis.com/css?family=Montserrat:600" rel="stylesheet">
    <link rel="stylesheet" href="./css/site.scss">
    <script type="text/javascript" src="./js/toggletheme.js" defer></script>
</head>
<body>
    <header>
        <div class="topnav">
            <a class="floatLeft"            href="#news"><h1>Website Title</h1></a>
            <a class="floatRight"           href="#contact">Coverage</a>
            <a class="floatRight"           href="#news">Archives</a>
            <a class="active floatRight"    href="#home">Home</a>
        </div>

    </header>
</body>
</html>

Answer №1

To achieve that layout, you can utilize flexbox.

Simply include display: flex; and align-items: center; in the .topnav class.

To align your content to the left or right, use two containers and apply justify-content: space-between; on the .topnav.

This will align the first container to the far left and the second to the far right.

Introducing an additional container will position the middle one in the center! The other two will remain in their original positions.

Applying flex to align the right part will also reverse the order of the elements! Make sure to adjust for this change as well.

I've provided a sample for your navbar, feel free to ask if you need further clarification on my solution.

/*
 Navigation
*/

.topnav {
    overflow: hidden;
    background: none !important;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.topnav button {
    border: none;
    cursor: pointer;
}

.topnav a {
    color: #0f1;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.topnav a:hover {
    color: #000;
}

.topnav a.active {
    color: #000;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Grundfos Metrics</title>
    <link href="https://fonts.googleapis.com/css?family=Montserrat:600" rel="stylesheet"></link>
    <link rel="stylesheet" href="./css/site.scss">
    <script type="text/javascript" src="./js/toggletheme.js" defer></script>
</head>
<body>
    <header>
        <div class="topnav">
          <div class="leftpart">
                      <a href="#news"><h1>Website Title</h1></a>
          </div>
          <div class="rightpart">
                      <a href="#contact">Coverage</a>
              <a href="#news">Archives</a>
              <a class="active" href="#home">Home</a>
          </div>
        </div>

    </header>
</body>
</html>

Answer №2

.topnav {
    overflow: hidden;
    background: none !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.topnav a {
    color: #0f1;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.topnav a h1 {
    margin: 0;
}

.topnav a:hover {
    color: #000;
}

.topnav a.active {
    color: #000;
}
<body>
    <header>
        <div class="topnav">
            <div>
                <a href="#news">
                    <h1>Website Title</h1>
                </a>
            </div>
            <div>
                <a class="active" href="#home">Home</a>
                <a href="#news">Archives</a>
                <a href="#contact">Coverage</a>
            </div>
        </div>
    </header>
</body>

To create a visually appealing layout, use flexbox instead of floats in your CSS. Utilize two separate divs for the logo and navigation links to achieve this desired structure.

Answer №3

For improved styling, consider incorporating the following code into your CSS:

.topnav a {
line-height:17px;
}

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

Looking for an element that includes "== $0" right after the closing tag in HTML can be done using Xpath, CSS, or any other locators in Selenium with Python

a link to an image <div class=""><div>Mumbai, Maharashtra</div></div> Take a look at the above example for guidance. I attempted to use the code below but it failed to produce results: driver.find_element(By.XPATH,'//di ...

Having trouble iterating through the elements with Beautifulsoup as I constantly encounter the error message stating 'NavigableString' object does not have the attribute 'a'

I've been encountering the following error message 'NavigableString' object has no attribute 'a' When attempting it this way def get_vlad(url): html = requests.get(url, headers=headers) soup = Be ...

Sleek and versatile sidebar reaching full height

Is there a way to make my sidebar cover the full height of the screen without using position: fixed? The code I've tried doesn't seem quite right. Any tips? JSFindle: http://jsfiddle.net/Pw4FN/57/ if($(window).height() > $('#page-contai ...

Displaying the contents of a text file line by line and outputting it to an HTML webpage

I have a text file containing PASS/FAIL/TestName data. I am looking to read the data row by row from the text file, and display it on an HTML page using Java. Could you please assist me with this? Best regards, Bhaskar ...

The scroll-to-top arrow remains concealed when the height of the html and body elements is set to 100

I recently added a scroll-to-top arrow using Jquery, and it's functioning flawlessly. However, I encountered an issue when I set body and html to a height of 100%, as it mysteriously disappears. View this fiddle for reference The HTML structure is a ...

jQuery is not active on responsive designs

I have implemented a script in my JavaScript code that changes the color of the navigation bar when scrolling. The navigation bar transitions to a white color as you scroll down. However, I am facing an issue with responsiveness and would like to deactivat ...

`Using a PHP function parameter within an array: An essential guide`

I'm encountering an issue while declaring a function parameter within my array. Here is the simplified version of what I have: function taken_value($value, $table, $row, $desc) { $value = trim($value); $response = array(); if (!$value) ...

Adjust the height of a responsive div to match its adjacent element

Two of my divs are set to specific widths using percentages. I'm looking for a way to make the right div match the height of the left div, which changes based on the dimensions of an image and the browser window size. Is there a method to achieve this ...

What is the best way to horizontally align a floated image with its text positioned at the bottom?

I centered the images inside the "main-content" class, but I'm unsure if I did it correctly. Now, I also want to center the text, but using margin affects the images as well. Apologies for the messy code, I'm just a beginner. Below is the HTML5 ...

Is it possible to iterate through div elements using .each while incorporating .append within an AJAX call?

After sending AJAX requests and receiving HTML with multiple div elements (.card), I am using .append to add new .card elements after each request, creating an infinite scroll effect. However, I am facing issues when trying to use .each to iterate over all ...

Tips for choosing an attribute within a list with a CSS selector and Selenium

Is it possible to use a css selector to target an element within a <li> using Selenium? Below is the code snippet in question: <div id= "popup"> <ul> <li> <div id="item" option= "1" ....> </div> < ...

Bootstrap 4 - Interactive horizontal timeline for optimal viewing on all devices

I'm working on creating a responsive horizontal timeline using Bootstrap 4 for my timeline and project. The code I've developed so far is working well in terms of being responsive. timeline.html <div className="container timeline-container"& ...

Having difficulty getting mask-image to function properly in Safari browser

My table row is styled with the following CSS code: .table-row-mask { mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0)); } <table><tr class="table-row-mask"><td>Test</td></tr></table> This ...

ShadowBox replacement for IE8

After much experimentation, I am on the verge of achieving an inset box shadow for IE8 without relying on JavaScript. Take a look at the screenshot below: https://i.sstatic.net/2kM3R.png Since Internet Explorer versions 5.5 through 8 only support Micros ...

Calculate the dimensions of each list item and dynamically increase the size of the unordered list with jQuery

Trying to extract the 'width' style attribute from li and assign it to ul. The HTML code looks like this: <div id="carousel_team" class="glide"> <ul class="slides" style="opacity: 1; width: 1464px;"> <li class="slide first active ...

Issue with initial width calculation of Slick slider

Having issues with implementing the Slick slider. The calculated width of each slide seems to be incorrect due to padding on the right side of the image. <div class="responsive-slick"> <img src="Gallery/large/01.jpeg"> <img src="Gal ...

How come my image is not aligned to the right of the header in my HTML code?

My attempt to align an image to the right side of my heading using code was unsuccessful. Here is the HTML snippet I used: /* Aligning with CSS */ #colorlib-logo img { display: flex; align-items: center; width: 30px; height: 30px; } #colorli ...

How to automatically scroll to the bottom using jquery/css

I've recently developed a chatroom using li elements, but I'm encountering an issue where the chat doesn't stick to the bottom once it reaches a certain number of messages. I suspect it has something to do with the height settings, but I can ...

A numeric input area that only accepts decimal numbers, with the ability to delete and use the back

I have successfully implemented a code for decimal numbers with only two digits after the decimal point. Now, I am looking to enhance the code in the following ways: Allow users to use backspace and delete keys. Create a dynamic code that does not rely o ...

Are there any other options available in JavaScript to replace the .unload() function?

I have a click event attached to multiple buttons on my page, each loading a different .php file into the same div. The issue arises when the time to load increases after several clicks. $('#start').click(function() { $('#mainbody&apos ...