简体   繁体   English

将div底部的元素与动态边框对齐(绝对位置+边距)

[英]Align element on bottom of div with dynamic border (absolute position + margin)

I´m currently trying to make an element with variable width and a border at it´s right. 我目前正在尝试制作宽度可变且边框正确的元素。 The border should always stay away a certain distance from the text elements, which vary in size and count. 边框应始终与文字元素保持一定距离,文字元素的大小和数量会有所不同。 So the Border should move dynamically. 因此,边界应动态移动。 It all works fine if I place my text elements inside the dynamic div but the text has to be positioned on the bottom of the page. 如果我将文本元素放在动态div内,则一切正常,但文本必须位于页面底部。 So I tried to use position: absolute; bottom: 0px; left: 0; 所以我尝试使用position: absolute; bottom: 0px; left: 0; position: absolute; bottom: 0px; left: 0; . But I learned due to the absolute positioning margin won´t work any further. 但是我了解到,由于绝对定位margin无法继续工作。

You can find my code here on codepen.io . 您可以在codepen.io上找到我的代码。 The current state is the working dynamic border but with the text on the top side. 当前状态是工作动态边框,但文本在顶部。 I commented out the non working parts, which you can find on the bottom of the CSS file. 我注释掉了非工作部分,您可以在CSS文件的底部找到它们。

So what is a better/working way to position my text on the bottom of my div but also have a dynamic border on the right ? 那么,什么是将我的文本放在div底部但在右边有动态边框的更好/可行的方法呢?

 .statusbar { padding: 0; margin: 0; height: 150px; width: 50vw; background: #303030; color: white; } .statusbar .statusbar-content { width: calc(100vw - 48px); height: 150px; margin-left: 48px; float: left; } .statusbar .statusbar-content .statusbar-col { margin-top: 20px; margin-bottom: 20px; display: inline-block; float: left; border: none; border-right: 1px solid #505050; position: relative; height: 110px; } .statusbar .statusbar-content #appserver-col, .statusbar .statusbar-content #command-col, .statusbar .statusbar-content #connection-col { margin-left: 23px; } .statusbar .statusbar-content #connection-col { border: none; } .statusbar .statusbar-content .col-content { padding-right: 23px; } .statusbar .statusbar-content .content-row { position: relative; height: 110px; } .statusbar .statusbar-content .content-item { display: inline-block; margin-right: 23px; } 
 <div class="statusbar"> <div class="statusbar-content"> <div class="statusbar-col" id="database-col"> <div class="col-content"> <div class="content-row"> <div class="content-item">Test_Text_1</div> <div class="content-item">Test_Text_2</div> </div> </div> </div> </div> 


EDIT 编辑

The output should look like this in the end. 最后的输出应如下所示。 在此处输入图片说明

This works : 这有效:

HTML HTML

<div class="statusbar">
    <div class="statusbar-content">
        <div class="statusbar-col" id="database-col">
            <div class="col-content">
                <div class="content-row">
                    <div class="content-extender"></div>
                    <div class="content-item">Test_Text_1</div>
                    <div class="content-item">Test_Text_2</div>
                </div>
            </div>
        </div>
    </div>          
</div>

CSS CSS

.col-content {
    padding-right: 23px;
}

.content-row .content-extender {
  display: inline-block;
  height: 110px;
}

.content-item {
    display: inline-block;
    margin-right: 23px;
    vertical-align: bottom;
}

You don't specify an height on your content-row , but you extend vertically this container using an invisible (beacause empty) inline-block div. 您无需在content-row上指定高度,但可以使用不可见(因为为空)的内联块div垂直扩展此容器。 Then you align the inline-block components on bottom. 然后,将内联块组件对准底部。

See here the forked codepen : http://codepen.io/anon/pen/ONLpOw (I added something so you don't have to specify a fixed height on every block : the content-extender height defines the height of the overall statusbar). 看到这里分叉的代码笔: http ://codepen.io/anon/pen/ONLpOw(我添加了一些东西,所以您不必在每个块上指定固定的高度: content-extender高度定义了整个状态栏的高度)。

Is that what you were trying to do ? 那是你想做的吗?

Flexbox can do that: Flexbox可以做到这一点:

.statusbar .statusbar-content .content-row {
  position: relative;
  height: 110px;
  display: flex;
  align-items: flex-end;
}

 .statusbar { padding: 0; margin: 0; height: 150px; width: 50vw; background: #303030; color: white; } .statusbar .statusbar-content { width: calc(100vw - 48px); height: 150px; margin-left: 48px; float: left; } .statusbar .statusbar-content .statusbar-col { margin-top: 20px; margin-bottom: 20px; display: inline-block; float: left; border: none; border-right: 1px solid #505050; position: relative; height: 110px; } .statusbar .statusbar-content #appserver-col, .statusbar .statusbar-content #command-col, .statusbar .statusbar-content #connection-col { margin-left: 23px; } .statusbar .statusbar-content #connection-col { border: none; } .statusbar .statusbar-content .col-content { padding-right: 23px; } .statusbar .statusbar-content .content-row { position: relative; height: 110px; display: flex; align-items: flex-end; } .statusbar .statusbar-content .content-item { display: inline-block; margin-right: 23px; } 
 <div class="statusbar"> <div class="statusbar-content"> <div class="statusbar-col" id="database-col"> <div class="col-content"> <div class="content-row"> <div class="content-item">Test_Text_1</div> <div class="content-item">Test_Text_2</div> </div> </div> </div> </div> 

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

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