I am working on a website that has a menu that is coming from a sitemap source. I can not get the menu to center for the life of me. It keeps floating left. It seems like any CSS i write does not affect it. Does anyone have any idea how to center a C# menu control?
This is really frustrating I have been trying to center this for hours.
If anyone has any suggestions please let me know!"
Ah, a typical UL as a horizontal menu thing. I had this issue a while back. I don’t typically do much complex css, but I solved it by just getting it to look right, then containing the whole thing in a div with a specified width, and left-right auto margins. Depending on the rest of your layout this might also work for you. This is, however, completely a css issue. It has nothing to do with the asp:Menu control, as it renders as straight html. If my solution doesn’t work for you, try posting this in the Web Page Design forum, where people more attuned to CSS and layout can help. Here in the .NET forum, we tend to be more code-heads than presentation.
Just mocked it up, and here’s the css, and the test file I used. It worked fine. Try that as a test. If it still isn’t working, you might have an odd doctype or compatability mode is on, or some other such oddness.
html
{
color: #000000;
}
div.hideSkiplink
{
background-color: #452f1a;
width:960px;
height:68px;
margin:0px auto 0px auto;
}
div.undNav
{
background-image: url('../Images/Roots.jpg');
width: 960px;
height: 69px;
}
div.menu
{
padding: 4px 0px 4px 8px;
text-align:center !important;
margin:0 auto !important;
width:960px;
}
div.menu ul
{
list-style: none;
margin: 0px;
padding: 0px;
width: inherit;
text-align:center !important;
}
div.menu ul li a, div.menu ul li a:visited
{
background-color: #465c71;
border: 1px #4e667d solid;
color: #dde4ec;
display: block;
line-height: 1.35em;
padding: 4px 20px;
text-decoration: none;
white-space: nowrap;
}
div.menu ul li a:hover
{
background-color: #bfcbd6;
color: #465c71;
text-decoration: none;
}
div.menu ul li a:active
{
background-color: #465c71;
color: #cfdbe6;
text-decoration: none;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Alignment
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Menu1.Items.Add(new MenuItem("Home"));
Menu1.Items.Add(new MenuItem("This"));
Menu1.Items.Add(new MenuItem("That"));
Menu1.Items.Add(new MenuItem("About"));
}
}
}
Compatability mode is something you’ll see in recent versions of IE. If you’re using FireFox, you probably won’t see it (or need it for that matter). In IE, it’s a little icon to the left of the refresh button that looks like a piece of torn paper. It’s either blue if on, and grey if off.
As to why it isn’t working, the only thing I can think of that might be interfering is something in the rest of your page html (especially something declared prior to this bit) and it’s related css. Remember, css cascades (hence the name). If this menu is being nested within something else, you need to look at that css as well.
Did you try a fresh aspx page, using exactly the code I posted?
Most likely an attempt on his part to see if he could force an alignment.
Considering this UL is nested within two outer divs, the outermost having a specific width, he’s probably going to want to apply the effect to the outermost div, rather than the UL directly. I get the impression that he is considering the entire block as the ‘menu’. All I did to get it to work was change his container css from:
The ASP.NET code doesn’t help. I’d need to see the rendered markup. A link to a live page would be best.
You misunderestimate me, sir.
@maverb: if you have the choice don’t use those junky <asp:menu> elements. It’s better to use an <asp:listview> or even an <asp:repeater> so that you can control the markup and avoid the stupid inline styling. This will also eliminate the need for !important.