Ante
Ante

Reputation: 111

How to set different spacing between navigation menu items?

Here's a picture:

enter image description here

Addendum: I hope this this picture makes it more clear to what is asked...

I have a horizontal navigation menu with equal spacing between items... But I need different spacing between some of them. e.g. the spacing between "Arts / Illustrations" and "Contact" should be wider than between "Interior Design" and Arts / Illustrations. Any suggestions how can that be achieved?

https://codepen.io/abudimir/pen/bXVdxW

I tried playing with padding and margins but then all the items move. I've tried adding a separate class for that items but all the items move regardless.

/* the whole menu */

#menu {
  text-align: center;
  width: 100%;
  margin: 0;
}


/* Horizontal list menu */

.top-link {
  display: inline-block;
  float: none;
  position: relative;
  font-size: 1.5rem;
  font-weight: bolder;
}


/*Style for menu links*/

.top-link a {
  display: block;
  /* links fill the block*/
  color: var(--text-color);
  width: 100%;
  padding: 1.2em 2.7em;
  line-height: 1rem;
  text-align: center;
}


/*Hover state for top level links*/

.top-link:hover a {
  background: red;
  color: rgb(255, 255, 255);
}
<nav>
  <ul id="menu">

    <li class="top-link">
      <a href="about.html">About</a>
    </li>

    <li class="top-link">
      <a href="#">Graphical Design</a>
    </li>

    <li class="top-link">
      <a href="#">Interior Design</a>
    </li>

    <li class="top-link">
      <a class="a3" href="#">Arts / Illustrations</a>
    </li>

    <li class="top-link">
      <a href="contact.html">Contact</a>
    </li>

  </ul>
</nav>

Upvotes: 0

Views: 1523

Answers (3)

Amarat
Amarat

Reputation: 630

Try this:-

 /* the whole menu */
 #menu {
  text-align: center;
  width: 80%;
  margin: auto;
 } 

/* Horizontal list menu */
.top-link {
  display: inline-block;
  float: left;
  position: relative;
  font-size: 1.5rem;
  font-weight: bolder;
}

/*Style for menu links*/
.top-link a {
  display: block; /* links fill the block*/
  color: var(--text-color);
  width: 100%;
  padding: 1.2em 2.7em;
  line-height: 1rem;
  text-align: center;
}

/*Hover state for top level links*/
.top-link:hover a {
  background: red;
  color: rgb(255, 255, 255);
}
#menu li:nth-child(4) {
  margin-right: 130px
}

Upvotes: 0

kcsujeet
kcsujeet

Reputation: 522

   ul li:last-child {
    margin-left: 50rem;
}

this should work.

/* the whole menu */
 #menu {
  text-align: center;
  width: 100%;
  margin: 0;
} 

/* Horizontal list menu */
.top-link {
  display: inline-block;
  float: none;
  position: relative;
  font-size: 1.5rem;
  font-weight: bolder;
}

/*Style for menu links*/
.top-link a {
  display: block; /* links fill the block*/
  color: var(--text-color);
  width: 100%;
  padding: 1.2em 2.7em;
  line-height: 1rem;
  text-align: center;
}

/*Hover state for top level links*/
.top-link:hover a {
  background: red;
  color: rgb(255, 255, 255);
}

ul li:last-child {
    margin-left: 50rem;
}
<nav>
      <ul id="menu">

        <li class="top-link"> <a href="about.html">About</a> </li>

        <li class="top-link">
            <a href="#">Graphical Design</a>
        </li>

        <li class="top-link"> <a href="#">Interior Design</a>
        </li>

        <li class="top-link"> <a class="a3" href="#">Arts / Illustrations</a>
          </li>

        <li class="top-link"> <a href="contact.html">Contact</a> </li>

      </ul>
  </nav>

Upvotes: 1

Nidhin Joseph
Nidhin Joseph

Reputation: 10227

You can alternatively do the layout using CSS flex and apply flex-grow: 2; for those elements that needs to be wider. Please find the demo below

/* the whole menu */

#menu {
  text-align: center;
  width: 100%;
  margin: 0;
  display: flex;
}


/* Horizontal list menu */

.top-link {
  flex-grow: 1;
  flex-basis: 0%;
  display: inline-block;
  flex-grow: 1;
  position: relative;
  font-size: 1.5rem;
  font-weight: bolder;
  padding: 1.2em 0;
}

.wider {
  flex-grow: 2;
}


/*Style for menu links*/

.top-link a {
  display: block;
  /* links fill the block*/
  color: var(--text-color);
  line-height: 1rem;
  text-align: center;
}


/*Hover state for top level links*/

.top-link:hover {
  background: red;
  color: rgb(255, 255, 255);
}
<nav>
  <ul id="menu">

    <li class="top-link"> <a href="about.html">About</a> </li>

    <li class="top-link wider">
      <a href="#">Graphical Design</a>
    </li>

    <li class="top-link"> <a href="#">Interior Design</a>
    </li>

    <li class="top-link"> <a class="a3" href="#">Arts / Illustration</a>
    </li>

    <li class="top-link"> <a href="contact.html">Contact</a> </li>

  </ul>
</nav>

Upvotes: 0

Related Questions