英文:
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: "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;
}
<!-- language: lang-html -->
<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>
<!-- 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
需要将<label>
添加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 <label>
:
<!-- 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: "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;
}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
More information
- List of HTML tags - MDN Docs
<label>
- MDN Docs
In any case, I consider this incorrect: if the function of the <label>
differs from the names of the <input>
fields, it is better to use an appropriate tag instead. In most cases, we use <div>
, but if you want to provide a heading, then the <h1>
, 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 <h1-6>
, it'll center properly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论