【问题标题】:Displaying a vertical dashed line centrally in a div在 div 中居中显示垂直虚线
【发布时间】:2021-09-16 12:15:14
【问题描述】:

使用 StyledComponents 处理 React,下面的每个 <div> 代表一个不同的 Styled Component。

试图实现这样的目标:

但理想情况下,我想在progress-indicator 中添加“虚线”样式,使其垂直位于指示器下方并跨越wrapping-container 的整个高度。我试过使用::before & ::after,但没有成功。

.wrapping-container {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.progress-indicator {
  height: 22px;
  width: 22px;
  margin-right: 16px;
  border-radius: 50%;
  border: 1px solid rgb(255, 255, 255);
  background: rgb(39, 40, 42);
  color: rgb(255, 255, 255);
  font-size: 13px;
  text-align: center;
}


.input {  
  border: 1px solid #ced4da;
  border-radius: 2px;
  overflow-y: hidden;
  padding: 0.375rem 1.75rem 0.375rem 0.75rem;
}
<div class="wrapping-container">
  <div class="progress-indicator">2</div>
  <div class="input">Input</div>
</div>  


<div class="wrapping-container">
  <div class="progress-indicator">3</div>
  <div class="input">Input</div>
</div>  

【问题讨论】:

标签: html css pseudo-element


【解决方案1】:

我会这样做的方式如下:

.wrapping-container {
    position: relative;
    display: flex;
}
.progress-indicator {
    position: relative;
    display: inline-block;
    margin-bottom: 40px;
    color: #fff;
}
.progress-indicator::after {
    content: "";
    width: 30px;
    height: 30px;
    background: #000;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    z-index: -1;
}
.progress-indicator::before {
    content: "";
    height: 40px;
    border: 1px dashed #000;
    position: absolute;
    left: 50%;
    top: -50px;
    transform: translateX(-50%);
    z-index: -1;
}
.input {
    margin-left: 30px;
}
<div class="wrapping-container">
    <div class="progress-indicator">2</div>
    <div class="input">Input</div>
</div>  
<div class="wrapping-container">
    <div class="progress-indicator">3</div>
    <div class="input">Input</div>
</div>

【讨论】:

  • 感谢这个 Jelle - 由于某种原因,我的进度指示器没有显示为圆圈,并且它们的文本是白色的。破折号存在于 DOM 中,但也是透明的。使用#000 会对此产生影响吗?
  • 通过检查器工具查看问题可能很聪明。看看什么样的 css,比如让元素变黑,对代码做了什么。如果有效,您可以将其添加到代码中。
  • 删除 z-index-1 似乎可以解决问题!
【解决方案2】:

实现此目的的简单(如果粗略)方法:

.dasher {
    border: 1px dashed #ced4da;
    max-width: 0px;
    min-height: 10px;
    margin-left: 10px;
    margin-top: -20px;
}

演示

.wrapping-container {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.progress-indicator {
  height: 22px;
  width: 22px;
  margin-right: 16px;
  border-radius: 50%;
  border: 1px solid rgb(255, 255, 255);
  background: rgb(39, 40, 42);
  color: rgb(255, 255, 255);
  font-size: 13px;
  text-align: center;
}


.input {  
  border: 1px solid #ced4da;
  border-radius: 2px;
  overflow-y: hidden;
  padding: 0.375rem 1.75rem 0.375rem 0.75rem;
}

.dasher {
    border: 1px dashed #ced4da;
    max-width: 0px;
    min-height: 10px;
    margin-left: 10px;
    margin-top: -20px;
}
<div class="wrapping-container">
  <div class="progress-indicator">2</div>
  <div class="input">Input</div>
</div>

<div class="dasher"></div>

<div class="wrapping-container">
  <div class="progress-indicator">3</div>
  <div class="input">Input</div>
</div>

应该给你这个:

【讨论】:

    【解决方案3】:

    我添加了一个容器和一个虚线块,它将直接穿过所有高度wrapping-container

    .container{
      position:relative;
    }
    .border-dash{
      position: absolute;
      top: 15px;
      left: 11px;
      border-left: 1px dashed black;
      height:calc(100% - 25px);
      width: 1px;
      display: block;
      z-index: -1;
    }
    

    演示

    .wrapping-container {
      display: flex;
      align-items: center;
      margin-bottom: 20px;
    }
    
    .progress-indicator {
      height: 22px;
      width: 22px;
      margin-right: 16px;
      border-radius: 50%;
      border: 1px solid rgb(255, 255, 255);
      background: rgb(39, 40, 42);
      color: rgb(255, 255, 255);
      font-size: 13px;
      text-align: center;
    }
    
    
    .input {  
      border: 1px solid #ced4da;
      border-radius: 2px;
      overflow-y: hidden;
      padding: 0.375rem 1.75rem 0.375rem 0.75rem;
    }
    .container{
      position:relative;
    }
    .border-dash{
      position: absolute;
      top: 15px;
      left: 11px;
      border-left: 1px dashed black;
      height:calc(100% - 25px);
      width: 1px;
      display: block;
      z-index: -1;
    }
    <div class="container">
    
      <span class="border-dash"></span>
      
      <div class="wrapping-container">
        <div class="progress-indicator">2</div>
        <div class="input">Input</div>
      </div>  
    
      <div class="wrapping-container">
        <div class="progress-indicator">3</div>
        <div class="input">Input</div>
      </div>
    
      <div class="wrapping-container">
        <div class="progress-indicator">4</div>
        <div class="input">Input</div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2022-11-04
      • 2014-09-16
      相关资源
      最近更新 更多