Dynamically centered nested vis
The problem is that I can't use any fixed widths or heights. I have a textbox which needs to vertically and horizontally centered inside a bordered box. There should be 10px of margin between my textbox and the bordered box. The bordered box, in turn, should be horizontally centered.
Basically, no matter what text I enter, I need the div "copy-container" to scale its height/width accordingly whilst still maintaining 1) horizontal and vertically centered text 2) horizontally centered "copy-container".
The code I have below achieves everything except that the text doesn't vertically align :(
.wrapper {
box-sizing: border-box;
width: 100%;
position: absolute;
bottom: 55px;
height: 300px;
background-color: #ecf0f1;
}
.copy-container {
position: relative;
display: table;
height: auto;
/*padding: 5px 10px 5px 10px;*/
border: 1px solid black;
margin: auto;
}
.copy-container p {
position: relative;
margin: 5px 5px 5px 5px;
}
<div class="body">
<div class="wrapper">
<div class="copy-container">
<p>WHERE BROOKLYN AT <br> WHERE BROOKLYN AT <br> WHERE BROOKLYN AT</p>
<div class="cta-container">
</div>
</div>
</div>
</div>
I usually use the same trick for vertical centering: set the element to position: absolute;
, then scoot it by 50% from the top and left, and translate it to the top and left by half of its size:
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
.wrapper {
box-sizing: border-box;
width: 100%;
position: absolute;
bottom: 55px;
height: 300px;
background-color: #ecf0f1;
}
.copy-container {
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
display: table;
height: auto;
/*padding: 5px 10px 5px 10px;*/
border: 1px solid black;
margin: auto;
}
.copy-container p {
position: relative;
margin: 5px 5px 5px 5px;
}
<div class="body">
<div class="wrapper">
<div class="copy-container">
<p>WHERE BROOKLYN AT <br> WHERE BROOKLYN AT <br> WHERE BROOKLYN AT</p>
<div class="cta-container">
</div>
</div>
</div>
</div>
链接地址: http://www.djcxy.com/p/13240.html
上一篇: 如何在悬停而不是点击的情况下制作Twitter Bootstrap菜单下拉菜单
下一篇: 动态集中嵌套vis