Как позиционировать предметы в div - PullRequest
0 голосов
/ 08 мая 2019

Я учусь делать сайты.

Это мой тег тела :

<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 file:

#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 в файле CSS, мой сайт выглядит следующим образом: Второе изображение

Почему все элементы интерфейса изменили положение? Я добавил margin-top: 150px; только в # passwordInput .

Я учусь создавать и разрабатывать веб-сайты. Пожалуйста, для вашего понимания.

Ответы [ 3 ]

0 голосов
/ 08 мая 2019

Я переписал некоторые 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>
0 голосов
/ 08 мая 2019

Я переписал некоторые 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>
0 голосов
/ 08 мая 2019

Поскольку все элементы находятся в одном и том же элементе div, который расширяется, когда вы предоставляете одному из элементов поле.

Скорее, дай ему position: relative и top: 150px

#passwordInput {
  position: relative;
  top: 150px;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...