我正在使用HTML建立自己的Pill组件:
.panel1 {
width: 100%;
}
.panel2 {
width: 100px;
}
.pill {
display: inline-block;
}
.pill-content {
display: flex;
flex-direction: row;
font-size: 12px;
vertical-align: middle;
padding: 0px;
background-color: white;
border: solid;
border-width: 1px;
border-radius: 4px;
color: black;
border-color: blue;
background-color: white;
}
.pill-text {
flex: 1;
}
.pill-icon {
flex: 1;
max-width: 18px;
padding: 0px 5px 0px 5px;
}
<div class="panel1">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Why is that text breaking as it fits in width?
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
<div class="panel2">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Test
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Very big text that does not fit in width
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div>
</div>
我的问题:
我需要将文本始终放在药丸的左侧。
我需要保持关闭图标(我在这里使用X
,但这将是一个字体真棒图标)始终垂直居中。
我无法理解为什么我的文字会在panel1
上中断。我希望只有当面板尺寸小于text + icon时,文本才会中断。
我可以简化HTML / CSS吗?
答案 0 :(得分:2)
我已对您的代码进行了一些修改,以便为您提供所需的结果:
/* recommended */
.panel1 *,
.panel2 * {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.panel1 {
/*width: 100%; by default*/
}
.panel2 {
width: 100px;
}
.pill {
display: inline-flex; /* modified, since you're using flexbox */
}
.pill-content {
display: flex;
/*flex-direction: row; by default*/
font-size: 12px;
/*vertical-align: middle; has no effect here*/
align-items: center; /* now its vertically centered */
/*padding: 0; already covered*/
border: 1px solid blue; /* shorthand */
border-radius: 4px;
}
.pill-text {
/*flex: 1; not necessary, the culprit for breaking*/
}
.pill-icon {
/*flex: 1; not necessary since you're using max-width*/
max-width: 18px;
padding: 0 5px;
text-align: center; /* for horizontal alignment */
}
&#13;
<div class="panel1">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Why is that text breaking as it fits in width?
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
<div class="panel2">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Test
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Very big text that does not fit in width
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
&#13;
答案 1 :(得分:0)
以下是一些简化提示(因为我无法评论):
border: 1px solid blue
。幸运的是,没有必要单独指定每个属性background-color: white;
为.pill-content
指定了两次,除非我遗漏了一些东西。
醇>