CSS 如何放置 div 或面板的 Gridview 中心。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16084575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:30:52  来源:igfitidea点击:

How can i place Gridview center of div or panel.?

css

提问by SANDEEP

Hello! Can anyone tell me how can I place any Gridviewin center in Divor panel? I have applied following CSS but its does not working:

你好!谁能告诉我如何将任何内容Gridview放在中心Div或面板中?我已经应用了以下 CSS,但它不起作用:

<asp:Panel ID="pnlGrid" CssClass="panel" runat="server">
 <div style="text-align:center">
   <asp:GridView ID="grdReqApproval" runat="server"   AutoGenerateColumns="false"     CssClass="SimpleGrid">
      <Columns>
  <asp:BoundField DataField="Approved By" HeaderText="Approved By" />
  <asp:BoundField DataField="User Name" HeaderText="User Name" />
  <asp:BoundField DataField="Approval Time" HeaderText="Approvel Time" />
</Columns>
</asp:GridView>
</div>
    </asp:Panel>
.panel
{
width: 330px;
padding: 10px;
min-height: 20px;
border: 1px solid #dcdcdc;
margin-left:auto;
margin-right:auto;
}

回答by Ritwik

HorizontalAlignproperty of Gridview can solve your problem

HorizontalAlignGridview 的属性可以解决您的问题

    <asp:Panel ID="pID" runat="server">
     <div>
       <asp:GridView ID="gvID" runat="server" AutoGenerateColumns="false"
 HorizontalAlign="Center">
          <Columns>
             ...
             ...
          </Columns>
    </asp:GridView>
    </div>

回答by Pranay Rana

Check this is working for me , Ultimatly gridview get converted to table so the apply following stylesheet to your gridview which i applied to table

检查这对我有用,最终 gridview 被转换为表格,因此将以下样式表应用于我应用于表格的 gridview

CSS

CSS

.centered-table {
   margin-left: auto;
   margin-right: auto;
}

HTML

HTML

<div>
<table class="centered-table" border="1">
    <tr><td>Pekin</td> <td>Illinois</td></tr>
    <tr><td>San Jose</td><td>California</td></tr>
</table>
</div>

JsFiddle Demo

JsFiddle 演示

回答by Thrasymakus

Slight case of thread necromancy, but I was having the same problem and managed to fix it by aligning the code block rather than the gridview itself.

线程死灵法的轻微情况,但我遇到了同样的问题,并设法通过对齐代码块而不是 gridview 本身来修复它。

Basically I set the width of the element to 40%, and left to 30% (which means right is also 30%, because maths) and that positions the whole lot, text, grid and all into the middle of the page.

基本上我将元素的宽度设置为 40%,左边设置为 30%(这意味着右边也是 30%,因为数学)并且将整个批次、文​​本、网格和所有内容都放置在页面的中间。

The css looks roughly like this

css看起来大致是这样的

GridExample
{
position:absolute;
left:30%;
width:40%;
padding:0;
margin:0;
}

回答by wazaminator

on your div text-align: center

在你的 div 上 text-align: center

回答by James

as you see here, your class works. Just be sure its parent container is wider so it can center

正如您在此处看到的,您的课程有效。只要确保它的父容器更宽,以便它可以居中

http://jsfiddle.net/C7ybw/

http://jsfiddle.net/C7ybw/

#container{
    width:800px;
    border:1px solid #000;
}

回答by Ata Hoseini

.div_text_center {
    text-align: center;
    margin-left: auto;
    margin-right: auto;
}

<div class="div_text_center">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Text="Button" />
            <asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center">
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

enter image description here

在此处输入图片说明