Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: Use .pull-right without having to hardcode a negative margin-top

This is my code (please see fiddle here):

<div class='container'>     <div class='hero-unit'>         <h2>Welcome</h2>          <p>Please log in</p>         <div id='login-box' class='pull-right control-group'>             <div class='clearfix'>                 <input type='text' placeholder='Username' />             </div>             <div class='clearfix'>                 <input type='password' placeholder='Password' />             </div>             <button type='button' class='btn btn-primary'>Log in</button>         </div>     </div> </div> 

I want #login-box to have .pull-right and be at the same height as the p. I can use margin-top: -100px but that feels wrong.

How can I have #login-box at the same height as the p without hardcoding a negative margin-top?

like image 873
Randomblue Avatar asked Jan 19 '13 16:01

Randomblue


People also ask

How do I set margin-right in bootstrap?

l - sets margin-left or padding-left. r - sets margin-right or padding-right. x - sets both padding-left and padding-right or margin-left and margin-right. y - sets both padding-top and padding-bottom or margin-top and margin-bottom.

How do I create a left and right margin in bootstrap?

l - for classes that set margin-left or padding-left. r - for classes that set margin-right or padding-right. x - for classes that set both *-left and *-right.


2 Answers

Float elements will be rendered at the line they are normally in the layout. To fix this, you have two choices:

Move the header and the p after the login box:

<div class='container'>     <div class='hero-unit'>          <div id='login-box' class='pull-right control-group'>             <div class='clearfix'>                 <input type='text' placeholder='Username' />             </div>             <div class='clearfix'>                 <input type='password' placeholder='Password' />             </div>             <button type='button' class='btn btn-primary'>Log in</button>         </div>          <h2>Welcome</h2>          <p>Please log in</p>      </div> </div> 

Or enclose the left block in a pull-left div, and add a clearfix at the bottom

<div class='container'>     <div class='hero-unit'>          <div class="pull-left">            <h2>Welcome</h2>            <p>Please log in</p>          </div>          <div id='login-box' class='pull-right control-group'>             <div class='clearfix'>                 <input type='text' placeholder='Username' />             </div>             <div class='clearfix'>                 <input type='password' placeholder='Password' />             </div>             <button type='button' class='btn btn-primary'>Log in</button>         </div>          <div class="clearfix"></div>      </div> </div> 
like image 136
Dutow Avatar answered Sep 20 '22 15:09

Dutow


just put #login-box before <h2>Welcome</h2> will be ok.

<div class='container'>     <div class='hero-unit'>         <div id='login-box' class='pull-right control-group'>             <div class='clearfix'>                 <input type='text' placeholder='Username' />             </div>             <div class='clearfix'>                 <input type='password' placeholder='Password' />             </div>             <button type='button' class='btn btn-primary'>Log in</button>         </div>         <h2>Welcome</h2>          <p>Please log in</p>      </div> </div> 

here is jsfiddle http://jsfiddle.net/SyjjW/4/

like image 35
pktangyue Avatar answered Sep 20 '22 15:09

pktangyue