如何将标签居中放置在网页中间的 div 内?

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

How do you center a label inside a div at the middle of a webpage?

问题

我花了相当多的时间尝试为我的简单JavaScript计数器创建一个类似计算器的设计,但我无法将蓝色屏幕(由显示值的标签表示)移到计数器的粉色主体(由一个div表示)内部。我想要将屏幕置于计数器的最顶部中间位置。相反,我得到了这个结果:

如果你无法看到我的代码,无论什么原因;我的计数器主体(counter_body)和计数器标签(counter_label)的CSS代码如下:

#counter_label{
  text-align: center;
  margin: auto;
  height: 200px;
  width: 300px;
  font-family: "Comic Sans MS";
  background-color: aqua;
  padding-left: 15%;
  padding-right: 15%;
}

#counter_body{
  color: red;
  border: 1px solid rgb(255, 0, 136);
  border-radius: 5%;
  padding-top: 20%;
  padding-left: 15%;
  padding-right: 15%;
  background: rgb(183, 154, 159);
  height: 200px;
  width: 300px;
  margin: auto;
}
<label id="counter_label">0</label>

<div id="counter_body">
  <button type="button" id="increase">Increase</button>
  <button type="button" id="decrease">Decrease</button>
  <button type="button" id="reset">Reset</button>
  <button type="submit" id="submit">Submit</button>
</div>

<p id="counter_output"></p>

我尝试了我能想到的一切,作为一个绝对的新手编码者,但我无法将蓝色标签移动到正确的位置。任何帮助将不胜感激。

英文:

I've spent quite some time trying to create a calculator-esque design for my simple JavaScript counter, but I can't move the blue screen, represented by a label which displays the value, inside the counter's pink body, which is represented by a div. I want the screen at the middle of the topmost part of the counter. Instead, I'm getting this:.

In case you can't see my code for whatever reason; my CSS code for the counter_body and counter_label is as follows;

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

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

#counter_label{
  text-align: center;
  margin: auto;
  height: 200px;
  width: 300px;
  font-family: &quot;Comic Sans MS&quot;;
  background-color: aqua;
  padding-left: 15%;
  padding-right: 15%;
}


#counter_body{
  color: red;
  border: 1px solid rgb(255, 0, 136);
  border-radius: 5%;
  padding-top: 20%;
  padding-left: 15%;
  padding-right: 15%;
  background: rgb(183, 154, 159);
  height: 200px;
  width: 300px;
  margin: auto;
}

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

&lt;label id = &quot;counter_label&quot;&gt;0&lt;/label&gt;

&lt;div id=&quot;counter_body&quot;&gt;
  &lt;button type=&quot;button&quot; id=&quot;increase&quot;&gt;Increase&lt;/button&gt;
  &lt;button type=&quot;button&quot; id=&quot;decrease&quot;&gt;Decrease&lt;/button&gt;
  &lt;button type=&quot;button&quot; id=&quot;reset&quot;&gt;Reset&lt;/button&gt;
  &lt;button type=&quot;submit&quot; id=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/div&gt;

&lt;p id=&quot;counter_output&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

I've tried everything I can think of, being an absolute newbie coder, but I can't push the blue label into place. Some help would be much appreciated.

答案1

得分: 0

需要将&lt;label&gt;添加display: block;属性:

#counter_label {
  display: block;
  text-align: center;
  margin: 0 auto;
  height: 200px;
  width: 300px;
  font-family: "Comic Sans MS";
  background-color: aqua;
  padding-left: 15%;
  padding-right: 15%;
}

更多信息:

无论如何,我认为这是不正确的:如果<label>的功能与<input>字段的名称不同,最好使用适当的标签。 在大多数情况下,我们使用<div>,但如果您想提供标题,那么<h1>等标签也可能有帮助。

英文:

Solution

Need to add display: block; to &lt;label&gt;:

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

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

#counter_label {
  display: block;
  text-align: center;
  margin: 0 auto;
  height: 200px;
  width: 300px;
  font-family: &quot;Comic Sans MS&quot;;
  background-color: aqua;
  padding-left: 15%;
  padding-right: 15%;
}


#counter_body{
  color: red;
  border: 1px solid rgb(255, 0, 136);
  border-radius: 5%;
  padding-top: 20%;
  padding-left: 15%;
  padding-right: 15%;
  background: rgb(183, 154, 159);
  height: 200px;
  width: 300px;
  margin: auto;
}

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

&lt;label id = &quot;counter_label&quot;&gt;0&lt;/label&gt;

&lt;div id=&quot;counter_body&quot;&gt;
  &lt;button type=&quot;button&quot; id=&quot;increase&quot;&gt;Increase&lt;/button&gt;
  &lt;button type=&quot;button&quot; id=&quot;decrease&quot;&gt;Decrease&lt;/button&gt;
  &lt;button type=&quot;button&quot; id=&quot;reset&quot;&gt;Reset&lt;/button&gt;
  &lt;button type=&quot;submit&quot; id=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/div&gt;

&lt;p id=&quot;counter_output&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

More information

In any case, I consider this incorrect: if the function of the &lt;label&gt; differs from the names of the &lt;input&gt; fields, it is better to use an appropriate tag instead. In most cases, we use &lt;div&gt;, but if you want to provide a heading, then the &lt;h1&gt;, etc. tags can also be helpful.

答案2

得分: -1

使用标签是不正确的HTML元素在这里使用。标签应该与Input标签配对使用,在这里查看更多详情。

如果您使用不同的元素,比如<h1-6>,它将正确居中。

英文:

Using a label is the incorrect html element to use here. Labels should be paired with the Input tag, see more details here.

If you use a different element, such as &lt;h1-6&gt;, it'll center properly.

huangapple
  • 本文由 发表于 2023年8月5日 04:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838841.html
匿名

发表评论

匿名网友

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

确定