如何显示HTML <FORM>作为内联元素?


83

这可能是一个基本的html / css问题...

我有一个简单的一键式表单,我想在段落文本中内联显示。

<p>Read this sentence 
     <form style='display:inline;'>
     <input style='display:inline;' 
            type='submit' 
            value='or push this button'/>
     </form>.
</p>

即使表单具有style = display:inline属性,我在表单之前也会换行。有办法摆脱它吗?

表单元素可以出现在里面<p>吗?


3
谢谢大家的回答和评论!
Evgeny

Answers:


73

将表单标签移到段落外,然后将边距/填充设置为零:

<form style="margin: 0; padding: 0;">
  <p>
    Read this sentence 
    <input style="display: inline;" type="submit" value="or push this button" />
  </p>
</form>

您的回答挽救了我的生命。
DrinkJavaCodeJava

3
我的表单中的边距和填充对我不起作用,我不得不使用display:inline,然后不需要边距/填充。
angularsen 2014年

2
display:inline也为我做了。为什么不将其添加到答案中的表单样式中呢?
Praesagus

也救了我!天哪,浪费了两个小时,我所要做的就是将这些段落移到输入之外。我希望我知道为什么这样做,但是感谢上帝。
gunslingor'1

54

<form>不能进去<p>,不。当浏览器试图处理它认为未关闭的段落元素<p>时,它会突然关闭您的元素,当它碰到开始<form>标记时:

<p>Read this sentence 
 </p><form style='display:inline;'>

22

您可以尝试以下代码:

<form action="#" method="get" id="login" style=" display:inline!important;">

  <label for='User'>User:</label> 
  <input type='text' name='User' id='User'>

  <label for='password'>Password:</label><input type='password' name='password' id='password'>
  <input type="submit" name="log" id="log" class="botton" value="Login" />

</form> 

要注意的重要一点是<form>标签中的css style属性。

display:inline!important;



0

我认为,只需在该段中包含“提交”按钮,就可以完成所需的工作:

     <pre>
     <p>Read this sentence  <input  type='submit' value='or push this button'/></p>
     </pre>   

0

只需float: left以这种方式使用样式:

<p style="float: left"> Lorem Ipsum </p> 
<form style="float: left">
   <input  type='submit'/>
</form>
<p style="float: left"> Lorem Ipsum </p>

3
最好按照其他答案中的建议来修复标记,而不是float技巧。
杰里米·J·斯塔彻

0

添加一个内联包装器。

<div style='display:flex'>
<form>
 <p>Read this sentence</p>
 <input type='submit' value='or push this button' />
</form>
<div>
    <p>Message here</p>
</div>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.