I'm trying to position a nav element in a coloured box. The text in this box should be on the bottom and center of this box.
HTML
<div class="sixteen columns">
<section id="mainNav">
<ul>
<li id="nav2" class="navitem">ux / ui</li>
</ul>
</section>
</div><!--end sixteen columns-->
CSS:
/*navigation styles*/
.sixteen.columns #mainNav {
width: 100%;
margin-top: 50px;
}
#mainNav .navitem {
width: 40%;
display: block;
float: left;
position: relative;
pointer: cursor;
}
#mainNav ul li a {
font-family: print_clearlyregular, "Arial", serif;
font-size: 2em;
color: rgb(255, 255, 255);
text-decoration: none;
position: absolute;
bottom: 15px;
left: 0;
}
#mainNav #nav2 {
background: rgb(3, 106, 113);
opacity:0.8;
filter:alpha(opacity=80);
/* For IE8 and earlier */
margin-right: 4px;
height: 150px;
text-align:center;
}
As soon as I absolute position the "ul li a", the text-align: center doesn't work. How should I fix that?
You can see this bit in jsfiddle as well:
jsFiddle
Add width:100%; to
#mainNav ul li a {}
the fiddle: http://jsfiddle.net/sablefoste/bcM2x/
Don't specify any value for left (or right for that matter) for ul li a.
I'd like to add an alternative solution that doesn't rely on floats or absolute positioning, which can get pretty messy.
Using display: table, display: table-cell, and vertical-align: bottom, you can achieve said result. This also scales well when other items around it may change.
Here is a working jsFiddle demo
CSS Changes
I removed all float and position styles, added display: table to the ul, added display: table-cell and vertical-align: bottom to the li, added display: inline-block to the a along with margin-bottom: 15px to retain that padding your absolute positioning created. I modified the selectors and added a few other minor things that you can use or adjust to your liking.
/*navigation styles*/
#mainNav
{
width: 100%;
margin-top: 50px;
}
#mainNav > ul
{
display: table;
width: 100%;
margin: 0;
padding: 0;
border: none;
}
#mainNav > ul > li
{
display: table-cell;
height: 150px;
text-align: center;
vertical-align: bottom;
pointer: cursor;
background: rgb(3, 106, 113);
opacity:0.8;
filter:alpha(opacity=80);
/* For IE8 and earlier */
margin-right: 4px;
}
#mainNav > ul > li > a
{
display: inline-block;
margin-bottom: 15px;
font-family: print_clearlyregular, "Arial", serif;
font-size: 2em;
color: rgb(255, 255, 255);
text-decoration: none;
background: blue; // added for demo to show width and margin
}
Related
I'm not sure why, but past a certain font size the text inside my navigation bar shows up on two lines. The box size isn't being updated for some reason in Chrome and Safari but still works fine in Firefox.
Firefox
Chrome
What would be the difference between these web browsers that would have such an effect on my code?
<nav id="topTab">
<ul>
<li>page1</li>
<li>page2</li>
<li>page3</li>
</ul>
<div>
<h1>
<b href="http://localhost:8000/home.html" title="Home">Example1</b></h1>
</div>
</nav>
CSS:
#media only screen and (min-width : 1024px) {
a {
background: #fcfcfc;
color: #000;
text-align: center;
text-decoration: none;
font-family: 'Gloria Hallelujah';
}
#topTab{
position:relative;
}
nav#topTab {
float: left;
width: 100%;
overflow: hidden;
}
nav#topTab ul {
float: left;
clear: left;
position: relative;
list-style: none;
margin: 0;
padding: 0;
left: 50%;
}
nav#topTab ul li {
display: block;
float: left;
list-style: none;
margin: 0;
padding: 0;
position: relative;
right: 50%;
}
nav#topTab ul li a {
display: block;
padding: 0 5% 0 5%;
margin: 0 15% 0 3%;
font-size: 2.2em;
}
nav#topTab ul li a:hover {
background: #000;
color: #fff;
}
h1 {
position: relative;
text-align: center;
top: 20%;
}
h1 b {
font-size: 2.3em;
text-decoration: none;
color: black;
font-weight: bold;
font-family: 'Caveat Brush';
}
}
Your unordered list is floated. Floating an element removes it from the "natural" flow of the document and as a consequence, your text is trying to adjust to this "unnatural" flow.
You have to clear your floats to restore the flow again. This can be done by adding an element with clear: both style attached to it. In this case, you would add clear both to your div wrapping the heading tag.
div {clear: both}
I am trying to make the top menu vertically center without assigning value like margin-top: 50px; because some of my friends say this is not the ideal approach.
/* Nav Section */
.nav {
width: 100%;
padding: 0;
margin: 0;
}
.nav-contain {
width: 1100px;
margin: 0 auto;
padding: 0;
overflow: hidden;
}
.logo {
z-index: 10;
display: inline-block;
background: #2980B9;
padding: 65px 50px 35px 45px;
font-size: 36px;
line-height: 42px;
color: #fff;
text-align: center;
}
.logo a {
color: #FFFFFF;
text-decoration: none;
}
#medical {
display: block;
text-transform: uppercase;
}
.menu {
padding: 0;
margin: 0;
float: right;
display: table-cell;
position: relative;
}
.menu a {
text-decoration: none;
color: #505050;
font-weight: bold;
}
.menu ul {
padding: 0;
margin: 0;
float: left;
top: 50%;
}
.menu ul ul {
padding: 0;
margin: 0;
}
.menu ul li {
float: left;
display: block;
margin-left: 45px;
}
.menu ul ul {
position: absolute;
left: -999px;
}
.menu ul li:hover ul {
left: auto;
}
.menu ul li ul li {
margin-left: 0;
float: none;
margin-top: 15px;
}
<div class="nav">
<div class="nav-contain">
<div class="logo">
<span id="medical">Medical</span><span id="company"> Company</span>
</div>
<!-- Logo -->
<div class="menu">
<ul>
<li>Home</li>
<li>About
<ul class="dropdown">
<li>Sample</li>
<li>Sample</li>
</ul>
</li>
<li>Gallery</li>
<li>Prices</li>
<li>Contact</li>
</ul>
</div>
<!-- Menu -->
</div>
<!-- Nav Contain -->
</div>
<!-- Nav -->
Remove float:right on .menu, and set both .logo and .menu to this:
.logo, .menu {
display: inline-block;
vertical-align: middle;
}
If you need .menu to stay on far right side, also add this:
.nav-contain {
text-align: justify;
}
.nav-contain:after{
content: '';
display: inline-block;
width: 100%;
}
How it works:
Set text-align: justify; will line up the two inner inline blocks to the left and right edges of the container.
Create an invisible 100% width element by using :after or :before pseudo-element stretching the box to occupy the entire space of the container. Otherwise inline element occupies only the space bounded by the tags that define the inline element.
One easy way to center here is to use Flexbox:
.nav-contain {
/* what is already there */
display: flex;
align-items: center;
}
Beware of browser support (check caniuse.com to see if the compatibility level is acceptable to you).
This is superior to the margin-top solution as it ensures that you won't have to manually change that 50px each time the size of the image or anything else in the navbar changes.
Try:
.menu > ul > li {
min-height:50px;
display: table;
}
.menu > ul > li > a {
display: table-cell;
vertical-align: middle;
}
http://jsfiddle.net/rawat/4h05rq2s/
Since your navbar remains the same height the whole time, I suggest you give the .nav-contain the following code:
.nav-contain {
width: 1100px;
margin: 0 auto;
line-height: 184px;
padding: 0;
overflow: hidden;
}
Note the line-height.
This will, once you smaller the available width of your device, result in a probably not so nice looking huge navigation bar. For this, I suggest media queries.
so this is how my navigation looks:
Quite unbalanced right.. my minds kinda fkd up. I have no idea what I am doing already. I'd like to ask some help how to center it out. This is the code of the css:
.navigation {
width: 886px;
height: 84px;
background: url('../img/nav_bg.png') no-repeat;
margin: 0 auto;
}
.navigation ul {
margin: 0;
padding: 0;
list-style: none outside;
padding-top: 22px;
padding-left: 63px;
}
.navigation ul li {
display: inline-block;
list-style-type: none;
}
.navigation ul li a {
display: block;
text-align: center;
line-height: 40px;
color: #fff;
text-decoration: none;
padding-left: 16px;
padding-right: 16px;
text-transform: uppercase;
font-size: 14px;
}
Flexbox is not yet 100% supported cross-browser
As you can see from this link: http://caniuse.com/#feat=flexbox
Flebox nowadays is quite well supported but if you carefully look at those symbols on each square you notice that the support is not full but partial on some browsers (especially mobile).
Global (with prefix)
82.47% + 10.5% = 92.97%
unprefixed:
71.8% + 0.38% = 72.18%
You should to add a prefix if you really want to use it.
**
Using a different method - display:table;
**
On the other hand, I suggest you to use another approach which is much more stable and supported on all browsers:
Look at the support of display:table;
Link: http://caniuse.com/#search=display%3Atable
To solve your problem you can remove all your padding-top with fixed values and use vertical-align:middle and display:table-cell. This way no matter what is the height of the ul it will be always vertically centered.
Center horizontally the ul element using text-align:center;
Center vertically the ul by setting the .navigation container with display:table; and the ul element with display:table-cell; and vertical-align:middle;
Code example:
.navigation {
width: 100%;
height: 84px;
background: lightgrey;
margin: 0 auto;
display:table;
}
.navigation ul {
padding: 0;
list-style: none outside;
text-align:center;
display:table-cell;
vertical-align:middle;
}
.navigation ul li {
display: inline-block;
list-style-type: none;
}
.navigation ul li a {
display: block;
text-align: center;
line-height: 40px;
color: #fff;
text-decoration: none;
padding-left: 16px;
padding-right: 16px;
text-transform: uppercase;
font-size: 14px;
}
Check Jsfiddle: http://jsfiddle.net/a_incarnati/bmkspm69/1/
Flexbox might be able to help you here. To center your li's with flexbox all you would have to do is add these styles to .navigation ul
.navigation ul{
display:flex;
justify-content: center;
align-items:center;
}
Give a text-align: center to the ul.
In general, for centering some elements inside a parent, parent should have text-align:center and the childs should have display: inline-block or display: inline.
HTML:
<ul>
<li>A</li>
<li>B</li>
<ul>
CSS:
ul {
width: 400px;
text-align: center;
}
li {
display: inline-block;
}
Quite difficult to explain, so I created a codepen for you to have a look at. When you hover over a navigation item and the dropdown menu appears. You'll see that some of the items don't line up correctly. I.e. the width of some items are bigger than their sibling items. I just want them all to stretch to the width of the widest list item. (confused yet?) Just have a look at the code pen..
You need to make changes to the below CSS to get them aligned.
For Instance.
#main-navigation ul li ul li {
background: none repeat scroll 0 0 #DE5D48;
width: 140px;
text-align:left;
}
WORKING DEMO
Hope this helps.
You should use display : inline-block instead of float: left in /* list item */ css and should add display : table-row; into /* sub list item */ .
#dark-blue: #31394C;
#light-gray: #E6E6E6;
#gray: #B1B2B5;
#dark-gray: #6D6D6D;
#dark-red: #d7351b;
#red: #ee2a2e;
#light-red: #f75d1f;
#dusty-red: #DE6450;
#light-dusty-red: #fdc29e;
#dark-dusty-red: #b50b03;
#sub-nav-red: #DE5D48;
#white: #ffffff;
#import url(http://fonts.googleapis.com/css?family=Rosario);
body {
background: #dark-blue;
}
#main-navigation {
/* initial list */
ul {
list-style: none;
/* list item */
li {
display: inline-block;
position: relative;
background: #dark-red;
min-width: 105px;
height: 21px;
text-align: center;
margin-right: 10px;
/* link */
a {
color: #white;
display: block;
line-height: 21px;
text-decoration: none;
font-family: 'Rosario', sans-serif;
font-size: 12px;
text-transform: uppercase;
}
a:hover {
color: #white;
background: #dark-red;
}
/* sub list */
ul {
position: absolute;
top: 100%;
left: -40px;
display: none;
border-top: 10px solid rgba(255, 255, 255, 0.0);
z-index: 99;
/* sub list item */
li {
background: #sub-nav-red;
display : table-row;
/* sub list link */
a {
padding: 0 5px 0 5px;
white-space: nowrap;
}
}
}
ul:hover {
display: block;
}
}
li:hover > ul {
display: block;
}
}
}
I suggest moving the background, size, and positioning from the li to the ul, that way the whole list will expand if one item is too big.
Try to give more min-width
#main-navigation ul li {
float: left;
position: relative;
background: #D7351B;
min-width: 140px;
in css, li element has: min-width: 105px;
change it to 155px and it will solve it.
hope that helps.
Its quite simple,
You can't use: top: 100%
You'll be wanting to update this to be top: 0 this will align it to the top of its parent.
You may need top: 11px in your exact case.
I have a navigation bar that has to be 900 pixels wide but the links inside don't necassarily always span the entire width of the bar so I'd like to center the links. The issue is no matter what I try, the links won't center. Here's my CSS:
.center {
text-align: center;
}
nav {
width: 900px;
text-align: center;
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
font-weight: bold;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 1px;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
margin: 0 auto 20px auto;
padding: 0 20px;
border-radius: 10px;
list-style: none;
position: relative;
background: #C0C0C0;
}
nav ul:after {
content:" \2022 ";
clear: both;
display: block;
}
nav ul:last-child:after {
content:"";
}
nav ul li {
display: inline-block;
}
nav ul li a {
text-align: center;
display: block;
padding: 20px 15px;
text-decoration: none;
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
top: 100%;
z-index: 100;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
border-top: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
nav ul ul ul {
position: absolute;
left: 100%;
top:0;
}
I tried adding auto margins to the nav id, and the nav ul id to no avail, then I tried wrapping the list in a div with a class that aligns the content in the center, but that didn't work either. Then I tried text-align to no avail. I don't know what to do so any help would be greatly appreciated!
Here's a JDFiddle I've been working with: http://jsfiddle.net/VCKMU/2/
UPDATE: After taking #Adrifts advice and changing the list items from float:left to inline-block, they now align in the center but now all the child items are inline blocks instead of vertical lists. Any ideas?
Updated JSFiddle: http://jsfiddle.net/VCKMU/6/
Instead of floating the list-items, just change their display value to inline-block; as you have specified text-align: center; on their containing block.
http://jsfiddle.net/VCKMU/6/
The only problem is that this causes the child items to turn into inline lists when they're supposed to be vertical.
You just need to modify the selector to only target the list-items are the children of the first <ul>:
.center > ul > li {
display: inline-block;
}
http://jsfiddle.net/VCKMU/8/
I think it should work if you give nav { width: auto; margin: auto; }
I usually fool around in the Chrome debugger for issues like this.