What is the best way to vertically center the <li> element within the <ul> tag?

Recently, I've been delving into html and css as a way to create my own navbar. It's been quite challenging for me to properly position elements using html, especially when compared to the relative ease of doing so on Android (after some trial and error, of course).


                header {
                    width: 100%;
                    position: fixed;
                    left: 0;
                    top: 0;
                }

                nav {
                    height: 64px;
                    background: black;
                    color: white;
                }

                div#brand {
                    width: 100px;
                    background: gray;
                    vertical-align: middle;
                    margin-left: 2em;
                    float: left;
                }

                div p {
                    display: block;
                    margin: 0;
                    line-height: 64px;
                    text-align: center;
                }

                div#menu {
                    float: right;
                }

                nav ul {
                    padding-left: 0px;
                    margin: 0;
                }

                nav li {
                    height: 100%;
                    display: inline-block;
                    margin: 0 1.25em;
                }

                nav li:hover {
                    background: gray;
                }

                nav li a {
                    display: inline-block;
                    text-decoration: none;
                    color: white;
                    text-align: center;
                }
                

                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <title>My Navbar</title;

                    <!-- include index.css -->
                    <link rel="stylesheet" type="text/css" href="index.css">
                </head>

                <body>
                    <header>
                        <nav>
                            <div id="brand"><p>My Brand</p></div>
                            <div id="menu">
                                <ul>
                                    <li><a href="">LinkedIn</a></li>
                                    <li><a href="">Github</a></li>
                                    <li><a href="">Twitter</a></li>
                                </ul>
                            </div>
                        </nav>
                    </header>
                </body>
                </html>
                

One particular challenge I'm facing is how to vertically center the content or text of the li elements within the navbar. Any suggestions?

Answer №1

integrated some CSS at the bottom of the file, utilizing flex-box for centering elements

header {
    width: 100%;
    position: fixed;
    left: 0;
    top: 0;
}

nav {
    height: 64px;
    background: black;
    color: white;
}

div#brand {
    width: 100px;
    background: gray;
    vertical-align: middle;
    margin-left: 2em;
    float: left;
}
div p {
    display: block;
    margin: 0;
    line-height: 64px;
    text-align: center;
}

div#menu {
    float: right;
}

nav ul {
    padding-left: 0px;
    margin: 0;
}

nav li {
    height: 100%;
    display: inline-block; /* to make list horizontal */
    margin: 0 1.25em;
}

nav li:hover {
    background: gray;
}

nav li a {
    display: inline-block;
    text-decoration: none;
    color: white;
    text-align: center;
}

/* additional lines inserted below */
#menu,
#menu ul {
  height: 100%;
 }
 
 #menu ul li{
  display: inline-flex;
  align-items: center
  }
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Navbar</title>

    <!-- include index.css -->
    <link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
    <header>
        <nav>
            <div id="brand"><p>My Brand</p></div>
            <div id="menu">
                <ul>
                    <li><a href="">LinkedIn</a></li>
                    <li><a href="">Github</a></li>
                    <li><a href="">Twitter</a></li>
                </ul>
            </div>
        </nav>
    </header>    
</body>
</html>

Answer №2

header {
    width: 100%;
    position: fixed;
    left: 0;
    top: 0;
}

nav {
    height: 64px;
    background: black;
    color: white;
}

div#brand {
    width: 100px;
    background: gray;
    vertical-align: middle;
    margin-left: 2em;
    float: left;
}
div p {
    display: block;
    margin: 0;
    line-height: 64px;
    text-align: center;
}

div#menu {
    float: right;
        padding: 22px;
}

nav ul {
    padding-left: 0px;
    margin: 0;
}

nav li {
    height: 100%;
    display: inline-block; /* to make list horizontal */
    margin: 0 1.25em;
}

nav li:hover {
    background: gray;
}

nav li a {
    display: inline-block;
    text-decoration: none;
    color: white;
    text-align: center;
}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mijn navbar</title>

    <!-- include index.css -->
    <link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
    <header>
        <nav>
            <div id="brand"><p>My Brand</p></div>
            <div id="menu">
                <ul>
                    <li><a href="">LinkedIn</a></li>
                    <li><a href="">Github</a></li>
                    <li><a href="">Twitter</a></li>
                </ul>
            </div>
        </nav>
    </header>    
</body>
</html>

added just :

    padding: 22px;

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

jQuery smooth scrolling problem causing a one-second page "blinking" issue

Currently, I have implemented a smooth scrolling effect on my website using the following jQuery code: jQuery('html,body').animate({scrollTop:scrollTarget}, 1000, "swing"); Although the effect works well in general, I have noticed that when mul ...

Using ExpressJS and Jade to submit a form using the POST method and redirecting to a specified path

I am exploring node, express and jade for the first time and working on a small application that requires users to enter their name and password in a form. The app then redirects them to a path based on their username. Here is the code snippet to achieve ...

Looking to align a Pinterest board to the right side of the page

Attempting to align a Pinterest board to float right is proving more challenging than expected. Any styling modifications applied to the span or Pinterest board result in rendering issues. A task that was supposed to take only 2 minutes has now become fr ...

How to use JQuery to retrieve multiple background image URLs from CSS styling

Initially, here is the CSS code that I am working with: background-image: url(/style_elements/img/first.png), url(/style_elements/img/second.png); I am trying to specifically target the second background image URL using jQuery: var item_img_url = item_i ...

The inspection of Microsoft VS Code and Angular 2 tags within an HTML code validator

Recently, I began delving into Angular 2 within VSCode. However, I encountered an issue with the built-in HTML linter not recognizing Angular 2 directives like *ng-if or (click) when working on the "Tour of Heroes" project from Angular.io. Here is a glimps ...

using javascript to change a link's state with a click action

I have a question that is related to the topic discussed here: Making a link stay active displaying hover effect upon click using javascript. I am looking for a way to disable the active class when the same link is clicked again. Any assistance on this mat ...

Unable to modify SVG color to a white hue

I am currently working on an Angular project where I am utilizing SVG to display some icons. These icons originally have a default grey color, but I want them to change to white when hovered over. Interestingly, while changing the color to red, green, or y ...

Retrieve data from a specific page on a URL using AJAX

window.onload= function(){ var page = window.location.hash; if(window.location.hash != ""){ page = page.replace('#page:', ''); getdata('src/'.page); } } Once the window has loaded, I want to check ...

The addClass and removeClass functions seem to be malfunctioning

Hey everyone, this is my first time reaching out for help here. I looked through previous questions but couldn't find anything similar to my issue. I'm currently working on a corporate website using bootstrap3 in Brackets. I've been testing ...

"Embedding Bootstrap, jQuery, and Twitter Bootstrap directly onto

I'm currently working on customizing some source code to fit my needs. To do this, I need to download and use Bootstrap, jQuery, and Bootstrap locally. I have already downloaded all of them to a directory named "libs". However, when I try to run the P ...

The functionality of $(this).data seems to be malfunctioning

I've spent the last couple of hours doing research but still can't seem to figure this out. I'm using ajax to load content, and for some reason $(this).data is not working as expected. If I replace this with the actual class, the content lo ...

Guide on adding font face to mui theme

I have a font-face defined in the index.css file. I am looking to move this configuration from the CSS file to a theme.js file created as part of my MUI library implementation. How can I achieve this? // index.css @font-face { font-family: 'ATTAlec ...

Send the td value to a PHP script with the help of JavaScript

I have an HTML table that displays records from a database. Below is a screenshot of my table There is a button in a TD (i.e column 5,7,9), when I click the button I want to perform a function to display a popup box with an HTML table. Before that, I wa ...

What is the best way to vertically center a column of images on mobile devices in a responsive manner?

Currently, I am developing an application that showcases the 9 newest photos with a specific tag. To ensure consistency in image sizes, I have set each photo to be 240px wide and 200px tall. My query now is how can I vertically center these images so they ...

Creating a Mobile-Friendly Centered Navigation Bar with Scroll Functionality Using Bootstrap 4 Alpha 6

Currently, I am working with the standard Bootstrap navbar that has the class="justify-content-center". My goal is to make this navbar scrollable when the elements do not have enough space. The issue I'm facing is that I can only scroll to the right a ...

Unable to retrieve list using Selenium in C# due to issues with FindElements method

Currently, I am working with C# and NUnit. I have encountered an issue where I need to extract the value from a span element and store it in an ArrayList. This is the code I am using to retrieve the price list: var productprice = driver.FindElements(By. ...

Ways to properly align a navigation bar at the center of the webpage while maintaining the

Whenever I zoom in or out on my page, the homepage button loses its color and fails to expand to the right side. So, I have two main goals: 1) To center the selected area (highlighted by a red border in the image). 2) To extend the green color of the hom ...

Direct your attention towards the bootstrap dropdown feature

I have encountered an issue with using a bootstrap dropdown. I need to focus on the dropdown in order to navigate through its options using the keyboard. However, my current attempt at achieving this using jQuery does not seem to be working when I use $(&a ...

Utilizing HTML in Grails messages.properties for email delivery: A beginner's guide

When working with Grails, I have implemented a GSP template to generate an HTML email using the mail plug-in. Everything functions properly, except for one issue - the GSP template utilizes a parameter that is retrieved from my messages.properties file. I ...

Is there a way to center text of varying lengths vertically and allow it to wrap around a floating image?

Looking to create a div with an image floated to the top left and text of varying lengths. The aim is to have the text centered in the middle if it's shorter than the image or wrap around it if there's enough content. The basic layout is: <d ...