在骷髅中居一个div

时间:2013-08-06 09:54:46

标签: html css skeleton-css-boilerplate

对于我的一个项目,我第一次使用Skeleton Boilerplate。而且我正在寻找在Skeleton中居中div而不会抨击Skeleton规则的最佳实践。

目前,我有一个登录页面的结构。

HTML:

<div class="container">
<div class="sixteen columns vertical-offset-by-one">
<div id="loginBox">
<img src="images/yeditepeLogo.png" alt="Yeditepe Logo" class="yeditepeLogo" />
<form action="" id="loginForm">
<input type="text" name="username" required placeholder="username" class="loginTextField">
<input type="password" name="password" required placeholder="password" class="loginTextField">
<input type="submit" value="Log In" class="loginButton" />
</form>
</div><!-- loginBox -->

</div><!-- sixteen columns -->

<div class="sixteen columns">
<p align="center"><a href="registration.html" target="_blank">Click here to register</a></p>
</div>
</div><!-- container -->

CSS:

#loginBox, #registrationBox {
  width: 470px;
  height: 450px;
  background-color: white;
  left: 245px; */
  top: 20px; */
  position: relative; 
  margin: 0px auto;  }

#registrationBox {
  height: 500px; }

.yeditepeLogo {
  position: relative;
  left: 40px;
  top: 33px; }

#loginForm, #registrationForm {
  position: relative;
  top: 45px; }

.loginTextField, .registrationTextField {
  position: relative;
  height: 40px;
  width: 388px;
  left: 40px;
  border-color: #dedede;
  border-style: solid;
  border-width: 1px;
  margin-bottom: 10px;
  text-align: left;
  font-size: 18px;
  text-indent: 10px;
  -webkit-appearance: none; }

.loginTextField:focus, .registrationTextField:focus {
  outline-color: #ff9800;
  outline-style: solid;
  outline-width: 1px;
  border-color: white; }

.loginTextField:nth-child(2), .registrationTextField:nth-child(3) {
  margin-bottom: 40px; }

.loginButton, .registrationButton {
  background-color: #77a942;
  position: relative;
  border: none;
  width: 390px;
  height: 60px;
  left: 40px;
  color: white;
  font-size: 24px;
  text-align: center;
  opacity: 0.8; }
  .loginButton:hover, .registrationButton:hover {
    opacity: 1; }

如您所见,#loginBox具有固定的宽度/高度,它应始终位于页面的中心。 margin:0px自动代码给它水平居中。但这是骷髅头的最佳做法吗? Skeleton是否提供了更好的方法?

另外我如何提供它的垂直居中?

5 个答案:

答案 0 :(得分:16)

实际上,这是一种以骷髅为中心的div的内置方式。

<div class="sixteen columns">
    <div class="four columns offset-by-six">
        <p>Lorem ipsum dolor sit amet.</p>
    </div>
</div>

在这种情况下,6的偏移量可以从1改为15,并且可以将输入的列数偏移到当前列。作为一个简洁的功能,当使用较小的屏幕时,偏移不影响对齐。

澄清:这并不是div中实际内容的中心,而是以div为中心。

答案 1 :(得分:6)

我知道问题已经有一段时间了,但也许其他人可以使用答案。 通过填充三分之一的列类和空格,然后是下一个带有内容的三分之一列,然后是另一个带有空格的三分之一列,我能够完成Skeleton的居中。

<div class="one-third column">&nbsp;</div>
<div class="one-third column"><p>Center of the screen.</p></div>
<div class="one-third column">&nbsp;</div>

答案 2 :(得分:1)

您可以将容器设置为

.container {
           position: absolute;
           top: 50%;
           left: 50%;
           margin-left: -43 //replace with half of the width of the container
           margin-top: -52 //replace with half of the height of the container
 }

将父容器或元素设置为position:relative;

这是一篇关于How to Center Anything With CSS

的好文章

答案 3 :(得分:0)

Asus3000的答案很好,因为这就是我的工作,而且效果很好。我只会在移动设备上添加它,它会增加相当多的不需要的垂直空间。为了避免移动垂直空间,我使用类.filler并将其隐藏在移动设备上。

HTML

<div class="one-third column filler">&nbsp;</div>
<div class="one-third column"><p>Center of the screen.</p></div>
<div class="one-third column filler">&nbsp;</div>

CSS

/* or whatever mobile viewport */
@media only screen and (max-width: 960px) {
  .filler { display: none}
}

答案 4 :(得分:0)

我认为一种非常好的方法是:

  <div class="container">
   <div class="row">
    <div class="two-half column">
     centered div content
    </div>
   </div>
  </div>

这使得div居中并且响应迅速。您可以更改margin-top以使其一直处于中间位置,但是更改宽度将(当然)不再使其居中。

如果我错了,请纠正我,但这对我有用! :)

相关问题