I have code which uses the Twitter Bootstrap 3, nav
with right-arrow
, which I created using border-*
properties. But if I use very long text in right-arrow
, it does not expand, and if I use percents, the code will not work...
Example on JsFiddle
.custom-nav-stacked>li>a {
border-radius: 2px;
}
.custom-nav-stacked>li.active>a:after,
.custom-nav-stacked>li.active>a:hover:after,
.custom-nav-stacked>li.active>a:focus:after {
content: '';
position: absolute;
left: 100%;
top: 50%;
margin-top: -19px;
/*margin-left: -1px;*/
border-top: 19px solid transparent;
border-left: 13px solid #428bca;
border-bottom: 19px solid transparent;
}
<div class="container" style="margin-top: 20px">
<div class="row">
<div class="col-md-2" style="width: 300px /* width there only for pretty demo */">
<ul class="nav nav-pills nav-stacked custom-nav-stacked">
<li class="active"><a href="#">A long long text</a></li>
<li><a href="#">Small text</a></li>
<li class="active"><a href="#">A long long long long long long long long long text</a></li>
<li><a href="#">Small text 111</a></li>
</ul>
</div>
</div>
</div>
Example on JsFiddle
How can I make the triangle responsive based on the amount of text?
The only relative unit (meaning reactive to something else) that
border
can use is thevh
andvw
units. As a result,border
can only be responsively sized when the element it is on is also sized relative to the viewport. DemoAs a result, what you're trying to do with CSS is not currently possible because if you set the height and border with viewport units then they will not respond to the text content. You'd have to give a class to the ones of varying height, thus defeat the purpose of relative sizing anyway.
However, this is pretty easy to do using javascript. You just need to iterate through the relevant elements, calculate the height of the element, divide that by 2 and make that the
border-top
andborder-bottom
, then make a proportion of that theborder-left
. Demo of thatActual (meaning in the DOM) elements are suggested as opposed to pseudo elements using the javascript approach because they are much easier to access using the DOM. If you do end up using pseudo elements, you will need to change the actual stylesheets which is more difficult