在骷髅中居一个div

Can*_*Can 6 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 -->
Run Code Online (Sandbox Code Playgroud)

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; }
Run Code Online (Sandbox Code Playgroud)

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

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

小智 16

实际上有一种在Skeleton中居中div的内置方式.

<div class="sixteen columns">
    <div class="four columns offset-by-six">
        <p>Lorem ipsum dolor sit amet.</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

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

澄清一下:这并不是div中实际内容的中心,而是将div本身集中在一起.


asu*_*000 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>
Run Code Online (Sandbox Code Playgroud)

  • 多么恶心的黑客 (3认同)

Pat*_*ick 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
 }
Run Code Online (Sandbox Code Playgroud)

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

这是一篇关于如何使用 CSS 将任何内容居中的好文章