2012-07-21 64 views
1

我需要居中對齊面板。我使用以下內容:ASP.net - 中心對齊面板?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestDefault.aspx.cs" Inherits="tregware.TestDefault" %> 

<!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"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<asp:Panel ID="Root" runat="server" BackColor="Red" HorizontalAlign="Center" Style="left: 0px; top: 0px; width: 100%; height: 100%; position: absolute; z-index: 0;"> 


<asp:Panel ID="Body" runat="server" BackColor="Blue" 
    Style="left: 0px; top: 24px;width: 800px; height: 100%; position: absolute; z-index: 0;" 
    HorizontalAlign="Center"> 
</asp:Panel> 

</asp:Panel> 
</form> 
</body> 
</html> 

但是面板(「Body」)不會居中。我會怎麼做?

回答

2

你已經設置position: absolute,所以很多的正規方法,您會使用可能居中元素將無法正常工作。

既然你已經設置的800px固定width,你可以使用:

Style="left: 50%; margin-left: -400px; ..." 
+0

這工作,謝謝。 – 2012-07-21 20:47:11

0

創建一個名爲.panel { align:center }

CSS,然後將其添加到面板CssClass = "panel"

+0

屬性請參閱更新的答案 – HatSoft 2012-07-21 20:39:11

1

面板具有Horizo​​ntalAlign

<asp:Panel id="reqId" runat="server" HorizontalAlign="center" > 
Your Text Here 
</asp:Panel>