gpt4 book ai didi

html - 如何对齐 Pill 元素并总体上改进我的代码?

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:35 25 4
gpt4 key购买 nike

我正在使用 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>

我的问题:

  1. 我需要将文本始终放在 Pill 的左侧。

  2. 我需要保持关闭图标(我在这里使用 X 但那将是一个很棒的字体图标)始终垂直居中。

  3. 我不明白为什么我的文本在 panel1 上中断。我预计只有当面板尺寸小于文本 + 图标时文本才会中断。

我可以让 HTML/CSS 更简单吗?

JSFiddle here

最佳答案

我已经对您的代码进行了一些修改,以便为您提供所需的结果:

/* 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 */
}
<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>

关于html - 如何对齐 Pill 元素并总体上改进我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48349733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com