How to center text both horizontally and vertically in the heart of an HTML/CSS layout

I need to center the text vertically and horizontally in the nav div. Below are both the HTML and CSS code snippets.

Here is the HTML snippet:

<html>
<head>
</head>

<title>
Dom Chronicles
</title>
<body>

<div id="header">

</div>

<div id="space">

</div>

<div id="nav">

<ul>
    <li>Home</li>
    <li>Music</li>
    <li>Blog</li>
    <li>Shop</li>
    <li>Contact</li>
</ul>

</div>

</body>
</html>

Here is the CSS snippet:

@font-face  {
font-family: Mager;
src: url(fonts/ElegantLux-Mager.otf);
 }

#header     {
width: 1000px;
height: 400px;
margin: 0 auto;
background-color: gray;
}

#space  {
width: 1000px;
height: 10px;
}

#nav    {
width: 1000px;
height: 50px;
margin: 0 auto;
background-color: gray;
color: black;
font-size: 25px;
font-family: Mager;
}

#nav li {
list-style: none;
display: inline;
text-align: center;
}

#nav li:hover   {
opacity: .6;
}

Answer â„–1

To style the #nav element, include display: table and text-align: center:

@font-face {
    font-family: Mager;
    src: url(fonts/ElegantLux-Mager.otf);
}
#header {
    width: 1000px;
    height: 400px;
    margin: 0 auto;
    background-color: gray;
}
#space {
    width: 1000px;
    height: 10px;
}
#nav {
    width: 1000px;
    height: 50px;
    margin: 0 auto;
    background-color: gray;
    color: black;
    font-size: 25px;
    font-family: Mager;
    display: table;/*Include display table*/
    text-align: center;/*Include text-align center*/
}
#nav li {
    list-style: none;
    display: inline;
    text-align: center;
    
}
#nav li:hover {
    opacity: .6;
}
<div id="nav">
    <ul>
        <li>Home</li>
        <li>Music</li>
        <li>Blog</li>
        <li>Shop</li>
        <li>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

Creating a dynamic table with CSS: implementing two unique hover colors

Currently, I am working with a table that serves as a check list for a collection. My goal is to find a way to change the hover color of the table to GREEN if it's an item I own, and to red if it's an item I don't own. Since I am a beginne ...

What is the best way to present the new segment?

Having trouble with creating new sections or content after the homepage? The issue is that the new content doesn't appear on a new page but rather on the homepage itself. An example of this problem can be seen on this CodePen project, where an h1 sect ...

It appears that the React MUI Grid is causing the page or its container to experience overflow issues

I'm utilizing Grid to maintain the organization of the entire page, but I keep encountering an overflow issue when adding multiple Grid items. This results in the scrollbar appearing even though the container size remains the same. Dashboard.tsx is pa ...

Tips on how to perfectly position an element in the middle of an HTML

I am struggling to get the textarea element centered using margin: 0 auto. I even attempted to place the element inside a div with margin: 0 auto style. <div style="margin: 0 auto;"> <textarea rows="4" cols"100"></textare> </div& ...

What is the best way to incorporate a Django variable as the width parameter in a style tag within HTML?

I'm attempting to utilize a Django variable as the percentage width value within a style tag in HTML, but it's not functioning correctly. Strangely, I've discovered that using curly braces {} within style tags is prohibited—this contradict ...

Firefox experiencing trouble handling flexbox overflow

Having some trouble with a test project that involves using flexbox. The task at hand is to create a dashboard with multiple lists of cards arranged side-by-side with infinite overflow. I was able to achieve this layout, however, the issue arises when eac ...

What is the best way to bridge the distance between a shrunken iframe and the following element?

https://i.sstatic.net/AZ7T4.png My iframe was resized to 65%, but the containing "div" still occupies the original iframe size, creating a large gap between the iframe and the next element. I'm not sure how to remove this gap. I want the next element ...

I am having trouble getting two similar Javascript codes to function simultaneously

Using a common JavaScript code, I am able to display a div when a certain value is selected. http://jsfiddle.net/FvMYz/ $(function() { $('#craft').change(function(){ $('.colors').hide(); $('#' + $(this ...

Inspecting HTML elements with Capybara for class and ID attributes

Trying to locate an element and use Capybara for validation. There's a specific element that I want to check for using page.should have_css. <i class="ui-grid-icon-cancel" ui-grid-one-bind-aria-label="aria.removeFilter" aria-lab ...

Is there a way to selectively load only the <head> content from AJAX data and update the current <head> element?

While attempting to retrieve the <head> content of data using AJAX and replace it with the current one, I am encountering errors and the code at the bottom is not functioning as expected. $('head').html($(data).find('head:first') ...

Using PHP to narrow down SQL results by date

When it comes to populating an HTML table with data from an SQL table using PHP, I have a specific method in place. Here's how I go about it: <table class="posts"> <?php $respost = mysqli_query($link,"SELECT * FROM table WHE ...

Endlessly triggering document.execCommand, the JavaScript selectionchange-EventListener seems to have a mind of

I recently implemented an event listener for selectionchange in the following manner: document.addEventListener("selectionchange", function() { highlight(); console.log("selectionchange-triggered"); }, false); After that, I included the code bel ...

Display the item with jQuery once the CSS generated by jQuery has finished loading

Is it possible to delay the visibility of an object until my script finishes loading? I tried using ajaxcomplete() and ajaxSuccess() without success. Check out the code snippet below for an example. There's an image with the id: imagefocus. Whenever ...

FadeOut Television static on Canvas after a period of inactivity

Is there a way to smoothly fade out the TV noise after a specific timeout period? I found this code snippet on Stack Overflow that seems to address a similar issue: var canvas = document.getElementById('canvas'), ctx = canvas.getContext( ...

Retrieve the dimensions of the image in degrees from the component

I need to retrieve the dimensions of an image I uploaded along with its base64 code. Below is the code snippet I am using: Image img = new Image(); img.src = Selectedimage.src; Img.onload = function { this.width = img.width; this.height = img.height; } T ...

Developing an interactive web interface with HTML

I've been attempting to design a flexible HTML page that replicates the layout of a Java applet clock, but I'm unsure if my current approach is correct. Below is an example of the three-clock image set that I am currently working on. My method i ...

A guide on incorporating a set of components to be utilized as custom HTML elements in Angular

I am in the process of revamping my application to be more modular on the UI side, with a focus on separating different elements including: App header Left navigation panel Main content on the right side of the nav panel I have successfully figured out ...

Progress Indicators Do Not Move

I have a collection of progress bars that I want to animate. They work fine with maxcdn, but not with local bootstrap references. Can someone please help me figure out what's causing this issue? .resume { width: 816px; margin: 48px 48px 48p ...

Remove an item from an Ionic list

I am currently in the process of developing an Ionic to-do list application. I am facing an issue with the delete function where the delete button is not working as expected. When the button is clicked, nothing happens. HTML: <ion-view view-title="To ...

Dropdown feature in the side navigation bar

Is it possible to create a drop-down section in a navigation bar using HTML/CSS/JS? For example, clicking on 'products' would reveal a list of products that disappears when clicked again. If this is achievable, how can it be done? I am currently ...