我希望标签和文本框都出现在同一行上(“搜索产品:”标签上面的“搜索”标签,我希望他们在同一行上),差别不大,但它存在,我希望他们是精确的。
在线版本: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指定为all left和具有特定宽度更容易。
标记

 <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;
}

09-17 01:40