简体   繁体   English

Bootstrap 4 单独对齐行或列中的项目

[英]Bootstrap 4 Align Items within a Row or Column Seperately

I'm using Bootstrap 4 to create a row of 2 items within a column where the first item stays all the way to the left in the row and the second item is always centered in the row.我正在使用 Bootstrap 4 在一列中创建一行 2 项,其中第一项一直保持在行的左侧,第二项始终位于行的中央。 I am able to do this with my own CSS. Is there a way to do this only using Bootstrap with no extra CSS?我可以用我自己的 CSS 来做到这一点。有没有办法只使用 Bootstrap 而没有额外的 CSS?

Here is my CSS:这是我的 CSS:

.float_left {
    float:left;
}

.float_center {
        float: right;
        position: relative;
        left: -50%;
        text-align: left;
}

.float_center > .child {
    position: relative;
    left: 50%;
}

My HTML:我的HTML:

<div class="row" >
    <div class="col-12" style="z-index:5000;">       

        <span class="float_left">Left</span>

        <div class="float_center">
            <span class="child">Center</span>                        
        </div>
    </div>
</div>
  1. display:flex; will make the data to sit in single row.将使数据位于单行中。
  2. When you assign width of equal size ie 50% and move the text to left, that will sit in the center without extra code.当您分配相同大小的width (即50% )并将文本向左移动时,它将位于中间而无需额外代码。
  3. Flex is good for layout design, so don't worry about responsiveness it will take care of that as well. Flex 非常适合布局设计,所以不用担心响应性,它也会处理好这一点。

 .align { display: flex; border: 1px solid red; width: 100%; }.float_left, .float_center { width: 50%; text-align: left; }
 <div class="row"> <div class="col-12 align" style="z-index:5000;"> <span class="float_left">Left</span> <div class="float_center"> <span class="child">Center</span> </div> </div> </div>

why don't you use col?你为什么不使用col? I think this will work.我认为这会奏效。

<div class="row" >
        <div class="col-12" style="z-index:5000;">       

            <div class="row">
                <span class="col">Left</span>

                <div class="col">
                    <span class="child">Center</span>                        
                </div>
            </div>
        </div.
    </div>

if you want fixed width for left side custom width如果您想要左侧自定义宽度的固定宽度

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

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