简体   繁体   English

左对齐和居中对齐 div 中的项目?

[英]Left and center align items in a div?

How can I layout the contents of a div with one element centred and the other aligned left.如何布局 div 的内容,其中一个元素居中,另一个元素左对齐。

I've tried flexbox - not possible without adding a hidden 3rd element and justifying content.我试过 flexbox - 如果不添加隐藏的第三个元素和证明内容是不可能的。

I've also tried text align on parent but then everything is aligned in one way (centered or left).我也试过在父级上对齐文本,但随后所有内容都以一种方式对齐(居中或左对齐)。

HTML: HTML:

 .left{ } .center{ }
 <div class="container"> <div class="left"> I'm left aligned </div> <div class="center"> I'm center aligned </div> </div>

Edit:编辑:

Im aligning the divs themselves, and centered relative to parent.我对齐 div 本身,并相对于父级居中。

you can use flex and :after element for the hidden element:您可以使用 flex 和 :after 元素作为隐藏元素:

 .container { display: flex; border: 1px solid #000; } .container:after { content: " "; display: block; flex: 1; } .left, .center { border: 1px solid #0F0; flex: 1; text-align: center; }
 <div class="container"> <div class="left"> I'm left aligned </div> <div class="center"> I'm center aligned </div> </div>

RESULT结果在此处输入图片说明

How can I layout the contents of a div with one element centred and the other aligned left.如何布局 div 的内容,其中一个元素居中,另一个元素左对齐。

You can use % based widths and just divide it up like so您可以使用基于 % 的宽度,然后像这样将其分开

 .container { border: 1px solid; width: 500px; height: 200px; //text-align: center; } .left { float: left; width: 33.33% } .right { float:left; width:33.33%; text-align:right; } .center { float: left; width: 33.33%; text-align:center; }
 <div class="container"> <div class="left"> I'm left aligned </div> <div class="center"> I'm center aligned </div> <div class="right"> An examplatory div... </div> </div>

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

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