如何让我的HTML登录页面中的多个
重叠?

huangapple go评论68阅读模式
英文:

How do I make multiple divs overlap in my html login page?

问题

以下是你的HTML表单的翻译部分:

这是我的HTML表单:

<div style="display:block; margin:0 auto;">
    <h2>欢迎</h2>
</div>
<div id="box">
    <div id="box2">
        <p id="pid"><b>员工号:</b></p>
        <input type="text" id="id" size="30" maxlength="11" onkeypress="return event.charCode != 32">
        <p id="ppass"><b>密码:</b></p>
        <input type="password" id="pass" size="30" maxlength="30"> <i class="far fa-eye-slash"></i><br><br><br><br>
    </div>
</div>
<div id="box1">
    <button id="login">登录</button>
</div>

以下是你的CSS的翻译部分:

#box {
  	text-align: center;
    box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2);
    transition: 0.5s;
    height: auto;
  	z-index: -1;
	margin: 0 auto;
    width: 25em;
    border-radius: 8px;
    background-color: #cbeef1;
    padding: 10px 10px 10px 10px;
}

#box2 {
	display: inline-block;
    margin: 0 auto;
}

#box:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}

#box1 {
   display: table;
   margin-left: auto;
   margin-right: auto;
}

#login {
    border-radius: 6px;
    cursor: pointer;
    font-size: medium;
  	padding: 5px;
}

h2 {
    background-color: aliceblue;
  	font-size: medium;
    padding: 10px 10px 10px 10px;
    width: 5em;
    border-style: solid;
    margin: 0 auto;
  	display: block;
}

如果你有其他问题,可以进一步指导你如何实现期望的布局。

英文:

This is my HTML form:

<div style="display:block; margin:0 auto;">
    <h2>Welcome</h2>
</div>
<div id="box">
    <div id="box2">
        <p id="pid"><b>Emp no:</b></p>
        <input type="text" id="id" size="30" maxlength="11" onkeypress="return event.charCode != 32">
        <p id="ppass"><b>Password:</b></p>
        <input type="password" id="pass" size="30" maxlength="30"> <i class="far fa-eye-slash"></i><br><br><br><br>
    </div>
</div>
<div id="box1">
    <button id="login">Sign in</button>
</div>

And this is my CSS:

#box{
  	text-align: center;
    box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2);
    transition: 0.5s;
    height:auto;
  	z-index: -1;
	margin:0 auto;
    width: 25em;
    border-radius: 8px;
    background-color: #cbeef1;
    padding:10px 10px 10px 10px;
}
#box2{
	display: inline-block;
    margin: 0 auto;
}
 
#box:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
#box1{
   display: table;
   margin-left: auto;
   margin-right: auto;
}
#login{
    border-radius: 6px;
    cursor: pointer;
    font-size: medium;
  	padding: 5px;
}
h2{
    background-color: aliceblue;
  	font-size: medium;
    padding: 10px 10px 10px 10px;
    width: 5em;
    border-style: solid;
    margin: 0 auto;
  	display:block;
}

This is my output:

My actual output


As you can see in the image above, I want the button and the h2 to overlap with the box div. I also want the input fields to start from the same point, like the image shown below:

What I expect


Please guide me, as to how to achieve this.

This is my runtime fiddle: click here

答案1

得分: 1

buttonh2放到#box里面,然后使用负边距使它们重叠。

#box {
  text-align: center;
  margin: 2em auto 0;
  width: 25em;
  border-radius: 8px;
  background-color: #cbeef1;
  padding: 1em;
}

h2 {
  background-color: aliceblue;
  font-size: medium;
  padding: 10px;
  width: 5em;
  border-style: solid;
  margin: -2em 0 0 1em;
}

#login {
  border-radius: 6px;
  cursor: pointer;
  font-size: medium;
  padding: 5px;
}

#last {
  margin: 2em 0 -30px;
}
<div id="box">
  <h2>Welcome</h2>
  <p><b>Emp no:</b></p>
  <p><input size="30" maxlength="11"></p>
  <p><b>Password:</b></p>
  <p><span style="visibility: hidden;">&#128065;️ </span><input size="30" maxlength="30"> &#128065;️</p>
  <p id="last"><button id="login">Sign in</button></p>
</div>

这是你要的翻译,没有其他内容。

英文:

Put the button and h2 inside the #box, then use negative margins to make them overlap.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#box {
  text-align: center;
  margin: 2em auto 0;
  width: 25em;
  border-radius: 8px;
  background-color: #cbeef1;
  padding: 1em;
}

h2 {
  background-color: aliceblue;
  font-size: medium;
  padding: 10px;
  width: 5em;
  border-style: solid;
  margin: -2em 0 0 1em;
}

#login {
  border-radius: 6px;
  cursor: pointer;
  font-size: medium;
  padding: 5px;
}

#last {
  margin: 2em 0 -30px;
}

<!-- language: lang-html -->

&lt;div id=&quot;box&quot;&gt;
  &lt;h2&gt;Welcome&lt;/h2&gt;
  &lt;p&gt;&lt;b&gt;Emp no:&lt;/b&gt;&lt;/p&gt;
  &lt;p&gt;&lt;input size=&quot;30&quot; maxlength=&quot;11&quot;&gt;&lt;/p&gt;
  &lt;p&gt;&lt;b&gt;Password:&lt;/b&gt;&lt;/p&gt;
  &lt;p&gt;&lt;span style=&quot;visibility: hidden;&quot;&gt;&#128065;️ &lt;/span&gt;&lt;input size=&quot;30&quot; maxlength=&quot;30&quot;&gt; &#128065;️&lt;/p&gt;
  &lt;p id=&quot;last&quot;&gt;&lt;button id=&quot;login&quot;&gt;Sign in&lt;/button&gt;&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

要实现所需的布局,您可以使用CSS的position属性。以下是一个示例CSS代码,您可以使用:

#box {
  position: relative;
  text-align: center;
  box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2);
  transition: 0.5s;
  height:auto;
  z-index: -1;
  margin:0 auto;
  width: 25em;
  border-radius: 8px;
  background-color: #cbeef1;
  padding:10px 10px 10px 10px;
}

#box2 {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#login {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
}

h2 {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  background-color: aliceblue;
  font-size: medium;
  padding: 10px 10px 10px 10px;
  width: 5em;
  border-style: solid;
  margin: 0 auto;
  display:block;
}

#id, #pass {
  display: block;
  margin: 0 auto;
}

解释:

  • 在#box元素上使用position: relative将其设置为绝对定位的子元素的参考点。
  • #box2元素使用position: absolute,top: 50%,left: 50%,以及transform: translate(-50%, -50%)属性在#box元素内居中。
  • #login按钮使用position: absolute,在#box元素底部定位,使用bottom: -20px,left: 50%,以及transform: translateX(-50%)属性。
  • h2元素使用position: absolute,在#box元素顶部定位,使用top: -20px,left: 50%,以及transform: translateX(-50%)属性。
  • #id和#pass输入字段使用display: block和margin: 0 auto属性在#box2元素内居中。

通过这些更改,按钮和h2应该重叠在box div上,输入字段应该从同一点开始。

英文:

To achieve the desired layout, you can make use of the CSS position property. Here is an example CSS code that you can use:

    #box {
  position: relative;
  text-align: center;
  box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2);
  transition: 0.5s;
  height:auto;
  z-index: -1;
  margin:0 auto;
  width: 25em;
  border-radius: 8px;
  background-color: #cbeef1;
  padding:10px 10px 10px 10px;
}

#box2 {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#login {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
}

h2 {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  background-color: aliceblue;
  font-size: medium;
  padding: 10px 10px 10px 10px;
  width: 5em;
  border-style: solid;
  margin: 0 auto;
  display:block;
}

#id, #pass {
  display: block;
  margin: 0 auto;
}

Explanation:

The position: relative on the #box element sets it as the reference point for the absolute positioned child elements.

  • The #box2 element is centered within the #box element using the
    position: absolute, top: 50%, left: 50%, and transform:
    translate(-50%, -50%) properties.

  • The #login button is positioned at the bottom of the #box element
    using the position: absolute, bottom: -20px, left: 50%, and
    transform: translateX(-50%) properties.

  • The h2 element is positioned at the top of the #box element using the
    position: absolute, top: -20px, left: 50%, and transform:
    translateX(-50%) properties.

  • The #id and #pass input fields are centered within the #box2 element
    using the display: block and margin: 0 auto properties.

With these changes, the button and the h2 should overlap with the box
div and the input fields should start from the same point.

huangapple
  • 本文由 发表于 2023年2月27日 13:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定