caramba
caramba

Reputation: 22480

ul with display block not taking up entire space of parent element

I have a <ul><li> tree where it's possible to toggle the visibility of the children. The :hover should show the entire width of the element. To illustrate the problem those elements have a orange background-color.

Now you will see the the most outer <ul class="tree"> will not take up the entire space from the parent.

The .tree has some wraps cause in real life the .tree is inside a resizable area. This is also the reason to use overflow-x: scroll; and the reason for

padding-left: 2000px;
margin-left: -2000px;

is again to get the entire element with a background-color decorated.

Also I need to use white-space: nowrap; cause every <li> should only take up one line and take as much space in width as it needs.

.grid {
  width: 200px;
}

.box {
    width: 100%;
    height: 100%;
    overflow-x: scroll;
    border: solid 1px fuchsia;
}

ul.tree {
    display: block;
    width: 100%; /* makes nothing */
    border: solid 1px lime;
}

ul.tree,
ul.tree ul,
ul.tree li {
    margin: 0;
    padding: 0;
    list-style: none;
}

ul.tree ul {
    margin: 0 0 0 20px;
}

ul.tree a {
    white-space: nowrap;
    display: block;
    padding: 5px;
    padding-left: 2000px;
    margin-left: -2000px;
    margin-bottom: 5px;
    color: #000;
    background-color: orange;
}
<div class="grid">
  <div>
    <div class="box">
      <ul class="tree">
        <li>
          <a href="#">xxx</a>
          <ul>
            <li>
              <a href="#">xxx xxx</a>
              <ul>
                <li>
                  <a href="#">xxx xxx xxx</a>
                  <ul>
                    <li>
                      <a href="#">xxx xxx xxx</a>
                      <ul>
                        <li>
                          <a href="#">xxx xxx xxx xxx 123456 7890</a>
                        </li>
                      </ul>
                    </li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</div>

So what I need is the orange color to go all the way to the fuchsia border and not stop where the lime border stops. How can I achieve this?

Upvotes: 0

Views: 91

Answers (2)

Karthik
Karthik

Reputation: 621

Just use inline-block and min-width 100% on ul.tree (it will work in IE as well)

.grid {
  width: 200px;
}

.box {
  width: 100%;
  height: 100%;
  overflow-x: scroll;
  border: solid 1px fuchsia;
}

ul.tree {
  display: inline-block;
  min-width: 100%;
  border: solid 1px lime;
}

ul.tree,
ul.tree ul,
ul.tree li {
  margin: 0;
  padding: 0;
  list-style: none;
}

ul.tree ul {
  margin: 0 0 0 20px;
}

ul.tree a {
  white-space: nowrap;
  display: block;
  padding: 5px;
  padding-left: 2000px;
  margin-left: -2000px;
  margin-bottom: 5px;
  color: #000;
  background-color: orange;
}
<div class="grid">
  <div>
    <div class="box">
      <ul class="tree">
        <li>
          <a href="#">xxx</a>
          <ul>
            <li>
              <a href="#">xxx xxx</a>
              <ul>
                <li>
                  <a href="#">xxx xxx xxx</a>
                  <ul>
                    <li>
                      <a href="#">xxx xxx xxx</a>
                      <ul>
                        <li>
                          <a href="#">xxx xxx xxx xxx 123456 7890</a>
                        </li>
                      </ul>
                    </li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</div>

Upvotes: 2

Vilsol
Vilsol

Reputation: 722

You can fix this by simply making the tree to fit the content width

.grid {
  width: 200px;
}

.box {
    width: 100%;
    height: 100%;
    overflow-x: scroll;
    border: solid 1px fuchsia;
}

ul.tree {
    display: block;
    width: fit-content;
    border: solid 1px lime;
}

ul.tree,
ul.tree ul,
ul.tree li {
    margin: 0;
    padding: 0;
    list-style: none;
}

ul.tree ul {
    margin: 0 0 0 20px;
}

ul.tree a {
    white-space: nowrap;
    display: block;
    padding: 5px;
    padding-left: 2000px;
    margin-left: -2000px;
    margin-bottom: 5px;
    color: #000;
    background-color: orange;
}
<div class="grid">
  <div>
    <div class="box">
      <ul class="tree">
        <li>
          <a href="#">xxx</a>
          <ul>
            <li>
              <a href="#">xxx xxx</a>
              <ul>
                <li>
                  <a href="#">xxx xxx xxx</a>
                  <ul>
                    <li>
                      <a href="#">xxx xxx xxx</a>
                      <ul>
                        <li>
                          <a href="#">xxx xxx xxx xxx 123456 7890</a>
                        </li>
                      </ul>
                    </li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions