0

If the text reaches the absolutely positioned span element, it should flow around it. If the text within the div element does not fill an entire line, the entire line should not be colored when hovering over it, but only the text, hence display: inline-block. So how can I achieve this?

.wrapper {
  position: relative;
  width: 500px;
}
ul {
  list-style: none;
}
ul > li > span {
  position: absolute;
  right: 0;
  font-size: 3em;
  line-height: 0.75;
  cursor: pointer;
}
ul > li > div {
  padding: 10px;
  font-size: 0.75em;
}
ul > li > div:hover {
  display: inline-block;
}
ul > li:nth-child(1) > div:hover {
  background: red;
}
ul > li:nth-child(2) > div:hover {
  background: yellow;
}
ul > li:nth-child(3) > div:hover {
  background: green;
}
<div class="wrapper">
  <ul>
    <li>
      <span>×</span>
      <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</div>
    </li>
    <li>
      <span>×</span>
      <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</div>
    </li>
    <li>
      <span>×</span>
      <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</div>
    </li>
  </ul>
</div>

3
  • @Paulie_D Yes, that is exactly the problem. But how can I get around it and achieve the desired effect? Commented May 24, 2024 at 15:40
  • @Paulie_D Thank you, but I need the text to be colored as a block. Commented May 24, 2024 at 16:17
  • @Paulie_D Yes, that is the requirement. Commented May 24, 2024 at 17:25

1 Answer 1

2

Use float combined with width: fit-content like below:

.wrapper {
  width: 500px;
  text-align: justify; /* to illustrate the wrapping */
}
ul {
  list-style: none;
}
ul > li {
  margin: 10px 0;
}

ul > li > span {
  float: right; /* use float */
  margin-left: 3px;
  font-size: 3em;
  line-height: 0.6;
  cursor: pointer;
}
ul > li > div {
  padding: 5px;
  font-size: 0.75em;
  width: fit-content; /* equivalent to your inline-block */
}
ul > li:nth-child(1) > div:hover {
  background: red;
}
ul > li:nth-child(2) > div:hover {
  background: yellow;
}
ul > li:nth-child(3) > div:hover {
  background: green;
}
<div class="wrapper">
  <ul>
    <li>
      <span>×</span>
      <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.partu rient m ontes, nasce tur ri diculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</div>
    </li>
    <li>
      <span>×</span>
      <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</div>
    </li>
    <li>
      <span>×</span>
      <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</div>
    </li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.