0

HTML

<div class="containerDiv">
    <p>Test</p>
    <input type="checkbox" checked="checked" />
</div>

CSS

.containerDiv 
{
    width:600px;
}

.containerDiv p 
{
    margin: 10px 0px 10px 10px;
    float: left; 
    width: 500px;
}

.containerDiv input[type='checkbox']
{
    float: right;
    margin: auto 10px;
}

Hello,

I have a div with a text (the text can be very long) in a p-tag and a input. I want vertical center the input right next to the text, but float: left and margin: auto 10px doesn't work.

freshbm
  • 5,525
  • 3
  • 43
  • 70

4 Answers4

1

You can place your checkbox in absolute position and use top:50% minus the checkbox height. So you don't need to float:right this element but give it a right position. See this demo: http://jsfiddle.net/4wXuC/1/

otinanai
  • 3,769
  • 2
  • 21
  • 42
0

For giving margin try margin: 10px auto; instead of margin: auto 10px;

Paul Lammertsma
  • 35,234
  • 14
  • 128
  • 182
Atif Azad
  • 487
  • 1
  • 4
  • 15
-1

Use label instead of p and set the line-height of the label to the total height of the input.

simbo
  • 334
  • 3
  • 6
-1
 <div style="width: 55px;">
<p style="float: left; margin: 0px 0px 10px 10px; width: auto;">Test</p>
<input checked="checked" style="float: right; margin: 5px auto;" type="checkbox">
 </div>
Atif Azad
  • 487
  • 1
  • 4
  • 15