簡體   English   中英

CSS:使文本顯示在我的div中的漸變上方

[英]CSS: Getting text to appear above the gradient in my div

我正在新聞匯總網站的主頁上工作,應該有幾個區域可以將背景圖片和頂部文本帶入內容,並在兩者之間形成漸變。 除了z-index較低之外,大部分情況下我都擁有它,但漸變仍然顯示在文本框上方。 為了確保這一點,我已經在堅實的背景下進行了實驗。 代碼和示例在這里: http : //jsfiddle.net/cx0uvshd/

<style type="text/css">
    .feature {
        position: relative;
        float: left; 
        width: 465px; 
        height: 170px;  
        margin-top: 24px; 
        margin-right: 30px; 
    }
    .feature.last { 
        margin-right: 0; 
    }
    .feature-bottom {
        background: none;
        position: absolute;
        bottom: 0;
        left: 0;
        padding: 0 30px 6px;
        width: 100%;
        z-index: 200;
        line-height: 1;
    }
    .feature-bottom::after {
        content: "";
        position: absolute;
        bottom: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
        z-index: 100;
        background: rgba(0,0,0,0);
        background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,0.5)), color-stop(100%, rgba(0,0,0,0.5)));
        background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: -o-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#000000', GradientType=0 );
    }
    .feature-bottom h3 { 
        color: #FFF; 
        font-size: 15px; 
        font-weight: 400; 
        margin: 0; 
    }
    .feature-bottom h2 { 
        color: #FFF; 
        font-size: 24px; 
        font-weight: 400; 
        margin: 0; 
    }
</style>

將(position:relative)和(z-index:201)添加到特征底部h3和h2。 在下面找到修改后的代碼:

.feature { position: relative; float: left; width: 465px; height: 170px;  margin-top: 24px; margin-right: 30px; }
.feature.last { margin-right: 0; }
.feature-bottom {
    background: none;
    position: absolute;
    bottom: 0;
    left: 0;
    padding: 0 30px 6px;
    width: 100%;
    z-index: 200;
    line-height: 1;
}
.feature-bottom::after {
    content: "";
    position: absolute;
    bottom: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 100;
    background: rgba(0,0,0,0);
    background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,0.5)), color-stop(100%, rgba(0,0,0,0.5)));
    background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: -o-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#000000', GradientType=0 );
}
.feature-bottom h3 { position:relative; z-index:201; color: #FFF; font-size: 15px; font-weight: 400; margin: 0; }
.feature-bottom h2 { position:relative; z-index:201; color: #FFF; font-size: 24px; font-weight: 400; margin: 0; }

為什么不對.feature-bottom應用漸變? 像這樣: http : //jsfiddle.net/cx0uvshd/2/

在當前版本中,盡管z-index較大,但您仍具有:before元素覆蓋。 您需要新的堆疊上下文

這是一個類似的問題

不太干凈的解決方案是添加另一個div,其內容低於漸變1。 然后給該div一個類,該類是feature-bottom的副本。 然后將特征底部的顏色設置為透明以隱藏文本。 同時更改最后兩個選擇器以使用復制類。 同時復制最后兩個選擇器以用於新類。 的jsfiddle

CSS:

.feature { position: relative; float: left; width: 465px; height: 170px; margin-top: 24px; margin-right: 30px; }
    .feature.last { margin-right: 0; }

/*Copy of feature bottom*/
.feature-bottom2 {
background: none;
position: absolute;
bottom: 0;
left: 0;
padding: 0 30px 6px;
width: 100%;
z-index: 200;
line-height: 1;
}
.feature-bottom {
    color: transparent;
background: none;
position: absolute;
bottom: 0;
left: 0;
padding: 0 30px 6px;
width: 100%;
z-index: 200;
line-height: 1;
}
.feature-bottom::after {
content: "";
position: absolute;
bottom: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 100;
background: rgba(0,0,0,0);
background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,0.5)), color-stop(100%, rgba(0,0,0,0.5)));
background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: -o-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#000000',  GradientType=0 );
}
/* Copy of below*/
.feature-bottom2 h3 { color: #FFF; font-size: 15px; font-    weight: 400; margin: 0; }
.feature-bottom2 h2 { color: #FFF; font-size: 24px; font-weight: 400; margin: 0; }

.feature-bottom h3 { font-size: 15px; font-weight: 400; margin: 0; }
.feature-bottom h2 { font-size: 24px; font-weight: 400; margin: 0; }

上面的線性漸變<div>帶有 CSS 的標簽</div><div id="text_translate"><p>我希望在一些獨立滾動的選項卡內容的頂部添加一個漸變,這樣當您向上滾動時,內容不會在選項卡底部被切掉。 目標是讓內容在上面的選項卡后面淡化為白色。 我將如何 go 添加此漸變以使其位於選項卡內容之上並以最佳方式實現此效果? 下面的圖片更好地解釋了我想要的效果:</p><p> <a href="https://i.stack.imgur.com/r6lEv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r6lEv.png" alt="我有這個"></a></p><p> <a href="https://i.stack.imgur.com/16qjM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/16qjM.png" alt="我要去這個"></a></p><p> 顯然我不希望它覆蓋第一張卡片,但我想我可以在選項卡和卡片之間調整它的大小,所以除非你滾動,否則不會覆蓋任何內容。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"&gt; &lt;div class="col-xs-12 col-sm-12 col-md-6"&gt; &lt;div class="row r-tabs"&gt; &lt;div class="tab-bar"&gt; &lt;button class="bar-item tab-active" onclick="openTab(event, 'section1')"&gt;Summary&lt;/button&gt; &lt;button class="bar-item" onclick="openTab(event, 'section2')"&gt;Images&lt;/button&gt; &lt;button class="bar-item" onclick="openTab(event, 'section3')"&gt;Contact&lt;/button&gt; &lt;button class="bar-item" onclick="openTab(event, 'section4')"&gt;Help&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="section1" class="tab-content"&gt; &lt;div class="card"&gt; &lt;div class="card-body model-btn-card"&gt; &lt;div class="viewing text-black"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div>

[英]Linear gradient above <div> tag with CSS

暫無
暫無

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

相關問題 上面的線性漸變<div>帶有 CSS 的標簽</div><div id="text_translate"><p>我希望在一些獨立滾動的選項卡內容的頂部添加一個漸變,這樣當您向上滾動時,內容不會在選項卡底部被切掉。 目標是讓內容在上面的選項卡后面淡化為白色。 我將如何 go 添加此漸變以使其位於選項卡內容之上並以最佳方式實現此效果? 下面的圖片更好地解釋了我想要的效果:</p><p> <a href="https://i.stack.imgur.com/r6lEv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r6lEv.png" alt="我有這個"></a></p><p> <a href="https://i.stack.imgur.com/16qjM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/16qjM.png" alt="我要去這個"></a></p><p> 顯然我不希望它覆蓋第一張卡片,但我想我可以在選項卡和卡片之間調整它的大小,所以除非你滾動,否則不會覆蓋任何內容。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"&gt; &lt;div class="col-xs-12 col-sm-12 col-md-6"&gt; &lt;div class="row r-tabs"&gt; &lt;div class="tab-bar"&gt; &lt;button class="bar-item tab-active" onclick="openTab(event, 'section1')"&gt;Summary&lt;/button&gt; &lt;button class="bar-item" onclick="openTab(event, 'section2')"&gt;Images&lt;/button&gt; &lt;button class="bar-item" onclick="openTab(event, 'section3')"&gt;Contact&lt;/button&gt; &lt;button class="bar-item" onclick="openTab(event, 'section4')"&gt;Help&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="section1" class="tab-content"&gt; &lt;div class="card"&gt; &lt;div class="card-body model-btn-card"&gt; &lt;div class="viewing text-black"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div> 使文本顯示在HTML + CSS的背景上方 使用CSS使文本顯示在邊框上方 使文字高於漸變 我的段落中的文字出現在我的大引號上方 CSS vh和vw:為什么我的div與上面的div重疊? CSS漸變未出現 CSS:div中的段落與div文本顯示在同一行 div上的CSS漸變背景 如何使文字高於img <div> ?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM