我正在以Angular 创建这个嵌套的树视图组件.所有的样式都工作得很好,但是对于嵌套的树第一个元素,在文本之前添加了额外的空格,这是我无法理解的.这是我正在做的项目的堆叠闪电战

enter image description here

tree.component.css

.open-btn {
  float: left;
  transform: rotate(90deg);
  position: relative;
  left: -30px;
  z-index: 10;
}

.close-btn {
  float: left;
  left: -30px;
  position: relative;
  z-index: 10;
}

.tree li {
  list-style-type: none;
  margin: 10px;
  position: relative;
}

.arrow-btn {
  width: 18px;
  height: 18px;
}

.tree li::before {
  content: '';
  position: absolute;
  top: -7px;
  left: -20px;
  border-left: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  border-radius: 0 0 0 0px;
  width: 20px;
  height: 15px;
}

.tree li::after {
  position: absolute;
  content: '';
  top: 8px;
  left: -20px;
  border-left: 1px solid #ccc;
  border-top: 1px solid #ccc;
  border-radius: 0px 0 0 0;
  width: 20px;
  height: 100%;
}

.tree li:last-child::after {
  display: none;
}

.tree li:last-child:before {
  border-radius: 0 0 0 5px;
}

ul.tree > li:first-child::before {
  border-left: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
}

.label-container {
  display: inline-block;
  /* background-color: rgb(123, 212, 239); */
}

tree.component.ts

import { Component, EventEmitter, Input, Output } from '@angular/core';
import { TreeNode } from './tree-mock';
@Component({
  selector: 'app-tree',
  templateUrl: './tree.component.html',
  styleUrls: ['./tree.component.css'],
})
export class TreeComponent {
  @Input() tree: any;
  @Output() selectedValue = new EventEmitter<any>();

  toggleChild(node) {
    this.selectedValue.emit(node);
    node.showChildren = !node.showChildren;
    node.isOpen = !node.isOpen;
  }

  /* Events are not bubbled up so emitting the parent event on <app-tree>
   * when one of the child emits an event - this will create a new EventEmitter per child.
   */
  emitOnChildClicked(node) {
    this.selectedValue.emit(node);
  }
}

tree.component.html

<ul *ngIf="tree" class="tree">
  <li *ngFor="let node of tree">
    <div class="label-container" (click)="toggleChild(node)">
      <span *ngIf="node.children != 0">
        <img
          src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Icons8_flat_document.svg/512px-Icons8_flat_document.svg.png"
          class="arrow-btn"
          [ngClass]="
            !node.children ? 'doc' : node.isOpen ? 'open-btn' : 'close-btn'
          "
        />
      </span>
      <span> {{ node.name }} </span>
    </div>
    <app-tree
      *ngIf="node.showChildren"
      (selectedValue)="emitOnChildClicked($event)"
      [tree]="node.children"
    ></app-tree>
  </li>
</ul>

tree-mock.ts

export interface TreeNode {
  name: string;
  showChildren: boolean;
  children: any[];
}

export const NODES: TreeNode[] = [
    {
        name: 'Africa',
        showChildren: false,
        children:[
            {
                name : 'Algeria',
                showChildren: false,
                children:[
                    {
                        name : 'Algeris',
                        showChildren: false,
                        children:[]
                    },
                    {
                        name : 'Algeria child 2',
                        showChildren: false,
                        children:[
                            
                        ]
                    },
                ]
            },
            {
                name : 'Angola',
                showChildren: false,
                children:[]
            },
            {
                name : 'Benin',
                showChildren: false,
                children:[]
            },

        ]
     },
     {
        name: 'Asia',
        showChildren: false,
        children:[
            {
                name : 'Afghanistan',
                showChildren: false,
                children:[
                    {
                        name : 'Kabul',
                        showChildren: false,
                        children:[]
                    }
                ]
            },
            {
                name : 'Armenia',
                showChildren: false,
                children:[]
            },
            {
                name : 'Azerbaijan',
                showChildren: false,
                children:[]
            },

        ]
     },
     {
        name: 'Europe',
        showChildren: false,
        children:[
            {
                name : 'Romania',
                showChildren: false,
                children:[
                    {
                        name : 'Bucuresti',
                        showChildren: false,
                        children:[]
                    }
                ]
            },
            {
                name : 'Hungary',
                showChildren: false,
                children:[]
            },
            {
                name : 'Benin',
                showChildren: false,
                children:[]
            },
        ]
     },
     {
        name: 'North America',
        showChildren: false,
        children: []
     }


]

推荐答案

空格来自箭头/关闭按钮.将其位置从相对更改为绝对.

.close-btn {
  float: left;
  left: -30px;
  /* position: relative; */
  position: absolute;
  z-index: 10;
}

Css相关问答推荐

当滚动到页脚时,是否可以隐藏固定元素?

如何在悬停时缓慢更改渐变 colored颜色 ?

此css Select 器的总体特性是什么?按钮:不是(#mainBtn,.cta)

正确的`最小/最大宽度`+`非(设备类型)`css媒体查询的语法

material 设计--Bootstrap输入域问题

如何更改Swiper Navegation默认 colored颜色

如何在 CSS 中使 Flex 容器内的网格响应,而不 destruct 外部 Flex 容器的布局?

使用纯 CSS 访问跨度内容并设置其样式

将重复的线性渐变锚定到元素的顶部

CSS 网格 - 具有独立可滚动区域的两列布局

清除 CSS 中已设置的填充属性以恢复 SVG 的默认值

Tailwind css 和 Next.js 导航组件 z-index 不工作

在 SVG 元素上方添加标签而不移动 SVG 元素

react 样式的条件类名称设置

Angular 导航栏问题

CSS Stepper - LI After Overlaying LI Before

有没有办法让 mui v5 呈现没有 css-xxx 前缀的类名?

css3比例周围的空白

如何在渲染之前获取react 组件的大小(高度/宽度)?

什么是 DOM 回流?