Bootstrap nav-bar: how to vertical align wrapped-/multiple line text - css

I'm trying to vertical align text on a bootstrap navbar item. The text is wrapped on two lines. Its no problem to align a single line text but it gets much more difficult to vertical-align when its on two lines. I cant use line-height are there any alternatives? I havent seen any good solutions on wrapped text, many solutions doesn't take into account multiple lines of text.
HTML code
<ul class="nav navbar-nav">
<li class="dropdown">
SHORT SINGLE LINE
</li>
<li class="dropdown">
VERY LONG TEXT, WRAPPED TEXT
</li>
</ul>
CSS code
.navbar-nav > li > a
{
height: 58px;
line-height: 19px;
padding-top: 10px;
text-align: center;
}

You can use display:table-cell and display:table on the parent div instead of float:left; on the lis.
I am sure bootstrap nav elements are floated. Check this code:
.navbar-nav > li > a
{
height: 58px;
line-height: 19px;
text-align: center;
padding: 0 5px;
}
.navbar-nav{
display: table;
background: #f5f5f5;
padding: 0;
margin: 0;
}
.navbar-nav > li{
display: table-cell;
vertical-align: middle;
padding: 10px;
float: none !important;
text-align: center;
border-left: 2px solid #fff;
}
.navbar-nav > li:first-child{border: 0;}
<ul class="nav navbar-nav">
<li class="dropdown">
SHORT SINGLE
</li>
<li class="dropdown">
VERY LONG TEXT, <br> done WRAPPED TEXT
</li>
</ul>

this is all what you need :
.navbar-nav > li > a
{
text-align: center;
display: inline-block;
vertical-align: middle;
}
and you have to remove these two :
height: 58px;
line-height: 19px;
LIVE DEMO

.navbar-nav > li > a
{
height: 58px;
line-height: 19px;
padding-top: 10px;
text-align: center;
}
you just remove that part, its not neccesary, the text stays vertically centered on its own

Related

Adding padding to <li> in CSS is making the content not move with consistent line-height

My predicament is this. I have a list, a simple cart, login, user registration list. I want to move the list up by adding padding. But I cannot with out the list adding line height. What is the way around this? See examples below. This list is in the header.
Before:
.content{
border: 2px solid #000;
}
li {
list-style-type: none;
font-size: 10px;
direction: right;
text-align: right;
padding-bottom: 20px;
line-height: 1;
display: block;
}
<div class="content">
<ul>
<li>Cart</li>
<li>Login</li>
<li>Customer Registration</li>
</ul>
</div>
After:
You need to add the padding to the ul and not the li .
give margin-top in minus so it will move upside
Try This:
ul {
margin-top: 0;
}
ul {
margin-top: 0;
}
li {
list-style-type: none;
font-size: 10px;
direction: right;
text-align: right;
padding-bottom: 20px;
line-height: 1;
display: block;
}
<ul>
<li>Cart</li>
<li>Login</li>
<li>Customer Registration</li>
</ul

Can't get text link to vertically center next to a set of image links

I am trying to vertically center the elements in the black rectangle. So far, I have the icons vertically centered, but for some reason, the text link to the right of it is slightly off-centered. I tried adding padding to the top and bottom but it still stays off-centered. I've also tried adding a vertical-align, but that didn't work either.
I made a fiddle to make it easier: http://jsfiddle.net/z0Lxfw6q/1/
HTML
<nav id="site-navigation" class="main-navigation" role="navigation">
<ul id="social-links">
<li class="behance"><img src="http://i.imgur.com/KTsyaQR.png" alt="Behance"></li>
<li class="twitter"><img src="http://i.imgur.com/cleZOWn.png" alt="Twitter"></li>
<li class="instagram"><img src="http://i.imgur.com/USZmKla.png" alt="Instagram"></li>
</ul>
<a class="contact" href="/contact">Get in touch</a>
</nav>
CSS
#site-navigation {
background: #000;
position: relative;
width: 100%;
}
#site-navigation #social-links {
border-right: 2px solid #E0E0E0;
display: inline-block;
line-height: 0;
margin: 0 30px 0 0;
padding: 25px 0;
padding-right: 30px
}
#site-navigation #social-links li {
display: inline-block;
list-style: none;
}
#site-navigation #social-links img {
height: 26px;
}
#site-navigation .contact {
color: #fff;
display: inline-block;
text-transform: uppercase;
font-size: 12px;
font-weight: bold;
}
How would I get the text link (i.e. "Get in touch") to vertically center?
You need to add vertical-align: middle to #site-navigation #social-links.
Updated jsFiddle
As pointed out by #4dgaurav, inline-block elements are always vertical-align: bottom by default. So you need to change that.
inline-block elements are vertical-align: bottom by default.
so just add vertical-align: middle to get it in middle where ever you want inline-block element vertically middle.

100% children height in horizontal navigation with dynamic logo/element height

I've browsed around for a while but I believe I have a slightly more unique situation here.
I have a simple navigation which is a <ul> list, with each <li> containing an anchor <a>. The first <li> contains an image which is the logo.
I'd like for each of the subsequent <li> AND <a> elements to expand to the height of the logo, whatever it may be (I would like for the only static height in this solution to be defined on the logo's image, if possible).
The elements should also be vertically centered.
When you hover on the <a> tags, the background and cursor selection should cover the entire height.
I've tried using display: flex; and display: table-cell, with vertical-align: middle and a bunch of other things. I've almost got it but there is some empty space above and below the <a> tags. The only way I can see to fix it is to use a static height on the <a> tags, but I'm posting here to see if anyone knows of any alternatives.
Please note that this doesn't have to be a cross-browser solution (although that would be nice).
Also note that these elements will include a sub-navigation dropdown, so solutions which might include overflow: hidden may not be applicable in that case.
Here is the code:
a {
font-family: 'Trebuchet MS';
text-decoration: none;
}
nav {
background: #444;
}
nav > ul > li {
display: table-cell;
vertical-align: middle;
}
nav > ul > li img {
display: table-cell;
vertical-align: middle;
height: 24px;
padding: 24px;
}
nav > ul > li a {
display: table-cell;
vertical-align: middle;
padding: 24px;
color: #fff;
}
nav > ul > li:hover a {
cursor: pointer;
background: white;
color: #444;
}
<nav role="navigation">
<ul>
<li>
<img src="http://i.imgur.com/ZPB7f3l.png" />
</li>
<li>About
</li>
<li>Work
</li>
<li>Ideas
</li>
<li>Contact
</li>
</ul>
</nav>
Here is the fiddle: http://jsfiddle.net/0br6r0sh/1
Any advice is appreciated!
Thanks,
Ryan
I've tried to implement your requirements using Flexbox. Unfortunately not in a cross browser fashion as it only works on Chrome.
The <img> is given a display: block to remove all padding/margin around it. I've used three sets of display: flex, for the <ul>, <li> and <a>.
a {
font-family: 'Trebuchet MS';
text-decoration: none;
}
nav {
background: #444;
}
nav > ul {
display: flex;
flex-direction: row;
align-items: stretch;
}
nav > ul > li {
background-color: #555;
display: flex;
}
nav > ul > li img {
height: 24px;
padding: 24px;
background-color: #ccc;
display: block;
}
nav > ul > li a {
display: flex;
flex-direction: column;
justify-content: center;
padding: 0 24px;
color: #fff;
background-color: #666;
}
nav > ul > li:hover a {
cursor: pointer;
background: white;
color: #444;
}
<nav role="navigation">
<ul>
<li>
<img src="http://i.imgur.com/ZPB7f3l.png" />
</li>
<li>About
</li>
<li>Work
</li>
<li>Ideas
</li>
<li>Contact
</li>
</ul>
</nav>

White Space between vertical nav bar and paragraph

I have some mysterious white space that I don't know how to get rid of. I have a vertical list and I want it to be right next to the paragraph with text. Here is my HTML and CSS
SOLVED BY jmgem I ALSO HAD TO READJUST THE SIZE OF MY PARAGRAPH ELEMENT SO IT WOULD FIT BESIDE THE NAV BAR
HTML
<div id="classOfferingList" class="classOfferingList" align="left">
<ul>
<li>
<a href="" >General U.S. K-12 English Speaking Course</a>
</li>
<li>
<a href="" >University Preperation Course</a>
</li>
<li>
<a href="" >SAT Preperation Course</a>
</li>
<li>
<a href="" >GRE Preperation Course</a>
</li>
</ul>
</div>
<div id="classOfferingInfo" >
<p>example text</p>
</div>
CSS
.classOfferingList ul {
list-style-type: none;
float: left;
}
.classOfferingList ul li {
margin: 5px 0px;
}
.classOfferingList ul li a {
list-style: none;
margin: 1px 0px;
display: inline-block;
background-color: blue;
width: 35%;
text-align: center;
}
First of all, I copied your code into jsfiddle. Go on in and have a look.
http://jsfiddle.net/GyuX5/
If I understand your question correctly, you wanted to put the paragraph right next to the vertical menu. So here's your adjusted CSS
.classOfferingList ul {
list-style-type: none;
float: left;
}
.classOfferingList ul li {
margin: 5px 0px;
}
.classOfferingList ul li a {
list-style: none;
margin: 0px;
display: inline-block;
background-color: grey;
text-align: center;
width: 150px;
}
#classOfferingInfo {
display: inline-block;
}
I had your paragraph display as an inline-block, then I changed the width of the li a to 150px instead of 35%. Voila.
Chose not to use a left float as they tend to disrupt layouts as they become more complicated. try to imagine html/css as blocks filling up a row in the browser from left to right.
Try floating the two main div to the left then they will be right next to each other. See Fiddle
.classOfferingList {
float: left;
}
.classOfferingList ul {
list-style-type: none;
float: left;
}
.classOfferingList ul li {
margin: 5px 0px;
}
#classOfferingInfo {
float: left;
}
.classOfferingList ul li a {
list-style: none;
margin: 1px 0px;
display: inline-block;
background-color: blue;
width: 35%;
text-align: center;
color: #fff;
}
http://jsfiddle.net/erenyener/TC856/
#classOfferingList > ul
{
padding:0px;
}
you need to reset ul element
or your question could be this
FIDDLE

White space on vertical menu IE7

I have an expandable menu.
On IE7 I'm getting a white gap at the top and the bottom. On IE8 just at the top. I've tried different suggestions but nothing seems to help.
You can see the site in here:
http://dffernandez.com/client_review_files/wap/
The problem is on the "infraestructure" button (The 2nd one)
Thank you in advance.
HTML
<!--Aside left nav-->
<div id="secondary-nav">
<ul>
<li class="leaf-first"><img src="img/left-aside/sec-nav-arrow.png" width="21" height="21" class="sec-nav-arrow" alt="">airport & location details</li>
<li class="expand-top"></li> <!--Extra li so it can expand background-->
<li class="leaf-expand">infraestructure
<!--Submenu-->
<ul class="asidel-submenu">
<li class="leaf-first">Current Tenants</li>
<li class="leaf">Industry Especific Info</li>
<li class="leaf">Aerospace</li>
<li class="leaf">Unmanned Aerial Vehicles</li>
<li class="leaf">Repair</li>
<li class="leaf-las">Summary of Master Plan</li>
</ul> <!--Submenu ends-->
</li> <!--Expand-->
<li class="expand-bottom"></li> <!--Extra li so it can expand background-->
<li class="leaf"><img src="img/left-aside/sec-nav-arrow.png" width="21" height="21" class="sec-nav-arrow" alt="">communities</li>
<li class="leaf"><img src="img/left-aside/sec-nav-arrow.png" width="21" height="21" class="sec-nav-arrow" alt="">newsroom</li>
<li class="leaf-last"><img src="img/left-aside/sec-nav-arrow.png" width="21" height="21" class="sec-nav-arrow" alt="">location map</li>
</ul>
</div> <!--Aside left nav-->
CSS
/*secondary nav*/
#aside-left #secondary-nav li{
list-style: url(none) none;
margin-bottom: 13px;
}
#aside-left #secondary-nav li a {
height: 31px;
display: block;
background: url(../img/left-aside/sec-nav-back.png);
text-transform: uppercase;
text-decoration: none;
padding-top: 12px;
padding-left: 12px;
}
#aside-left #secondary-nav ul .leaf-expand a {
background: url(../img/left-aside/sec-nav-expand-back.png) repeat-y;
height: auto;
padding-top: 6px;
padding-bottom: 6px;
}
#aside-left #secondary-nav ul .leaf-expand img { /*controls the arrow position next to the expand. For changing the image go to js/script.js*/
display: block;
float: left;
margin-right: 6px;
padding-left: 12px;
margin-top: 3px;
}
#aside-left #secondary-nav ul .expand-top {
background: url(../img/left-aside/sec-nav-expand-back-top.png) no-repeat;
height: 7px;
margin-bottom: 0px;
}
#aside-left #secondary-nav ul .expand-bottom {
background: url(../img/left-aside/sec-nav-expand-back-bottom.png) no-repeat;
height: 6px;
margin-top: -13px;
}
#aside-left #secondary-nav .asidel-submenu {
padding-left: 39px;
padding-right: 12px;
padding-bottom: 9px;
background: url(../img/left-aside/sec-nav-expand-back.png) repeat-y;
}
#aside-left #secondary-nav .asidel-submenu li {
list-style: disc inside;
margin-bottom: 3px;
color: #0073BC;
}
#aside-left #secondary-nav .asidel-submenu li a {
text-transform: none;
display: inline;
padding-left: 0px;
}
#aside-left #secondary-nav .sec-nav-arrow {
margin-right: 6px;
display: block;
float: left;
margin-top: -3px;
}
I've come across this problem before, the fix is simple, yet somewhat odd. All explained fully here - http://www.456bereastreet.com/archive/200610/closing_the_gap_between_list_items_in_ie/
The gist of it, in IE7, is this:
li a {display:inline-block;}
li a {display:block;}
That's right, you have to say display: inline-block BEFORE you say display: block. Weird, huh? But it works!
And you can do it in all browsers too.
EDIT:
My bad, I jumped to a conclusion about your post - here's a better answer for you:
The way you're doing it is a really crazy way to try and accomplish it. The "graphic" you're trying to preserve can be replicated in CSS by a simple border. Take a look at this fiddle:
http://jsfiddle.net/CC4gv/
Now you can get rid of the extra li's altogether.
The important rules of styling list-based menus:
Reset your list ul, li {padding:0;margin:0}
Do not style the list elements (ul, li) for anything other than position, float or display
Use display:block and put all styling on the A-tag
This takes care of 99% of all list layout problems.
Also, UI elements should be CSS background images, not inline tags. This will help you maintain more control over the layout.
This is all explained in my tutorial: I Love Lists.

Resources