css - 横向多维列表

标签 css multidimensional-array html-lists nested-lists

我有一个多维数组,它在同一层次结构中呈现为 HTML 列表:

<ul>
    <li>
        First A
        <ul>
            <li>
                Second A (Child of First A)
                <ul>
                    <li>
                        Third A (Child of Second A)
                    </li>
                    <li>
                        Third B (Child of Second A)
                    </li>
                    <li>
                        Third B (Child of Second A)
                    </li>
                </ul>
            </li>
            <li>
                Second B (Child of First A)
            </li>
            <li>
                Second C (Child of First A)
            </li>
        </ul>
    </li>
</ul>

我想在不改变层次结构的情况下像下面这样显示这个列表……有什么想法吗?

enter image description here

最佳答案

有两种选择,您可以如何以不同于当前解决方案的方式进行处理。

第一个:没有 JS 但有 float ,因此您需要在 ULLI 构造的末尾添加一个 clear: both
(我不会使用这个,因为它部分不稳定,但我认为我应该发布它)

See the Pen

<style>
div {
  float: left;
}

ul {
  margin-left: 50px;
  padding: 0;
  float: right;
}

div > ul {
  margin-left: 0;
}

span {
  clear: both;
}
</style>

<div id="container">
<ul>
  <li>
      First A
      <ul>
          <li>
              Second A (Child of First A)
              <ul>
                  <li>
                      Third A (Child of Second A)
                  </li>
                  <li>
                      Third B (Child of Second A)
                  </li>
                  <li>
                      Third B (Child of Second A)
                  </li>
              </ul>
          </li>
          <li>
              Second B (Child of First A)
          </li>
          <li>
              Second C (Child of First A)
          </li>
      </ul>
  </li>
</ul>
<span></span>
</div>

或者第二个:
使用提供容器高度的小脚本。

CSS:

div {
  position: relative;
}

ul {
  position: absolute;
  top: 0;
  width: 200px;
  margin: 0 0 0 250px;
  padding: 0;
}

div > ul {
  margin-left: 0;
}

Javascript:

  let absHeight = 0;
  let uls = document.getElementsByTagName('ul');
  Array.from(uls).forEach(ul => {
    absHeight = (absHeight < ul.offsetHeight) ? ul.offsetHeight : absHeight
  });
  document.getElementById('container').style.height = absHeight + 'px';

关于css - 横向多维列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57266151/

相关文章:

python - 如何扩展某些选定行的列大小并添加 2D 数组 python2.7 的附加行

html - 列表的跨浏览器兼容悬挂缩进

javascript - icloud.com上的 checkout 模糊效果是如何实现的?

css - "NetworkError: 404 Not Found"/样式.css

Javascript:将多维数组展平为唯一路径数组

javascript - 从多维数组创建逗号分隔的字符串

javascript - 单击页面加载触发器 (UL > LI)

css - 无法在 nav 的 li 中垂直对齐 ul

javascript - Angular:使用 NgIf 删除组件、内部表单和元素?

css - 命名 Div 和骨架 CSS