1

I'm working on a navigation bar that needs to display multiple divs side by side. But the parent div doesn't have a fixed width. Just some padding. So the question is how can I achieve this?

.nav {
  width: 100%;
  height: 50px;
}

.nav ul {
  width: 100%;
  height: 50px;
}

.nav ul li {
  display: inline;
  list-style-type: none;
  background: gray;
  padding: 10px 30px;
  margin: 0 10px;
  height: 100%;
  position: relative;
}

#container {
  position: absolute;
  left: 0;
  top: 100%;
  background: black;
  padding: 10px;
}

#container .category {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 80px;
}
<div class="nav">
  <ul>
    <li>AB
      <div id="container">
        <div class="category" style="background: #4285f4;"></div>
        <div class="category" style="background: #ea4335;"></div>
        <div class="category" style="background: #fbbc05;"></div>
        <div class="category" style="background: #34a853;"></div>
      </div>
    </li>
    <li>CD</li>
    <li>EF</li>
    <li>GH</li>
  </ul>
</div>

Those four colored divs must be inline.

hmak.me
  • 3,770
  • 1
  • 20
  • 33

1 Answers1

2

You can use display: flex on the #container element. The default direction for the elements inside a container with display set to flex is row. So the elements go from left to right.

.nav {
    width: 100%;
    height: 50px;
}

.nav ul {
    width: 100%;
    height: 50px;
}

.nav ul li {
    display: inline;
    list-style-type: none;
    background: gray;
    padding: 10px 30px;
    margin: 0 10px;
    height: 100%;
    position: relative;
}

#container {
    position: absolute;
    left: 0;
    top: 100%;
    background: black;
    padding: 10px;
    display: none;
}

.nav li:hover > #container {
    display: flex;
}

#container .category {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 80px;
}
<div class="nav">
  <ul>
    <li>AB
      <div id="container">
        <div class="category" style="background: #4285f4;"></div>
        <div class="category" style="background: #ea4335;"></div>
        <div class="category" style="background: #fbbc05;"></div>
        <div class="category" style="background: #34a853;"></div>
      </div>
    </li>
    <li>CD</li>
    <li>EF</li>
    <li>GH</li>
  </ul>
</div>
DavidDomain
  • 14,976
  • 4
  • 42
  • 50
  • thanks. but if i do this, how can i hide this to show it on hover of the AB item with jquery? this is the issue. this should be initially hidden – hmak.me Sep 12 '15 at 13:59
  • You could set the `opacity` to `0` and on `:hover` set it to `1` – DavidDomain Sep 12 '15 at 14:02
  • this is not a good solution to hide a flex box. any other idea? – hmak.me Sep 12 '15 at 14:04
  • Why is it not a good idea. Pleas explain what you actually are trying to do. So i can try to help. – DavidDomain Sep 12 '15 at 14:06
  • @HosseinMaktoobian - I have added the hover to my answer. Is this wha t you are looking for? – DavidDomain Sep 12 '15 at 14:09
  • i want to slide down and up the container with jquery when user hovers the AB item in the nav bar. setting `display: flex` solves the layout problem. but how can i hide a flex box? setting `display: none` will destroy every thing. – hmak.me Sep 12 '15 at 14:09
  • no the sub menu is still there. user should not be able to interact with it when it is hidden. – hmak.me Sep 12 '15 at 14:10
  • @HosseinMaktoobian - OK, you should have added this to your question. But anyway what is the problem with changing the `display` form `none` to `flex`? – DavidDomain Sep 12 '15 at 14:11
  • yes. this is not working with toggle. or I think it needs a little more coding to do this. – hmak.me Sep 12 '15 at 14:12
  • or i think an additional container may solve the problem. thank you David – hmak.me Sep 12 '15 at 14:13
  • @HosseinMaktoobian - Please take a look again. You can do the same thing with jQuery. Should not be a problem. – DavidDomain Sep 12 '15 at 14:16
  • 1
    It works with an additional container. the inner one must be flex and the outer none. so we can toggle the outer with a single line of code – hmak.me Sep 12 '15 at 14:16