I add the menu inside div in Master page of my project. I wish to align the following div to center of the page. i have tried margin: 0px auto; display: block; in CSS. It isn't workout.
<div>
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
<Items>
<asp:MenuItem Text="Home" NavigateUrl="Home.aspx" />
<asp:MenuItem Text="test" NavigateUrl="test.aspx" />
<asp:MenuItem Text="Reports" NavigateUrl="Reports.aspx" />
<asp:MenuItem Text="Review" NavigateUrl="Review.aspx" />
<asp:MenuItem Text="Management" NavigateUrl="mg.aspx" />
<asp:MenuItem Text="Scripts" NavigateUrl="scr.aspx" />
<asp:MenuItem Text="Notification" NavigateUrl="Notification.aspx" />
</Items>
</asp:Menu></div>
CSS
div
{
margin:0px auto;
display: block;
}
.menu ul
{
border-bottom: 1px solid gray;
}
.menu ul li
{
border: 1px solid white;
background-color: gray;
text-decoration: none;
padding: 3px;
margin: 3px;
}
.menu ul li a
{
color: White;
}
.menu ul li a:hover
{
font-weight:bold;
}
margin auto do not work until you do not give the width of that element.
<div style="margin:0px auto; display:block; width:500px;">
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
<Items>
<asp:MenuItem Text="Home" NavigateUrl="Home.aspx" />
<asp:MenuItem Text="test" NavigateUrl="test.aspx" />
<asp:MenuItem Text="Reports" NavigateUrl="Reports.aspx" />
<asp:MenuItem Text="Review" NavigateUrl="Review.aspx" />
<asp:MenuItem Text="Management" NavigateUrl="mg.aspx" />
<asp:MenuItem Text="Scripts" NavigateUrl="scr.aspx" />
<asp:MenuItem Text="Notification" NavigateUrl="Notification.aspx" />
</Items>
</asp:Menu>
</div>
You can also use align property to center it.
<div align="center">
<ul>
<li> Menu Item 1</li>
<li> Menu Item 2</li>
</ul>
</div>
Fiddle
If you don't specify the width attribute, a DIV will always take up 100% of its parent's width - unless floating or positioned absolutely.
Try adding a fixed or even flexible width, e.g. 500px or 75%. This will allow the "margin: 0 auto" part kick in and center your div properly.
Also, unless the div is supposed to be hidden by default, there's no need for the "display:block", as DIV is a block element already.
One small change in css gave me a solution i'm using display:table istead of display:block
div
{
margin:0px auto;
display: table;
}
Thanks all
Use align property in your CSS file e.g.
div
{
margin:0px auto;
display: block;
text-align:center;
}
use this code because we have added style in div tag
<div style="margin:0 auto;width:255px;">
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
<Items>
<asp:MenuItem Text="Home" NavigateUrl="Home.aspx" />
<asp:MenuItem Text="test" NavigateUrl="test.aspx" />
<asp:MenuItem Text="Reports" NavigateUrl="Reports.aspx" />
<asp:MenuItem Text="Review" NavigateUrl="Review.aspx" />
<asp:MenuItem Text="Management" NavigateUrl="mg.aspx" />
<asp:MenuItem Text="Scripts" NavigateUrl="scr.aspx" />
<asp:MenuItem Text="Notificati NavigateUrl="Notification.aspx" />
</Items>
</asp:Menu>
<div>
Related
I want to set transparent background of menu for Asp.Net project. I use next code for it:
<%# Master Language="C#" CodeBehind="Site.master.cs" Inherits="Solution.Site" %>
<!DOCTYPE html>
<html lang="ru" style="height: 100%; margin: 0; padding: 0; position:relative">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8" />
<title>My title</title>
<link href="~/Site.css" rel="stylesheet" type="text/css" />
<link href="Styles/ByClasses.css" rel="stylesheet" type="text/css" />
<link href="Styles/ByElements.css" rel="stylesheet" type="text/css" />
</head>
<body class="template">
<form id="form1" runat="server">
<div class="logoDiv">
<asp:Image runat="server"
AlternateText="MyCompanyLogo"
ImageAlign="Top"
Height ="100%"
ImageUrl="DynamicData/Content/Images/MyCompanyLogo.png"/>
</div>
<div class="menuDiv">
<asp:Menu runat="server" StaticDisplayLevels="3" CssClass="menu">
<Items>
<asp:MenuItem Text="File" Value="File">
<asp:MenuItem Text="New" Value="New"></asp:MenuItem>
<asp:MenuItem Text="Open" Value="Open"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Edit" Value="Edit">
<asp:MenuItem Text="Copy" Value="Copy"></asp:MenuItem>
<asp:MenuItem Text="Paste" Value="Paste"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="View" Value="View">
<asp:MenuItem Text="Normal" Value="Normal"></asp:MenuItem>
<asp:MenuItem Text="Preview" Value="Preview"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
</div>
<div class="mainContentDiv">
<asp:ContentPlaceHolder id="master_contentPlaceHolder" runat="server"/>
</div>
<div class="footerDiv">Copyright MyCompany</div>
</form>
</body>
</html>
next "ByClasses.Css:"
.logoDiv {
background:White;
width:100%;
height:10%;
}
.menuDiv {
background:Yellow;
float:left;
width:15%;
height:88%;
}
.mainContentDiv {
background:Green;
float:left;
width:85%;
height:88%;
}
.footerDiv {
background:White;
clear:both;
width:100%;
height:2%;
}
.menu{
background-color:transparent;
width:100%;
float: left;
clear: both;
font-family: Arial, Helvetica, sans-serif;
}
and "ByElements.Css":
body {
height:100%;
width:100%;
margin:0;
padding:0;
position:relative;
}
form {
width:100%;
height:100%
}
But my menu have white background and I cann't understand why. Can anybody tell me, what's wrong and how I can set transparent background for menu?
I don't understand what exactly you need
if you need to remove the menu background color first you need remove .menuDiv background
.menuDiv{background:none}
I am styling my horizontal menu in asp.net
<div id="menu" style="margin-left:auto;margin-right:auto" >
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" RenderingMode="list">
<Items>
<asp:MenuItem NavigateUrl="" Text="" Value=""></asp:MenuItem>
<asp:MenuItem NavigateUrl="" Text="" Value=""></asp:MenuItem>
<asp:MenuItem Text="" Value="">
<asp:MenuItem NavigateUrl="" Text="" Value="" ></asp:MenuItem>
<asp:MenuItem NavigateUrl="" Text="" Value="" ></asp:MenuItem>
<asp:MenuItem Text="" Value="">
<asp:MenuItem NavigateUrl="" Text="" Value=""></asp:MenuItem>
<asp:MenuItem NavigateUrl="" Text="" Value=""></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="" Value="">
<asp:MenuItem NavigateUrl="" Text="" Value=""></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="" Value="">
<asp:MenuItem NavigateUrl="#" Text="" Value=""></asp:MenuItem>
<asp:MenuItem NavigateUrl="" Text="" Value=""></asp:MenuItem>
<asp:MenuItem NavigateUrl="" Text="" Value="" Target="_blank"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
</div><!-- #menu-->
My page width is 1200px.
However sometime I have 5 menu lists, sometimes I have 4 menu lists depend on the user.
I need to make a webpage that can always show the menu in the center.
Is anyone know how to set the horizontal menu to be center without setting the specific width with CSS?
#menu {
display: table;
margin: 0 auto;
}
It will make the inner element center horizontally and it works without setting a specific width. Works on IE8+ and modern browsers.
set the css of the div with id menu
margin:0 auto;
Display: inline-block;
This will place it to center of the browser
set the css of the div with id menu
#menu {
margin:0 auto;
Display: inline-block;
}
This will place it to center of the browser
You could always use the css rule: text-align
Also another option would be use:
display: inline-block;
vertical-align: bottom;
The vertical align is to remove the extra margin at the bottom
It would work fine
I was faced with this same problem, and after trying all the answers I came across I stumbled on the following which worked for me when placed in the style tag/css for the Div:
table-layout:fixed;
Hope that helps.
Here is my Code for the asp.net page and my CSS, hoping someone can find out what I am doing wrong. I'm new to css, and asp.net. I used most of the knowledge I know with Div tags, etc.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="MHConstruction.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/NewStyles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Homes.jpg" ImageAlign="Middle" Width="960px"/>
<div class="title">
</div>
<div class="loginDisplay" dir="ltr">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ Log In ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<center>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" >
<Items>
<asp:MenuItem NavigateUrl="~/HomePage.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
<asp:MenuItem NavigateUrl="~/Basic.aspx" Text="Basic"/>
<asp:MenuItem NavigateUrl="~/Standard.aspx" Text="Standard"/>
<asp:MenuItem NavigateUrl="~/Luxury.aspx" Text="Luxury"/>
<asp:MenuItem NavigateUrl="~/CustomerHome.aspx" Text="Customers"/>
<asp:MenuItem NavigateUrl="~/AdminHome.aspx" Text="Admin"/>
<asp:MenuItem NavigateUrl="~/Logout.aspx" Text="Logout"/>
</Items>
</asp:Menu>
</div>
</center>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
Also Here is the CSS that I am currently using. I posted just the css that is applied to the nav menu.
div.hideSkiplink
{
background-color:#3a4f63;
width:100%;
height: 51px;
}
div.menu
{
float:right;
position:relative;
text-align:left;
}
div.menu ul
{
list-style:none;
position:relative;
left:35%;
}
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;
}
Two things: you should set up your style the same way as the style block for the menu
<style type="text/css">
/* <![CDATA[ */
#NavigationMenu img.icon { border-style:none;vertical-align:middle; }
#NavigationMenu img.separator { border-style:none;display:block; }
#NavigationMenu img.horizontal-separator { border-style:none;vertical-align:middle; }
#NavigationMenu ul { list-style:none;margin:0;padding:0;width:auto; }
#NavigationMenu ul.dynamic { z-index:1; }
#NavigationMenu a { text-decoration:none;white-space:nowrap;display:block; }
#NavigationMenu a.static { padding-left:0.15em;padding-right:0.15em; }
#NavigationMenu a.popout { background-image:url("/WebResource.axd?d=EMSWJvMqJTxHa4d4S135BwGFA2dQw5pII3m5syZ8p1nYnD3jAotY_XzYmVV_PYohvnNLCZwIu1hO3tdfjxDCY21kymwZShHj9fcW_mi5QQ41&t=634933845276428750");background-repeat:no-repeat;background-position:right center;padding-right:14px; }
/* ]]> */
</style>
Then you should make the wrapping div for the menu 50 percent:
<div style="border:1px solid red; width:50%">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="true" Orientation="Horizontal" >
<Items>
<asp:MenuItem NavigateUrl="~/HomePage.aspx" Text="Home" />
</Items>
</asp:Menu>
</div>
Then use the white-space: nowrap; setting in the menu class itself (not the ul/li/a class) to make sure the items don't wrap.
you can replicate my problem with this small code snippet.
<div ID="topcell" runat="server" align="center" style="text-align:center">
<asp:Menu ID="testmenu" runat="server" Width="300px" Orientation="Horizontal" RenderingMode="List">
<Items>
<asp:MenuItem Text="Testing"></asp:MenuItem>
<asp:MenuItem Text="Testing"></asp:MenuItem>
<asp:MenuItem Text="Testing"></asp:MenuItem>
<asp:MenuItem Text="Testing"></asp:MenuItem>
<asp:MenuItem Text="Testing"></asp:MenuItem>
<asp:MenuItem Text="Testing"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
Result:
Testing-Testing-Testing-Testing-Testing-Testing
Testing----------------------------------------
Desired results:
Testing-Testing-Testing-Testing-Testing-Testing
------------------Testing----------------------
I have tried all three RenderingModes with no luck! Am I doing something wrong?
Try adding following CSS to your page:
<style type="text/css" >
#testmenu ul li {
display:inline-block;
float:none !important;
}
</style>
It will overwrite inline menu styles and force the unordered list to center.
<div id="nav-main" align="center" style="width: 95%; height: 35px;">
<div style="float:left; height: 29px; width: 260px;">
<a href="javascript:void(0);" onclick="select();"
style="font-size: small; background-color: #FFFFFF">
Изменить код и примечание</a>
</div>
<div style="float:right; width: 639px;" align="center">
<asp:Menu ID="Menu2" runat="server" Orientation="Horizontal" Width="521px" MaximumDynamicDisplayLevels="0"
StaticSelectedStyle-CssClass="StaticSelectedStyle" Height="32px" StaticSubMenuIndent="18px"
OnMenuItemClick="Menu2_MenuItemClick">
<StaticSelectedStyle CssClass="StaticSelectedStyle"></StaticSelectedStyle>
<Items>
<asp:MenuItem Text="Часовые" Value="1" />
<asp:MenuItem Text="Суточные" Value="2"
Selected="True" />
</Items>
</asp:Menu>
</div>
</div>
Can't see the left div.
and firebug shows it under right div :(
How to self left div to the left side and sure what I'm doing wrong ?
Not sure I totally get what you're trying to do, but as far as getting 2 divs to float next to each other, which I think is what you're asking, try the following. I changed the container from 95% width to fixed width to allow for the fixed size you defined for your divs. And I changed the second div to float:left as well. Even if you want one div to float to the right of the left one, doesn't mean you need to use float:right. If they are both styled with float:left and their defined widths fit within their containing element, the second will float to the right of the first. Hope that helps.
<div id="nav-main" align="center" style="min-width: 900px; width:95%; height: 35px;">
<div style="float:left; height: 29px; width: 260px;">
<a href="javascript:void(0);" onclick="select();"
style="font-size: small; background-color: #FFFFFF">
Изменить код и примечание</a>
</div>
<div style="float:left; width: 639px;" align="center">
<asp:Menu ID="Menu2" runat="server" Orientation="Horizontal" Width="521px" MaximumDynamicDisplayLevels="0"
StaticSelectedStyle-CssClass="StaticSelectedStyle" Height="32px" StaticSubMenuIndent="18px"
OnMenuItemClick="Menu2_MenuItemClick">
<StaticSelectedStyle CssClass="StaticSelectedStyle"></StaticSelectedStyle>
<Items>
<asp:MenuItem Text="Часовые" Value="1" />
<asp:MenuItem Text="Суточные" Value="2"
Selected="True" />
</Items>
</asp:Menu>
</div>
</div>
Is this the sort of ting your looking for?
<div id="nav-main" style=" width: 100%; min-width: 900px; border:1px solid green;">
<div style="min-width:258px; min-width:30%; float: left; background-color:#CCC;"> <a href="javascript:void(0);" onclick="select();"
style="font-size: small;"> Изменить код и примечание</a></div>
<div style="float: left; background-color:#FFFFCC; width:70%;">
<asp:Menu ID="Menu2" runat="server" Orientation="Horizontal" Width="521px" MaximumDynamicDisplayLevels="0"
StaticSelectedStyle-CssClass="StaticSelectedStyle" Height="32px" StaticSubMenuIndent="18px"
OnMenuItemClick="Menu2_MenuItemClick">
<StaticSelectedStyle CssClass="StaticSelectedStyle"></StaticSelectedStyle>
<Items>
<asp:MenuItem Text="Часовые" Value="1" />
<asp:MenuItem Text="Суточные" Value="2"
Selected="True" />
</Items>
</asp:Menu>
right</div>
<div style="clear:both;"></div>
</div>