简体   繁体   English

无论如何将占位符文本放在输入字段上方而不是放在里面

[英]Is there anyway to place the placeholder text above the input field instead of placing it in the inside

So I'm basically making a form but I was requested to place the placeholder text above the text like this form that I need to make所以我基本上是在制作一个表格,但我被要求将占位符文本放在文本上方,就像我需要制作的表格一样

But I can't figure out how to do that.但我不知道该怎么做。 I tried placing the text as a label over the input field but when I change the size of the browser the alignment is all over the place so I want to the label to align with the input field above it.我尝试将文本作为 label 放置在输入字段上,但是当我更改浏览器的大小时,alignment 到处都是,所以我想让 label 与其上方的输入字段对齐。

Here is what I tried doing:这是我尝试做的事情:

 .row { display: flex; justify-content: space-around; padding: 10px; }
 <div class="row"> <:-- Name of Company --> <label class="comp" for="company" style="left; 40px;">Company</label> <input type="text" id="company" name="company" placeholder="Company" /> <!-- Clients First Name --> <label class="comp" for="firstname">First Name</label> <input type="text" id="firstname" name="firstN" placeholder="First Name" /> </div>

But what I get is this .但我得到的是这个

Is there anyone that can help me with this?有没有人可以帮我解决这个问题?

Move the <input /> inside of the <label /> and display: block;<input />移到<label />内部并display: block; it.它。 If you want to separately style the text, wrap it in a <span /> .如果要单独设置文本样式,请将其包装在<span />中。

 .row { display: flex; justify-content: space-around; padding: 10px; } input { display: block; }
 <div class="row"> <!-- Name of Company --> <label class="comp"> Company <input type="text" id="company" name="company" placeholder="Company" /> </label> <!-- Clients First Name --> <label class="comp" for="firstname"> First Name <input type="text" id="firstname" name="firstN" placeholder="First Name" /> </label> </div>

Placeholder vs. Label.占位符与 Label。
You can not move the input placeholder text outside of the designated input.您不能将输入占位符文本移到指定输入之外。 Placeholder text is strictly handled by the browser.占位符文本由浏览器严格处理。 You may only modify the content using the placeholder="" HTML property and style it with CSS pseudo class ::placeholder .您只能使用placeholder="" HTML 属性修改内容,并使用 CSS pseudo class ::placeholder设置样式。

Instead use the label HTML element like so:而是像这样使用 label HTML 元素:
(reference:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label ) (参考:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label

<div class="preference">
    <label for="cheese">Do you like cheese?</label>
    <input type="checkbox" name="cheese" id="cheese">
</div>

Display Label Above Input.在输入上方显示 Label。
Now, you mentioned your label is appearing to the left of the input instead above it?现在,您提到您的 label 出现在输入的左侧而不是上方? You will need to style your CSS like this.您需要像这样设置 CSS 的样式。 Add the following CSS markup:添加以下 CSS 标记:

label {
    display: block;
}

I would also recommend, but not required, setting the display element on your input fields to block as well.我还建议,但不是必需的,将输入字段上的显示元素也设置为阻止。

input {
    display: block;
}

Why?为什么?
By default label is rendered as display: inline;默认情况下 label 呈现为display: inline; or display: inline-block;display: inline-block; by your browser user-agent.由您的浏览器用户代理。 Setting it to display: block;将其设置为display: block; will ensure it will always appear above and not inline with the input.将确保它始终出现在上方而不是与输入内联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM