简体   繁体   English

如何将PanelGrid对齐到中心? JSF-Primefaces

[英]How to align PanelGrid to center? JSF-Primefaces

I know that there are many questions about this issue, but nothing worked for me properly. 我知道关于这个问题有很多问题,但没有任何问题对我有用。

I need to align my PanelGrid to center(horizontal). 我需要将PanelGrid对齐到中心(水平)。

this is my panelgrid 这是我的panelgrid

<p:panelGrid styleClass="panelGridCenter">

and my CSS: 和我的CSS:

.panelGridCenter td,.panelGridCenter tr {
    text-align: center;
}

It just aligns the content to center, but not the panelGrid 它只是将内容对齐到中心,而不是panelGrid

The JSF <p:panelGrid> component renders a HTML <table> element which is by default a block level element . JSF <p:panelGrid>组件呈现HTML <table>元素,默认情况下是块级元素 To center the block level element itself, you should set its horizontal margin to auto instead of attempting to center its inline contents. 要使块级元素本身居中,您应将其水平边距设置为auto而不是尝试将其内联内容居中。

.panelGridCenter {
    margin: 0 auto;
}

See also: 也可以看看:

The above answer is technically correct but also incomplete. 上述答案在技术上是正确的,但也是不完整的。

If you want to center something like a div, the above technique of playing with the left and right margin as auto will work, provided that your DIV has limited width. 如果你想把像div这样的东西居中,只要你的DIV宽度有限,上面用左边和右边距作为自动的方法就可以了。 Eg For you to start being any effect you would have to put something like a width=60%. 例如,为了让你开始产生任何效果,你必须放置宽度= 60%的东西。

And then, once you realize you need to play with fixed widths... you immediately are prompted to the next question: So what exactly should I type in as my fixed width? 然后,一旦你意识到你需要使用固定宽度...你会立即提示你下一个问题:那么我应该输入什么作为我的固定宽度呢?

That is why I believe the better answer for this question is: CSS techniques like the one above, are OK for the small details on a web page. 这就是为什么我相信这个问题的更好答案是:像上面那样的CSS技术可以用于网页上的小细节。 But your coarse grained approach for centering anything on a web page should be to make use of a grid system. 但是,在网页上居中任何内容的粗粒度方法应该是使用网格系统。 Most grid systems use 12 cells. 大多数网格系统使用12个单元。 If for example your grid system would be by default make 12 cells = 100% width. 例如,如果您的网格系统默认为12个单元格= 100%宽度。 You could center something by, for example placing your content to be centered in cells [5-8] leaving out as centurion space cells [1-4] and cells [9-12]. 您可以将某些东西放在中心位置,例如将您的内容置于单元格[5-8]中心,留下百夫长空间单元格[1-4]和单元格[9-12]。

Here is an example based in prime faces grid system: 以下是基于素面网格系统的示例:

  <h3 id="signInTitle" class="first">Sign in - FIXME - i18n</h3>
  <form id="loginFormOld" (ngSubmit)="onLoginFormSubmit()">
    <!-- (a) Start a grid system-->
    <div class="ui-g ui-fluid">

      <!-- (b) Eat the first four cells of the grid -->
      <div class="ui-g-12 ui-md-4"></div>

      <!-- (c) In the center location of the grid put in the Login form -->
      <div class="ui-g-12 ui-md-4">
        <div class="ui-inputgroup">
          <span class="ui-inputgroup-addon"><i class="fa fa-user"></i></span>
          <input id="emailInput" pInputText type="email" placeholder="Email" [(ngModel)]="eMail" name="eMail">
        </div>
        <div class="ui-inputgroup">
          <span class="ui-inputgroup-addon"><i class="fa fa-key" aria-hidden="true"></i></span>
          <input id="passwordInput" pInputText type="password" class="form-control" placeholder="Password" [(ngModel)]="password" name="password">
        </div>
      </div>

      <!-- (d) Eat the rest of the first row of the grid without setting any contents -->
      <div class="ui-g-12 ui-md-4"></div>

      <!-- (e) Start the second row and eat the first four cells -->
      <div class="ui-g-12 ui-md-4"></div>

      <!-- (f) Position a form submit button on the fifth cell -->
      <div class="ui-g-12 ui-md-1">
        <button id="loginSubmit" pButton type="submit" label="Submit"></button>
      </div>
    </div>
  </form>

The comments on the above form should make it pretty clear what I meant above. 对上述表格的评论应该清楚地说明我的意思。 The grid system will normally offer CSS classes to allow your UI to be working across multiple form factors of devices, although ... on this regard I am of the opinion that you can not make a good mobile UI using a desktop UI nor a good desktop UI using a mobile UI. 网格系统通常会提供CSS类,以允许您的UI跨设备的多种形状因素工作,虽然......就此而言,我认为您无法使用桌面UI创建良好的移动UI使用移动UI的桌面UI。 On my opinion you can get a good Tablet/Desktop UI cooked up, but you should write pages from scratch with the minimal an necessary contents for mobile. 在我看来,你可以得到一个很好的平板电脑/桌面用户界面,但你应该从头开始编写页面,只需要最少的移动内容。 But that is a different discussion ... just to say, that the flex grid css classes will only take you so far. 但这是一个不同的讨论...只是说,flex网格css类只会带你到目前为止。 A lot of potential in theory, much better than hard coding some arbitrary fixed length on your div elements ... but not a silver bullet for all of your problems either. 理论上有很多潜力,比在你的div元素上硬编码一些任意固定长度要好得多......但也不是你所有问题的银弹。

In case if you want right align 如果你想要正确对齐

.rightAlign{
     margin-left: auto;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM