Bootstrap 4: Truncate text within dropdown button - css

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.

Related

What is causing extra whitespace in Bootstrap dropdown?

What is causing my dropdown to have extra whitespace? (Apologies for the screenshot)
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="caret">Snooze</span>
</button>
<div class="dropdown-menu">
<button class="dropdown-item">1 Hour</li>
</div>
</div>
As far as I know there is nothing strange in my CSS files that would cause this. I am using Bootstrap4.
Check out this code. I removed <span class="caret"></span> because it's not a valid class in bootstrap. I changed btn-default to other button, btn-primary in this case.
The most important error - look at dropdown-menu. You open button tag and close li so I changed that to div and it works nicely.
Check out Bootstrap Docs for more info here https://getbootstrap.com/docs/4.3/components/dropdowns/.
/*DEMO*/
body{padding:3rem}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Snooze</button>
<div class="dropdown-menu">
<div class="dropdown-item">1 Hour</div>
</div>
</div>
Your HTML structure is wrong. Check what you have inside the .dropdown-menu element.
Instead of <button class="dropdown-item">1 Hour</li> use <li class="dropdown-item">1 Hour</li>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Snooze
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li class="dropdown-item">1 Hour</li>
</div>
</div>

image background instead of button for dropdown

I already have a dropdown with the following code:
note: class='bg_r' is in CSS for the background image (url) globe
<span class="bg_r">
Language: <a id="Lang" href="#">DropUpâ–˛</a>
</span>
<div id="language_menu" class="lang_switch_panel">
<div class="lang_switch">
<ul>
<li>Action</li>
<li>Action</li>
<li>Action</li>
</ul>
</div>
</div>
The output for above code is as shown in figure 1 below:
Now, I'm doing similar to that(above figure) using bootstrap 4.But i'm not able to override the button style using background image.Instead of button type I replaced with anchor tag but of no use(its wrong method but i tried).
Following is the code I tried so far:
<span class="bg_r">
Language:
<button type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropup
</button>
</span>
<!--<span class="bg_r">Language:
<a type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropup
</a>
</span>-->
<div class="dropdown-menu">
<!-- Dropdown menu links -->
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Action</a>
</div>
The output for the code using bootstrap is as shown in figure 2 below:
How can I override button type with (background) image to get output similar to figure 1?
Thanks in advance.
Add btn-link class to button
Additionally add these styles to center align it
.bg_r {
background-color: aqua;
display: inline-flex;
align-items: center;
}
or add these 2 classes d-inline-flex align-items-center to bg_r span
.bg_r {
background-color: aqua;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<span class="bg_r d-inline-flex align-items-center p-1">
Language:
<button class="btn btn-sm btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropup
</button>
</span>
<div class="dropdown-menu">
<!-- Dropdown menu links -->
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Action</a>
</div>
You can also use bg-transparent on the button that will remove the background-color from button, but will not remove the focus styles when you click on the button ! So preferably add btn-link class to it only.
The above answer works, or you can also replace this line
<button type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropup</button>
by
Dropup

Bootstrap - Aligning content in table header

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!

Angular Bootstrap split dropdown button not working

I am trying to get bootstrap dropdown list working on plnkr but it is not dropping down the list, I have created a plunk here:
http://plnkr.co/edit/tw3O5ihT7YB79BcAGlsh
This code was copied from bootstrap:
<body ng-controller="MainCtrl as vm">
<!-- Split button -->
<div class="btn-group">
<button type="button" class="btn btn-danger">Action</button>
<button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li ng-repeat="a in vm.exportAction">
<a ng-if="a.visble == true">
<span ng-click="vm.selectedExport(a)">{{a.action}}</span>
</a>
<div ng-if="a.divider == true">
<div role="separator" class="divider"></div>
</div>
</li>
</ul>
</div>
</body>
Any ideas?
Add Jquery and Bootstrap JavaScript files in head after Bootstrap's CSS - plnkr:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

BootStrap DropMenu Not Working

I have created a dropdown menu but it is not working:
<div class="col-xs-6 col-md-4">
<ul id="hld" style="margin-top:10px;">
<li class="dropdown" style="border:1px solid #333;padding:5px;text-align:center;">
Menu
<ul class="dropdown-menu">
<li>Tom</li>
<li>Greg</li>
<li>Jane</li>
</ul>
</li>
</ul>
</div>
I am using Bootstrap CSS framework and have included all the appropriate CSS and JS files. What am I doing wrong? While looking at other questions similar to mines, I haven't been able to figure it out.
One solution may be that you are not referencing the jquery.js source before bootstrap.js source
You're missing some key elements.
This is what your dropdown element should look like.
<li class="dropdown" style="border:1px solid #333;padding:5px;text-align:center;">
This goes with the dropdown-toggle class.
data-toggle="dropdown" aria-expanded="false"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-xs-6 col-md-4">
<ul id="hld" style="margin-top:10px;">
<li class="dropdown" style="border:1px solid #333;padding:5px;text-align:center;"> Menu <span class="caret"></span>
<ul class="dropdown-menu ">
<li>Tom
</li>
<li>Greg
</li>
<li>Jane
</li>
</ul>
</li>
</ul>
</div>
First mistake was a couple of quotation marks missing. On top of that, you are not following the syntax and classes listed here: http://getbootstrap.com/components/#dropdowns
Here is the correct version:
<div class="col-xs-6 col-md-4">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Menu
<span class="caret"></span>
</button>
<ul id="hld" class="dropdown-menu" style="margin-top:10px;">
<li>Tom</li>
<li>Greg</li>
<li>Jane</li>
</ul>
</div>

Resources