html - 对齐元素在同一行

标签 html css

我希望标签和文本框都出现在同一行(“搜索产品:”标签在“搜索”标签上方,我希望它们在同一行),差异很小但存在,我想要它们准确地说。

在线版:link

标记:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  <html lang="en">
    <head>
       <title>Test</title>
    </head>
    <body>
      <div class="panel">
        <div class="displayModes">
           Search for products:          
        </div>
      <div class="searchPanel">
        Search :
        <input name="txtSearch" type="text" id="txtSearch" style="height:14px;" />          
      </div>
   </div>    
 </body>
</html>

CSS:

.displayModes
{
        float:left;
    padding-left: 2px;  
    text-align: center;
}

.searchPanel
{
    float: right;
    margin-right: 150px;
    text-align: center;
}
.panel
{
font-family: Arial, Verdana, Sans-serif;
font-size: 12px;

    padding-top:10px;
    width:600px;
 }

最佳答案

将所有文本包裹在 div 中,这样您就可以添加顶部填充,使它们都很好地排列在同一行上。 float 时始终指定宽度。我发现将 float 指定为全部左侧并具有特定宽度更容易。

标记

 <div class="panel">
    <div class="displayModes">
       Search for products:          
    </div>
    <div class="searchPanel">
        <div style="float:left;width:60px;padding-top:2px;"> 
            Search :
        </div>
        <div style="float:left;width:100px;"> 
            <input name="txtSearch" type="text" id="txtSearch" style="height:14px;" />  
        </div>        
     </div>
  </div>    

CSS

.displayModes
{
    float:left;
    width:200px;
    padding-top:2px;
    padding-left: 2px;  
    text-align: center;
}

.searchPanel
{
    float: left;
    width:200px;
    margin-right: 150px;
    text-align: center;
}

.panel
{
    font-family: Arial, Verdana, Sans-serif;
    font-size: 12px;
    padding-top:10px;
    width:600px;
}

在没有额外 div 的情况下更新标签居中

标记

<div class="panel">
    <div class="displayModes">
       Search for products:          
    </div>
    <div class="searchPanel">        
        Search :        
        <input name="txtSearch" type="text" id="txtSearch" style="height:14px;"/>                
     </div>
</div>

CSS

.displayModes
{
    float:left;
    width:200px;
    padding-top:4px;
    padding-left: 2px;  
    text-align: center;
}

.searchPanel
{
    float: left;
    width:200px;
    margin-right: 150px;
    text-align: center;
}

.panel
{
    font-family: Arial, Verdana, Sans-serif;
    font-size: 12px;
    padding-top:10px;
    width:600px;
}

关于html - 对齐元素在同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1614071/

相关文章:

javascript - 尝试使用 javascript 验证 IP 地址

css - react 原生 :"order"不是有效的样式属性

html - 如何将第一个表td设置为100%宽度并使其他td低于第一个td

javascript - 如何在 Angular Material 中固定 mat-select-panel 的位置

css - 推特 Bootstrap : Collapsing menu for higher resolution screen such as Large desktop

css - Sass for 循环中的变量范围

jquery - removeClass 并保持效果转换 jQuery

javascript - 滚动条中包含的选项卡名称

javascript - 使用 React 显示图像

html - 自定义复选框不适用于多个复选框