如何在Div中定位项目



我正在学习如何制作网站。

这是我的身体标签

<body>
    <h1 id="title">Title</h1>
    <div id="loginContainer">
        <img id="usernameIcon" src="assets/images/usernameIcon.png" alt="">
        <input id="usernameInput" type="text">
        <img id="passwordIcon" src="assets/images/passwordIcon.png" alt="">
        <input id="passwordInput" type="password">
        <button id="loginButton">Sign in</button>
    </div>
</body>

CSS 文件:

#title {
    color: black;
    text-align: center;
    letter-spacing: 1px;
    font-weight: bold;
}
#loginContainer{
    width: 60%;
    height: 400px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 80px;
    background-color: white;
    border-radius: 20px;
    box-shadow: 10px 10px 72px -26px rgba(0,0,0,0.75);
}
#usernameIcon{
    width: 30px;
    height: 30px;
    margin-top: 70px;
    margin-left: 30%;
}
#usernameInput{
    width: 30%;
    height: 30px;
}
#passwordInput{
    width: 30%;
    height: 30px;
}    
#passwordInput:focus-within{
}
#passwordIcon{
    width: 30px;
    height: 30px;
}
#loginButton{
    width: 40%;
    height: 40px;
    border-radius: 15px;
    border-width: 0;
    background-color: green;
    margin-top: 230px;
    margin-left: 30%;
    margin-right: 30%;
}

我做了一个网站的模板:模板

上面的代码设计我的网站:第一个图像

当我将margin-top: 150px;添加到 #passwordinput 中时,我的网站看起来像这样:第二image

为什么所有UI元素都更改了位置?我仅将margin-top: 150px;添加到 #passwordinput 中。

我正在学习如何制作和设计网站。请您理解。

,因为所有元素均在同一div中,当您给出一个元素之一时会扩展。

而是给它position: relativetop: 150px

#passwordInput {
  position: relative;
  top: 150px;
}

我已经重写了一些HTML和CSS来完成设计。希望它能正常。您在Codepen上获得相同的代码

#title {
  color: black;
  text-align: center;
  letter-spacing: 1px;
  font-weight: bold;
  font-family: sans-serif;
}
#loginContainer {
  width: 60%;
  margin-left: auto;
  margin-right: auto;
  margin-top: 80px;
  background-color: white;
  border-radius: 20px;
  box-shadow: 10px 10px 72px -26px rgba(0, 0, 0, 0.75);
  box-sizing: border-box;
  padding: 125px 0 50px;
}
#usernameIcon,
#passwordIcon {
  width: 30px;
  height: 30px;
  position: relative;
  top: 10px;
  margin-right: 15px;
}
#usernameInput,
#passwordInput {
  width: 30%;
  height: 30px;
}
#passwordInput:focus-within {}
#loginButton {
  width: 40%;
  height: 40px;
  border-radius: 15px;
  border-width: 0;
  background-color: green;
  margin-top: 150px;
  margin-left: 30%;
  margin-right: 30%;
}
.wraper {
  outline: 1px solid red;
  width: 40%;
  margin: 0 auto;
}
/* input-group */
.input-group {
  overflow: hidden;
  text-align: center;
}
<body>
  <h1 id="title">Title</h1>
  <form id="loginContainer">
    <div class="input-group">
      <img id="usernameIcon" src="https://ya-webdesign.com/images600_/vector-login-username-1.png" alt="">
      <input id="usernameInput" type="text">
    </div>
    <div class="input-group">
      <img id="passwordIcon" src="https://ya-webdesign.com/images600_/password-icon-png-2.png" alt="">
      <input id="passwordInput" type="password">
    </div>
    <button id="loginButton">Sign in</button>
  </form>
</body>

我已经重写了一些HTML和CSS来完成设计。希望它能正常。您在Codepen上获得相同的代码

#title {
  color: black;
  text-align: center;
  letter-spacing: 1px;
  font-weight: bold;
  font-family: sans-serif;
}
#loginContainer {
  width: 60%;
  margin-left: auto;
  margin-right: auto;
  margin-top: 80px;
  background-color: white;
  border-radius: 20px;
  box-shadow: 10px 10px 72px -26px rgba(0, 0, 0, 0.75);
  box-sizing: border-box;
  padding: 125px 0 50px;
}
#usernameIcon,
#passwordIcon {
  width: 30px;
  height: 30px;
  position: relative;
  top: 10px;
  margin-right: 15px;
}
#usernameInput,
#passwordInput {
  width: 30%;
  height: 30px;
}
#passwordInput:focus-within {}
#loginButton {
  width: 40%;
  height: 40px;
  border-radius: 15px;
  border-width: 0;
  background-color: green;
  margin-top: 120px;
  margin-left: 30%;
  margin-right: 30%;
}
.wraper {
  outline: 1px solid red;
  width: 40%;
  margin: 0 auto;
}
/* input-group */
.input-group {
  overflow: hidden;
  text-align: center;
  margin-bottom: 15px;
}
<body>
  <h1 id="title">Title</h1>
  <form id="loginContainer">
    <div class="input-group">
      <img id="usernameIcon" src="https://ya-webdesign.com/images600_/vector-login-username-1.png" alt="">
      <input id="usernameInput" type="text">
    </div>
    <div class="input-group">
      <img id="passwordIcon" src="https://ya-webdesign.com/images600_/password-icon-png-2.png" alt="">
      <input id="passwordInput" type="password">
    </div>
    <button id="loginButton">Sign in</button>
  </form>
</body>

最新更新