问题
@michael-benjamin Center and right align flexbox elements almost what I want, but with 2 differences:
- Div have to take empty space at left side too (when it be wide)
- Div have to collapse text when there is no empty space around at all.
What I have?
A
is main centered object and BBBBBBBBBBBBBBB
is right object.
This is fine till A
will grow:
What I want?
Is it possible to do with flex/grid/table-tr-td/any other css tricks?
What I tried?
- CSS Positioning Properties - can't stop when grow to
B
- Flex Auto Margins & Invisible Flex Item (DOM element) - invisible item don't give empty space at left side
- CSS Grid Layout -
1fr
don't give empty space at left side
回答1:
CSS grid can do it like below:
.container {
margin: 10px;
display: grid;
grid-template-columns: 1fr minmax(0,max-content) 1fr;
color: #fff;
}
.container::before {
content:"";
}
.b {
margin-left: auto;
background: grey;
}
.a {
margin: auto;
background: blue;
overflow:hidden;
text-overflow:ellipsis;
max-width:100%;
}
body {
background:linear-gradient(red,red) center/ 2px 100% no-repeat;
}
<div class="container">
<div class="a">AA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AAAAAAAAAAAAAA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
来源:https://stackoverflow.com/questions/62471264/center-and-right-align-flexbox-elements-with-text-oveflow-and-taking-empty-space