Doubt with Flex-Box CSS

Asked

Viewed 71 times

0

I am excited about a flex display issue in this code snippet: https://jsfiddle.net/h6tgnu19/7/

I want the box to stick to the top of the page, regardless of the scroll menu or not. That there is no margin. I tried margin: 0 in the Nav element, but it did not function.

And as for the search box, shouldn’t it also be bundled with the Nav? How do I include inside the menu?

To finish. How could you make the same menu using block or inline-block? I tried and never appeared if you want the box.

Grateful

1 answer

0


You need to take away the standard margin of body:

body {
    background: #f9f9eb;
    margin: 0; ← ← ← ← ← ← ← ←
}

Also take out the padding of <ul>, which is the class .navigation:

.navigation {
    list-style: none;
    margin: 0;
    padding: 0; ← ← ← ← ← ← ← ←
    background: #0b5eec;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-flow: row wrap;
    flex-flow: row wrap;
    justify-content: center;
}

To div that holds your search box is outside the nav:

<nav>
   ...
</nav>
<div class="search-box">
   ...
</div>

Place at the end of nav and insert styles into the CSS to center and give an upper margin of the menu:

.search-box{
   text-align: center;
   margin-top: 15px;
}

How does it look:

body {
   background: #f9f9eb;
   margin: 0;
}

nav{
   background: #0b5eec;
}

.navigation {
   list-style: none;
   margin: 0;
   padding: 0;
   display: -webkit-box;
   display: -ms-flexbox;
   display: flex;
   -webkit-box-orient: horizontal;
   -webkit-box-direction: normal;
   -ms-flex-flow: row wrap;
   flex-flow: row wrap;
   justify-content: center;
}

.search-box{
    margin: 15px 15px 0;
}

.navigation a {
   text-decoration: none;
   display: block;
   padding: 15px;
   color: white;
}

.navigation a:hover {
   background: #000;
   text-decoration: underline;
}
<header>
  <nav>
      <ul class="navigation">
          <li>
              <div class="search-box">
                <label>
                  <input name="search-box" value="Search..."/>
                </label>
              </div>
          </li>
          <li><a href="#">Index</a></li>
          <li><a href="#">Products</a></li>
          <li><a href="#">Gallery</a></li>
          <li><a href="#">Contact</a></li>
      </ul>
  </nav>
</header>

  • Thank you so much for your help. Really forgotten to put the class in the div here in the file, but not in the link I passed rsrs Well this is right in the margin 0 of the body. I always get distracted by this. When the ul padding hadn’t noticed this. If you don’t give the padding, there’s a space in the left corner. If I change Justify-content to flex-end, then this padding would not be necessary, but to the left is accurate. The only problem is still the search text box issue. I want it to stay inside the u,. be it in the right or left corner and not out of context.

  • That’s exactly what I wanted as shown in the answer?

  • Almost this. Only part of the text box was missing as I explained above. I want the search box to be inside the box model like the links. Be it in the left or right corner. It is out of content as if starting a new box model.

  • Look now.....

  • Perfect. Thank you very much. If you don’t mind, could you explain to me about the changes? 1. You removed the ul background and passed it to Nav; .

  • That’s right. The background should be on Nav and not UL. Then I centered the search box and put a margin above and to the right so it doesn’t stick to the links.

  • Thank you very much indeed. 1. You said that the background has to be in Nav and not in ul . Any technical/semantic explanation? 2. Finally, I think it really ends my doubts. If I want to position the search box for the beginning or the end of the box how would I do? I tried to modify the margin, but I changed the configuration of the central links, which I don’t want. I tried the align-item for flex-start and it didn’t work. Align-content has no effect when flexbox has only one line. Any suggestions?

  • It is because the blue background was only in UL. And this was affecting only UL and not Nav. You position the box by changing the position of the LI where it is. In the case of my example, I put as first LI.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.