我有两个divs包含width: 50%在容器中的文本内容。问题是当文本内容改变时,宽度也会改变:
我的容器的固定宽度等于200px:
.container {
display: block;
width: 200px;
}
.text1 {
background-color: red;
width: 50%;
display: inline;
}
.text2 {
background-color: blue;
width: 50%;
display: inline;
}Run Code Online (Sandbox Code Playgroud)
<div class='container'>
<div class='text1'>Text1</div>
<div class='text2'>Text2</div>
</div>Run Code Online (Sandbox Code Playgroud)
div即使文本发生变化,如何保持子宽度的 50% ?链接到JSFiddle:
小智 5
您可以在这里使用CSS Flexbox:
.container {
display: flex;
width: 200px;
}
.text1 {
background-color: red;
width: 50%;
}
.text2 {
background-color: blue;
width: 50%;
}Run Code Online (Sandbox Code Playgroud)
<div class='container'>
<div class='text1'>Text1</div>
<div class='text2'>Text2</div>
</div>Run Code Online (Sandbox Code Playgroud)