Aligning a list of items vertically inside a div container

Struggling to align inline list elements vertically within a div, but unable to figure out why it's not working. Here is the CSS I have included:

.nav li{
    display: inline;
    display: table-cell;
    vertical-align: middle;
    }
.nav ul {
    vertical-align: middle;
    display: table-cell;
    margin: 0% 6% 0% 0%;
    }

Any help would be greatly appreciated.

https://jsfiddle.net/y1paacbh/

It seems like the issue may not be clear due to how jsfiddle is displaying it. Here is an image from my browser: https://i.sstatic.net/WtCH7.png

Answer №1

To address the problem, simply apply these styles:

  .nav li {
   position: relative;
   top: 8px;  
    }

Answer №2

You have employed two instances of the display property.

Consider using the following code snippet instead:

.nav li {
    display: block;
    vertical-align: middle;
}

Answer №3

To easily update your website's styling, try replacing your current CSS with this code snippet.

.nav {
  background-color: teal;
}

ul li {
  display: inline-block;
}

If my advice was helpful, kindly let me know by upvoting this response. If you encounter any issues, feel free to describe them for further assistance.

Answer №4

I have updated the view for you in a way that meets your vertical requirements

<style>
.nav {
    background-color: green;
    height: 10%;
    width: 100%;
}
.nav li{
    //display: inline;
    //display: table-cell;
    vertical-align: middle;

}
.nav ul {
    vertical-align: middle;
    //display: table-cell;
    margin: 0% 6% 0% 0%;
    list-style-type: none;
}

</style>

https://jsfiddle.net/y1paacbh/1/

Answer №5

It seems like this is the desired outcome you are looking for.

* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
html,
body {
  height: 100%;
}
.nav {
  background-color: green;
  height: 30%; /* for demonstration purposes */
}
.nav li {
  display: table-cell;
  vertical-align: middle;
}
.nav ul {
  display: table;
  height: 100%;
}
<div class="nav">
  <ul>
    <li class="navright">Home</li>
    <li class="navright">About</li>
    <li class="navright">Contact</li>
  </ul>
</div>

JSfiddle Example

Answer №6

body {
  height: 1000px;
}
.nav ul{
  background-color:green;
  height: 10%;
  line-height: 100px;
  width: 100%;
}
.nav li{
  display: inline;
}
<div class="nav">
  <ul>
    <li class="navright">Home</li>
    <li class="navright">About</li>
    <li class="navright">Contact</li>
  </ul>
</div>

Answer №7

I'm not entirely sure if I understood your question correctly, but here's a way to achieve the following: 1) Elements that are vertically centered AND 2) Elements that are horizontally distributed

.nav {
  background-color: green;
  height: 30px;
  width: 100%;
}

.nav li {
  margin-left: auto;
  margin-right: auto;
  width: 33%;
  height: 30px;
  display: table-cell;
  vertical-align: middle;
}

.nav ul {
  list-style-type: none; /*if you want to remove bullets*/
  text-align: center;
  margin: 0 auto;
  padding: 0px;
  display: table;
  overflow: hidden;
  width: 100%;
}

Here is a working Fiddle for reference

Answer №8

If you're looking to tackle this issue, consider utilizing the power of flex. This technique is incredibly simple and requires just two rules on your parent element.

align-items:center;
display:flex;

ul {
  align-items:center;
  display:flex;
  
  background:orange;
  height:100%;
  list-style:none;
  margin:0;
  padding:0;
}

li {
  padding:20px;
}

.nav { height:300px; }
<div class="nav">
    <ul>
        <li class="navright">Home</li>
        <li class="navright">About</li>
        <li class="navright">Contact</li>
    </ul>
</div>

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

What steps should I follow to make a section stay in place on swipepages?

Looking for help to create a sticky section similar to the one on (mobile version). I want to replicate this section with 2 buttons on swipepages.com but I'm not sure about the custom CSS and HTML needed to achieve this. Any assistance would be appre ...

Disappearing White spaces in a Java Swing Label with HTML TagsIn a Java

I'm facing an issue where coloring one character in a string causes most of the whitespaces to disappear. I'm puzzled as to why this is happening and if there's a viable solution? map[9] = "# # ...

In HTML, align the <p> element in the center between two floating <img> elements

Can someone explain why the code provided below is not centering the <p> text and instead placing it around 60-70% of the width? <div> <img src="logo.jpg" style="float: left;"> <p style="text-align: center;"> hello world </p&g ...

Is it possible to still alter options in a read-only Select component in Twitter Bootstrap

Even though I have set my bootstrap select to readonly="true", I am still able to change the selected option. I tried using disabled="true", but then the select is not included when submitted. Is there a way to achieve a combination where the selected op ...

Tips for integrating Angular 4 with SASS

As I embark on my inaugural project utilizing Angular4 and sass, I am grappling with the best approach for working with sass. My setup involves using angular-cli with a default style configuration set to utilize scss syntax. I have also configured the comp ...

Positioning textboxes of varying heights in a vertical alignment, causing one to commence directly below the other

Seeking assistance with creating text boxes for displaying reviews on a website. Each review is contained in a box, with varying heights based on the amount of text. Desired layout is shown in the image below, with black boxes representing review container ...

PHP page showcasing a dynamic list of products

Hello, I am fairly new to this platform. I have a question regarding how to display product data dynamically from a database in one row with 4 different products side by side rather than in one column. <div class="row"> <div class="col-sm-12 co ...

Is there a way to eliminate the bottom border on the active navigation tab?

Here's the information I've gathered: Below is a snippet of code that I have: .nav-tabs { border-bottom: 3px solid #3FA2F7 !important; } .nav-tabs .nav-link { color: #02194A; font-size: 13px; padding: 12.5px !important; } ...

How to display unique content within a div based on individual element hover using AngularJS

As a newcomer to AngularJS, I am facing some challenges while working with directives. Currently, I am working on a navigation menu where my goal is to dynamically display different information within the .services-content section upon hovering over each ...

Utilizing jQuery to Toggle Visibility of Table Rows on Button Click

I have a unique layout on my page where there are two tables positioned side by side. The table on the left consists of buttons with company names, and the table on the right should display employees associated with each specific company. Upon initially l ...

Open the overflow div within a table data cell

My goal is to create a drag-and-drop schedule tool similar to Fullcalendar. I have decided to use a table layout with a rectangular div inside each TD cell to represent tasks. <table onDragEnd={dragend}> <tr> <th>0</th> ...

PHP: A guide on printing a string that includes both single and double quotation marks

Looking to display the following echo ( <script> $(document).ready(function() { $('#example').dataTable( { "bPaginate": false, "order": [[ 3, "asc" ]], "dom": '<"top"lp>', "aoColumnDefs": [ { ...

Trouble with the x-cloak attribute in alpine.js

Experience with TailwindCSS and AlpineJS in my current project has brought to my attention a slight issue with the header dropdowns flashing open momentarily when the login page loads. I attempted to use x-cloak to address this, but encountered some diffic ...

Trouble with Bootstrap 5 Padding Distorting the Aspect Ratio of Image Collection

I am struggling to manage three images in a layout where they span one row and two columns (please refer to the image). While I can make it look perfect at one screen resolution, the ratio of the images becomes distorted when viewed in other screen sizes d ...

precise placement of image inside pop-up

Having trouble positioning an image (an arrow) within a pop-up window. Attempted relative positioning to move the image precisely, but facing difficulties. Decided to go with absolute positioning instead, nesting a div within another div as shown below: ...

Creating Interactive Lookup Field with JQuery and PHP

Recently, I followed a guide on how to develop a lookup field that dynamically searches a MySQL database table - the main objective is to facilitate contact search by postcode. While I managed to get the search functionality working and display the value o ...

When the background image is concealed, the table will have no border

Is there a way to retain the borders inside the table even when the background images are hidden? I am using JS and jQuery to add the picture when the site is loaded. Below is a snippet of my HTML and CSS: <!DOCTYPE html> <html> <head> ...

Tips on how to eliminate focus after choosing a radio button in Angular Material?

This is a snippet from my HTML template file, which includes two Angular Material radio buttons. My goal is to disable the focus on the rings quickly after selecting any radio button. <div id="login-form" class="vh-100 overflow-auto" ...

Concealing Columns in the Beta version of Bootstrap 4

Currently utilizing Bootstrap 4.0 Beta for website design, encountering a roadblock with a responsive-utility feature. In previous Bootstrap 3 versions, it was achieved using classes like: hidden-xs, hidden-md, among others. The addition of -up and -down ...

Is there a mistake in the way I am creating a horizontal navigation bar?

Currently working on creating a simple navigation bar using HTML/CSS for a Java/Spring Boot project. Admittedly, my HTML/CSS skills are quite limited as you can see below. I'm aware that there might be some mistakes in my approach. Thank you in advanc ...