简体   繁体   English

将DIV放在DIV中

[英]Center the DIV in DIV

Hello i have a next html code and CSS: 您好,我有下一个HTML代码和CSS:

 html, body { height: 100%; padding: 0; margin: 0; } .teaser { width: 100%; height: 25vh; background-image: url("http://www.1zoom.me/big2/365/321125-alexfas01.jpg"); background-size: cover; background-repeat: no-repeat; background-position: 50% 50%; } a.menuButton { padding: 0; margin: 0; width: 49.7%; height: 33.33%; float: left; background-color: white; border-collapse: collapse; border: 1px solid #E6F0F0; text-decoration: none; position: relative; color: black; } .rectangle { padding: 0; width: 70px; height: 7px; background: blue; top: 0; bottom: 0; left: 0; right: 0; margin: auto; } .menuButtonContent { position: absolute; top: 0; bottom: 0; left: 0; right: 0; padding: 0; height: 50%; width: 50%; background-color: red; margin: auto; } .menuButtonContent h1 { text-align: center; } .buttonsWrapper { width: 100%; height: 73vh; } .rectangle#citizenship { background-color: #38CCBA; } .rectangle#fastsearch { background-color: #00899B; } .rectangle#forillegals { background-color: #D80C00; } .rectangle#soonillegals { background-color: #FF8A00; } .rectangle#aboutapp { background-color: #B8B8B8; } .rectangle#settings { background-color: #626262; } 
 <div class="teaser"></div> <div class="buttonsWrapper"> <a class="menuButton" href="#"> <div class="menuButtonContent"> <h1>Citizenship</h1> <div class="rectangle" id="citizenship"></div> </div> </a> <a class="menuButton" href="#"> <div class="menuButtonContent"> <h1>Fast search</h1> <div class="rectangle" id="fastsearch"></div> </div> </a> <a class="menuButton" href="#"> <div class="menuButtonContent"> <h1>For illegals</h1> <div class="rectangle" id="forillegals"></div> </div> </a> <a class="menuButton" href="#"> <div class="menuButtonContent"> <h1>Soon illegals</h1> <div class="rectangle" id="soonillegals"></div> </div> </a> <a class="menuButton" href="#"> <div class="menuButtonContent"> <h1>About app</h1> <div class="rectangle" id="aboutapp"></div> </div> </a> <a class="menuButton" href="#"> <div class="menuButtonContent"> <h1>Settings</h1> <div class="rectangle" id="settings"></div> </div> </a> </div> 

I have 2 problems: 我有2个问题:

1) I cannot find an apropriate solution to center the .menuButtonContent, it is centered horizontaly, but not vertically, on some screen sizes it gets centered vertically, but not at all - how can i center it? 1)我找不到合适的解决方案来使.menuButtonContent居中,它在水平方向上居中,但在垂直方向上居中,在某些屏幕尺寸上,它在垂直方向上居中,但根本没有居中-我如何居中?

2) The background-image: url doesn't get rendered, why? 2)background-image:url无法渲染,为什么?

To make an element vertically align centered, set the display property of the parent element as display:table; 要使元素垂直居中对齐,请将父元素的显示属性设置为display:table; then set the element that has to be centered as display:table-cell; 然后将必须居中的元素设置为display:table-cell; and vertical-align:midlle; vertical-align:midlle; .

.parent{
    display:table;
}

.child-element-centered{
   display:table-cell;
   vertical-align:midlle;
}

Try using the calc() function in CSS 尝试在CSS中使用calc()函数

lets say the parent div is called "a" and its child was "b" 假设父div称为“ a”,其子名为“ b”

<div id="a" style="width:400px;height:400px;position:relative;">
     <div id="b" style="position:relative;top:calc(25% - 100px);left:calc(25% - 100px);width:50%;height:50%;"></div>
</div>

first set the display of child divs to inline-block 首先将子div的display设置为inline-block

then set the parent text-align to center . 然后将父text-aligncenter This will horizontally center your divs 这将使div水平居中

and then use a "hack" to accomplish vertically aligning your child divs. 然后使用“ hack”来垂直对齐您的孩子div。 See this fiddle for an example 看到这个小提琴的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM