【问题标题】:Multiple Identical RGBA colours not working多个相同的 RGBA 颜色不起作用
【发布时间】:2015-02-19 13:35:18
【问题描述】:

基本上我想尝试在三个不同的区域使用相同的 rgba 值。我希望我的 body、nav 和 footer 都有 rgba(0,0,0,.8)。

我的身体正常,但导航和页脚只是显示为纯黑色,没有透明度...

我尝试稍微改变透明度,使它们有所不同,但这也不起作用。

你只能使用一次吗?

@font-face {
        font-family: 'telegraficoregular';
        src: url('../FONTS/telegrafico-webfont.eot');
        src: url('../FONTS/telegrafico-webfont.eot?#iefix') format('embedded-opentype'),
             url('../FONTS/telegrafico-webfont.woff2') format('woff2'),
             url('../FONTS/telegrafico-webfont.woff') format('woff'),
             url('../FONTS/telegrafico-webfont.ttf') format('truetype'),
             url('../FONTS/telegrafico-webfont.svg#telegraficoregular') format('svg');
        font-weight: normal;
        font-style: normal;

    }

    .cf:after {
      content: "";
      display: table;
      clear: both;
    }

    html, body, head, nav, article, aside, section, h3, h4, footer, ul, li {

    margin: 0;
    padding: 0;

    }

    /*Global Styles*/

    html {

    margin: 0;
    padding: 0;

    }


    body {

    width: 100%;
    margin: 0 auto;
    padding: 0;
    background:rgba(0,0,0,.9);
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;


    }

    h1 {

    font-family: telegraficoregular;
    font-size: 3em;
    color: rgb(50,153,187);
    padding: 0;
    margin:0;
    text-align: center;

    }

    h2 {

    font-family: telegraficoregular;
    font-size: 1em;
    font-style: italic;
    color: #003366;
    padding: 0;
    margin: 0;
    text-align: center;

    }

    p {

    font-family: serif;
    font-size: 1.1em;
    color: black;
    text-align:left;
    padding-left:70px;
    }

    h4 + p {

    font-family: serif;
    font-size: 1.1em;
    color: black;
    text-align:left;
    padding-left:70px;
    padding-top: 10px;
    }
    /*Nav Styles */

    header {

    background:rgb(233,233,233);
    width: 100%;
    height: 160px;
    margin:0 auto;

    }

    nav {

    background:rgba(0,0,0,.8);
    width: auto;
    height: 64px;

    }

    nav ul.TopMenu li {

    float: left;
    list-style: none;
    width: 150px;

    }

    nav ul.TopMenu {

    width: 600px;
    margin: 0 auto;

    }

    nav ul.TopMenu li a {

    display:block;
    line-height: 64px;
    padding:0 2em;
    text-align: center;
    font-family: telegraficoregular;
    font-size: 1.2em;
    text-decoration: none;
    }

    li {

    position: relative;

    }

    nav ul.SubMenu{

    float: none;
    text-align: center;
    border-bottom: 1px solid white;
    position: absolute;
    left: -9000em;
    top: 64px;
    height: auto;
    max-height: 0;
    -moz-transition: max-height 0.5s ease-in-out;
    -webkit-transition: max-height 0.5s ease-in-out;
    -o-transition: max-height 0.5s ease-in-out;
    transition: max-height 0.5s ease-in-out;
    overflow:hidden;
    z-index:999;
    }

    nav ul.SubMenu li{

    height: 32px;
    font-family: telegraficoregular;
    font-size: 1em;
    padding: 0;
    margin: 0;
    border-bottom: 1px solid white;

    }

    nav ul.SubMenu li a{

    display:block;
    line-height: 32px;
    padding: 0px;
    margin: 0;
    }





    .TopMenu li:hover ul{

    left: 0;
    max-height: 20em;

    }

    ul.SubMenu li a{

    background: rgba(0,0,0,.8);
    border-bottom: 1px solid white;


    }

    ul.SubMenu li a:visited {

    color: white;

    }

    ul.SubMenu li a:hover {

    background: rgba(255,165,0,.8);

    }


    a:visited {

    color: white;

    }

    a:hover {

    color: orange; 

    }


    /* Article Styles */

    article {

    width: 896px;
    padding: 32px;

    }


    /*Section Styles */


    section.China {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    section h3 {

    font-family: telegraficoregular;
    font-size: 2em;
    border-bottom: 1px solid black;
    padding: 5px;
    }

    section h4 {

    font-family: telegraficoregular;
    font-size: 1.4em;
    padding-top: 60px;
    padding-left: 70px;
    text-align: left;
    text-decoration: underline;

    }

    section.China img {

    position: relative;
    top: 20px;
    border-radius: 5px;
    }

    section.Australia {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    section.Australia img {

    position: relative;
    top: 20px;
    border-radius: 5px;
    }

    section.Thailand {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    section.Thailand img {

    position: relative;
    top: 20px;
    border-radius: 5px;
    }


    /* Link Styles*/



    /* About Styles */



    #AboutContent{

    width: 960px;
    height: 1200px;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }

    #AboutHeader {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    #AboutHeader img {

    display: block;
    margin: 0 auto;
    text-align: center;

    }

    #AboutHeader h2 {

    font-family: telegraficoregular;
    font-size: 1.5em;
    text-decoration: underline;
    color: black;
    padding: 0;
    margin-top: 20px;
    text-align: center;

    }

    h2 + img {

    position: relative;
    top: 50px;
    border-radius: 5px;
    }

    #AboutHeader p {

    font-family: serif;
    font-size: 1.1em;
    color: black;
    text-align:left;
    padding:50px 0 0 100px;
    }

    /*Photos Content */

    #PhotoContent{

    width: 960px;
    height:auto;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }

    /* Photos Gallery */

    #ChinaPhotos {

    width: 960px;
    height:1200px;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }

    #ChinaPhotos h3 {

    font-family: telegraficoregular;
    font-size: 2em;
    border-bottom: 1px solid black;
    padding: 5px;
    text-align: center;
    margin-top: 20px;

    }

    #ChinaPhotos img {

    margin-top: 20px;
    border: 1px solid black;

    }

    #ChinaPhotos a .Thumb:hover {

    width: 320px;
    height: 240px;

    }

    /* Container Styles */

    #Content {

    width: 960px;
    height: auto;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }


    /* Footer Styles */

    .Footer {
    width: 100%;
    height: 80px;
    background: rgba(0,0,0,.7);
    }

    .Footer p {

    font-family: telegraficoregular;
    color: white;
    font-style: italic;
    line-height: 80px;
    text-align: center;

    }

【问题讨论】:

  • 很难在没有看到您的 html 的情况下判断,但元素可能是重叠的,因此会变得更暗(因为它是相加的)。重叠的两个不透明度为 80% 的元素看起来是黑色的。尝试另一种颜色(完全不透明)进行调试。您绝对可以在多个元素上使用 rgba。
  • @Patrick - 虽然它们不只是加在一起。即,将 50% 不透明的子代放在 50% 不透明的父代上不会给您 100% 不透明的结果。但是,如果您比较单个 75% 不透明元素的结果,结果是相同的。在 2 元素的情况下,最终不透明度由 parentOpacity + child_opacity * (1 - parentOpacity) 确定 - 或使用上图:0.5 + 0.5*(1-0.5) = 0.5 + 0.5*0.5 = 0.5 + 0.25 = 0.75。如果我们为每个使用 25%,则单个 43.75% 的不透明元素是相同的。即 0.25 + 0.25 * (1-0.25) = 0.25 + 0.25 * 0.75 = 0.25 + 0.1875 = 0.4375
  • @enhzflep 哈哈,是的。为了解释清楚,我很简单。他想知道为什么会这样,所以我给出了最容易理解的答案,尽管在技术上并不准确。感谢您的澄清。
  • @Patrick - 谢谢你让我思考和进一步调查。 :)

标签: css background rgba


【解决方案1】:

所以,看起来好像是重叠导致了您的问题(请参阅上面的评论。我更改了导航和页脚中的背景颜色,它们只是找到了。

事实上,你有两个不透明度为 80% 的元素重叠,结果看起来是黑色的。

    html, body, head, nav, article, aside, section, h3, h4, footer, ul, li {

    margin: 0;
    padding: 0;

    }

    /*Global Styles*/

    html {

    margin: 0;
    padding: 0;

    }


    body {

    width: 100%;
    margin: 0 auto;
    padding: 0;
    background:rgba(0,0,0,.9);
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;


    }

    h1 {

    font-family: telegraficoregular;
    font-size: 3em;
    color: rgb(50,153,187);
    padding: 0;
    margin:0;
    text-align: center;

    }

    h2 {

    font-family: telegraficoregular;
    font-size: 1em;
    font-style: italic;
    color: #003366;
    padding: 0;
    margin: 0;
    text-align: center;

    }

    p {

    font-family: serif;
    font-size: 1.1em;
    color: black;
    text-align:left;
    padding-left:70px;
    }

    h4 + p {

    font-family: serif;
    font-size: 1.1em;
    color: black;
    text-align:left;
    padding-left:70px;
    padding-top: 10px;
    }
    /*Nav Styles */

    header {

    background:rgb(233,233,233);
    width: 100%;
    height: 160px;
    margin:0 auto;

    }

    nav {

    background:rgba(255,0,0,.8);
    width: auto;
    height: 64px;

    }

    nav ul.TopMenu li {

    float: left;
    list-style: none;
    width: 150px;

    }

    nav ul.TopMenu {

    width: 600px;
    margin: 0 auto;

    }

    nav ul.TopMenu li a {

    display:block;
    line-height: 64px;
    padding:0 2em;
    text-align: center;
    font-family: telegraficoregular;
    font-size: 1.2em;
    text-decoration: none;
    }

    li {

    position: relative;

    }

    nav ul.SubMenu{

    float: none;
    text-align: center;
    border-bottom: 1px solid white;
    position: absolute;
    left: -9000em;
    top: 64px;
    height: auto;
    max-height: 0;
    -moz-transition: max-height 0.5s ease-in-out;
    -webkit-transition: max-height 0.5s ease-in-out;
    -o-transition: max-height 0.5s ease-in-out;
    transition: max-height 0.5s ease-in-out;
    overflow:hidden;
    z-index:999;
    }

    nav ul.SubMenu li{

    height: 32px;
    font-family: telegraficoregular;
    font-size: 1em;
    padding: 0;
    margin: 0;
    border-bottom: 1px solid white;

    }

    nav ul.SubMenu li a{

    display:block;
    line-height: 32px;
    padding: 0px;
    margin: 0;
    }





    .TopMenu li:hover ul{

    left: 0;
    max-height: 20em;

    }

    ul.SubMenu li a{

    background: rgba(0,0,0,.8);
    border-bottom: 1px solid white;


    }

    ul.SubMenu li a:visited {

    color: white;

    }

    ul.SubMenu li a:hover {

    background: rgba(255,165,0,.8);

    }


    a:visited {

    color: white;

    }

    a:hover {

    color: orange; 

    }


    /* Article Styles */

    article {

    width: 896px;
    padding: 32px;

    }


    /*Section Styles */


    section.China {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    section h3 {

    font-family: telegraficoregular;
    font-size: 2em;
    border-bottom: 1px solid black;
    padding: 5px;
    }

    section h4 {

    font-family: telegraficoregular;
    font-size: 1.4em;
    padding-top: 60px;
    padding-left: 70px;
    text-align: left;
    text-decoration: underline;

    }

    section.China img {

    position: relative;
    top: 20px;
    border-radius: 5px;
    }

    section.Australia {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    section.Australia img {

    position: relative;
    top: 20px;
    border-radius: 5px;
    }

    section.Thailand {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    section.Thailand img {

    position: relative;
    top: 20px;
    border-radius: 5px;
    }


    /* Link Styles*/



    /* About Styles */



    #AboutContent{

    width: 960px;
    height: 1200px;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }

    #AboutHeader {

    text-align: center;
    margin: 20px auto;
    padding: 20px;

    }

    #AboutHeader img {

    display: block;
    margin: 0 auto;
    text-align: center;

    }

    #AboutHeader h2 {

    font-family: telegraficoregular;
    font-size: 1.5em;
    text-decoration: underline;
    color: black;
    padding: 0;
    margin-top: 20px;
    text-align: center;

    }

    h2 + img {

    position: relative;
    top: 50px;
    border-radius: 5px;
    }

    #AboutHeader p {

    font-family: serif;
    font-size: 1.1em;
    color: black;
    text-align:left;
    padding:50px 0 0 100px;
    }

    /*Photos Content */

    #PhotoContent{

    width: 960px;
    height:auto;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }

    /* Photos Gallery */

    #ChinaPhotos {

    width: 960px;
    height:1200px;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }

    #ChinaPhotos h3 {

    font-family: telegraficoregular;
    font-size: 2em;
    border-bottom: 1px solid black;
    padding: 5px;
    text-align: center;
    margin-top: 20px;

    }

    #ChinaPhotos img {

    margin-top: 20px;
    border: 1px solid black;

    }

    #ChinaPhotos a .Thumb:hover {

    width: 320px;
    height: 240px;

    }

    /* Container Styles */

    #Content {

    width: 960px;
    height: auto;
    margin: 0 auto;
    padding: 0;
    background: white;
    font-family: serif, arial, helvetica, georgia;
    font-size: 16px;
    color: black;

    }


    /* Footer Styles */

    .Footer {
    width: 100%;
    height: 80px;
    background: rgba(0,0,255,.5);
    }

    .Footer p {

    font-family: telegraficoregular;
    color: white;
    font-style: italic;
    line-height: 80px;
    text-align: center;

    }
<nav>
    <ul>
        <li>something</li>
        <li>something else</li>
    </ul>
</nav>

<footer class="Footer">
This is a footer
</footer>

【讨论】:

  • 是的,好像是这样!我将导航和页脚设置为透明度“.1”,这似乎足够体面了!有没有其他解决方案?谢谢!
  • 您有几个选择,而不是样式主体,您可以使用元素的固定或绝对定位来避免堆叠。
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 2016-10-27
  • 1970-01-01
  • 2011-10-11
相关资源
最近更新 更多