[英]Inline-block and margin: auto not working for input
I am new to CSS. 我是CSS新手。 I want my textbox (input) to be center aligned, also, I want to set a specific height to it. 我想让我的文本框(输入)居中对齐,而且我想为其设置一个特定的高度。 I want it to be responsive, so I am using % instead of px. 我希望它具有响应性,所以我使用%而不是px。 It (%) seems to be working for width, but not for the height. 它(%)似乎适用于宽度,但不适用于高度。 For height, only px values are working. 对于高度,仅px值有效。 Also, I assigned my input's display property to be inline-block
and margin 0 auto
, but it is not working. 另外,我将输入的显示属性分配为inline-block
和margin 0 auto
,但是它不起作用。 Where am I going wrong?? 我要去哪里错了?
Here's the code: 这是代码:
input {
width: 70%;
margin: 0 auto;
display: inline-block;
height: 20%;
}
Here's a code sample. 这是一个代码示例。
input { width: 70%; margin: 0 auto; display: inline-block; height: 20%; }
<input type="text">
If you want to use %
for height you will have to add some helper CSS first. 如果要使用%
作为高度,则必须首先添加一些辅助CSS。 Like the following: 如下所示:
html,body{
height:100%;
}
and for making the <input>
centre use 并用于使用<input>
中心
margin: 0 auto;
display: block;
A working sample for you: 适合您的工作示例:
input { width: 70%; margin: 0 auto; display: block; height: 20%; } html, body { height: 100%; }
<input type="text">
There is an easy another way if you are Interested you can go with vh
[Viewport-height] , for this method, there is no need to use any other helping CSS to use, so the choice is yours.. 如果您有兴趣,可以使用另一种简单的方法,可以使用vh
[Viewport-height] ,对于这种方法,不需要使用任何其他帮助CSS的方法,因此选择就是您自己。
A working sample for this method: 此方法的工作示例:
input { width: 70%; margin: 0 auto; display: block; height: 20vh; }
<input type="text">
Hope this was helpfull. 希望这对您有所帮助。
In CSS, some, if not most, properties of an element depends on its parent's properties. 在CSS中,元素的某些(如果不是大多数)属性取决于其父级的属性。
So, to make height: 20%
work, you need to set a height for the input
's parent. 因此,要使height: 20%
,您需要为input
的父项设置高度。 In the example below, body
is the parent of input
. 在下面的示例中, body
是input
的父级。
To center-align an element using margin: 0 auto
, the element must have display: block
. 要使用margin: 0 auto
将元素居中对齐,该元素必须具有display: block
。
html, body { height: 100%; /* Set a parent height */ } input { width: 70%; margin: 0 auto; display: block; /* Use display: block; */ height: 20%; }
<input type="text">
Set this style to input
tag. 将此样式设置为input
标签。
input { width: 70%; margin: 0 auto; display: block; height: 20vh; }
<input type="text">
You need to give display:block for aligned center to input and display:inline-block is already apply here and give height to body,html for applying height to input 您需要给display:block对齐中心以输入和显示:inline-block已在此处应用,并将高度赋给body,html则将高度应用于输入
html,body{ height:100%; } input { width: 70%; margin: 0 auto; display: block; height: 20%; }
<input type="text">
If you will not work with flex
then display:block;
如果您不使用flex
display:block;
and width
is important for centering elements. width
对于元素居中很重要。
div { width: 100%; display: block; } input{ display:block; width: 70%; padding: 5px; font-size:18px; margin:0 auto; }
<div> <input type="text"> </div>
Change display: inline-block;
更改display: inline-block;
to display: block;
display: block;
and height: 20%;
height: 20%;
to height: 20vh;
达到height: 20vh;
input { width: 70%; margin: 0 auto; display: block; height: 20vh; }
<input type="text">
UPDATE UPDATE
Only change display: inline-block;
仅更改display: inline-block;
to display: flex;
display: flex;
input { width: 70%; margin: 0 auto; display: flex; height: 20%; }
<input type="text">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.