Home > Net >  Bootstrap Navbar, background to only be on the right and not the entire width
Bootstrap Navbar, background to only be on the right and not the entire width

Time:02-02

Using bootstrap's navbar, I'm only using the dropdown toggler icon on the right. I want a background with a width that covers only the right portion and not the entire width. Any insight would be appreciated.

Attempts 1 and 2:

Attempt 1 Attempt 2

Trying to get:

Solution

.menu {
  font-family: header;
  color: white !important;
  font-size: 3rem;
  background-color: rgba(0, 0, 0, 0.3);
}

.menu-bg {
  background-color: rgba(0, 0, 0, 0.3);
}

body {
  background: #333 !important;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<nav >
  <div >
    <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span ></span> MENU
    </button>

    <div  id="navbarSupportedContent">
      <ul >
        <li >
          <a  href="#features">FEATURES</a>
        </li>
        <li >
          <a  href="#counter">SHARK COUNTER</a>
        </li>
        <li >
          <a  href="#about">ABOUT US</a>
        </li>
        <li >
          <a  href="#faq">FAQ</a>
        </li>
        <li >
          <a  href="#">BACK TO TOP</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>

CodePudding user response:

Kindly try

.menu-bg {
    width: 250px;
}

CodePudding user response:

Here is a working example. The gist is to use position:absolute and set width to dropdown so that it will stick to the right.

.menu {
font-family: header;
color: white !important;
font-size: 3rem;
background-color: rgba(0, 0, 0, 0.3);
}

.menu-bg {
    background-color: rgba(0, 0, 0, 0.3);
}
#navbarSupportedContent {
  width:150px;
  position: absolute;
  right:0px;
  top:100%;
}

.dropdown-wrap {
  position: relative;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<nav >
  <div >
    <div >
      <div ></div>
      <div >
        <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span ></span> MENU
        </button>
        <div  id="navbarSupportedContent">
          <ul >
            <li >
              <a  href="#features">FEATURES</a>
            </li>
            <li >
              <a  href="#counter">SHARK COUNTER</a>
            </li>
            <li >
              <a  href="#about">ABOUT US</a>
            </li>
            <li >
              <a  href="#faq">FAQ</a>
            </li>
            <li >
              <a  href="#">BACK TO TOP</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</nav>

Working JSfiddle

CodePudding user response:

Only:

.menu-bg {
    background-color: rgba(0, 0, 0, 0.3);
    display: inline-block;
}
  •  Tags:  
  • Related