How can I design a horizontal navigation bar using only CSS and CSS tables?

As a newcomer, I am attempting to create the navigation bar that I described. I came across a tutorial at . However, it uses SCSS which I am not familiar with, rather than pure CSS.

Here is my code - http://codepen.io/rsing4dev/pen/LNqvVN

<p>Please refer to my CodePen for the code. 
I want to keep the question concise without pasting too much code here.

The Issue - When hovering over the "services" menu, its submenu should be displayed vertically instead of horizontally. Why isn't this happening and how can I fix it?

Thank you!

PS - I would rather avoid using inline block and floats to create my navigation menu.

Answer №1

(Check out the updated code snippet (below) that still utilizes CSS table display.)

The issue you're encountering is:

1) assigning both the top navigation and the sub-navigation as tables, along with all their cells as table-cell. This setup is only necessary for the topnav and its list items.

3) ensuring that your sub-navigation can extend beyond the boundaries of the top-nav container; otherwise, the container will expand to accommodate the sub-nav.

Review my example below to observe how I applied table display and utilized position absolute (with a parent of position relative -- crucial) to position my subnav accordingly.

#nav {
    background-image: linear-gradient(to top, rgba(9, 9, 44, 0.9), rgba(82, 82, 115, 0.9));
    line-height: 2em;
    width: 100%;
}
#nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.main-nav-menu {
  display: table;
  width: 100%;
}
.main-nav-menu-item {
  display: table-cell;
  padding: 0;
  margin: 0;
  text-align: center;
  width: 20%;
  border-right: 1px solid #131332;
  position: relative;
}
.sub-nav-menu {
  background-color: gray;
  left: 0;
  position: absolute;
  width: 100%;
}

#nav a {
    text-decoration: none;
    color: #CFCFE0;
}

/*
CSS For the submenus of the main navigation menu.
*/
.sub-nav-menu {
    display: none;
}

.main-nav-menu:hover .sub-nav-menu {
    display: block;
}
<div id="nav">
  <ul class="main-nav-menu">
    <li class="main-nav-menu-item">
      <a href="#Services" class="nav-tab selected">Services</a>
      <ul class="sub-nav-menu">
        <li class="sub-nav-menu-item"><a href="#our-services">Our Services</a></li>
        <li class="sub-nav-menu-item"><a href="#client-list">Client List</a></li>
      </ul>
    </li>

    <li class="main-nav-menu-item">
      <a href="#Resources" class="nav-tab">Resources</a>
      <ul>
        <li></li>
        <li></li>
      </ul>
    </li>

    <li class="main-nav-menu-item">
      <a href="#Contact-Us" class="nav-tab">Contact Us</a>
      <ul>
        <li></li>
      </ul>
    </li>

    <li class="main-nav-menu-item">
      <a href="#Company" class="nav-tab">Company</a>
      <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </li>

    <li class="main-nav-menu-item">
      <form class="searchbar" action="http://www.google.com/search" name="f" target="_blank" style="margin: 0px">
        <input type="hidden" name="any selected">
        <input size="10" name="q" value="" class="searchform" placeholder="Web Search">&nbsp;
        <input type="submit" value="Go!" name="btnG" class="searchbutton"><br>
      </form>
    </li>
  </ul>
</div>

Answer №2

Utilizing inline blocks is often the most straightforward approach.

The key disparity between inline blocks and table cells becomes evident when you resize the browser. If you resize the browser with blocks, they will stack vertically one after the other, whereas table cells will maintain their default arrangement.

See an example here (resize the output window):

https://jsfiddle.net/dv7k720s/

You can also explore the solution presented by haltersweb for working with a table layout.

Furthermore, I quickly restructured your menu based on the w3schools tutorial for you to review and understand.

https://jsfiddle.net/LmLbefts/1/

<ul>
<li  class="dropdown">
    <a href="#" class="dropbtn">Services</a>
    <div class="dropdown-content">
      <a href="#">Our Services</a>
      <a href="#">Client List</a>
    </div>
  </li>
  <li><a href="#resources">Resources</a></li>
  <li><a href="#contact">Contact Us</a></li>
  <li><a href="#company">Company</a></li>
  <li><a href="#">
  <form class="searchbar" action="http://www.google.com/search" name="f" target="_blank" style="margin: 0px">
       <input type="hidden" name="any selected">
       <input size="10" name="q" value="" class="searchform" placeholder="Web Search">&nbsp;
       <input type="submit" value="Go!" name="btnG" class="searchbutton"><br>
  </form>
  </a></li>
</ul>

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-image: linear-gradient(to top, rgba(9, 9, 44, 0.9), rgba(82, 82, 115, 0.9));
    line-height: 2em;
    text-align: center;
    vertical-align: middle;
}

li {
display:inline;
margin-left:2%;
margin-right:2%;

}


li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
/* To edit if you want a hover color   */
/*background-color: red;*/
}

li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-image: linear-gradient(to top, rgba(9, 9, 44, 0.9), rgba(82, 82, 115, 0.9)); color:white;}

.dropdown:hover .dropdown-content {
    display: block;
}

The W3Schools provides a basic CSS tutorial explaining how to create vertical or horizontal navigation bars:

Link: http://www.w3schools.com/css/css_navbar.asp

For understanding how dropdown menus function, you can refer to this tutorial:

http://www.w3schools.com/css/css_dropdowns.asp

Take a peek at the preview to see how it looks like: https://i.sstatic.net/G0jNh.png

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

Is there a way to make all Bootstrap column heights equal using only jQuery?

I am currently working on matching the heights of two columns without using an existing library like match-height.js. In this specific case, I have a row with 2 columns where the first column contains a centered black square and the second column contains ...

Having trouble implementing uppercase text in Bootstrap editable modules

Recently, I implemented the bootstrap editable function in my application and it was functioning perfectly. However, I am now looking to have the editable text always display in uppercase format. Unfortunately, I am unsure of how to achieve this. Any sugge ...

What happens when we use an asterisk (*) in front of a CSS property?

input, textarea, select { font-family: inherit; font-size: inherit; font-weight: inherit; } input, textarea, select { *font-size: 100%; } The code snippet above is taken from the YUI reset css. Can you explain the significance of the asterisk (*) ...

Initiating CSS3 animations when the display changes

In an attempt to incorporate CSS3 animations for fading elements in during page load, I am faced with the challenge of setting the element to display: none; and then back to display: block;. My goal is to exclusively apply the CSS3 animation upon the init ...

Refreshing the ASP classic by resetting the request.cookies("user")

Apologies for my limited programming skills - I am facing an issue with customers placing orders... ...how can I clear REQUEST.COOKIES("user") after a customer places an order on the thank you page - using this code snippet Dim pay_status, c_id pay_statu ...

Is There a Way to Verify if JqxGrid Contains Any Errors?

Within the code below, I have successfully displayed highlighted borders when there are validation failures. Is it possible to determine any validation errors in a grid when a button is clicked? By clicking the button at the bottom, I would like to be able ...

Ensure that the content fills the entire height of the container and include a scrollbar

I'm currently utilizing CKEditor () and have developed my own customized file browser. The issue I'm encountering is that when the filebrowser is opened, it appears in a new popup window without scrollbars. I reached out for support through a ti ...

Using CSS transform to enhance Flash content

I am currently working on a web application that is contained within an iframe. The iframe has been scaled using -webkit-transform:scale(0.8), which is functioning well. However, I am encountering an issue with the flash audio recorder within the iframe no ...

Customize hover effects for MuiCheckbox

For some reason, my checkboxes are displaying a circle on hover that I want to remove. https://i.sstatic.net/62TLL.png I attempted to make overrides in MuiTheme: export const MUI_THEME = createMuiTheme({ overrides: { MuiCheckbox: { root: { ...

The text in the image caption has been drastically reduced along with the size of the image

Currently, I am dealing with the following code: <div style="min-width: 1000px;"> <p> Some text </p> <div class='div_img_left'> <img src="images/sth.png" alt="missing image" style="max-width ...

What are the steps to creating an API for a web application?

I have been tasked with creating an API for my web application that will utilize another website's authentication system. While I have a basic understanding of JavaScript, PHP, and HTML, I need guidance on how to proceed with building the API. Here i ...

reverting the effects of a javascript animation

I am expanding the size of a carousel on a specific pane by adjusting its height and position. Here is how I achieve this: if(currentPane==2) { $("#carousel").animate({height:320},1000); $("#carousel").animate({top:411},1000); $("#dropShadow") ...

Cross-Browser Compatibility of CSS

I have noticed that lists styled with the following CSS code appear differently in Internet Explorer 8 and Firefox 4. In IE8, the rounded corners are missing, while FF4 does not change color when hovering over it. Which browser should I trust for accurate ...

What is the process to retrieve a variable from a Node.js file in an HTML document?

What is the best way to showcase a variable from a node.js route in an HTML File? I have a node.js route structure as follows: router.post("/login", async (req,res) => { try { const formData = req.body const name = formData.name ...

Ways to refresh a component on a webpage without having to refresh the entire page

I am looking to dynamically load a specific component on my webpage when another component is altered <input ..... onchange="callFunctionToReloadNextTag()"> <input ... /> <--This is the tag I want to reload Is it possible to reload th ...

How can you selectively add a CSS class during the iteration of a VueJs object?

Using Vuetify components: <v-layout row wrap> <v-flex xs2 v-for="(month, key) in months" :key ="key"> <router-link class = "decoration" :to="month.target">{{(month.month)}}</router-link> </v-flex> The "v ...

Instructions on allowing the user to enter text for searching through an array of objects, and displaying a message if a match is found. Use only pure JavaScript

As a newcomer to JavaScript, I greatly appreciate the support from everyone on this platform. Thank you in advance for your help! I am currently working on building a basic music app as a learning project in JavaScript. The main goal is to understand how J ...

Enhancing Yii2 Navbar with Icons

I recently started working with Yii2 and I am in the process of creating a simple navigation bar using the built-in widget. The main challenge I am facing is how to include icons next to the menu options. The current state of my navbar is as follows: Na ...

enhancing the appearance of the initial sentence in the closing passage through CSS styling

Is there a way to make only the first sentence in the final paragraph bold using CSS? Specifically, I would like to add extra padding to the top of this last paragraph so that it wraps around an image. However, when I increase the bottom padding of the flo ...

What is the best method for accommodating various web devices effectively?

Those deciding to close this as not conducive, please read through the entire post. Specific questions will be posed at the end. Seeking practical examples and strategies. Situation As more and more people use devices like smart-phones and tablets to acce ...