tags:

views:

189

answers:

3

I want both labels and textbox appear on same line ("Search for products:" label is above "search" label, i want them to be on the same line), the difference is slight but it exists, I want them to be precise.

online version: link

Markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
  <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;
 }
A: 

I did it by adding some padding to the following section of the CSS:

.displayModes
{
    float:left;
    padding-left: 2px;
    padding-top: 2px;  
    text-align: center;
}
Buggabill
This doesn't help. they still not aligned.
markiz
+1  A: 

Wrap all text in divs so you can add top padding so they all line up on the same line well. Always specify widths when floating. I find it easier to specify floats as all left and having specific widths.

Markup

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

Update without extra divs for label centering

Markup

<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;
}
OG
Can't I have both "Search" label and textbox inside one div?
markiz
Yes, you can. It's just that I don't like the default vertical alignment between inputs and text that browsers give. On the first example the text appears a little low off of the vertical center of the input. But when you have them in separate divs you can make them line up better.
OG
Can you please add the code(the one with out extra divs) to your answer ? and i will accept your answer.
markiz
A: 

Hi, just set up the line heigh of the div "displayModes" to 20px:

<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;
   padding-left: 2px;  
   text-align: center;
   line-height: 20px;
}

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

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

Small suggestion: you have a problem of overflow, just add a "clearer" div to force the childs div to be contained from "panel":

<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 class="clearer"></div>
</div>

CSS:

.clearer{ clear: both; }


FIX: The line-height, of the "displayModes" div, has to be 21px! Tested on FF 3.5.3 and Safari 4.0.3

BitDrink
can you please explain what do you mean "problem of overflow"? the divs seemed to be displayed fine...
markiz
Btw, your solution doesn't help, elements still not aligned.
markiz
On IE I don't know, but in FF and Safari it works fine! The Div "panel" has not a fixed height, so, when you float both the div "displayModes" and the div "searchPanel", they exceed the area of the div father ... so, they overflow the div container! Even if the browser displays all fine, they are not actually contained from the div "panel" ... try to set a background color to the div "panel" and you will see what I meant!
BitDrink