Align separate li to right

Joined
Aug 21, 2023
Messages
36
Reaction score
0
Hello everyone. I have nav-tabs aligned to. the left, but I want she search bar and the logo (svg) align to the right side of the nav-tabs. I tried with text-align:right and float:right but that didn't work. Is this maybe because of I use bootstrap, and how can I do this?

HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sloppy music</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap-4.4.1.css" rel="stylesheet">
    <link href="styles.css" type="text/css" rel="stylesheet">
    
  </head>
  <body>
      <div class="container">
        <div class="row">
      <div class="col-xl-12">
        <ul class="nav nav-tabs">
          <li class="nav-item"> <a class="nav-link active" href="#">Artists</a> </li>
          <li class="nav-item"> <a class="nav-link" href="#">Playlists</a> </li>
          <li class="nav-item"> <a class="nav-link" href="#">About</a> </li>
          <li class="nav-item"> <a class="nav-link " href="#">Contact</a> </li>
            <li><input type="text" placeholder="Type text">
              <button type="button" class="btn btn-primary">Search</button>
            </li>
            <li><img src="images/sloppy.svg" class="img-fluid" width="50" id="logo"></li>
        </ul>
      </div></div>
      </div>
    </body>
</html>
 
Joined
Jul 4, 2023
Messages
406
Reaction score
47
Change this part of code html in <ul> ...
HTML:
<li>
  <input type="text" placeholder="Type text">
  <button type="button" class="btn btn-primary">Search</button>
</li>
<li>
  <img src="images/sloppy.svg" class="img-fluid" width="50" id="logo">
</li>
... to
HTML:
<li class="ml-auto d-flex align-items-center">
  <input type="text" placeholder="Type text" class="form-control mr-2">
  <button type="button" class="btn btn-primary">Search</button>
  <img src="images/sloppy.svg" class="img-fluid ml-2" width="50" id="logo">
</li>

The <li> element now has the d-flex class to make it a flex container, with the ml-auto class to push elements inside to the right within the flex container and align-items-center class are added too for ensure the items inside it are aligned properly and spaced out.

full code:
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sloppy music</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="css/bootstrap-4.4.1.css" >
    <link rel="stylesheet" href="styles.css" type="text/css">
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-xl-12">
          <ul class="nav nav-tabs">
            <li class="nav-item"> <a class="nav-link active" href="#">Artists</a> </li>
            <li class="nav-item"> <a class="nav-link" href="#">Playlists</a> </li>
            <li class="nav-item"> <a class="nav-link" href="#">About</a> </li>
            <li class="nav-item"> <a class="nav-link " href="#">Contact</a> </li>
            <li class="ml-auto d-flex align-items-center">
              <input type="text" placeholder="Type text" class="form-control mr-2">
              <button type="button" class="btn btn-primary">Search</button>
              <img src="images/sloppy.svg" class="img-fluid ml-2" width="50" id="logo">
            </li>
          </ul>
        </div>
      </div>
    </div>
  </body>
</html>

1718828958830.png


According to this article: How Auto Margins Work in Flexbox
 
Last edited:
Joined
Aug 21, 2023
Messages
36
Reaction score
0
Change this part of code html in <ul> ...
HTML:
<li>
  <input type="text" placeholder="Type text">
  <button type="button" class="btn btn-primary">Search</button>
</li>
<li>
  <img src="images/sloppy.svg" class="img-fluid" width="50" id="logo">
</li>
... to
HTML:
<li class="ml-auto d-flex align-items-center">
  <input type="text" placeholder="Type text" class="form-control mr-2">
  <button type="button" class="btn btn-primary">Search</button>
  <img src="images/sloppy.svg" class="img-fluid ml-2" width="50" id="logo">
</li>

The <li> element now has the d-flex class to make it a flex container, with the ml-auto class to push elements inside to the right within the flex container and align-items-center class are added too for ensure the items inside it are aligned properly and spaced out.

full code:
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sloppy music</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="css/bootstrap-4.4.1.css" >
    <link rel="stylesheet" href="styles.css" type="text/css">
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-xl-12">
          <ul class="nav nav-tabs">
            <li class="nav-item"> <a class="nav-link active" href="#">Artists</a> </li>
            <li class="nav-item"> <a class="nav-link" href="#">Playlists</a> </li>
            <li class="nav-item"> <a class="nav-link" href="#">About</a> </li>
            <li class="nav-item"> <a class="nav-link " href="#">Contact</a> </li>
            <li class="ml-auto d-flex align-items-center">
              <input type="text" placeholder="Type text" class="form-control mr-2">
              <button type="button" class="btn btn-primary">Search</button>
              <img src="images/sloppy.svg" class="img-fluid ml-2" width="50" id="logo">
            </li>
          </ul>
        </div>
      </div>
    </div>
  </body>
</html>

View attachment 2843

According to this article: How Auto Margins Work in Flexbox
Thank you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,664
Latest member
Phil79581

Latest Threads

Top