How to stylishly showcase a list in a horizontal row using HTML and CSS`

Is there a way to modify this code so that the list is displayed in a horizontal line menu?

Currently, the code displays the list vertically but I want it to be displayed in a horizontal line. How can I achieve this?

<html><head><style>

.metromenu {
  display:block;
  position:relative;
  width:40%;
  margin:0 auto;
  z-index:1;
}
.metromenu, .sub-metromenu {
  list-style:none;
}
.metromenu li {
  display:inline-block;
  float:left;
  margin-right:2px;
  margin-top:2px;
}
.metromenu  a{
  display:block;
  position:relative;
  width:120px;
  height:32px;  
  text-decoration:none;
  font-family:'arial';
  text-align:center;
  letter-spacing:2px;
  line-height:26px;
  color:#fff;
  padding:6px 20px 0 20px;
  background-color: hsl(200,70%,50%);
  -webkit-transition:all 0.2s ease-out;
  -moz-transition:all 0.2s ease-out;
  -o-transition:all 0.2s ease-out;
}
.metromenu a:hover {
  background-color: hsl(200,80%,65%);
}
.metromenu span {
  display:inline-block;;
  position:absolute;
  top:18px;
  right:10px;
  width: 0; 
    height: 0; 
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 6px solid #fff;
}
.metromenu li:hover > a{ /* activates link when mouse over sub-metromenu */
    background-color: hsl(200,80%,65%);
}
/*sub-metromenu trigger*/
.metromenu li a:hover ~ ul{
  opacity:1;
  visibility:visible;
}
.sub-metromenu {
  opacity:0; 
  visibility:hidden;
  position:absolute;
   z-index:10;
  -webkit-transition:all 0.2s ease-out;
  -moz-transition:all 0.2s ease-out;
  -o-transition:all 0.2s ease-out;
}
.sub-metromenu:hover {
  opacity:1;
  visibility:visible;
}
.sub-metromenu li a{
  background-color: hsl(250,70%,60%);
}
.sub-metromenu li:first-child a{
  height:72px;
}
.sub-metromenu li a:hover{
  background-color: hsl(250,80%,70%);
}
.metromenu .orange {
  background-color: hsl(20,70%,60%);
}
.metromenu .orange:hover {
  background-color: hsl(20,80%,70%);
}
.metromenu .green {
  background-color: hsl(110,60%,60%);
}
.metromenu .green:hover {
  background-color: hsl(110,70%,70%);
}
</style>
</head>
<body>

<div>
  <ul class="metromenu">
    <li><a href="http://ntools.infoexpo.in/" class="green">Web Tools</a></li>


    <li><a href="http://shoponline.infoexpo.in/">Smartphones<span></span></a>
        <ul class="sub-metromenu">
          <li><a href="#">Latest Smartphones</a></li>
          <li><a href="#">Windows</a></li>
          <li><a href="#">Android</a></li>
        </ul>
    </li>


    <li><a href="http://bit.ly/shopindia" class="orange">Others</a></li>
  </ul>
</div> </body> </html>

The original code was taken from here http://codepen.io/maxim/pen/DrvLx

Answer №1

Give this a try

.modernmenu {
display: block;
margin: 0 auto;
position: relative;
width: 100%;
z-index: 1;
}

I made a change to the width, increasing it from 40% to 100%. You can view the result on this demo link. Feel free to let me know if you found this helpful or have any questions.

Update for sub-menu fix: Don't forget to set the width for the sub-menus to prevent them from stacking vertically.

.sub-modernmenu {
opacity: 0; 
visibility: hidden;
position: absolute;
z-index: 10;
-webkit-transition: all 0.2s ease-out;
-moz-transition: all 0.2s ease-out;
-o-transition: all 0.2s ease-out;
width: 10%;
}

Check out the revised version on this link

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 method for embedding the creation date of a page within a paragraph on Wagtail?

Whenever a page is created in the admin panel of Wagtail, it automatically displays how much time has elapsed since its creation. https://i.stack.imgur.com/6InSV.png I am looking to include the timestamp of the page's creation within a paragraph in ...

Having trouble getting the Bootstrap 5 Modal to function properly within an Electron app

Facing an issue with my web app using Bootstrap 5, as the modal is not displaying properly. Below is the HTML code for the modal and the button that triggers it: <div class="modal" tabindex="-1" id=&quo ...

Creating PHP scripts that can securely handle cross-domain variables within an iframe

I am managing customer data on my server A. My customers, who have their own server B (over which I have no control), embed an IFRAME on their webpage (referred to as the page of origin) that calls a page on my server A to display some customer informatio ...

What is the process to transform HTML elements into user-friendly HTML content?

I'm facing an issue with querying data from a markdown database and then using Marked.js to convert it back to HTML on the server. However, when I insert it into the DOM, it's displaying the content in a coded format like this: <h1 id="h1 ...

Updating Kendo Treeview's data source dynamically

I am working with a kendoTreeView connected to an OData source, following the demo on the official site. However, I encounter an issue when trying to change the dataSource of the kendoTreeView. Despite changing the tree successfully, all nodes are displaye ...

When I click on something else, the dropdown doesn't automatically close

In my current setup, I have 3 dropdowns on the page. When I click outside of any dropdown, they are correctly closed. However, if one dropdown is already open and I click on another one, the previously opened dropdown remains open. I want all dropdowns to ...

Adjust the background color of the choices in the select box

I'm looking to customize the appearance of a drop-down menu in a form on a website by making the background of the options transparent, similar to the initial selection. I've tried using background:rgba(255, 255, 255, 0.2) for this purpose, but ...

What is the process for inserting a hyperlink into text within a tooltip?

I have implemented a tooltip within my input field using an external component, as illustrated in the code snippet below. Within the tooltip, I am trying to include a hyperlink so that users can click on the word 'world' and be directed to anothe ...

Check if the input value was chosen by pressing Enter instead of clicking on it

There is an input tag with the following attributes: <input type="text" name="cOperator" class="form-control scale-input operator" placeholder="Enter your ID" autocomplete="off" onkeyup="ajax_showOptions(this,'getEmp',event)" required> ...

Carousel-item height in Bootstrap 4.6

I am facing an issue with a component in Angular 14 using Bootstrap v4.6: <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> <div class="carousel-inner"> <div cl ...

Extracting data from a dropdown list with beautifulsoup

Could someone please assist me in scraping a list of dates from: The dates can be found within a dropdown menu just above the option chain. I have experience scraping text from websites, but this specific text is utilizing 'select' & 'o ...

Content and footer being covered by the sidebar

I have an illustration here to help explain my issue. The problem I am facing is that the Sidebar is overlapping both my Content and Footer when the content consists of only small items. In my _layout file, I'm referencing the sidebar like thi ...

Oops! Looks like we have encountered an error: X is not recognized

As I navigate through the WebOS enyo framework, frustration starts to set in. The error message displayed in my log is causing me a headache. Despite examining samples within the framework, pinpointing the source of the error has proven to be a challenge. ...

Switch pages with Jquery

Having an issue with this code - when I click on the next page, the page refreshes twice. Is it possible to stop this behavior? Apologies for my poor English. Code jQuery(document).ready(function() {</p> <pre>$(".container").css("display", ...

Adjust the size of points positioned absolutely on an image map

Are you struggling to position POIs on a picture map correctly? Check out this link for more information: Even after trying CSS scale and re-positioning in media queries, getting the points in the right place seems impossible. What do you think? Can this ...

Automatically filling text fields in JSP using data population algorithms

Currently working on a project using jsp. The page features two text boxes. When the first text box is selected, a list of countries is retrieved from a MySQL database using jQuery. The task at hand now is: Without utilizing any events (especially button ...

AngularJs - $watch feature is only functional when Chrome Developer Tools are active

My goal is to create a $watch function within a directive that monitors changes in the height and width of an element. This will allow the element to be centered on top of an image using the height and width values. Below is my app.js code: app.directive ...

Unusual thin border of white pixels on IE 9

Having some issues with border radius in IE-9. The left border is showing white pixels. Any thoughts on why? This problem is only in IE 9, other browsers look fine. Here is the code snippet: <ul class="tags clearfix"> ...

What is the best way to store selected items from a multi-select box in AngularJS that is generated using ng-repeat?

I have a scenario where I need to handle a group of select boxes instead of just one. Each select box holds a different option, and when the user changes their selection, I want to save that value in a variable or an array. I've managed to do this for ...

What could be causing my radio button list to stop functioning correctly? (After applying CSS)

While working on a simple postage calculator in Visual Studio with Web Forms (yes, we had to use Web Forms in Class), I included some Bootstrap for styling. However, there was an issue where my RadioButtonList suddenly stopped functioning properly. None ...