简体   繁体   English

做一个<input>看起来像段落中的普通文本

[英]Make an <input> look like normal text in a paragraph

Take the code here:把代码放在这里:

<p>Lorem ipsum <input type="text" value="algo" /> dolor sit ...</p>

Sample: http://codepen.io/dbugger/pen/KrAmPx示例: http://codepen.io/dbugger/pen/KrAmPx

How can I make the input look like totally normal text, inside the paragraph?如何使段落内的输入看起来像完全正常的文本? I set it to display: inline but the width seems still fixed.我将它设置为display: inline但宽度似乎仍然固定。

Elements inherit certain default values from browsers.元素从浏览器继承某些默认值。 You need to "reset" all of them in order to make the input element appear as the surrounding text:您需要“重置”所有这些以使输入元素显示为周围的文本:

p input {
  border: none;
  display: inline;
  font-family: inherit;
  font-size: inherit;
  padding: none;
  width: auto;
}

This is as close as you can get with CSS alone.这与单独使用 CSS 所能达到的效果非常接近。 If you want a variable width, you will have to resort to JS instead of CSS, as adjusting an element to it's value is way beyond the scope of CSS .如果你想要一个可变的宽度,你将不得不求助于 JS 而不是 CSS,因为将元素调整为它的值超出了 CSS 的范围 Modifying elements after the fact, based on user input or changes due to just-in-time effects, is what JS/jQuery are used for. JS/jQuery 用于事后修改元素,基于用户输入或由于即时效果而发生的更改。

Note that depending on the browser you're using (and due to the possibility that future browsers might do things radically different that nowadays' practices), this list is not necessarily exhaustive.请注意,根据您使用的浏览器(并且由于未来的浏览器可能会做与现在的做法截然不同的事情),此列表不一定详尽无遗。


The only way you can "fake" this effect in a clean manner without JS is to use an element with a contenteditable attribute, which (unlike an input element) will store user input in the content of the element instead of its value.您可以在没有 JS 的情况下以干净的方式“伪造”这种效果的唯一方法是使用具有contenteditable属性的元素,该属性(与输入元素不同)将用户输入存储在元素的内容中而不是其值中。 For an example of this technique, see this answer有关此技术的示例,请参阅此答案

Though while you won't need JS to get the effect, you would need it to retrieve the content of the element.虽然您不需要 JS 来获得效果,但您需要它来检索元素的内容。 The only use past that I can imagine is if you're providing a printable document that never needs to programmatically handle the input or store it.我可以想象的唯一用途是,如果您提供一个可打印的文档,它从不需要以编程方式处理输入或存储它。

Yes it is possible to do this by mimicing the styling with CSS and by using javascript to automatically adjust the length of the text.是的,可以通过使用 CSS 模仿样式并使用 javascript 自动调整文本长度来做到这一点。


Resize an input to the size of its content.将输入调整为其内容的大小。

 $(function(){ $('#hide').text($('#txt').val()); $('#txt').width($('#hide').width()); }).on('input', function () { $('#hide').text($('#txt').val()); $('#txt').width($('#hide').width()); });
 body, #txt, #hide{ font:inherit; margin:0; padding:0; } #txt{ border:none; color:inherit; min-width:10px; } #hide{ display:none; white-space:pre; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Lorem ipsum <span id="hide"></span><input id="txt" type="text" value="type here ..."> egestas arcu. </p>

It looks like this is possible now.现在看起来这是可能的。 I found a post describing how to style the input so the HTML form styles are stripped.我找到了一篇描述如何设置输入样式的帖子,以便去除 HTML 表单样式。

Styling HTML forms 样式化 HTML 表单

They used the following CSS, and for what I was trying to do, it worked perfectly:他们使用了以下 CSS,对于我尝试做的事情,它运行良好:

input, textarea {
  font    : .9em/1.5em "handwriting", sans-serif;
  border  : none;
  padding : 0 10px;
  margin  : 0;
  width   : 240px;
  background: none;
}

Obviously this is too late for the original author, but I'm hoping other people will benefit from it.显然这对原作者来说为时已晚,但我希望其他人会从中受益。

破解此问题的最佳方法可能只是将您的文本字段设为编辑文本字段,但使其不可编辑,这样您的文本字段和编辑文本字段将看起来相同。

2020 update. 2020 更新。 Partially you can make it look like normal text with appearance property, setting it to none .部分地,您可以使其看起来像具有appearance属性的普通文本,将其设置为none Unfortunately, there is nothing much you can do to make the lines wrap, except use js to replace it with the value.不幸的是,除了使用 js 将其替换为值之外,您无法做任何事情来使行换行。 https://developer.mozilla.org/en-US/docs/Web/CSS/appearance https://developer.mozilla.org/en-US/docs/Web/CSS/appearance

I hope this is the answer you're looking for, but if you want to make an input field look like a normal paragraph (assumably so you can edit some of the text of the paragraph), the best way to do so is to:我希望这是您正在寻找的答案,但是如果您想让输入字段看起来像一个普通的段落(大概是为了您可以编辑段落的某些文本),最好的方法是:

  1. Disable the input's border禁用输入的边框
.maskedinput {
  border-style: none;
}
  1. And then give it the same styles as the parent element, ie text color and bacground color etc etc, and then add a :focus to your CSS that changes the background color such that when the field is clicked, it will be highlighted.然后赋予它与父元素相同的样式,即文本颜色和背景颜色等,然后​​向您的 CSS 添加一个:focus以更改背景颜色,以便在单击该字段时将其突出显示。

Instead of using the <input> tags, you can use the <textarea> tags.您可以使用<textarea>标签而不是使用<input>标签。 They work almost exactly like <input> tags.它们的工作方式几乎与<input>标签完全一样。

 <textarea name="variable" rows="4" cols="50"> Placeholder </textarea>

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

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