hossein
hossein

Reputation: 45

I want to set the logo on left and links on right but float dont work on nav tag ... why?

I use this code fot navigation and also using bootstrap for my practice

<!DOCTYPE html>
  <html lang="fa">
    <head>
        <meta charset="UTF-8">
        <title>welcome to your site</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" ></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" ></script>
    </head>
    <body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <a class="navbar-brand" href="#"><img src="img/mehrlogo.png" width="120" alt=""></a>
      <button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Features</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Pricing</a>
          </li>
          <li class="nav-item">
            <a class="nav-link disabled" href="#">Disabled</a>
          </li>
        </ul>
      </div>
    </nav>
    </body>
    </html>

How can I set logo on right or left, and the links on the other side?

By default the link and logo are near each other and float left or right didn't work - what should I do?

Upvotes: 1

Views: 45

Answers (2)

Amir7
Amir7

Reputation: 42

It is better to use this: If you want to put a link (or other elements) to the right, add ml-auto class to your element; and if you want to put it on left, add mr-auto class to your element.

Upvotes: 0

tao
tao

Reputation: 90038

The .navbar uses display: flex to position its elements. Which means you can reposition or arange them by changing flex-direction and justify-content on .navbar but also pay attention to flex-grow, flex-shrink, flex-basis and left and right margins set on each immediate child of .navbar.

To start from left to right flex-direction: row (default). Right to left: flex-direction: row-reverse.

In terms of rearranging the space between and around them, for any of the above, change justify-content to any of the space-between (default), space-evenly, flex-end, flex-start, center.

Here's an example with them in reverse order and evenly spaced:

nav.navbar {
  flex-direction: row-reverse;
  justify-content: space-evenly;
}
div.navbar-collapse {
  flex-grow: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">LOGO HERE</a>
  <button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
  </div>
</nav>

Note of caution: A lot more properties set on either the parent or any of the children can influence their display behavior. Some of the most common:

  • setting margin: auto on any child will steal all the positive space and give it to that particular gap, making justify-content seem like it's not working. It is working, but its distributing exactly 0 space. If you want to divide the free space to a few select gaps, place a margin:auto in each of those gaps using any adjacent element.
  • setting flex-grow, flex-shrink or flex-basis on any of the children will influence their size and therefore the resulting distribution of available space. A sub-note here would be that by default, on some responsiveness intervals .navbar-collapse has flex-grow: 1 so you need to set it to 0 to be able to redistribute the free space. Otherwise there's no free space.

Upvotes: 2

Related Questions