簡體   English   中英

如何使用 CSS 在兩個圓圈之間繪制一條水平線?

[英]How do I draw a horizontal line between two circles with CSS?

如何在 CSS 中的 2 個圓圈之間繪制一條水平線?

正如屏幕截圖所示,它必須位於它們的中間。

這里的例子:

在此處輸入圖片說明

我畫了兩個圓圈,但不知道如何連接它們。

 #status-buttons a { color: black; display: inline-block; font-size: 17px; font-weight: normal; margin-right: 0; text-align: center; text-transform: uppercase; min-width: 150px; text-decoration: none; } #status-buttons a:hover { text-decoration: none; } #status-buttons a.active span { color: white; background: #ACCF5B; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; } #status-buttons span { color: white; background: #22bacb; display: block; height: 45px; margin: 0 auto 10px; padding-top: 20px; width: 60px; border-radius: 50%; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; }
 <div id="status-buttons" class="text-center"> <a href="#/form/regalo" class="active"><span>1</span> Step 1</a> <a href="#/form/tusdatos"><span>2</span> Step 2</a> </div>

在 JSFiddle 上查看演示

您可以使用偽元素插入絕對定位的邊框:

 #status-buttons { position: relative; /* 1 */ display: inline-block; /* 2 */ } #status-buttons::after { /* 3 */ content: ""; position: absolute; width: 50%; z-index: -1; /* 4 */ top: 35%; left: 25%; border: 3px solid #ACCF5B; } #status-buttons a { color: black; display: inline-block; font-size: 17px; font-weight: normal; margin-right: 0; text-align: center; text-transform: uppercase; min-width: 150px; text-decoration: none; } #status-buttons a:hover { text-decoration: none; } #status-buttons a.active span { color: white; background: #ACCF5B; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; } #status-buttons span { color: white; background: #22bacb; display: block; height: 45px; margin: 0 auto 10px; padding-top: 20px; width: 60px; border-radius: 50%; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; }
 <div id="status-buttons" class="text-center"> <a href="#/form/regalo" class="active"><span>1</span> Step 1</a> <a href="#/form/tusdatos"><span>2</span> Step 2</a> </div>

注意事項:

  1. 為絕對定位建立最近的定位祖先。
  2. 使容器僅消耗必要的寬度。
  3. 插入偽元素
  4. 確保任何水平線重疊都不會出現在圓圈上方

您可以添加一個新元素並將其放置在兩個圓圈之間:

 #status-buttons a { color: black; display: inline-block; font-size: 17px; font-weight: normal; margin-right: 0; text-align: center; text-transform: uppercase; min-width: 150px; text-decoration: none; } #status-buttons a:hover { text-decoration: none; } #status-buttons a.active span { color: white; background: #ACCF5B; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; } #status-buttons span { color: white; background: #22bacb; display: block; height: 45px; margin: 0 auto 10px; padding-top: 20px; width: 60px; border-radius: 50%; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; } #line { position: absolute; top: 42px; left: 112px; width: 96px; height: 5px; background: #ACCF5B; }
 <div id="status-buttons" class="text-center"> <a href="#/form/regalo" class="active"><span>1</span> Step 1</a> <div id="line"> </div> <a href="#/form/tusdatos"><span>2</span> Step 2</a> </div>

這是一種解決方案:

https://jsfiddle.net/sfyuxrs9/

它包含一個具有position: absolutediv (形成線) position: absolute和負z-index值。 其余的只是調整寬度/高度/頂部和左側的所有值

我想你可以做一些這樣的事情檢查下面的代碼片段

 #status-buttons a { color: black; display: inline-block; font-size: 17px; font-weight: normal; margin-right: 0; text-align: center; text-transform: uppercase; min-width: 150px; text-decoration: none; } #status-buttons a:hover { text-decoration: none; } #status-buttons a.active span { color: white; background: #ACCF5B; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; } #status-buttons span { color: white; background: #22bacb; display: block; height: 45px; margin: 0 auto 10px; padding-top: 20px; width: 60px; border-radius: 50%; box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0; } div.linetop { border-top: 1px solid #111111; width:95px; position:absolute; top:40px; left:115px; }
 <div id="status-buttons" class="text-center"> <a href="#/form/regalo" class="active"><span>1</span> Step 1</a> <a href="#/form/tusdatos"><span>2</span> Step 2</a> </div> <div class="linetop"></div>

希望這有幫助

給你。

<div id="status-buttons" class="text-center">
    <a href="#/form/regalo" class="active"><span>1</span> Step 1</a>
    <a href="#/form/tusdatos"><span>2</span> Step 2</a>
</div>
<div class="line">
</div>

CSS

 #status-buttons a {
    position: relative;
    color: black;
    display: inline-block;
    font-size: 17px;
    font-weight: normal;
    margin-right: 0;
    text-align: center;
    text-transform: uppercase;
    min-width: 150px;
    text-decoration: none;
        z-index: 1;
}

#status-buttons a:hover {
  text-decoration: none;
}

#status-buttons a.active span {
    color: white;
    background: #ACCF5B;
    box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0;
}

#status-buttons span {
    color: white;
    background: #22bacb;
    display: block;
    height: 45px;
    margin: 0 auto 10px;
    padding-top: 20px;
    width: 60px;
    border-radius: 50%;
    box-shadow: rgba(0, 0, 0, 0.792157) 3px 3px 3px 0;

}
.line {
    position: absolute;
    border-bottom: 5px solid black;
    width: 20%;
    left: 71px;
    top: 39px;
    z-index: 0;
}

https://jsfiddle.net/norcaljohnny/nwjz2010/

這是使用flex的最干凈的解決方案

 .timeline { display: flex; align-items: center; justify-content: center; } .circle { width: 13px; height: 13px; background: black; border-radius: 50%; } .dashed { width: 100px; border: 1px dashed #C4C4C4; }
 <div class="timeline"> <div class="circle"></div> <div class="dashed"></div> <div class="circle"></div> <div class="dashed"></div> <div class="circle"></div> </div>

您也可以使用此方法:)

 .his-bar { display: flex; position: relative; padding-top: 50px; width: 100%; margin:auto; margin-top: 40px; } .his-bar:before { content: ''; border: 1px solid #727272; position: absolute; top: 60px; right: 0; width: 99%; } .his-bar .point { border: 2px solid #872071; width: 20px; height: 20px; border-radius: 50%; display: inline-block; background-color: #F8F8F8; z-index: 2; position: relative; } .his-bar .point-start:after { content: '2000'; position: absolute; top: 30px; left: -7px; } .his-bar .point-end { margin-left: auto; } .his-bar .point-end:after { content: '2021'; position: absolute; top: 30px; left: -7px; }
 <div class="his-bar"> <span class="point point-start"></span> <span class="point point-end"></span> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM