<h1>位置的问题

时间:2018-02-03 16:40:41

标签: html css css-position

我希望h1显示在容器的顶部,但使用以下代码会显示在底部。

&#13;
&#13;
/*RESETCSS*/

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
font,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  font-weight: inherit;
  font-style: inherit;
  font-size: 100%;
  font-family: inherit;
  vertical-align: baseline;
}


/* remember to define focus styles! */

 :focus {
  outline: 0;
}

body {
  line-height: 1;
  color: black;
  background: white;
}

ol,
ul {
  list-style: none;
}


/* tables still need 'cellspacing="0"' in the markup */

table {
  border-collapse: separate;
  border-spacing: 0;
}

caption,
th,
td {
  text-align: left;
  font-weight: normal;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: "";
}

blockquote,
q {
  quotes: "" "";
}

body,
html {
  height: 100vh;
}


/*SITE*/

.site {
  font-family: 'Oswald';
  width: auto;
  height: 100%;
  position: relative;
  padding: 0 250px;
}

.site-login {
  height: 100%;
  position: relative;
  border: 1px solid black;
}

.site-login-leftimg {
  width: 50%;
  height: 100%;
  background-color: black;
  float: left;
}

.site-login-form-container {
  height: 100%;
  width: 50%;
  position: relative;
  left: 50%;
}

.form-box {
  height: 600px;
  position: relative;
  top: 20%;
  border: 1px solid red;
}

.form-header {
  border: 1px solid black;
  width: 100%;
  position: relative;
}
&#13;
<div class="site">
  <div class="site-login">
    <div class="site-login-leftimg"></div>
    <div class="site-login-form-container">
      <div class="form-box">
        <div class="form-header">
          <h1>hi</h1>
        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

您可以查看Jsfiddle 我无法找到它出现在那个地方的原因。

1 个答案:

答案 0 :(得分:2)

您的.site-login-leftimg填充其父容器100%的完整高度(.site-login)。因此,其余内容位于下面该内容。您可能希望将元素的流量设置为inline-block,以便图像和容器并排放置:

.site-login {
    display: inline-block;
}

您还可能需要调整元素的宽度,使它们并排放置。

考虑使用网格系统为您处理此布局抽象:CSS Grid