How to make horizontal list in HTML using CSS
Tags: html, css, webdev
Published: 2023-04-06 12:21:08

Have you started hacking with CSS recently and wanted to display a list or menu in a horizontal fashion? Then you are at the right place. In this article, we will explore different ways to display a horizontal list in HTML.

## Project setup

Below will be the HTML code we will be using throughout this article:

```html
<!DOCTYPE html>
<html>
  <head>
    <title>Horizontal List</title>
    <style></style>
  </head>
  <body>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/pricing">Pricing</a></li>
      <li><a href="/blog">Blog</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </body>
</html>
```

The above HTML will render the page as shown below:

![basic setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsqecrh84aw1hk6qgg1t.png)

## Using display inline

```html
<!DOCTYPE html>
<html>
  <head>
    <title>Horizontal List</title>
    <style>
      li {
        display: inline-block;
      }
    </style>
  </head>
  <body>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/pricing">Pricing</a></li>
      <li><a href="/blog">Blog</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </body>
</html>
```

If you add `display:inline-block` style as shown below, the items will be aligned next to each other:

![display inline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuxzuor8bm4l6rqbxn5f.png)

Let's add some styling to the menu:

```html
<!DOCTYPE html>
<html>
  <head>
    <title>Horizontal List</title>
    <style>
      li {
        display: inline-block;
        padding: 1rem;
      }
      li a {
        color: white;
        text-decoration: none;
      }
      li a:hover {
        color: #333;
      }
      ul {
        background: teal;
        margin: 0;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/pricing">Pricing</a></li>
      <li><a href="/blog">Blog</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </body>
</html>
```

If you run the code now, you will get:

![display inline decorated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95okiiinfpe7ex3jx2dx.png)

## Using display flex

We can make the `ul` element flex to align the menu horizontally:

```html
<!DOCTYPE html>
<html>
  <head>
    <title>Horizontal List</title>
    <style>
      ul {
        display: flex;
      }
      li {
        list-style-type: none;
        padding: 1rem;
      }
      li a {
        color: white;
        text-decoration: none;
      }
      li a:hover {
        color: #333;
      }
      ul {
        background: teal;
        margin: 0;
        text-align: center;
        padding-left: 0;
      }
    </style>
  </head>
  <body>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/pricing">Pricing</a></li>
      <li><a href="/blog">Blog</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </body>
</html>
```

## Using grid

You can use the CSS grid as shown below to align the items horizontally:

```html
<!DOCTYPE html>
<html>
  <head>
    <title>Horizontal List</title>
    <style>
      ul {
        display: grid;
        grid-template-columns: auto auto auto auto;
      }
      li {
        list-style-type: none;
        padding: 1rem;
      }
      li a {
        color: white;
        text-decoration: none;
      }
      li a:hover {
        color: #333;
      }
      ul {
        background: teal;
        margin: 0;
        text-align: center;
        padding-left: 0;
      }
    </style>
  </head>
  <body>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/pricing">Pricing</a></li>
      <li><a href="/blog">Blog</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </body>
</html>
```