文本在块元素中的位置

Position of text within the block element

我需要确保如果文本行太长,它不会超过价格,而是在接近价格块时立即转到第二行。我希望这是有道理的。请参考行'Green tea with a tiny piece of lemon'。 'of'这个词不应该超过这条线,它应该移到第二行。

有什么解决办法吗?

.lst {
  font-family: montserrat;
  width: 300px;
  list-style-type: none;
  position: relative;
}

ul.lst li {
  position: relative;
}

ul.lst li a {
  text-decoration: none;
  color: #252525;
  display: block;
  padding: 5px;
  position: relative;
  border-bottom: 2px solid #f3f2e8;
  z-index: 2;
}

ul.lst li em {
  font-style: normal;
  float: right;
  width: 25%;
  color: orange;
}

ul.lst li span {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  height: 100%;
  text-indent: -9999px;
  background: #BFD3EE;
  opacity: 0.5;
}

ul.lst li:hover span {
  background: #658BBB;
}

ul.lst li:hover a{
  color: #61304B;
}

ul.lst li:hover em{
  color: #61304B;
}
<ul class="lst">
  <li><a href="#">Americano<em>.24</em></a><span style="width:20%">20%</span></li>
  <li><a href="#">Green tea with a tiny piece of lemon<em>.50</em></a><span style="width: 50%">50%</span></li>
  <li><a href="#">Black Tea<em></em></a><span style="width: 75%">75%</span></li>
  <li><a href="#">Black coffee<em></em></a><span style="width: 90%">90%</span></li>
  <li><a href="#">Latte<em></em></a><span style="width: 50%">100%</span></li>
</ul>

您可以在 a 上设置 display: flex,这应该可以解决问题。

.lst {
  font-family: montserrat;
  width: 300px;
  list-style-type: none;
  position: relative;
}
ul.lst li {
  position: relative;
}
ul.lst li a {
  text-decoration: none;
  color: #252525;
  display: flex;
  align-items: center;
  padding: 5px;
  position: relative;
  border-bottom: 2px solid #f3f2e8;
  z-index: 2;
}
ul.lst li em {
  font-style: normal;
  margin-left: auto;
  width: 25%;
  color: orange;
}
ul.lst li span {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  height: 100%;
  text-indent: -9999px;
  background: #BFD3EE;
  opacity: 0.5;
}
ul.lst li:hover span {
  background: #658BBB;
}
ul.lst li:hover a {
  color: #61304B;
}
ul.lst li:hover em {
  color: #61304B;
}
<ul class="lst">
  <li><a href="#">Americano<em>.24</em></a><span style="width:20%">20%</span></li>
  <li><a href="#">Green tea with a tiny piece of lemon<em>.50</em></a><span style="width: 50%">50%</span></li>
  <li><a href="#">Black Tea<em></em></a><span style="width: 75%">75%</span></li>
  <li><a href="#">Black coffee<em></em></a><span style="width: 90%">90%</span></li>
  <li><a href="#">Latte<em></em></a><span style="width: 50%">100%</span></li>
</ul>

具有绝对位置和 link 文本的一些填充的价格怎么样?像这样:

ul.lst li a {
    text-decoration: none;
    color: #252525;
    display: block;
    padding: 5px;
    position: relative;
    border-bottom: 2px solid #f3f2e8;
    z-index: 2;
    padding-right: 60px;
}

ul.lst li em {
    font-style: normal;
    float: right;
    color: orange;
    position: absolute;
    right: 0;
    width: auto;
}

稍微更改一下 HTML 的结构,将 <a> 更改为 flexbox,即从 display: block 更改为 display: flex

看看下面的代码片段:

.lst {
  font-family: montserrat;
  width: 300px;
  list-style-type: none;
  position: relative;
}

ul.lst li {
  position: relative;
}

ul.lst li a {
  text-decoration: none;
  color: #252525;
  display: flex;
  padding: 5px;
  position: relative;
  border-bottom: 2px solid #f3f2e8;
  z-index: 2;
}

ul.lst li a .text {
  flex: 1;
}

ul.lst li em {
  font-style: normal;
  float: right;
  width: 25%;
  color: orange;
}

ul.lst li span {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  height: 100%;
  text-indent: -9999px;
  background: #BFD3EE;
  opacity: 0.5;
}

ul.lst li:hover span {
  background: #658BBB;
}

ul.lst li:hover a{
  color: #61304B;
}

ul.lst li:hover em{
  color: #61304B;
}
<ul class="lst">
  <li>
    <a href="#">
      <div class="text">Americano</div>
      <em>.24</em>
    </a>
    <span style="width:20%">20%</span>
  </li>
  <li>
    <a href="#">
      <div class="text">Green tea with a tiny piece of lemon</div>
      <em>.50</em>
    </a>
    <span style="width: 50%">50%</span></li>
  <li>
    <a href="#">
      <div class="text">Black Tea</div>
      <em></em>
    </a>
    <span style="width: 75%">75%</span>
  </li>
  <li>
    <a href="#">
      <div class="text">Black coffee</div>
      <em></em>
    </a>
    <span style="width: 90%">90%</span>
  </li>
  <li>
    <a href="#">
      <div class="text">Latte</div>
      <em></em>
    </a>
    <span style="width: 50%">100%</span>
  </li>
</ul>

希望对您有所帮助!