【问题标题】:Draw dynamic lines using html and css使用 html 和 css 绘制动态线条
【发布时间】:2019-01-21 01:43:05
【问题描述】:

是否可以使用html和css绘制以下内容?

我只对线条部分感兴趣。 Here 是我迄今为止所尝试的。

html:

<div class="outer">
  <div class="inner">
    <div class="topPart">
      TOP
    </div>
    <div class="middlePart">
      <div class="program selectedProgram">
        <div class="programName">
          foo program
        </div>

        <div class="modulesDiv">
          <div class="module">
           <div class="moduleName">
             foo module foo module foo module foo module foo module
           </div>
          </div>
          <div class="module">
           <div class="moduleName">
             foo short name
           </div>
          </div>
          <div class="module">
           <div class="moduleName">
             foo module foo module foo module foo module foo module
           </div>
          </div>
        </div>
      </div>
    </div>
    <div class="bottomPart">
      BOTTOM
    </div>
  </div>
</div>

css:

.outer {
  background-color: red;
  position: relative;
  margin: 0 auto;
  top: 40px;
  width: 200px;
  height: 2000px;
  background-image: url('https://mdn.mozillademos.org/files/7693/catfront.png');
  background-repeat: no-repeat;
  background-size: cover;
}

.inner {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background-color: cyan;
  width: 200px;
  height: 400px/* 100vh */
  ;
}

.topPart {
  position: absolute;
  height: 10%;
  width: 100%;
  top: 0;
  background-color: pink;
  text-align: center;
}

.middlePart {
  position: absolute;
  height: 80%;
  width: 100%;
  top: 10%;
  background-color: yellow;
  text-align: center;
}

.bottomPart {
  position: absolute;
  height: 10%;
  width: 100%;
  top: 90%;
  background-color: #66aa11;
  text-align: center;
}

.program {
  height: auto;
  text-align: left;
  width: 100%;
  cursor: pointer;
  padding-top: 5%;
  padding-bottom: 5%;
  padding-left: 5%;
  padding-right: 5%;
  word-wrap: break-word;
  border-bottom: 1px solid #afadad;
  position: relative;
}

.program.selectedProgram {
  border-left: 4px solid #289efd;
}

.program .programName {
  display: inline;
  font-size: 110%;
  color: #484646;
  padding-left: 5%;
  vertical-align: middle;
}

.selectedProgram.program .programName {
  font-weight: bold;
  color: #289efd;
}

.program:hover .programName {
  color: #289efd;
}

.program .programName {
  white-space: pre-wrap;
  white-space: -moz-pre-wrap !important;
  white-space: -webkit-pre-wrap;
  white-space: -pre-wrap;
  white-space: -o-pre-wrap;
  white-space: pre-wrap;
  word-wrap: break-word;
  word-break: break-all;
  white-space: normal;
}

.modulesDiv {
  height: auto;
  width: 100%;
  position: relative;
  padding-left: 5%;
  padding-top: 5%;
  display: none;
}

.selectedProgram .modulesDiv {
  display: block;
}

.module {
  height: auto;
  text-align: left;
  width: 100%;
  cursor: pointer;
  padding-top: 5%;
  padding-bottom: 5%;
  padding-left: 5%;
  padding-right: 5%;
  word-wrap: break-word;
  position: relative;
}

.program .moduleName {
  display: inline;
  font-size: 110%;
  color: #484646;
  padding-left: 5%;
  vertical-align: middle;
}

.selectedModule.module .moduleName {
  font-weight: bold;
  color: #289efd;
}

.module:hover .moduleName {
  color: #289efd;
}

我在这里面临两个问题。

  1. 我不知道如何仅使用htmlcss 绘制一条随机定位的线。
  2. 我无法事先猜测每个模块将占用多少高度,因此无法在代码中推断出何时必须在模块名称上画一个水平叉。

我将屏幕截图中粗体的主名称称为程序名称,将左侧缩进的名称称为模块名称。

我希望有人能够在这里至少提供帮助。

【问题讨论】:

    标签: html css dynamic line draw


    【解决方案1】:

    我不打算实现细节,但您可以使用组织良好的结构和background 大小来创建您正在寻找的效果。我正在使用单像素 SVG 背景图像,您可以使用 background-size 拉伸它以生成您想要的外观。它也不会牺牲实际的语义:

    ul {
      padding: 0;
      margin: 0;
      list-style: none;
      padding-left: 5px;
      background: url( "data:image/svg+xml;charset=utf8,<svg width='100%' height='100%' viewBox='0 0 1 1' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xml:space='preserve' preserveAspectRatio='none'><rect x='0' y='0' width='1' height='1' fill='gray' stroke='none' /></svg>" );
      background-size: 1px calc(100% - .5em);
      background-position: top 0 left 5px;
      background-repeat: no-repeat;
    }
    
    li {
      padding: 0;
      padding-left: 10px;
      background: url( "data:image/svg+xml;charset=utf8,<svg width='100%' height='100%' viewBox='0 0 1 1' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xml:space='preserve' preserveAspectRatio='none'><rect x='0' y='0' width='1' height='1' fill='gray' stroke='none' /></svg>" );
      background-size: 8px 1px;
      background-position: top .5em left 0;
      background-repeat: no-repeat;
    }
    <h1>List</h1>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Lipsum Dolor</li>
      <li>
        Sit Amet:
        <ul>
          <li>Dolor</li>
          <li>Lorem</li>
          <li>Ipsum</li>
          <li>Datia</li>
        </ul>
      </li>
      <li>Other</li>
    </ul>

    为了使最后一项不会像疯了一样伸展,我们将不得不使用乘法background 定义,因此这不能与某些旧浏览器兼容,但无论如何它都可以工作:

    ul, h1 {
      padding: 0;
      margin: 0;
      list-style: none;
    }
    
    h1 {
      font-size: 1rem;
      color: gray;
      border-bottom: 1px solid gray;
    }
    
    
    li {
      padding: 0;
      padding-left: 13px;
      background: url( "data:image/svg+xml;charset=utf8,<svg width='100%' height='100%' viewBox='0 0 1 1' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xml:space='preserve' preserveAspectRatio='none'><rect x='0' y='0' width='1' height='1' fill='gray' stroke='none' /></svg>" ), url( "data:image/svg+xml;charset=utf8,<svg width='100%' height='100%' viewBox='0 0 1 1' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xml:space='preserve' preserveAspectRatio='none'><rect x='0' y='0' width='1' height='1' fill='gray' stroke='none' /></svg>" );
      background-size: 8px 1px, 1px 100%;
      background-position: top .5em left 5px, top 0 left 5px;
      background-repeat: no-repeat, no-repeat;
    }
    
    li:last-of-type {
      background-size: 8px 1px, 1px .5em;
    }
    <h1>List</h1>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Lipsum Dolor</li>
      <li>
        Sit Amet:
        <ul>
          <li>Dolor</li>
          <li>Lorem</li>
          <li>Ipsum</li>
          <li>Datia</li>
        </ul>
      </li>
    </ul>
    
    <h1>'Nother List</h1>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Lipsum Dolor</li>
      <li>
        Sit Amet:
        <ul>
          <li>Dolor</li>
          <li>Lorem</li>
          <li>Ipsum</li>
          <li>Datia</li>
        </ul>
      </li>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Lipsum Dolor<br />Sit Amet Nanaba</li>
    </ul>

    【讨论】:

    • 如果最后一个li 占用多行,你怎么解决这个问题?例如,here
    • @iloveseven 是的,但事情会变得稍微复杂一些。在这种情况下,您的所有填充都需要转到
    • ,并且您需要两个背景图像。让我试着给你举个小例子。
  • @iloveseven 我添加了一点 sn-p 方法略有不同 - 我认为这个更好,但它需要支持多重背景(目前大多数浏览器都有!)。然后你可以专注于最后一个孩子(或类型)并在那里结束列表。你可以用这种方式解决很多这样的边缘情况。
  • 您绘制的线条是使用 svg 的 rect 绘制的。我尝试调整viewBoxwidthheight 属性,添加stroke-widthstroke="black" 属性,但它没有改变线条的宽度。是否有可能以某种方式改变线条的宽度?
  • 哦。 background-size 的把戏太棒了。万分感谢。大声笑
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签