I'm trying to make a menu, but I'm having trouble centering the text to the middle.
<div id="menucontainer">
<ul id="menu">
<li>#Html.ActionLink("Home", "Index", "Home")</li> |
<li>#Html.ActionLink("About", "About", "Home")</li> |
<li>#Html.ActionLink("Projects", "Projects", "Home")</li> |
<li>#Html.ActionLink("Forum", "Forum", "Home")</li>
</ul>
</div>
#menu
{
background-image: url("../Content/img/bg-menu.png");
height:50px;
padding-left:30px;
padding-right:25px;
text-align:center;
border-radius:20px;
background-repeat:repeat;
display:block;
list-style: none;
margin-left:55%;
position:absolute;
color:#aa4dc6;
}
#menu li
{
display:inline;
padding:5px 10px 9px 10px;
}
#menu a
{
text-decoration:none;
color:#606060;
text-decoration:none;
text-transform:capitalize;
font-size:19px;
font-weight:bold;
font-family: 'Open Sans', sans-serif;
}
give it the same line-height as the element's height and the text should be properly aligned
There are a few solutions to this problem.
Here's a jsFiddle that demonstrates all of the solutions below.
First, you can try setting the display property to table-cell for the list items in the menu, and then you can use vertical-align:middle to center the text.
The solution would probably work well, because instead of floating the list items, you can use CSS to make them behave like table cells.
ul#menu {
display: table-row;
}
ul#menu li {
display: table-cell;
vertical-align: middle;
}
Second, you can set the line-height property to the height of the list item. Be careful with this one though, because if the text wraps it will break the layout:
ul#menu li {
height: 30px;
line-height: 30px;
}
Lastly, instead of setting a fixed height, you can use padding to set the height of the list items. Assuming you use the same padding for the top and bottom, the text should be aligned in the center:
ul#menu li {
padding: 15px 5px 15px 5px;
}
Related
I am trying to align my text to be on single line. I tried everything vertical-align: middle, vertical-align: bottom.. Please help me :
http://jsfiddle.net/2wDEw/
My simple example looks like that :
<div class="logo spanHalf"> My site </div>
<nav id="menu" class="spanHalf">
<li>Fetured</li>
<li>Latest Posts</li>
<li>Contacts</li>
</nav>
And my styling :
#menu {
float : right;
display:inline-block;
vertical-align:middle;
}
#menu li{
padding : 10px;
display:inline;
vertical-align:middle;
}
#menu a, .logo a {
text-decoration:none;
text-transform:uppercase;
color: black;
font-weight:bold;
}
#menu a:hover, .logo a:hover {
color: red;
}
.logo {
float:left;
display:inline-block;
vertical-align:middle;
font-size : 28px;
}
Desired result :
MY SITE FETURED LATEST POSTS CONTACTS
but your example is invalid html, li´s must be nested in ul or ol use span istead, this also is your problem, because for li´s the clients browser calculates some padding to make it look like a list even if theres no stylesheet, unless you override it
http://jsfiddle.net/2wDEw/2/
<nav id="menu" class="spanHalf">
<span>Fetured</span>
<span>Latest Posts</span>
<span>Contacts</span>
</nav>
Just give same line-height to both the elements.
#menu, .row{
line-height: 40px; /* change this value as needed */
}
Also change your HTML as mentioned by John Smith.
Working Fiddle
I have the following menu
The two lines are both 'hr' tags and the menu is a div containing a ul. I have been googling for a while now and trying adjusting the css with margin and padding but I want to reduce the white space between the lines and the text bringing them closer to the text.
HTML:
<hr id="header_line"/>
<div id="menu_bar">
<ul>
<li>Add new Form</li>
<li>View old forms</li>
<li>Reports</li>
<li>Site Administration</li>
</ul>
</div>
<hr id="under_menu_line"/>
CSS:
#menu_bar ul {
list-style:none;
padding-left:0px;
}
#menu_bar ul li {
display:inline;
padding-left:10px;
}
#menu_bar ul li a {
text-decoration:none;
color:Black;
font-family:Century Gothic;
font-size:12pt;
}
#menu_bar ul li a:hover {
color:#007C5A;
}
#header_line {
margin-top:5px;
}
#under_menu_line {
margin-top:5px;
}
Any ideas?
The best solution would be to drop the <hr>s, and use border-top and border-bottom in conjunction with padding on the div.
<hr> should be used as a horizontal rule. For instance, a hard separation of paragraphs or a long break. And not as a visual element.
Just like with any other element, the <hr> is controlled by CSS. The space you want to control is just the margin. This is the default from Firefox:
hr {
-moz-box-sizing: border-box;
-moz-float-edge: margin-box;
border: 1px inset;
color: gray;
display: block;
height: 2px;
margin: 0.5em auto;
}
So, the following will make the space 0.1em instead of 0.5em:
hr { margin: 0.1em auto; }
Try this and tell me if this is what you wanted.
#header_line { margin-top:5px; margin-bottom:-10px;}
#under_menu_line { margin-top:-10px; }
Use
#header_line{
margin-bottom:0px;
}
#under_menu_line{
margin-top:0px;
}
I've recently come across a nasty issue in IE8/IE9 (surprisingly, it aligns at it should in IE7). Please check out http://targettedmedia.co.uk/httpdocs
As you can see in Firefox/Chrome or other modern browser, on hover over the top menu, the submenu items should be vertically centered (regardless of one or 2-lined items). Yet, in IE8 and IE9 it just doesn't want to work. I've tried editing the code but to no avail.
Can you please help me? It's just getting too frustrating to handle it. I sincerely thank you in advance!
Here's a sample HTML sub-menu structure:
<ul id="nav" class="dropdown dropdown-horizontal">
<li>Health & Safety Courses
<ul class="sub-menu">
<li>NEBOSH National Diploma</li>
<li>NEBOSH Construction Certificate</li>
<li>NEBOSH Fire Certificate</li>
<li>NEBOSH Distance Learning Couse</li>
<li>IOSH Managing Safely</li>
<li>IOSH Working Safely</li>
<li>Site Managers Safety Training Scheme</li>
<li>Health & Safety Awareness for Employees</li>
<li>Health & Safety Awareness for Managers</li>
</ul>
</li>
</ul>
Here are the specific CSS bits that handle the vertical alignment:
ul.dropdown ul {
width:400px;
padding:20px 0 25px 7px;
background:#6c6c6c;
font-size:14px;
font-weight:normal;
}
ul.dropdown ul li {
font-weight: normal;
height:42px;
line-height:42px;
float:left;
margin:0 0 0 15px;
border-top:1px solid #484848;
}
ul.dropdown ul li a:link {
display:inline-block;
width:165px;
padding:0;
color:#fff;
text-align:left;
line-height:normal;
vertical-align:middle;
}
Avoid depending vertical-align: middle as it's not supported in many browsers. Instead, remove all your padding and margins from ul.dropdown ul li a:link and add padding to the <li> itself. If you add equal padding to the top and bottom (I would also do padding in em, that way the padding ratio looks the same no matter what size your user has their text set to) it will be equal all around.
ul.dropdown ul li {
border-top: 1px solid #484848;
float: left;
font-weight: normal;
height: 2em;
margin: 0 0 0 15px;
padding: 1em 0;
}
ul.dropdown ul li a {
color: #FFFFFF;
padding: 0;
text-align: left;
width: 165px;
}
Please help,
i want to align the header menu/nav links to vertically align. See:
http://hyindia.com/demo/myoffshore/index.html
See the CODE here:
nav ul { list-style-type:none; padding:0px; margin:0px; float:left; width:100%;}
nav ul li { float:left; width:119px; height:66px;}
nav ul li a {
float:left;
width:119px;
height:66px;
font:bold 15px 'Myriad Pro';
color:#fff;
text-shadow:1px 1px #1f1f1f;
text-align:center;
}
<nav>
<ul>
<li>HOME</li>
<li>HEALTH INSURANCE</li>
<li>LIFE INSURANCE</li>
<li>OVERSEAS MORTGAGES</li>
<li>ESTATE PLANNING</li>
<li>BANKING</li>
<li>WEALTH MANAGEMENT</li>
<li>QROPS</li>
</ul>
Since some of your nav items have text spanning several rows you won't be able to use the classic line-height-trick (which would be to set the line-height equal to the height).
Instead I'd suggest changing your menu styling to use display: table/table-row/table-cell since tables are excellent at vertically aligning things in the middle.
What you need to do is to change your entire nav styling to this:
nav {
display: table;
width: 100%;
}
nav ul {
display: table-row;
list-style: none;
padding: 0;
margin: 0;
}
nav ul li {
display: table-cell;
vertical-align: middle;
}
nav ul li a {
display: block;
padding: 5px 10px;
}
Remove all the floats and widths + heights (using padding on the a instead) etc (what I have above is all you should have).
You'll also need to move the actual background styling from the as to the lis since the as won't be equal in height any more (but the lis will).
Here is five methods very well explained : http://blog.themeforest.net/tutorials/vertical-centering-with-css/
I would like to know the better way to arrange the list items for the below menu.
As If I mention the list items as Item1,tem2,Item3 and so on as the Item is a small word it occupies less space and all are looking fine in the width as shown below:
But if I take the name as "Features", Support","How it works?" then it is not arranging them properly as it shows me lot of space n between each list item.
So is there any better way to get rid of this such as expanded menu by it's width or something like that
This is my css for the menu:
.menu
{
background-image: url('../images/header.png');
background-repeat: no-repeat;
}
ul.menu {
display:block;
margin:0;
padding:0;
height:60px;
text-align:right;
}
ul.menu li {
display:inline-block;
width:50px;
height:30px;
margin-right:10px;
}
ul.menu li:first-child {
float:left;
margin-left:10px;
}
ul.menu li a
{
text-decoration: none;
padding: 15px 0;
width: 50%;
color: #eee;
float: left;
text-align: center;
font-weight: bold;
font-style: normal;
font-family: Verdana;
}
This is my markup:
<div class="menu">
<ul class="menu">
<li>Home</li>
<li>Features</li>
<li>Support</li>
<li>Blog</li>
<li>How it work's</li>
</ul>
</div>
Actually according to Derek answer If I changed my width to 100px then it shows me this way
width:50px; is your problem.
You can remove it and:
manually set each item OR
create a function to measure the width of it's contents and adjust accordingly OR
leave them with no defined width
<li style="width: 60px;">Blog</li>
<li style="width: 130px;">How it work's</li>