the alignment of my vertical text in the middle failed to function

I'm having trouble with my code. I want to center the text vertically in the middle of each box.

#navcontainer {
    padding: 0 5 20px 10px;
}
ul#navlist {
    font-family: sans-serif;
}
ul#navlist a {
    font-weight: bold;
    text-decoration: none;
    display: inline-block;
    line-height:20px vertical-align: middle;
}
ul#navlist, ul#navlist ul, ul#navlist li {
    margin: 0 8px;
    padding: 0px;
    list-style-type: none;
    box-shadow: 8px 8px 12px #aaa;
}
ul#navlist li {
    float: left;
}
ul#navlist li a {
    color: #ffffff;
    background-color: #EF634A;
    //padding:10px;
    padding: 10px 5px 10px 5px;
    border: 1px #ffffff outset;
    height: 40px;
}
ul#navlist li a:hover {
    color: #ffff00;
    background-color: #003366;
}
ul#navlist li a:active {
    color: #cccccc;
    background-color: #003366;
    border: 1px #ffffff inset;

    position:relative; 
    top:50%; 
    transform:translateY(-50%);
}
ul#subnavlist {
    display: none;
}
ul#subnavlist li {
    float: none;
}
ul#subnavlist li a {
    padding: 0px;
    margin: 0px;
    height: 20px;
}
ul#navlist li:hover ul#subnavlist {
    display: block;
    //display: inline-block;
    //display: table-cell;
    position: absolute;
    font-size: 8pt;
    padding-top: 5px;
}
ul#navlist li:hover ul#subnavlist li a {
    display: block;
    width : 260;
    height : 100;
    border: none;
    padding: 2px;
}
ul#navlist li:hover ul#subnavlist li a:before {
    content:" >> ";
}

and

<div id="navcontainer">
    <ul id="navlist">
        <li><a href="obs-geostrategique-sport.php?cat=1">EUROPEAN PROGRAM TO COMBAT MATCH FIXING</a>
        </li>
        <li><a href="obs-geostrategique-sport.php?cat=2">SPORTS NEWS</a>
        </li>
        <li><a href="obs-geostrategique-sport.php?cat=3">PRESS RELEASES</a>
        </li>
        <li id="active"><a href="obs-geostrategique-sport.php?cat=4" id="current">THEMES</a> 
            <ul id="subnavlist">
                <li id="subactive"><a href="#" id="subcurrent">Fight against corruption</a>
                </li>
                <li><a href="obs-geostrategique-sport.php?cat=4&id=1">Sporting events </a>
                </li>
                <li><a href="obs-geostrategique-sport.php?cat=4&id=2">Good governance in sports</a>
                </li>
                <li><a href="obs-geostrategique-sport.php?cat=4&id=3">Sports economy</a>
                </li>
                <li><a href="obs-geostrategique-sport.php?cat=4&id=4">Fight against doping</a>
                </li>
                <li><a href="obs-geostrategique-sport.php?cat=4&id=5">Integrity in sports</a>
                </li>
            </ul>
        </li>
    </ul>
</div>

Check out the fiddle http://jsfiddle.net/R8S7q/

Answer №1

1) To center text on li elements, use line-height and revert back to normal on anchor tags for multiline text.

2) Add padding and hover classes to the li element instead of the anchor tag.

Check out the FIDDLE

   ul#navlist li
   {
    padding: 10px 5px 10px 5px;
    background-color: #EF634A;
   }
   ul#navlist > li
   {
    height:62px; /* <----  */
    line-height:62px; /* <----  */
   }
    ul#navlist li:hover
    {
        color: #ffff00;
        background-color: #003366;
    }
    ul#navlist a
    {
    font-weight: bold;
    text-decoration: none;
    display: inline-block;
    line-height:normal; /* <----  */
    vertical-align: middle; /* <----  */
    }

Answer №2

Ensure that all your hyperlinks are styled with

display: block; width: 100%; height: 100%;
to occupy the entire parent container. Make certain that the parent containers themselves also have width: 100%; height: 100%; for proper expansion.

If you need to centrally align the text content, refer to this post: CSS Center text (Horizontal and Vertical) inside a DIV block

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

The search query highlighted in pagination takes us to page 2

I am currently facing an issue with a search function that provides paginated results. The result I receive is correct, but the problem arises when the highlighted page defaults to page 2 instead of page 1 upon clicking the search button. Despite trying ...

Retrieve data from an HTML form within an Angular 2 ag-grid component

I'm facing a challenge with incorporating form values from a modal into an ag-grid array in my HTML file. I'm unsure of the process to achieve this. Below is an excerpt from my file.html: <template #addTrainContent let-c="close" let-d="dismi ...

Trouble getting SVG line to display within Bootstrap 4 flex divs

I am attempting to establish a connection between the first two circular div elements using an SVG line. While inspecting, I can visualize the line but it remains hidden on the page. I tried adjusting the z-index without success. However, increasing the wi ...

My node.js code is not producing the expected result. Can anyone point out where I may be going wrong?

I've been working on a BMI calculator where I input two numbers, they get calculated on my server, and then the answer is displayed. However, I'm having trouble receiving the output. When I click submit, instead of getting the answer, I see a lis ...

Expand the sibling html element to fill the entire parent container with css styling

Hey there! I recently ran the code below in this online sandbox and noticed that the elements ".title-wrap" and ".bg-wrap" are showing up next to each other. Do you know of a way to adjust the code so that ".bg-wrap" automatically fills the entire space of ...

What is causing .attr('title') to retrieve the title of the element before?

UPDATE Flexslider now includes several callback spaces for use, making this functionality possible. after: function(){}, //Callback: function(slider) - Fires after each slider animation completes Thank you to everyone who contributed! LIVE CO ...

Strategizing button placement for maximum profit expansion

I am attempting to design a budget board using Bootstrap 4. Here is what I have done so far, and I want the buttons to look like the image below. I'm clueless on how to achieve this.. Current Output: Desired Output: .btn-warning { border: 1px s ...

Utilizing Bootstrap 3: Mastering the Implementation of .col Classes with .form-control Classes

In my form, I have utilized the .form-control classes for the form elements. To arrange two inputs side by side, I am applying .col-md-6 on each of these inputs. However, the width generated by .col-md-6 is being overridden by the .form-control class. Is ...

Toggle the visibility of divs using jQuery for easy filtering

I am looking to enhance my filtering system by allowing users to apply and view multiple filters simultaneously. For example, I want to be able to select 'High' and 'pizza' filters and see the results that match both criteria. Currently ...

What is the best way to remove input focus when clicked away?

I am in the process of developing an application using next js, and I need assistance with designing a search field. The primary functionality I am looking to implement is displaying search suggestions when the user starts typing, but hiding them when the ...

Turn off the nicescroll scroll bar that appears as a default feature

Is there a way to disable the nicescroll scroll bar that appears in red by default on my html page? It's causing issues with zooming in and breaking the layout. ...

The button remains active in Internet Explorer 10 and cannot be disabled

The button has been specified as <input type="button" id="disable" disabled="disabled" value="Upload" /> However, the appearance of the button does not show it as disabled in Chrome, Firefox, and Safari. Additionally, in Internet Explorer, the bu ...

Incorporating Button Value from Anchor Tag Title Upon Page Load

Currently, I am working on a real estate project and facing an issue with a contact modal box. My goal is to extract the title from tag "a" and place it as the button value in the modal box. English isn't my strong suit, so please excuse any errors i ...

Tips for designing a table with a stationary first column in HTML/CSS

I am looking to design a table that can be horizontally scrolled with a dynamic number of columns. The goal is to keep the first column fixed/frozen while scrolling horizontally. I attempted to achieve this using the following CSS, which successfully keeps ...

Can a background image be displayed within a DIV using position:absolute?

I have scoured every corner, yet the answer still eludes me. In my style sheet, I have a DIV with absolute positioning and a background image. Within that DIV, I want to place a paragraph that appears underneath the background. I attempted to use z-inde ...

Selecting a pair of radio buttons to toggle the visibility of different div elements using JavaScript

I've been working on two radio button categories that control the visibility of different input fields. I'm making progress, but I'm still struggling to get it to work perfectly. Below are the images for reference: The combination of &apos ...

Customizing Material-UI styles using makeStyles

Currently, I am in the process of building a small portfolio website. Despite my attempts to style an avatar using the makeStyles class, it seems that the Mui avatar root is overriding my desired styling. I am wondering if there is a way to achieve this w ...

problem specifically occurring on tablets with fixed positioning

Have you checked out the site I'm referring to here? An unusual issue seems to be occurring specifically on tablets. We implemented the scroll to fixed jQuery plugin to fix the position of the sidebar after scrolling, which works perfectly on all de ...

How can you confirm the validity of a dropdown menu using JavaScript?

<FORM NAME="form1" METHOD="POST" ACTION="survey.php"> <P>q1: How would you rate the performance of John Doe? <P> <INPUT TYPE='Radio' Name='q1' value='1' id='q1'>1 ...

Having trouble displaying API JSON data in a Vue.js table component

In my HTML file, I have implemented fetching data from an API and displaying it in a table successfully. Now, I am trying to convert the HTML file to use Vue.js, but encountering some issues. Despite being able to fetch data from the API with Vue, the tab ...