無法獲得白色和灰色的 CSS 進度條 (Not able to get the CSS progress bar with white and grey)


問題描述

無法獲得白色和灰色的 CSS 進度條 (Not able to get the CSS progress bar with white and grey)

我正在嘗試製作一個進度條,這是我的要求,如下圖所示:

在此處輸入圖片描述

我的代碼是:

<!DOCTYPE html>
<html>
<head>
<style>
.bar {
box‑sizing: content‑box;
height: 20px;
margin: 0 20px 50px;
padding‑bottom: 60px;
padding‑left: 50px;
padding‑right: 50px;
}
.bar span {
display: block;
height: 100%;
border‑top‑right‑radius: 20px;
border‑bottom‑right‑radius: 20px;
border‑top‑left‑radius: 20px;
border‑bottom‑left‑radius: 20px;
background‑color: rgb(0 0 0 / 26%);
position: relative;
}
.bar span:after {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background‑size: 50px 50px;
background‑image: linear‑gradient(
‑45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent );
animation: move 2s linear infinite;
}
@keyframes move {
0% { background‑position: 0 0; }
100% { background‑position: 50px 50px; }
}
</style>
</head>
<body>

<div class="bar animate"><span style="width: 100%"></span></div>

</body>
</html>

我通過我的代碼得到了這個:

在此處輸入圖片描述

誰能幫我獲取確切的進度條或類似的,如預期的那樣,我哪裡出錯了?

提前致謝


參考解法

方法 1:

You need a background with repeating‑linear‑gradient

.background‑lines {
  background: repeating‑linear‑gradient(‑45deg, grey, 
  grey 5px, white 5px, white 10px);
}
<div class="background‑lines">
text if needed
</div>

方法 2:

I am not saying this is the only way of fixing this. But you could just go ahead and invert the colors you have.

background‑image: linear‑gradient( 
    ‑45deg, 
    rgb(0 0 0 / 20%) 25%, 
    transparent 25%, 
    transparent 50%, 
    rgb(0 0 0 / 20%) 50%, 
    rgb(0 0 0 / 20%) 75%, 
    transparent 75%, 
    transparent );

And invert the colors for this one as well:

background‑color: rgb(255 255 255 / 26%);

.bar {
    box‑sizing: content‑box;
    height: 20px;
    margin: 0 20px 50px;
    padding‑bottom: 60px;
    padding‑left: 50px;
    padding‑right: 50px;
}
.bar span {
    display: block;
    height: 100%;
    border‑top‑right‑radius: 20px;
    border‑bottom‑right‑radius: 20px;
    border‑top‑left‑radius: 20px;
    border‑bottom‑left‑radius: 20px;
    background‑color: rgb(255 255 255 / 26%);
    position: relative;
}
.bar span:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background‑size: 50px 50px;
background‑image: linear‑gradient( 
    ‑45deg, 
    rgb(0 0 0 / 20%) 25%, 
    transparent 25%, 
    transparent 50%, 
    rgb(0 0 0 / 20%) 50%, 
    rgb(0 0 0 / 20%) 75%, 
    transparent 75%, 
    transparent );
    animation: move 2s linear infinite;
}
@keyframes move {
    0% {
      background‑position: 0 0;
    }
    100% {
      background‑position: 50px 50px;
    }
}
<body>
    <div class="bar animate">
        <span style="width: 100%"></span>
    </div>
</body>

方法 3:

You added your colors the wrong way. At first the background‑color from .bar span gets applied, resulting in a gray background for the whole bar. And at second the gradient from .bar span:after get's appliced since it comes AFTER the actual html element.

Therefore just swap your colors and you are good to go. So the white/transparent color on your span and the gray color on your :after.

Like so:

body {
  background: white;
}

.bar {
  box‑sizing: content‑box;
  height: 20px;
  margin: 0 20px 50px;
  padding‑bottom: 60px;
  padding‑left: 50px;
  padding‑right: 50px;
}

.bar span {
  display: block;
  height: 100%;
  border‑top‑right‑radius: 20px;
  border‑bottom‑right‑radius: 20px;
  border‑top‑left‑radius: 20px;
  border‑bottom‑left‑radius: 20px;
  background‑color: rgba(255, 255, 255, .2);
  position: relative;
}

.bar span:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background‑size: 50px 50px;
  background‑image: linear‑gradient(
  ‑45deg, rgba(0, 0, 0, .26) 25%, transparent 25%, transparent 50%, rgba(0, 0, 0, .26) 50%, rgba(0, 0, 0, .26) 75%, transparent 75%, transparent );
  animation: move 2s linear infinite;
}

@keyframes move {
  0% { background‑position: 0 0; }
  100% { background‑position: 50px 50px; }
}
<div class="bar animate"><span style="width: 100%"></span></div>

Also keep in mind, as soon as you change the background color for the outer container for your bar (eg. body tag), the bar wil result in another color as well, since you are using transparent colors.

(by SaAsh Techsmia‑wallaceReality‑TorrentHoargarth)

參考文件

  1. Not able to get the CSS progress bar with white and grey (CC BY‑SA 2.5/3.0/4.0)

#css #progress-bar






相關問題

在圖像上淡入文本 (Fade in text over an image)

可以使用 css3 轉換輪廓顏色 (possible to transition outline color with css3)

以背景色為條件的 if 語句 (If statement with background color as condition)

Nội dung từ <p> biến mất khi tôi quay lại trang (Content from <p> disappears when I return to the page)

當按鈕向右浮動時,ng-click 不起作用 (ng-click doesn't work when the button is floated to the right)

帶有偽 css 的背景顏色層 (Background color layer with pseudo css)

是否可以製作離線工作的網絡應用程序? (Is it possible to make a web-app which works offline?)

chrome中帶有省略號的多行文本問題 (issue with multiline text with ellipsis in chrome)

將字符串轉換為類的安全名稱 (Convert string to safe name for class)

絕對定位跨度收縮 (Absolute Positioned Span Shrinks)

如何設置單選按鈕的樣式,使其看起來像普通的可點擊按鈕? (How do I style a radio button to look like a normal clickable button?)

無法獲得白色和灰色的 CSS 進度條 (Not able to get the CSS progress bar with white and grey)







留言討論