Click here to Skip to main content
14,878,829 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I cannot get my menu control centered on my pages using a master page. Everything else in my project is centered properly, except the menu control. I've tried everything I can think of and nothing has worked. I'm using Visual Studio 2010 and IE9. I'm having to emulate IE7 or else nothing is centered. Here's the complete source code for my master page:
ASP.NET
<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPages_MasterPage" %>
<%@ Register Src="../UserControls/MainMenu.ascx" TagName="MainMenu" TagPrefix="uc2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>Untitled Page</title>
	<link href="../StyleSheets/OTCSStyles.css" rel="stylesheet" type="text/css" />
	<link href="../StyleSheets/Calendar.css" rel="stylesheet" type="text/css" />
</head>
<body>
	<form id="form1"  runat="server">
	<div style="height: 43px; width: 100%; background-color: #225CAC; margin-removed auto; margin-removed auto;">
		<table width="640px" cellpadding="0" cellspacing="0">
			<tr bgcolor="#225CAC" style="height: 40px;">
				<td align="left" valign="baseline" width="120px">
					<br />
					<asp:Label ID="LBL_User" runat="server" Text="User:" CssClass="bannerleft"></asp:Label>
				</td>
				<td align="center" width="400px">
					<img id="IMG_Banner"  runat="server" src="~/Images/BannerTest.jpg" alt="O.T.C.S."
						width="400" height="40"  önclick="javascript:window.navigate('Home.aspx');" />
				</td>
				<td align="right" valign="baseline" width="120px">
					<br />
					<asp:Label ID="LBL_Date" runat="server" Text="Date:" CssClass="bannerright"></asp:Label>
				</td>
			</tr>
		</table>
	</div>
	<div style="margin:0 auto 0 auto;text-align:center">
		<div class="mainmenu">
			<uc2:MainMenu ID="MainMenu1"  runat="server" />
		</div>
		<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
		
		</asp:ContentPlaceHolder>
	</div>
	</form>
</body>
</html>

And here's the CSS for the "mainmenu" class:
CSS
DIV.mainmenu
{
    margin: 0 auto 0 auto;
    text-align: center;
	background-color: #6699CC;
}
Posted
Comments
Sergey Alexandrovich Kryukov 18-Oct-12 15:27pm
   
Basically, this is a pretty good question.
--SA

1 solution

Without looking much into your code: there is one non quite obvious trick:

Consider you have some block element. If you apply center style to it, its content will center, but you need to leave content as it is, but provide equal empty margins on left and right. This is how:
CSS
margin-left: auto;
margin-right: auto;


Hope it will help you,
—SA
   
Comments
Lonnie Raffray 18-Oct-12 15:36pm
   
Thanks. But I've been using margin:0 auto 0 auto; which amounts to the same thing and it has not helped. It works with any other element, but not the menu control.
Sergey Alexandrovich Kryukov 18-Oct-12 15:47pm
   
No, you need to right it the way I suggested. This would be very different style. Please try; it should work this way. In worst case, enclose the whole thing you need to center in some div, and apply the style I've shown to this outer div. I should work.

If it helps, please accept this answer formally (green button) -- thanks.
--SA
Lonnie Raffray 18-Oct-12 15:59pm
   
I tried that but it didn't work. I did solve it by changing the .NET Framework from 4.0 to 3.5, but I don't know why it shouldn't work in 4.0.
Sergey Alexandrovich Kryukov 18-Oct-12 16:12pm
   
Such things depend on the client (a Web browser), not the version of .NET. Well, it could be something else.
I'm not sure your observations are correct.
--SA
Lonnie Raffray 18-Oct-12 16:16pm
   
Well, that won't be an issue in my case. I'm not writing for the general public and all the users in my organization use IE as a requirement.
Sergey Alexandrovich Kryukov 18-Oct-12 18:23pm
   
Whatever. OK, anyways, it just reduced to this: is your problem resolved or not? I still think my approach should work; it may only depend on the browser.
--SA
Lonnie Raffray 22-Oct-12 8:11am
   
I am considering the problem solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900