The alignment of content in the <a> tag is not centered within a <ul> list item (horizontal menu)

I am new to web development and have recently started creating my first webpage using HTML and CSS. I'm still learning and don't have a strong understanding yet.

One issue I'm facing is with centering elements on the page. I have tried using the CSS property text-align: center;, but it doesn't seem to be working as expected.

Another challenge I encountered is trying to expand the clickable area of a link to cover the entire button. I attempted to set the width: 100%; property, but it didn't yield the desired results.

Below is an excerpt of my code:

li.hMenu {
    display: inline;
    background: red;
    width: 33.33%; /*for older browsers fallback */
    width: calc(100% / 3);
    float: left;
}

li.hMenu:hover{
    background-color: darkred;
}

li.hMenu:active{
    position: relative;
    left: 1px;
    top: 1px;
    background-color: darkred;
}

#menu {
    width: 100%;
    float: left;
    margin: 0;
    padding: 0;
    list-style-type: none;
}

li a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="CommonLayout.css" />
    <script src="ProjectPage.js"></script>
</head>
<body>
    <ul id="menu">
        <li style="z-index: 10" class="hMenu">
            <a href="ProjectPage.html">Main Page</a>
        </li>
        <li style="z-index: 9" class="hMenu">
            <a href="About.html">About</a>
        </li>
        <li style="z-index: 8" class="hMenu">
            <a href="Calculator.html">Calculator</a>
        </li>
    </ul>

    <p>This is <em>Main Page</em> page.</p>

</body>
</html>

Answer №1

To ensure your anchor element takes up the full width of its containing box, you simply need to set its display property to block:

li a {
    display: block;

By doing this, it will expand to fill 100% of the width and your text alignment styles will be applied as intended.

li.hMenu {
    display: inline;
    background: red;
    width: 33.33%; /*for older browsers */
    width: calc(100% / 3);
    float: left;
}

li.hMenu:hover{
    background-color: darkred;
}

li.hMenu:active{
    position: relative;
    left: 1px;
    top: 1px;
    background-color: darkred;
}

#menu {
    width: 100%;
    float: left;
    margin: 0;
    padding: 0;
    list-style-type: none;
}

li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="CommonLayout.css" />
    <script src="ProjectPage.js"></script>
</head>
<body>
    <ul id="menu">
        <li style="z-index: 10" class="hMenu">
            <a href="ProjectPage.html">Main Page</a>
        </li>
        <li style="z-index: 9" class="hMenu">
            <a href="About.html">About</a>
        </li>
        <li style="z-index: 8" class="hMenu">
            <a href="Calculator.html">Calculator</a>
        </li>
    </ul>

    <p>This is <em>Main Page</em> page.</p>

</body>
</html>

Answer №2

It is necessary to modify the following code snippet:

li.hMenu {
text-align: center;
display: inline;
background: red;
width: 33.33%;
width: calc(100% / 3);
float: left; }

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

Acquiring the underlying code of websites within the local network

In the tool I am creating, it takes a URL as a parameter and retrieves the source code from that URL for analysis. The tool will be hosted on our domain, with the capability of analyzing web pages of sites within our internal network domain. Here is the Jq ...

How can I update the content of a span in Angular 2 by clicking a div, and then change the text above it when the span is filled?

As a newcomer to this community with basic English proficiency, I hope to communicate effectively. My journey into learning Angular 2 has led me to a specific question: I've set up a keyboard with four spans above it, each intended to hold a digit th ...

navigation tabs and vertical navigation not functioning

I have been attempting to create a vertical menu with nav pills using the nav stacked class. However, I am encountering an issue where the menu remains horizontal instead of stacking vertically. Although I have come across alternative solutions that do not ...

The system could not identify 'react-scripts' as a valid command, either internal or external, and the JSON file has been deleted

The error message reads 'react-scripts' is not recognized as an internal or external command, operable program or batch file. I have attempted the following: npm install npm install react-scripts --save npm i -g react-scripts https://github.com/ ...

Experience the magic of Materialize CSS SideNav

I'm currently attempting to implement the mobile side navigation provided by Materialize. I've managed to display the menu icon, but when I click on it, nothing happens. It seems like the function is not being triggered, and there are no errors s ...

Update the image source when hovering over the parent element

Check out this snippet of HTML code: <div class="custom text-center threeBox ofsted"> <a class="ofsted" title="ofsted report" href="http://reports.ofsted.gov.uk/"> <img class="text-center ofstedLogo" src="images/ofsted_good_transparen ...

Why isn't my function being triggered by the Click event?

I can't figure out why the click event isn't triggering the btn() function. function btn() { var radio = document.getElementsByTagName("input"); for (var i = 0; i > radio.length; i++){ if (radio[i].checked){ alert(radio[i].value); } ...

Redirecting mobile devices to a specific URL using htaccess

I have set up a duplicate of my mobile website at m.website.com that contains mobile-friendly content. However, there are still some desktop pages for which I have not created a mobile version, and I do not want to redirect them to the mobile site. Is the ...

What is the most efficient way to organize an array by date?

Here is the data I have: const data = [{date: "2022-05-10 13:36:00", open: 155.535, low: 155.4, high: 155.67, close: 155.44}, {date: "2022-05-10 13:35:00", open: 155.23, low: 155.2102, high: 155.62, close: 155.53}, {date: "2022-05 ...

adjust the value of an attribute in a dynamic stylesheet using jquery

I'm trying to create a dynamic stylesheet using a combination of jquery, css, php, and html. My approach involves making an ajax request (using jquery) to pass the width of my screen to a php css file. However, I am facing an issue where the php css f ...

Issue with dropdown menu appearing beneath specific elements in Internet Explorer versions 7 and 8

I've been encountering some troublesome issues with a website I'm working on, specifically in Internet Explorer 7/8. On certain pages, the navigation elements are getting hidden below text and images, causing confusion for me. I've attempted ...

What is the method for determining the number of unique tags on a webpage using JavaScript?

Does anyone have a method for determining the number of unique tags present on a page? For example, counting html, body, div, td as separate tags would result in a total of 4 unique tags. ...

Place checkboxes adjacent to newly added choices

Currently, my HTML code looks like this: <label name="diet" for="">Diet:</label> @foreach($diets as $diet) <p> {{ $diet->titel }} </p> <input type="checkbox" id="diet[]" for="diet[]" name="diet[]" value="{{ $di ...

How can I expand my Jquery slideshow to occupy the entire screen?

I am working on stretching my JQuery slideshow to fit the entire browser, but I am running into some difficulty. I have downloaded the jQuery Vegas plugin, but I can't seem to figure out what the problem is. I would like my slideshow to resemble the o ...

Choosing an item prior to a break in the text line

I designed a navigation bar with the following CSS structure nav { text-align: center; } nav > ul { list-style: none; padding: 0; margin: 8px auto; } nav > ul > li { display: inline- ...

It is not possible to modify the appearance of visited :before pseudo-elements

After attempting to enhance the appearance of visited links with various effects, I encountered an issue. Below is the code snippet in question: It seems that only Opera browser is able to modify the content of the :before pseudo-elements successfully. O ...

Include HTML tag and class inside a JavaScript code block

A dynamic button is loaded when a certain condition in the JavaScript is met. Upon loading, I want to trigger a function (ClickMe) by clicking the button. However, I'm facing difficulty connecting the function with the button in my code. In my scrip ...

Achieving Full Height and Width of Tab Panel with Bootstrap 4.3

I recently upgraded to bootstrap 4.3 and discovered the handy sizing classes m-* and w-* for setting element height and width. After successfully achieving 100% height and width for the container, I'm now looking to apply these dimensions to the tab-p ...

The stylesheet failed to load due to an incorrect MIME type (text/html instead of text/css)

As I delve into creating a personal project centered around a basic expense tracker web app, I encountered an obstacle right at the start of my journey. The main page's stylesheet (index.html) refuses to load, resulting in the common error message: Th ...

Challenges with styling inputs using Bootstrap CSS

https://i.sstatic.net/zk26B.png <div class="form-group"> <label for="AppName">Application Name</label> <input type="text" class="form-control" name="AppName" id="AppName" tabindex="1" #aName="ngModel" [(ngModel) ...