2017-04-25 76 views
0

稍後我將提取樣式。我想學習如何用直線HTML做到這一點。如何將較大的複選框垂直對齊到中間

請注意,只要我的複選框是默認大小,我的wrap1 div中的所有內容垂直對齊在中間。

enter image description here

如果我改變複選框的大小,然後垂直對齊似乎停止工作。 Here is my JSFiddle

enter image description here

<div id="wrapper" style="width: 80%; height: 100%; overflow:hidden; margin: 0 auto; float: left"> 
 
    <div class="row" style="width: 100%; height: 80%; margin: 0 0 0 0; float: left; background-color: aqua;"> 
 
    <div id="heading" class="row"> 
 
     <p style="text-align: center;">This is a title</p> 
 
    </div> 
 
    <div class="wrap1" style="vertical-align: middle;"> 
 
     <div style="width: 15%; line-height: 53px; text-align: center; background-color: yellow; display: inline-block;"> 
 
     <input type="checkbox" style="height: 30px; width: 30px;"> 
 
     </div><div style="width: 70%; line-height: 53px; background-color: orange; display: inline-block;"><label>Description</label> 
 
     </div><div style="width: 15%; line-height: 53px; text-align: center; background-color:green; display: inline-block;"> 
 
     <label>100</label> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+0

你使用' transform:scale(X)'改變複選框的大小? –

+0

不!我用style =「height:30px; width:30px;」在html中。 – Patricia

+0

它在Chrome中不起作用,並將對齊分隔爲其他'div'塊。 –

回答

1

你可以在這裏嘗試幾件事情:如果你想垂直對齊的複選框,你可以嘗試造型的複選框,這樣的:

vertical-align: inherit; 

這會給你的複選框父元素的垂直對齊屬性,並應fi x你的問題。但是,如果該選項無效,請嘗試使用垂直對齊:長度屬性。這允許您通過px值調整對齊,並且可以採用正數或負數。這可能不是你所需要的價值,但例如:

vertical-align: -10px; 
+0

垂直對齊:-10px;做到了! :-) – Patricia

+0

@Patricia - 很高興聽到! :) – TCharb

2

最好的辦法是使用CSS Flexbox的是誠實的,因爲它會自動對齊和居中對齊,垂直:

<style type="text/css"> 

    .main__container { 
     position: relative; 
     width: 500px; 
     height: auto; 
    } 

    .main__container .content__list { 
     position: relative; 
     list-style: none; 
     width: 100%; 
     height: auto; 
    } 

    .main__container .content__list .content__col { 
     position: relative; 
     height: 60px; 
    } 

    .flex-grid { 
     display: -webkit-box; 
     display: -ms-flexbox; 
     display: -webkit-flex; 
     display: -moz-box; 
     flex-wrap: wrap; 
    } 

    .flex-row { 
     -webkit-box-direction: normal; 
     -moz-box-direction: normal; 
     -webkit-box-orient: horizontal; 
     -moz-box-orient: horizontal; 
     -webkit-flex-direction: row; 
     -ms-flex-direction: row; 
     flex-direction: row; 
    } 

    .flex-column { 
     flex-flow: column; 
    } 

    .flex-wrap { 
     -webkit-flex-wrap: wrap; 
     -ms-flex-wrap: wrap; 
     flex-wrap: wrap; 
    } 

    .flex-nowrap { 
     -webkit-flex-wrap: nowrap; 
     -ms-flex-wrap: nowrap; 
     flex-wrap: nowrap; 
    } 

    .flex-cell { 
     flex: 1; 
    } 

    .flex-20 { 
     -webkit-box-flex: 1; 
     -moz-box-flex: 1; 
     -webkit-flex: 1 1 20%; 
     -ms-flex: 1 1 20%; 
     flex: 1 1 20%; 
    } 

    .flex-60 { 
     -webkit-box-flex: 1; 
     -moz-box-flex: 1; 
     -webkit-flex: 1 1 60%; 
     -ms-flex: 1 1 60%; 
     flex: 1 1 60%; 
    } 

    .flex-100 { 
     -webkit-box-flex: 1; 
     -moz-box-flex: 1; 
     -webkit-flex: 1 1 100%; 
     -ms-flex: 1 1 100%; 
     flex: 1 1 100%; 
    } 


    .grid-center { 
     justify-content: center; 
     align-items: center; 
    } 

</style> 

<main class="main__container flex-grid flex-row flex-wrap grid-center "> 
    <header class="title__container flex-cell flex-100"> 
     {{--Nav HTML here --}} 
    </header> 
    <ul class="content__list flex-grid flex-nowrap flex-column grid-center"> 
     <li class="content__col flex-20 one"> 
      <input type="checkbox" style="height: 30px; width: 30px;"> 
     </li> 
     <li class="content__col flex-60 two"></li> 
     <li class="content__col flex-20 there"></li> 
    </ul> 
</main> 
+0

我同意!我現在正在學習如何使用HTML佈局功能,並且在瞭解基礎知識時將樣式移出到CSS。謝謝! – Patricia