简体   繁体   English

如何在50%高度的不同宽度的两个div中对齐内容?

[英]How to align content inside two divs of different width at 50% height?

There are two div-containers: leftcontainer covers the left half of my screen, rightcontainer the right half. 有两个div容器:leftcontainer覆盖了屏幕的左半部分,rightcontainer覆盖了我的右半部分。 Inside both containers is a textbox with variable height 在两个容器中都有一个高度可变的文本框

.leftcontainer{
    position: relative;
    float:left;
    width:60%;
    height:100%;
}
.rightcontainer{
    position: relative;
    float: right;
    width:40%;
    height:100%;
}
.textbox{
    margin-top: 50%;
    width: 100%;
    background:#333333;
}
<div class="leftcontainer">
    <div class="textbox">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore
    </div>
</div>
<div class="rightcontainer">
    <div class="textbox">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore
    </div>
</div>

I want the textboxes to be horizontally aligned: both should start at 50% height. 我希望文本框水平对齐:两者都应从50%的高度开始。 But they are only aligned, if I set the width of leftcontainer and rightcontainer both to 50%. 但是,如果我将leftcontainer和rightcontainer的宽度都设置为50%,它们只会对齐。
If I do 如果我做

.leftcontainer{width:60%;}
.rightcontainer{width:40%;}

the right text box moves up. 右边的文本框向上移动。 Could someone tell me, how to fix this? 有人可以告诉我,如何解决这个问题? Thank you. 谢谢。

If you are using the containers to span the entire visible height of the window, then this is easy to accomplish by switching the height and margin-top units from % to vh which is a unit that represents 1/100th of the the view-port height. 如果您使用容器来跨越窗口的整个可见高度,则可以通过将heightmargin-top单位从%切换为vh来轻松实现, vh是代表视口的1/100的单位高度。

.leftcontainer{
    position: relative;
    float:left;
    width:60%;
    height:100vh;
}
.rightcontainer{
    position: relative;
    float: right;
    width:40%;
    height:100vh;
}
.textbox{
    margin-top: 50vh;
    width: 100%;
    background:#333333;
}

http://jsbin.com/ahAfUba/1/edit?html,css,output http://jsbin.com/ahAfUba/1/edit?html,css,输出

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

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