0

I have a List, and I want to set a static maximum height on each item of the list and show a scrollbar when the text overflows it: I have the following code:

HTML

<ul>
    @foreach(var item in List)
    {
        <li>
            <a />
        </li>
    }
</ul>

CSS (I have more styles, but for the sake of exemplifying, I posted only the relevant ones)

ul, li
{
    overflow-y:auto;
}

ul li a
{
    background: #ffc;
    display: block;
    height: 08em;
}

PROBLEM

The background color will only apply for the defined height. So how can I resolve it? I assume by envolving <li> on another tag and applying the styles to that tag instead, but I'm not sure how to apply that

Edit I would like to point to the answer of Michael Benjamin on Make background color extend into overflow area

Henrique Pombo
  • 537
  • 1
  • 6
  • 20

2 Answers2

1

That's because you applied the background-color to your <a> element, not your <li>

ul li {
max-height : 100px;
overflow-y : auto;
background-color : yellow;
}

ul li:nth-child(3) {
background-color : red; /* for example purpose only */
}

ul li a {
     writing-mode: vertical-rl; /* for example purpose only */
     text-orientation: mixed; /* for example purpose only */
}
<ul>
  <li> Item </li>
  <li> Item </li>
  <li> <a> Very long item that willd force a vertical scroll </a> </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
  <li> Item </li>
</ul>
Xanthous
  • 442
  • 3
  • 12
  • What I ended up doing was applying the overflow-y to `` (applying the styles to the `
  • ` doesn´t work that well in my current program), but this pointed me in the right direction. – Henrique Pombo Jul 16 '21 at 11:19