Bootstrap - Aligning content in table header - css

I have a web page that is using Bootstrap 3. In this web page, I am using a table with column headers. The column headers need to be centered. However, I also need to have a drop down menu in the upper-right corner of each column header. This last part is what's throwing everything off.
As you can see in this Bootply, my column headers aren't centered properly because of the drop down menu. That drop down menu isn't even positioned properly. I've tried using relative positioning, but apparently that doesn't work in a column header. My code looks like this:
<table class="table">
<thead>
<tr><th>Name</th>
<th class="text-center">
<div>Chicago</div>
<small class="text-muted">Illinois</small>
<div class="pull-right">
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" id="chicagoDropDownMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<i>:</i>
</button>
<ul class="dropdown-menu dropdown-menu-right pull-right" aria-labelledby="chicagoDropDownMenu">
<li>Weather</li>
<li>Visitors Guide</li>
<li>Directions</li>
</ul>
</div>
</div>
</th>
<th class="text-center">
<div>New York</div>
<small class="text-muted">New York</small>
<div class="pull-right">
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" id="nyDropDownMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<i>:</i>
</button>
<ul class="dropdown-menu dropdown-menu-right pull-right" aria-labelledby="nyDropDownMenu">
<li>Weather</li>
<li>Visitors Guide</li>
<li>Directions</li>
</ul>
</div>
</div>
</th>
<th class="text-center">
<div>San Francisco</div>
<small class="text-muted">California</small>
<div class="pull-right">
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" id="sfDropDownMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<i>:</i>
</button>
<ul class="dropdown-menu dropdown-menu-right pull-right" aria-labelledby="sfDropDownMenu">
<li>Weather</li>
<li>Visitors Guide</li>
<li>Directions</li>
</ul>
</div>
</div>
</th>
</tr></thead>
</table>
My question is, how do I position a drop down menu in the upper-right corner of a column header, and still center the rest of the content?

Just use absolute positioning to make the text ignore the dropdown buttons. You can then move them around relative to their parent (the th tag). Note that you do need to give the th tag a position of relative for this to work:
CSS
.dropdown {
position: absolute;
top: 0;
right: 0;
}
th {
position: relative;
border: 1px solid red;
}
Note: The red border is just there to help clarify what's going on.
Hope this helps!

Related

Bootstrap 4: Truncate text within dropdown button

I have a dropdown button the text of which is composed from a fixed part and a second part from a database. This second text might be rather long and I'd like to truncate it. To be able to use bootstrap's .text-truncate class (or overflow:hidden and text-overflow:ellipsis), I have to add display:inline-block to the text in the button. However, this messes the baselines up (for some reasons I don't know, the buttons in the following snippet don't look like expected, but the problem of the misaligned baselines is still visible):
.mw-15 {
max-width: 15rem;
}
.mw-10 {
max-width: 10rem;
}
.display-ib {
display: inline-block;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<p>
In the following, the caret pointing down is missing.
</p>
<div class="drowpdown ml-3">
<button type="button" class="btn dropdown-toggle text-truncate mw-15" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Fixed text: Some variable text which might be rather long.
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="?foo=bar">An item</a>
<a class="dropdown-item" href="?foo=baz">Another item</a>
</div>
</div>
<p class="mt-5">
Here, the baselines don't match.
</p>
<div class="drowpdown ml-3">
<button type="button" class="btn dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Fixed text: <span class="text-truncate mw-10 display-ib">Some variable text which might be rather long.</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="?foo=bar">An item</a>
<a class="dropdown-item" href="?foo=baz">Another item with quite longish text.</a>
</div>
</div>
<p class="mt-5">
Here, the text is not centered vertically in the button.
</p>
<div class="drowpdown ml-3">
<button type="button" class="btn dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="text-truncate mw-10 display-ib">Fixed text: Some variable text which might be rather long.</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="?foo=bar">An item</a>
<a class="dropdown-item" href="?foo=baz">Another item with quite longish text.</a>
</div>
</div>
Does anyone know how to truncate the text within a dropdown button without messing up the baselines?
Try adding d-flex align-items-center css classes on the button (.btn.dropdown-toggle) element which holds the text-truncate.

First dropdown works, second doesnt bootstrap 3

In bootstrap I want to have 2 dropdowns, Only the first one in the code works. Even if I copy and paste
<div class="dropdown" style="position: absolute; left: 30%">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Games
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>1</li>
<li>2</li>
<li><a href="comingsoon/index.html">Coming Soon!</h5></li>
</ul>
</div>
<div class="dropdown" style="position: absolute; left: 60%">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Games
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>1</li>
<li>2</li>
<li><a href="comingsoon/index.html">Coming Soon!</h5></li>
</ul>
</div>
(If i click the first one it opens the menu, if I click the second one it does nothing. These both are the same exact ones and do not work together)
The issue is that you have unclosed anchor tags. In two places you have </h5> in place of </a>. If you resolve that problem it works correctly.
<div class="dropdown" style="position: absolute; left: 30%">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Games
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>1</li>
<li>2</li>
<li>Coming Soon!</li>
</ul>
</div>
Bootply

Setting dropdown menu width to auto

My question is best described by simple JSFiddle. All I want to do is to make my dropdown menu width be dependent on its content width so that all option labels are on single line without line breaks.
JSFiddle
<div>
<span style='cursor:pointer; float:right'>
<div class="btn-group pull-right">
<button id = 'majic' type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-cog"></span>
</button>
<ul style = 'padding:10px' class="dropdown-menu">
<li class="dropdown-submenu">
<input id="full_package_filter" type="checkbox" name="vehicle" checked>С полным пакетом документов<span class='useful_stat'></span>
<br>
<input id="not_full_package_filter" type="checkbox" name="vehicle" checked>С неполным пакетом документов<span class='useful_stat'></span>
<br>
</li>
<li class="dropdown-submenu"></li>
<li class="dropdown-submenu"></li>
<li class="dropdown-submenu"></li>
</ul>
</div>
</span>
</div>
Try adding white-space: nowrap; rule to your dropdown menu.
.dropdown-submenu {
white-space: nowrap;
}

how should i eliminate button shrinkage?

I'm looking for the right way of displaying buttons around an input field in one row, and avoid (if possible) the use the "width" attribute. I want the buttons and field to line up in a fixed width and position even as the value (that the user chooses from the drop down) change
<div class="row" style="margin-top: 30px;">
<div class="col-md-3"></div>
<div class="col-md-6">
<form class="form-inline">
<div class="form-group" style="padding-left: 40px;">
<div class="dropdown">
<button type="button" class="btn btn-default btn-lg dropdown-toggle btn-block" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-map-marker" style="color:blue"></i> United States <span class="caret"></span>
</button>
<ul class="dropdown-menu columns">
<li><strong>United States</strong></li>
<li>France</li>
<li>United Kingdom</li>
</ul>
</div>
</div>
<div class="form-group">
<div class="dropdown">
<button type="button" class="btn btn-default btn-lg dropdown-toggle btn-block" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
USD <span class="caret"></span>
</button>
<ul class="dropdown-menu columns">
<li><strong>USD</strong></li>
<li>EUR</li>
</ul>
</div>
</div>
<div class="form-group">
<label for="amountnum"> </label>
<input style="font-weight: bold;text-align: center" type="text" class="form-control input-lg" id="amountnum" placeholder="number" value="10">
</div>
<div class="form-group">
<div class="dropdown">
<button type="button" class="btn btn-default btn-lg dropdown-toggle btn-block" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
1 month <span class="caret"></span>
</button>
<ul class="dropdown-menu columns">
<li>1 Weeks</li>
<li>2 Weeks</li>
<li><strong>1 month</strong></li>
</ul>
</div>
</div>
</form>
</div>
<div class="col-md-3"></div>
</div>
</div>
jsfiddle: https://jsfiddle.net/drorm/k43kyfpw/
when the screen opens, make the left pane narrower to see the buttons line up (instead of falling dropping to separate rows)
Suggestions are appreciated
Add the following CSS style
.navbar-form,.form-inline {
display: flex;
}
The use of flex (or inline-flex) is quite powerful. Combine with the css flex property on items to control the growth and shrinking of controls.
Google "CSS flex" for more.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

how to make bootstrap dropdown to fill the entire col width

By default, dropdown on bootstrap are dynamic based on the length of the text of the selected item. This will break the harmony of my layout and I can't find a solution.
Basically I want a row containing a search input and on the right side a dropbox. Something very simple...How to make this possible ?
<div class="container-fluid">
<div class="row" id="searchAndLogin">
<div class="col-md-9">
<div class="input-group">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div>
</div>
<div class="col-md-3">
<div class="btn-group">
<button type="button" class="btn btn-default">Action</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
</ul>
</div>
</div>
</div>
You can set the width of the btn-group and its btn elements as well as the dropdown-menu to fit the width of the column you place it on.
CSS
#searchAndLogin .btn-group {width:100%;}
#searchAndLogin .btn-group .btn {width:90%;}
#searchAndLogin .btn-group .btn.dropdown-toggle {width:10%;}
#searchAndLogin .btn-group .dropdown-menu {width:100%;}
Live example here: http://www.bootply.com/WPhb5tLZmh

Resources