MihaiMacarie
MihaiMacarie

Reputation: 75

No space between li elements in dropdown menu

I made a dropdown menu in css and I can't add space between the elements in the dropdown. They collapse. There is the code: jsfiddle

There is the CSS part. I tried different things to fix it.

    *
{
    margin:0px;
}
html,body
{
    height:100%;
}
.wrapper
{
    height:100%;
    min-height:100%;
    height:auto !important;
    margin:0 auto -50px;
}
.footer,.push
{
    height:50px;
}
.footer
{
    background-color:lightblue;

}
.footer center
{
        vertical-align:middle;
}
.header
{
    margin-top:10px;
    height:150px;
}
#meniu li
{
    padding:5px;
    border:1px solid black;
    border-radius:10px;
    display:inline;
}
#meniu
{
    margin-top:5px;
    background-color:lightblue;
    width:100%;
    height:50px;
}

#meniu a
{
    text-decoration:none;
}
.dropdown-content {
    display: none;
    position: absolute;
    width:100%;
    margin:0px;
}
.dropdown-content ul
{
    height:100%;
    width:100%;
    position:relative;
    list-style-type:none;
    margin-top:20px;
    left:-25%;
}
.dropdown-content li
{
    position:relative;
}
.dropdown {
    position: relative;
    display: inline-block;

}

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

}

There is the the HTML:

<body>
        <div class="wrapper">
            <div class="header">
            <div id="meniu">
                <ul>
                    <li>PRIMA PAGINA</li>
                    <div class="dropdown"><li>FISIERELE MELE<div class="dropdown-content">
                            <ul>
                                <li>MENIUL MEU</li>
                                <li>PLANETE NOI</li>
                            </ul>
                        </div>
                    </div></li>

                    <li>MENIUL MEU</li>
                    <li>PLANETE NOI</li>
                </ul>
            </div>
            </div>
            <div class="push"></div>
        </div>
        <div class="footer"><center>Olimpiada Nationala de Tehnologia Informatiei si Comunicarii Buzau 2015</center></div>

Upvotes: 0

Views: 1360

Answers (3)

Ben Bodan
Ben Bodan

Reputation: 81

You had an error in your html

    <div class="wrapper">
        <div class="header">
        <div id="meniu">
            <ul>
                <li>PRIMA PAGINA</li>
                <div class="dropdown">
                <li>FISIERELE MELE
                <div class="dropdown-content">
                        <ul>
                            <li>MENIUL MEU</li>
                            <li>PLANETE NOI</li>
                        </ul>
                    </div>
                </div>
                </li>

                <li>MENIUL MEU</li>
                <li>PLANETE NOI</li>
            </ul>
        </div>
        </div>
        <div class="push"></div>
    </div>
    <div class="footer"><center>Olimpiada Nationala de Tehnologia Informatiei si Comunicarii Buzau 2015</center></div>

https://jsfiddle.net/ggtpufuw/1/

After fixing the html you have to make your list display as inline-block and margin them as you like.

#meniu li
{
    padding:5px;
    border:1px solid black;
    border-radius:10px;
    display:inline-block;
    margin-top:10px;
}

Upvotes: 0

Aziz
Aziz

Reputation: 7783

I made a dropdown menu in css and I can't add space between the elements in the dropdown. They collapse.

They collapse because you have made them as inline elements. I think you meant inline-block, otherwise padding/margin would not render properly.

The correct code should be:

#meniu li {
  ⋮
  display: inline-block; /* make `li` as inline-blocks */
  margin-bottom: 10px; /* add spacing */
}

You also have some HTML syntax errors (extra li), please run it through a validator. My final result is:

* { margin: 0px; }

html, body { height: 100%; }

.wrapper {
  height: 100%;
  min-height: 100%;
  height: auto !important;
  margin: 0 auto -50px;
}

.footer, .push { height: 50px; }

.footer { background-color: lightblue; text-align: center; }

.header { margin-top: 10px; height: 150px; }

#meniu li {
  padding: 5px;
  border: 1px solid black;
  border-radius: 10px;
  display: inline-block;
  margin-bottom: 10px;
}

#meniu {
  margin-top: 5px;
  background-color: lightblue;
  width: 100%;
  height: 50px;
}

#meniu a {
  text-decoration: none;
}

.dropdown-content {
  display: none;
  position: absolute;
  width: 100%;
  margin: 0px;
}

.dropdown-content ul {
  height: 100%;
  width: 100%;
  position: relative;
  list-style-type: none;
  margin-top: 20px;
  left: -25%;
}

.dropdown-content li {
  position: relative;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="wrapper">
  <div class="header">
    <div id="meniu">
      <ul>
        <li>PRIMA PAGINA</li>
        <div class="dropdown">
          <li>FISIERELE MELE
            <div class="dropdown-content">
              <ul>
                <li>MENIUL MEU</li>
                <li>PLANETE NOI</li>
              </ul>
            </div>
        </div>
        <li>MENIUL MEU</li>
        <li>PLANETE NOI</li>
      </ul>
    </div>
  </div>
  <div class="push"></div>
</div>

<div class="footer">
  Olimpiada Nationala de Tehnologia Informatiei si Comunicarii Buzau 2015
</div>

jsFiddle: https://jsfiddle.net/azizn/snL1gwsm/

Upvotes: 1

Adi Azarya
Adi Azarya

Reputation: 4473

Hope i understood your question correctly... i made some drop-down bar for you. you can add elements as you wants, take a look on the html file and the css it will help you understand what is going on. take a look :)

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown {
    position: relative;
    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;
}

.dropdown-content a:hover {background-color:lightblue}

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

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}
<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

Upvotes: 0

Related Questions