What is the best way to align vertically a horizontal list that includes both headers and icon images?

My goal is to create a horizontal navigation bar that features menu options with headings and icons below them. Upon hovering over each option, the block's background color should change and slightly increase in size. I initially used percentages for sizing everything in the navigation bar, which was functioning properly until I needed to vertically align the menu options.

To achieve vertical alignment, I switched the display property from inline-block to table/table-cell. While this resolved the alignment issue, it created a thin space underneath the list and caused each list item to no longer occupy 100% of the navigation height, thus disrupting the hover effect on the background color.

You can view a screenshot illustrating the navigation bar here. The pink background highlights the issue, but ideally, there would only be a green background without any color, and the blue box hovering over the "learn" button should extend to the edges of the navigation bar as it did before the display change.

Another screenshot showing how it looks when the li display is set to inline-block can be seen here. This image represents what I aim for, except for the lack of vertical alignment.

This simplified version depicts my HTML structure:

<nav class="nav_bar">
    <span id="logo">
        <a href="home.html"><img src="images/nav_bar/logo_img.png"></a>
        <a href="home.html"><img src="images/nav_bar/logo_text.png"></a>
    </span>
    <ul class="nav_menu">
        <li id="learn"><a href="learn.html">LEARN<br><img src="images/nav_bar/learn.png"></a></li
        ><li id="watch"><a href="watch.html">WATCH<br><img src="images/nav_bar/watch.png"></a></li
        ><li id="share"><a href="share.html">SHARE<br><img src="images/nav_bar/share.png"></a></li
        ><li id="more"><a href="more.html">MORE<br><img src="images/nav_bar/more.png"></a></li
        ><li id="active"><a href="home.html">HOME<br><img src="images/nav_bar/home.png"></a></li>
    </ul>
</nav>

Here are the relevant CSS styles:

.nav_menu {
    display: table;
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    padding: 0;
    height: 100%;
    width: 35%;
    list-style-type: none;
}
.nav_menu img {
    width: 70%;
}
.nav_menu li {
    display: table-cell;
    height: 100%;
    width: 20%;
    vertical-align: middle;
    overflow: hidden;
}
.nav_menu a {
    display: table-cell;
    height: 100%;
    width: 100%;
    text-align: center;
    font-size: 1.5vw;
    text-decoration: none;
}
.nav_menu a:hover {
    transform: scale(1.1);  
}

#learn a:hover {
    background-color: #69D1ED;
}

I briefly attempted using icons as list background images, but encountered difficulties positioning them beneath the text.

Since this is an assignment, I am limited to using only HTML and CSS. Additionally, the solution must be compatible with Chrome, Safari, Firefox, Internet Explorer, and Opera browsers.

Answer №1

Looking for something similar to this design?

body {
  margin: 0;
  padding: 0;
  background: #ccc;
}
 
.nav ul {
  list-style: none;
  background-color: #444;
  text-align: center;
  padding: 0;
  margin: 0;
}
.nav li {
  font-family: 'Oswald', sans-serif;
  font-size: 1.2em;
  line-height: 40px;
  height: 60px;
  border-bottom: 1px solid #888;
}
 
.nav a {
  text-decoration: none;
  color: #fff;
  display: block;
  transition: .3s background-color;
}
 
.nav a:hover {
  background-color: #005f5f;
  transform: scale(1.1);  
}
 
.nav a.active {
  background-color: #fff;
  color: #444;
  cursor: default;
}
 
@media screen and (min-width: 600px) {
  .nav li {
    width: 120px;
    border-bottom: none;
    height: 50px;
    line-height: 50px;
    font-size: 1.4em;
  }
 
  /* Option 1 - Display Inline */
  .nav li {
    display: inline-block;
    margin-right: -4px;
  }

  /* Options 2 - Float
  .nav li {
    float: left;
  }
  .nav ul {
    overflow: auto;
    width: 600px;
    margin: 0 auto;
  }
  .nav {
    background-color: #444;
  }
  */
}
<div class="nav">
  <ul>
    <li class="home"><a href="#">Home</a></li>
    <li class="tutorials"><a class="active" href="#">Tutorials</a></li>
    <li class="about"><a href="#">About</a></li>
    <li class="news"><a href="#">Newsletter</a></li>
    <li class="contact"><a href="#">Contact</a></li>
  </ul>
</div>

Check out the demo here

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 is the reason behind negative numbers appearing as "5-" rather than "-5" in the basic calculator coded using HTML and JavaScript?

As I am practicing my coding skills, I encountered an interesting issue. Any operation that results in a negative number displays as wrong, but when using console.logs it shows the correct result. Can someone explain why this is happening? <!DOCTYPE h ...

Determining the browser width's pixel value to enhance responsiveness in design

Lately, I've been delving into the world of responsive design and trying to grasp the most effective strategies. From what I've gathered, focusing on content-driven breakpoints rather than device-specific ones is key. One thing that would greatl ...

Utilize Flexbox's column direction to ensure that all flex items within the same column have uniform height

Having trouble creating a responsive web page. Here are the specifics: Sharing my HTML and CSS code below: - <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA ...

Issue with HTML form not updating MySQL database with PHP

Oh no, this PHP situation is really confusing me! I've been at it for a month now and this is the kind of challenge I thought I would face four weeks ago. Sigh! I keep getting an "Undefined index" warning, and the database is updating with a date of ...

Exploring Angular2's interaction with HTML5 local storage

Currently, I am following a tutorial on authentication in Angular2 which can be found at the following link: https://medium.com/@blacksonic86/authentication-in-angular-2-958052c64492 I have encountered an issue with the code snippet below: import localSt ...

Expanding DIV Box with jQuery

Have you ever come across those cool jQuery Expandable box features that expand to reveal a larger image in the center of the screen when you click on a Thumbnail image? But what if I want to achieve something similar, where instead of just an image insid ...

Finding web page links from competition rankings using pattern matching

Challenge I am currently delving into the realm of natural language processing projects. Before diving in, I intend to explore existing works on datasets, particularly those organized on a leaderboard (refer to the "Three-way Classification" section). Ho ...

Is it mandatory to include a DOCTYPE/DTD in XHTML5?

Currently in the process of developing a new language that converts to XML, I am facing the limitation of not being able to support DOCTYPE/DTD. Would it be possible for me to utilize XHTML5 without needing to declare , or will I have no choice but to in ...

Is there a way to automatically hide divs with the style "visibility:hidden" if they are not visible within the viewport?

Currently, I am working on developing a mobile web app. Unfortunately, Safari in iOS 5.1 or earlier has limited memory capabilities. In order to reduce memory usage while using css3 transitions, I have discovered that utilizing the css styles "display:none ...

Generate a series of buttons with generic identifiers that can be easily targeted using getElementById. The IDs will be dynamically assigned

I am looking to dynamically generate unique IDs for a list of buttons, allowing me to easily target specific buttons using getElementById. Here is an example code snippet where each button has the same ID: @foreach (var cat in Model) { <div clas ...

How can I make Material UI's grid spacing function properly in React?

I've been utilizing Material UI's Grid for my layout design. While the columns and rows are functioning properly, I've encountered an issue with the spacing attribute not working as expected. To import Grid, I have used the following code: ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...

How can I align an image and text alongside another image using HTML and CSS?

Having some trouble positioning an Image and Text next to another Image. Check out an example here: I attempted using floats, but it doesn't seem to be working. Here is the code I have: .left {float: left;} .right{float: right;} <div class="left ...

Can you explain the purpose of the .col-form-label class in Bootstrap and how it affects labels?

Here in the initial label, the class is specifically identified. <form> <div class="form-group row"> <label for="staticEmail" class="col-sm-2 col-form-label">Email Address</label> <div ...

Enhancing visual appeal with Carousel Slider images

I am facing an issue with the Carousel Slider. I added an image to index.php, but when I tried adding a second image, it appeared just below the first one in the carousel. Click here for the first image Click here for the second image Slider.php < ...

Having trouble with the nth-child CSS property in Bootstrap?

My cards need some styling adjustments - specifically, I want the first and third card in each row to have a top margin of 50px. Strangely, this doesn't seem to work with Bootstrap, but it does with pure CSS. Is there a specific class in Bootstrap tha ...

Guide to activating animation on one element when hovering over another element?

I am setting up an HTML 5 range element and looking to enhance the user experience. Specifically, I want to implement a feature where when the user hovers over the range, the height and width of the thumb should increase to 12 pixels. CSS .myrange::-webk ...

Is it possible to insert clickable links within the content of a Twilio text message?

Currently, I am utilizing Twilio and Express to send programmable SMSs to the users of my web application. I'm curious if it's possible to include hyperlinks within the body of these text messages. Is there a method to achieve this? I have attem ...

Revolutionizing Interaction: Unveiling the Power of Bootstrap 3

Would you be able to assist me in customizing buttons to resemble a typical dropdown link? <div class="dropdown"> <button class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">Dropdown <span class="caret"&g ...

Looking to create a custom loader that adapts to different screen sizes

Can anyone help me make the text in my centered div responsive on my website loader? I'm new to coding and struggling to figure it out. Any assistance would be greatly appreciated! Make sure to view the full page snippet so you can see the text prope ...