0

我有一个输入元素,它的左侧和右侧都有 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>

它有什么问题?

4

2 回答 2

0

在标签中添加 display: flex。它将解决溢出问题。

display: flex填充整个容器,同时考虑边距和填充。这就是为什么它没有溢出。

display: 块默认为 100% 宽度,然后在导致溢出后添加边距和填充。

于 2021-09-24T11:56:09.560 回答
0

问题很可能是当您在 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>

于 2021-09-24T11:57:42.470 回答