【问题标题】:Making text background transparent but not text itself使文本背景透明而不是文本本身
【发布时间】:2013-03-05 03:55:36
【问题描述】:

所以我遇到了问题。我环顾四周,但没有运气。我想让我的身体背景透明,但文字不透明。就像现在一样,我一直在制作相同的不透明度。这是我的代码:

@charset "utf-8";
body {
    font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
    background-color: #42413C;
    margin: 0;
    padding: 0;
    color: #000;
}

/* ~~ Element/tag selectors ~~ */
ul, ol, dl { 
    padding: 0;
    margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
    margin-top: 0;
    padding-right: 15px;
    padding-left: 15px;
    opacity:1;
}
a img { 
    border: none;
}
a:link {
    color: #42413C;
    text-decoration: underline;
}
a:visited {
    color: #6E6C64;
    text-decoration: underline;
}
a:hover, a:active, a:focus {
    text-decoration: none;
}
.container {
    width: 750px;
    margin: 0 auto;
}
.content {
    padding:20px;
    width:710px;
    position:relative;
    background:#CCC;
    opacity: 0.5;
}
.fltrt {
    float: right;
    margin-left: 8px;
}
.fltlft { 
    float: left;
    margin-right: 8px;
}
.clearfloat { 
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}
.header {
    top:0%;
    width: 750px;
    height: 200px;
    background-image: url(images/header.png);
    background-repeat:no-repeat;
    background-position:center;
}
.navbar {
    height: 50px;
    width: 750px;
    position: relative;
    z-index: 2;
}
#bg {
    position: fixed;
    top: 25%;
    left: 15%;
    z-index: -1;
}
div {
display: block;
}

这是我的网站(点击链接不要在您的网址中输入“tccraft.net”,它会带您进入 Facebook 页面):http://tccraft.net/index.php 谢谢!

【问题讨论】:

标签: html css text opacity


【解决方案1】:

不要为此使用opacity,而是将背景设置为RGBA 值,以仅使背景半透明。你的情况是这样的。

.content {
    padding:20px;
    width:710px;
    position:relative;
    background: rgb(204, 204, 204); /* Fallback for older browsers without RGBA-support */
    background: rgba(204, 204, 204, 0.5);
}

有关更多信息和 css 中的 rgba 值示例,请参阅 http://css-tricks.com/rgba-browser-support/

【讨论】:

  • 知道如何使用背景图片来实现这一点吗?
  • 对于背景图片,只需使用带有 Alpha 通道的 PNG 或 WEBP 作为您的图片。
【解决方案2】:

对于完全透明的背景使用:

background: transparent;

否则使用半透明颜色填充:

background: rgba(255,255,255,0.5); // or hsla(0, 0%, 100%, 0.5)

值在哪里:

background: rgba(red,green,blue,opacity); // or hsla(hue, saturation, lightness, opacity)

您还可以将 rgba 值用于渐变背景。

要获得图像背景的透明度,只需事先在您选择的图像编辑器中降低图像的不透明度。

【讨论】:

  • 是否有关于background: transparent; 的更多信息的链接?我无法让它工作或找到它的文档。
【解决方案3】:

opacity 将使文本和背景都透明。改用半透明背景色,例如使用 rgba() 值。适用于 IE8+

【讨论】:

    【解决方案4】:

    如果您在现代浏览器中使用 RGBA,则不需要让旧版 IE 仅使用 RGB 给定颜色的非透明版本。

    如果您不坚持纯 CSS 解决方案,请尝试 CSS3PIE。使用这种语法,您可以在旧 IE 中看到与在现代浏览器中看到的完全相同的结果:

    div {
        -pie-background: rgba(223,231,233,0.8);
        behavior: url(../PIE.htc);
    }
    

    【讨论】:

      【解决方案5】:

      要让你的身体背景透明,请尝试这种风格,它对我有用,在你想要的颜色代码的末尾添加“广告”

      background-color: #42413Cad !important;
      

      【讨论】:

        【解决方案6】:

        我会为透明度添加十六进制颜色代码

        背景色:#42413C+hexcode

        https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4 大概是这样的:

        background-color: #42413C2E
        

        等于那个颜色 + 18% alpha

        【讨论】:

          【解决方案7】:
          box-shadow: inset 1px 2000px rgba(208, 208, 208, 0.54);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-12-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多