如何对齐Pill项目并通常改善我的代码?

时间:2018-01-19 21:22:09

标签: html css

我正在使用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. 我需要将文本始终放在药丸的左侧。

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

  3. 我无法理解为什么我的文字会在panel1上中断。我希望只有当面板尺寸小于text + icon时,文本才会中断。

  4. 我可以简化HTML / CSS吗?

    JSFiddle here

2 个答案:

答案 0 :(得分:2)

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

&#13;
&#13;
/* 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;
&#13;
&#13;

答案 1 :(得分:0)

以下是一些简化提示(因为我无法评论):

  1. 对于0的所有值,您可以删除单位(px等)
  2. 您可以使用边框简写来缩短代码:border: 1px solid blue。幸运的是,没有必要单独指定每个属性
  3. {li> background-color: white;.pill-content指定了两次,除非我遗漏了一些东西。