2017-04-25 39 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