WutWut February 2016

Setting width of an inner div to the width of the container

I am trying to modify the bootstrap css, I want the .dropdown-menu to expand to the full width of the container, but setting width:100% sets the width to parent width. I tried using jquery and this works but as the menus increase they don't fit into the container. how do I fix this such that each dropdown menu has the same width and fits the whole container from left to right? jsfiddle

jquery

$(window).bind("load resize", function(){
$('.extend').width($('.container').width());
});

html

<div class="container">

  <!-- Static navbar -->
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu extend">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li role="separator" class="divider"></li>
                      

Answers


Sahil February 2016

$(window).bind("load resize", function(){
    adjust();
 $('.extend').width($('.container').width());
});

function adjust(){
$('.dropdown-toggle').each(function(){
var left = ($('body').width()-$('.container').width())/2;
  $(this).parent().find('.dropdown-menu').css('margin-left','-'+($(this).offset().left-left)+'px');
 });
}
adjust();

Please try this out. Here is the working fiddle.

Post Status

Asked in February 2016
Viewed 2,923 times
Voted 14
Answered 1 times

Search




Leave an answer