您可以通过两种方式使用display property。对于旧浏览器 display: table; 和对于新浏览器 display: flex;。下面是新旧浏览器的例子。
对于旧浏览器:
<div class="item2">
<div class="name">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
<div class="labels">label</div>
<div class="value">value value value</div>
.item2 {
background-color: #cccccc;
display: table;
margin-left: auto;
margin-right: auto;
padding: 5px;
width: 500px;
}
.item2 div {
display: table-cell;
padding: 10px;
vertical-align: top;
}
.item2 div.name {
background-color: #ff0;
color: #000;
}
.item2 div.labels {
background-color: #ff00ff;
}
.item2 div.value {
background-color: #ff0000;
}
对于新浏览器:
<div class="item">
<div class="name">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
<div class="labels">label</div>
<div class="value">value value value</div>
</div>
.item {
-webkit-box-align: stretch;
-webkit-align-items: stretch;
-moz-box-align: stretch;
-ms-flex-align: stretch;
align-items: stretch;
background-color: #000;
color: #ffffff;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-flex: 0;
-webkit-flex: 0 0 500px;
-moz-box-flex: 0;
-ms-flex: 0 0 500px;
flex: 0 0 500px;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-webkit-flex-direction: row;
-moz-box-orient: horizontal;
-moz-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-box-pack: start;
-webkit-justify-content: flex-start;
-moz-box-pack: start;
-ms-flex-pack: start;
justify-content: flex-start;
margin-left: auto;
margin-right: auto;
max-width: 500px;
padding: 5px;
width: 500px;
}
.item div {
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-flex: 1;
-webkit-flex-grow: 1;
-moz-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
padding: 10px;
}
.item div.name {
background-color: #ff0;
color: #000;
}
.item div.labels {
background-color: #ff00ff;
}
.item div.value {
background-color: #ff0000;
}
*{
box-sizing: border-box;
}
*:after,
*:before{
box-sizing: border-box;
}
.item {
-webkit-box-align: stretch;
-webkit-align-items: stretch;
-moz-box-align: stretch;
-ms-flex-align: stretch;
align-items: stretch;
background-color: #000;
color: #ffffff;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-flex: 0;
-webkit-flex: 0 0 500px;
-moz-box-flex: 0;
-ms-flex: 0 0 500px;
flex: 0 0 500px;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-webkit-flex-direction: row;
-moz-box-orient: horizontal;
-moz-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-box-pack: start;
-webkit-justify-content: flex-start;
-moz-box-pack: start;
-ms-flex-pack: start;
justify-content: flex-start;
margin-left: auto;
margin-right: auto;
max-width: 500px;
padding: 5px;
width: 500px;
}
.item div {
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-flex: 1;
-webkit-flex-grow: 1;
-moz-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
padding: 10px;
}
.item div.name {
background-color: #ff0;
color: #000;
}
.item div.labels {
background-color: #ff00ff;
flex: 0 0 100px;
max-width: 100px;
}
.item div.value {
background-color: #ff0000;
flex: 0 0 100px;
max-width: 100px;
}
.item2 {
background-color: #cccccc;
display: table;
margin-left: auto;
margin-right: auto;
padding: 5px;
width: 500px;
}
.item2 div {
display: table-cell;
padding: 10px;
vertical-align: top;
}
.item2 div.name {
background-color: #ff0;
color: #000;
}
.item2 div.labels {
background-color: #ff00ff;
font-size: 24px;
width: 100px;
}
.item2 div.value {
background-color: #ff0000;
width: 100px;
}
<div class="item">
<div class="name">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
<div class="labels"><h1>For New browser</h1></div>
<div class="value">value value value</div>
</div>
<div class="item2">
<div class="name">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
<div class="labels"><h1>For Old browser</h1></div>
<div class="value">value value value</div>
</div>
注意:如果您使用flexbox,那么您将不会在最后两列width 中发现任何问题。 display: table-cell; 将占用宽度直到一个单词不完整。
已编辑
我看到了你的小提琴。我明白你实际上想要什么。 flexbox 对这个问题没有帮助。您需要更改 html 结构。你可以试试这个。
HTML
<div class="item">
<div class="child-item1">
<div class="name">Lorem Ipsum is simply</div>
<div class="value">value value value</div>
</div>
<div class="child-item2">
label
</div>
</div>
CSS
.item {
background-color: #ff00ff;
display: table;
margin-left: auto;
margin-right: auto;
padding: 5px;
width: 500px;
}
.item .child-item1,
.item .child-item2 {
display: table-cell;
vertical-align: middle;
}
.item .child-item1 {
width: -webkit-calc(100% - 200px);
width: -moz-calc(100% - 200px);
width: calc(100% - 200px);
}
.item .child-item1 .name,
.item .child-item1 .value {
border: 2px solid #eaeaea;
display: block;
min-height: 100px;
max-width: 100%;
width: 100%;
}
.item .child-item1 .name {
background-color: #ff0685;
}
.item .child-item1 .value {
background-color: yellowgreen;
}
.item .child-item2 {
border: 2px solid #eaeaea;
width: 200px;
vertical-align: middle;
text-align: center;
}
*{
box-sizing: border-box;
}
.item {
background-color: #ff00ff;
display: table;
margin-left: auto;
margin-right: auto;
padding: 5px;
width: 500px;
}
.item .child-item1,
.item .child-item2 {
display: table-cell;
vertical-align: middle;
}
.item .child-item1 {
width: -webkit-calc(100% - 200px);
width: -moz-calc(100% - 200px);
width: calc(100% - 200px);
}
.item .child-item1 .name,
.item .child-item1 .value {
border: 2px solid #eaeaea;
display: block;
min-height: 100px;
max-width: 100%;
width: 100%;
}
.item .child-item1 .name {
background-color: #ff0685;
}
.item .child-item1 .value {
background-color: yellowgreen;
}
.item .child-item2 {
border: 2px solid #eaeaea;
width: 200px;
vertical-align: middle;
text-align: center;
}
<div class="item">
<div class="child-item1">
<div class="name">Lorem Ipsum is simply</div>
<div class="value">value value value</div>
</div>
<div class="child-item2">
label
</div>
</div>