为什么我的输入元素比其他元素宽,即使我已经应用了 "box-sizing: border-box"?

Why is my input element wider than the other elements even though I already applied "box-sizing: border-box"?

我有一个输入元素,它在左侧和右侧都有 1em 的填充。但是我已经对它应用了“box-sizing: border-box”。但是,它的宽度仍然比其他元素多。我认为这可能是因为我需要删除一些代码,但我不确定。输入元素肯定是一个问题,因为另一个元素正确居中对齐。 下面是代码:

:root {
  --main-color: #00308f;
  --secondary-color: #7cb9e8;
  --dark-color: #444;
  --light-color: #fafafa
}

body {
  font-family: Montserrat, sans-serif;
  background-color: var(--light-color);
  color: var(--dark-color);
  text-align: justify;
  margin-top: 70px;
  margin-left: 0;
  margin-right: 0;
  margin-bottom: 0;
  padding: 1em
}



.my-contacts-div {
  align-items: center
}

.contacts-list {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center
}

.contact-card {
  width: 288px;
  margin: .5em;
  border-radius: .5em;
  box-shadow: var(--secondary-color) 1px 1px 10px;
  padding: 0 .75em;
  word-wrap: break-word
}

.contact-form {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: grid;
  align-self: center;
  width: 350px;
  z-index: 1;
  background-color: var(--secondary-color);
  border-radius: 1em;
  padding: 1em;
  box-shadow: 1px 1px 1.5em var(--main-color);
  visibility: hidden
}

.contact-form:target {
  visibility: visible
}

.input-field {
  margin: .5em 0;
  border: solid 1px var(--secondary-color);
  border-radius: .5em;
  padding: 0 1em;
  height: 40px;
  box-sizing: border-box
}

.searchbar {
  margin: .5em;
  width: 100%
}

@media screen and (max-width:687px) {
  .my-contacts-div {
    padding: 0
  }

  .contact-card {
    width: 100%
  }
}

@media screen and (max-width:614px) {
  body {
    margin-top: 130px
  }
}
<div class="my-contacts-div">
  <h2>Heading</h2>
  <form><input class="input-field searchbar" type="text" placeholder="Search here..."></form>
  <div class="contacts-list">
    <div class="contact-card">
      <h3>Other component</h3>
    </div>
  </div>
</div>

有什么问题吗?

Add display: flex in the tag. It will solve the overflowing issue.

display: flex 考虑边距和填充填充整个容器。这就是它没有溢出的原因。

display: block默认100%宽度,溢出后添加margin和padding

问题很可能是当您在 CSS 中使用“.input-field”时,它可能没有正确使用它,所以我只是输入 'form input.input-field' 并添加了一些 CSS 到表单元素。现在它看起来完全对齐了。

:root {
  --main-color: #00308f;
  --secondary-color: #7cb9e8;
  --dark-color: #444;
  --light-color: #fafafa
}

body {
  font-family: Montserrat, sans-serif;
  background-color: var(--light-color);
  color: var(--dark-color);
  text-align: justify;
  margin-top: 70px;
  margin-left: 0;
  margin-right: 0;
  margin-bottom: 0;
  padding: 1em
}



.my-contacts-div {
  align-items: center
}

.contacts-list {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center
}

.contact-card {
  width: 288px;
  margin: .5em;
  border-radius: .5em;
  box-shadow: var(--secondary-color) 1px 1px 10px;
  padding: 0 .75em;
  word-wrap: break-word
}

.contact-form {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: grid;
  align-self: center;
  width: 350px;
  z-index: 1;
  background-color: var(--secondary-color);
  border-radius: 1em;
  padding: 1em;
  box-shadow: 1px 1px 1.5em var(--main-color);
  visibility: hidden
}

.contact-form:target {
  visibility: visible
}

form input.input-field {
  margin: .5em 0;
  border: solid 1px var(--secondary-color);
  border-radius: .5em;
  padding: 0 1em;
  height: 40px;
  box-sizing: border-box;
}

form {
  box-sizing: border-box;
  padding: 0 0.5em;
}

.searchbar {
  margin: .5em;
  width: 100%
}

@media screen and (max-width:687px) {
  .my-contacts-div {
    padding: 0
  }

  .contact-card {
    width: 100%
  }
}

@media screen and (max-width:614px) {
  body {
    margin-top: 130px
  }
}
<div class="my-contacts-div">
  <h2>Heading</h2>
  <form><input class="input-field searchbar" type="text" placeholder="Search here..."></form>
  <div class="contacts-list">
    <div class="contact-card">
      <h3>Other component</h3>
    </div>
  </div>
</div>