[英]Unable to vertically align a label and a button
Please excuse me if the question may sound silly, hoping that it will be helpful to someone else. 如果这个问题听起来很愚蠢,请原谅我,希望对其他人有帮助。
I'm trying to have my buttons well aligned with a given label. 我正在尝试使按钮与给定标签对齐。 Unfortunately, I am quite not very successful, since despite the same height and padding values, the various elements are not aligned on the vertical plane as I would like them to be. 不幸的是,我并不是很成功,因为尽管高度和填充值相同,但各种元素并未按照我希望的那样在垂直平面上对齐。
What I basically did is to copy the CSS bits that I have for my label .resizedTextbox
and pasted them in those of my buttons button
. 我基本上要做的是复制标签.resizedTextbox
拥有的CSS位,并将它们粘贴到按钮button
。
Does any one have the same issue or what am I missing here? 有人有同样的问题吗?或者我在这里想念什么?
.resizedTextbox { width: 40px; height: 20px; padding: 5px 5px 5px 5px; border-color: #3e12cc; text-shadow: 0px 1px 1px #154682; background-color: #f5effb; vertical-align: text-top; text-align: center; font-size: 18px; } .button { border-radius: 3px; background-color: blue; border: none; color: white; padding: 5px 5px 5px 5px; text-align: center; text-decoration: none; cursor: pointer; width: 80px; height: 20px; letter-spacing: 2px; font-size: 18px; }
<div id="section"> <h2>Trying to align buttons and label</h2> <div id="home_automation_tab"> <p></p> <td class="tablerows"><input id="lab91" type="text" name="country" class="resizedTextbox" value="0" readonly> <button class="button">Off</button> <button class="button">30%</button> <button class="button">50%</button> <button class="button">80%</button> <button class="button">On</button> </div> </div>
Try to use Flexbox 尝试使用Flexbox
Also no need to write padding: 5px 5px 5px 5px
...just use padding: 5px
if values for top, right, bottom, left are same. 同样也不需要写padding: 5px 5px 5px 5px
...只要top,right,bottom,left的值相同,就使用padding: 5px
。
And also remove <td class="tablerows">
from your code..no need here 并从您的代码中删除<td class="tablerows">
。
#home_automation_tab { display: flex; } .resizedTextbox { width: 40px; padding: 5px; border-color: #3e12cc; text-shadow: 0px 1px 1px #154682; background-color: #f5effb; text-align: center; font-size: 18px; } .button { border-radius: 3px; background-color: blue; border: none; color: white; padding: 5px; text-align: center; cursor: pointer; width: 80px; letter-spacing: 2px; font-size: 18px; margin-left: 5px; }
<div id="section"> <h2>Trying to align buttons and label</h2> <div id="home_automation_tab"> <p></p> <input id="lab91" type="text" name="country" class="resizedTextbox" value="0" readonly> <button class="button">Off</button> <button class="button">30%</button> <button class="button">50%</button> <button class="button">80%</button> <button class="button">On</button> </div> </div>
Also your code is working fine as it written..you will need to use box-sizing: border-box
for both button
and label
and try to change the value of height so that the content get visible 同样,您的代码在编写时也可以正常工作。.您将需要使用box-sizing: border-box
button
和label
box-sizing: border-box
并尝试更改height的值,以使内容可见
.resizedTextbox { width: 40px; height: 30px; padding: 5px; border-color: #3e12cc; text-shadow: 0px 1px 1px #154682; background-color: #f5effb; vertical-align: top; text-align: center; font-size: 18px; box-sizing: border-box; } .button { border-radius: 3px; background-color: blue; border: none; color: white; padding: 5px; text-align: center; text-decoration: none; cursor: pointer; width: 80px; height: 30px; letter-spacing: 2px; font-size: 18px; box-sizing: border-box; }
<div id="section"> <h2>Trying to align buttons and label</h2> <div id="home_automation_tab"> <p></p> <input id="lab91" type="text" name="country" class="resizedTextbox" value="0" readonly> <button class="button">Off</button> <button class="button">30%</button> <button class="button">50%</button> <button class="button">80%</button> <button class="button">On</button> </div> </div>
There are multiple ways to solve this problem. 有多种方法可以解决此问题。 The flexbox answer above by Bhuwan is probably the best way to go. Bhuwan的上述flexbox答案可能是最好的方法。
If you are stuck with the HTML structure and just wanted a css solution, do the following: 如果您坚持使用HTML结构并且只想要CSS解决方案,请执行以下操作:
vertical-align:text-top
to both css classes 确保将vertical-align:text-top
复制到两个css类 box-sizing: border-box
to each (I used the wildcard selector below, but you can just add it to each). 为每个box-sizing: border-box
添加box-sizing: border-box
(我在下面使用了通配符选择器,但是您可以将其添加到每个box-sizing: border-box
)。 * { box-sizing: border-box; } .resizedTextbox { width: 40px; height: 40px; padding: 5px; border-color:#3e12cc; text-shadow:0px 1px 1px #154682; background-color:#f5effb; vertical-align:text-top; text-align:center; font-size:18px; } .button { border-radius:3px; background-color: blue; border: none; color: white; padding: 5px; text-align: center; text-decoration: none; cursor: pointer; width: 80px; height: 40px; letter-spacing: 2px; font-size:18px; vertical-align:text-top; }
<div id="section"> <h2>Trying to align buttons and label</h2> <div id="home_automation_tab"> <p></p> <td class="tablerows"> <input id="lab91" type="text" name="country" class="resizedTextbox" value="0" readonly> <button class="button">Off</button> <button class="button">30%</button> <button class="button">50%</button> <button class="button">80%</button> <button class="button">On</button> </td> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.