Align Vertically Horizontally Aligned Objects

Asked

Viewed 939 times

1

The idea here is centralizar verticalmente the radio button with his label (horizontally aligned).

Where am I going wrong?

inserir a descrição da imagem aqui

<ul class="formasPgto">

 <li>
  <input type="radio" name="pagamento" value="pagseguro" id="pagseguro" />
  <label for="pagseguro">Pag Seguro</label>
 </li>

 <li>
  <input type="radio" name="pagamento" value="boleto"  id="boleto" />
  <label for="boleto">Boleto</label>
 </li>

 <li>
  <input type="radio" name="pagamento" value="cartao" id="cartao" />
  <label for="cartao">Cartão</label>
 </li>

</ul>

CSS

.formasPgto {
  list-style: none;
  margin: 0 auto;
  padding: 0;  
  width: 800px;
  height: 100px;
  line-height: 100px;
  border: #000 .3px solid;
  text-align: center;
}

.formasPgto li {
  display: inline-block;
  width: 250px;
  height: 50px;
  border: #000 .3px solid;
  vertical-align: middle;
}

.formasPgto li label, .formasPgto li input[type=radio]  {
  display: inline-block;
  vertical-align: middle;  
}

1 answer

1


The problem is in those two lines, take it out of the <ul>:

...
line-height:100px;
...

There are several ways to center vertically/horizontally elements in CSS:

  1. https://jsfiddle.net/wgoc40bk/2/ In this case, keeping the html structure you have is what I would do

  2. Changing a little HTML structure, putting the elements to center on a parent box, so we can opt for this solution: https://jsfiddle.net/wgoc40bk/5/ . We play with the displays (display:table/display:table-cell) in the mother box (.formasPgto li) and create another mother box (<div>) to help us in this task, to group the elements to center. A Simple Example: http://www.vanseodesign.com/blog/demo/vertical-centering/table-cell.php

  3. Taking advantage of the html structure we created at point two we can also choose to: https://jsfiddle.net/wgoc40bk/6/ . I also use this way a lot, defining position:relative in the mother box (.formasPgto li) and the box to be centred (div) we define among other things the position:absolute. Here we manually define a height for our <div>, else by default position Absolute sets the height to 100%

I always recommend to put a wrapper (mother box), in these last two cases a div, to group the elements to be centred

PS: I just haven’t written the whole CSS here so it doesn’t get too long... In these examples above CSS has been developed

  • I didn’t understand it very well. Come on: the ul box with width: 800px height: 100px. Internal content aligned horizontally: text-Alig: center. Each li width: 250px height: 50px. vertical-align: Middle. Thus li does not align vertically

  • In this case you can use this example: https://jsfiddle.net/wgoc40bk/2/

  • It seems that label can not align vertically with vertical-align: Middle. Is that right? And in this case the feature is to use padding?

  • Ha vertical-align: middle would give the effect you want if the hand box had display table and the element to center display: table-cell;. I will complete the answer min

  • Then I understood correctly. Some elements accept the vertical-aligment without display table, while others do not. In this case I will prefer to use even padding. Thank you. I am entering the tableless world.

  • vertical-aligment is useful for example also for aligned elements. Play with vertical-align in this example: https://jsfiddle.net/87vL52fz/ , here the alignment is done in relation to the other 'brother' elements'

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.