将所有元素保持在盒子中央应该如何实现?

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

How do I keep all elements inside a box in the middle of the box?

问题

如何将输入框保持在绿色框的中间?

我已尝试使用margin属性来使输入框居中。但在小屏幕上,它不起作用。我应该怎么做?

英文:

How do I keep the input box in the middle of the green box? 将所有元素保持在盒子中央应该如何实现?

Github-Link for the page: https://tahsinttalha.github.io/fcc-survey-form/

I have tried magin property to center the inout boxes. But in small screen, It's not working properly. What can I do?

答案1

得分: 1

尝试将 box-sizing: border-box; 添加到您输入框的 CSS 定义中:

#input-info-section > input,
#dropdown,
textarea {
    width: 100%;
    height: 40px;
    margin: 10px auto;
    border: 2px solid #333333;
    border-radius: 5px;
    padding: 10px;
    background-color: #fff;
    color: #333;
    font-family: "Inter", sans-serif;
    /* 添加以下行: */
    box-sizing: border-box;
}

这样,填充和边框会与所有其他测量一起计算,而不是在居中元素之后添加。

英文:

Try adding box-sizing: border-box; to the CSS-definition of your input:

#input-info-section > input,
#dropdown,
textarea {
    width: 100%;
    height: 40px;
    margin: 10px auto;
    border: 2px solid #333333;
    border-radius: 5px;
    padding: 10px;
    background-color: #fff;
    color: #333;
    font-family: "Inter", sans-serif;
    /* add the following line: */
    box-sizing: border-box;
}

This way, the paddings and borders are calculated with all other measurements and not added after centering the element.

答案2

得分: 0

可以使用 flex 将元素保持在盒子中央,例如:

<div class="box">
  <input />
</div>

div 中添加 CSS:

.box {
  display: flex;
  flex-direction: row;
  justify-content: center;
}
英文:

you can use flex to keep elements in the middle of box.like:


<div class="box">
  <input />
</div>

add CSS to the div

.box {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

答案3

得分: 0

我建议在主容器内使用填充,这样所有的div和块元素都会占据100%的宽度。

只需要将所有的输入框添加width: 100%。

我做了一个基本的示例:

* {
  margin: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
}

.container {
  max-width: 500px;
  background: lime;
  border-radius: 10px;
  border: 2px solid black;
  padding: 1rem;
}

.title {
  text-align: center;
  margin-bottom: 1rem;
}

.description {
  margin-bottom: 2rem;
}

.form-input, select-input {
  width: 100%;
  outline: none;
  border: 2px solid black;
  border-radius: 5px;
  height: 40px;
  margin: 1rem 0 ;
  padding: 1rem;
}

.select-input {
  width: 100%;
  height: 40px;
  border: 2px solid black;
  border-radius: 10px;
  margin: 1rem 0;
}
<div class="container">
  <h1 class="title">
    个人调查
  </h1>
  <p class="description">
    此调查仅用于网站开发的目的。您的数据安全保密,我们将在分析完成后处理数据。非常感谢您的时间和努力。
  </p>
  <form action="#">
    <label for="" class="form-label">
      姓名
    </label>
    <input type="text" class="form-input" placeholder="输入您的姓名">
    <label for="" class="form-label">
      电子邮件
    </label>
    <input type="text" class="form-input" placeholder="输入您的电子邮件">
    <label for="" class="form-label">
      年龄
    </label>
    <input type="text" class="form-input" placeholder="输入您的年龄">
    <div class="select-container">
      <select class="select-input" name="" id="dropdown">
        <option value="选择">选择您的职业类型</option>
        <option value="">选项1</option>
        <option value="">选项2</option>
        <option value="">选项3</option>
        <option value="">选项4</option>
      </select>
    </div>
  </form>
</div>

希望对您有所帮助 将所有元素保持在盒子中央应该如何实现?

英文:

I suggest to use padding inside the main container, so all the div and block elements will take 100% of the width.

Just need to put width: 100% to all inputs.

I made a basic example :

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

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

* {
  margin: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
}

.container {
  max-width: 500px;
  background: lime;
  border-radius: 10px;
  border: 2px solid black;
  padding: 1rem;
}

.title {
  text-align: center;
  margin-bottom: 1rem;
}

.description {
  margin-bottom: 2rem;
}

.form-input, select-input {
  width: 100%;
  outline: none;
  border: 2px solid black;
  border-radius: 5px;
  height: 40px;
  margin: 1rem 0 ;
  padding: 1rem;
}

.select-input {
  width: 100%;
  height: 40px;
  border: 2px solid black;
  border-radius: 10px;
  margin: 1rem 0;
}

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

&lt;div class=&quot;container&quot;&gt;
    &lt;h1 class=&quot;title&quot;&gt;
      Personal Survey
    &lt;/h1&gt;
    &lt;p class=&quot;description&quot;&gt;
      This survey is being conducted solely for the purpose of the developement of the website. Your data is safe and secured and we will be disposed of the data after our   analysis is completed. Thank your very much for your time and effort.
    &lt;/p&gt;
  &lt;form action=&quot;#&quot;&gt;
    &lt;label for=&quot;&quot; class=&quot;form-label&quot;&gt;
      Name
    &lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-input&quot; placeholder=&quot;Type your name&quot;&gt;
        &lt;label for=&quot;&quot; class=&quot;form-label&quot;&gt;
      Name
    &lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-input&quot; placeholder=&quot;Type your email&quot;&gt;
        &lt;label for=&quot;&quot; class=&quot;form-label&quot;&gt;
      Name
    &lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-input&quot; placeholder=&quot;Type your age&quot;&gt;
    &lt;div class=&quot;select-container&quot;&gt;
      &lt;select class=&quot;select-input&quot; name=&quot;&quot; id=&quot;dropdown&quot;&gt;        
        &lt;option value=&quot;Select&quot;&gt;Select your profession type&lt;/option&gt;
        &lt;option value=&quot;&quot;&gt;option 1&lt;/option&gt;
        &lt;option value=&quot;&quot;&gt;option 2&lt;/option&gt;
        &lt;option value=&quot;&quot;&gt;option 3&lt;/option&gt;
        &lt;option value=&quot;&quot;&gt;option 4&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

I hope it will helps you 将所有元素保持在盒子中央应该如何实现?

huangapple
  • 本文由 发表于 2023年5月10日 16:45:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216499.html
匿名

发表评论

匿名网友

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

确定