Html 如何在不使内容(图像和文本)也透明的情况下使我网站的背景透明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8565969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:05:28  来源:igfitidea点击:

How can I make my website's background transparent without making the content (images & text) transparent too?

htmlcssbackgroundopacitytransparent

提问by KaiZ

I'm doing a website for a school project, and I'm currently having a small problem... I can't make the body's background transparent without it also affecting the content in it.

我正在为一个学校项目做一个网站,我遇到了一个小问题......我目前无法在不影响其中内容的情况下使主体的背景透明。

Here's my HTML code:

这是我的 HTML 代码:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <title>text</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="head">
</div>
<div id="wrapper">
    <ul id="nav">
        <li><a href="index.htm">Inicio</a></li>
        <li><a href="sobre.htm">Sobre a banda</a></li>
        <li><a href="membros.htm">Membros</a></li>
        <li><a href="bilhetes.htm">Bilhetes</a></li>
        <li><a href="galeria.htm">Galeria</a></li>
        <li><a href="areapessoal.php">área Pessoal</a></li>
    </ul>
    <h1><a href="index.htm"><img src="images/a7x-avenged-sevenfold-7050435-1024-831.jpg" width="130" height="25" alt="Rock Band" /></a></h1>
    <div id="body">
        <div id="bodyi">
            <div id="bodyj">
                <div id="sidebar">
                    <div class="content">
                        <h2>Galeria de imagens</h2>
                        <p>Aqui poderá encontrar uma galeria de imagens da banda, com fotos de concertos,
                        <img src="images/denmark.jpg" width="91" height="72" alt="" />
                        entre outras imagens.</p>
                        <p class="readmore"><a href="galeria.htm">Ver</a></p>
                    </div>
                    <div class="content">
                        <h2>Noticias</h2>
                        <h3>18 de Abril, 2011</h3>
                        <h4>"So Far Away" vai ter videoclip</h4>
                        <p>Muitos se questionavam se o mais recente single dos Avenged Sevenfold, "So Far Away", ia ter direito a um videoclip. Eis que surge a boa notícia para os f?s dos A7X: So Far Away vai ter videoclip.
                        <img src="images/63781127.jpg" width="95" height="73" alt="pic 3" />
                        Já foram divulgadas algumas fotos do set de filmagens.</p>
                        <h3>10 de Mar?o, 2011</h3>
                        <h4>A7X nomeados para a Metal Hammer Golden Gods Awards 2011</h4>
                        <p>Os Avenged Sevenfold foram nomeados para a Metal Hammer Golden Gods Awards 2011 em duas categorias.
                        As categorias onde os A7X est?o a participar s?o as seguintes:<p>
                        - Best International Band (Melhor Banda Internacional)<p>
                        - Best Shredder (com o Synyster Gates) (Melhor Shredder)</p>
                    </div>
                </div>
                <div id="content">
                    <center><img src="images/avengeds.jpg" width="346" height="234" alt="four men walking" /></center>
                    <div class="content">
                        <h2>O album mais recente</h2>
                        <img src="images/nightmare.jpg" width="82" height="80" alt="Unwired album cover" class="left" />
                        <p>Os californianos Avenged Sevenfold est?o de volta aos discos com "Nightmare". Solos de guitarra, vocaliza??es rasgadas e um som contagiante continuam a ser a base do sucesso de uma das bandas de heavy-metal com mais f?s no mundo inteiro. 
                        Fundados em 1999 por M. Shadows, Synyster Gates, Zacky Vengeance, Johnny Christ e The Rev - baterista que faleceu em Dezembro de 2009 passado e que foi substituido neste disco por Mike Portnoy, dos Dream Theater - os Avenged Sevenfold já contam com cinco discos de originais na sua carreira.</p>
                        <div class="divider"></div>
                        <h2>Musicas com maior sucesso</h2>
                        <table summary="track downloads" border="0" cellspacing="0">
                        <tr>
                            <th width="55%">Faixa</th>
                            <th>Album</th>
                            <th class="hidden">Dowload links</th>
                        </tr>
                        <tr>
                            <td>Bat Country</td>
                            <td>City of Evil</td>
                            <td class="download"><a href="http://www.youtube.com/watch?v=IHS3qJdxefY">Ouvir</a></td>
                        </tr>
                        <tr>
                            <td>Beast and the Harlot</td>
                            <td>City of Evil</td>
                            <td class="download"><a href="http://www.youtube.com/watch?v=7bDg7n-chhU">Ouvir</a></td>
                        </tr>
                        <tr>
                            <td>Seize the Day</td>
                            <td>City of Evil</td>
                            <td class="download"><a href="http://www.youtube.com/watch?v=jUkoL9RE72o">Ouvir</a></td>
                        </tr>
                        <tr>
                            <td>Almost Easy</td>
                            <td>Avenged Sevenfold</td>
                            <td class="download"><a href="http://www.youtube.com/watch?v=Fi_GN1pHCVc">Ouvir</a></td>
                        </tr>
                        <tr>
                            <td>Afterlife</td>
                            <td>Avenged Sevenfold</td>
                            <td class="download"><a href="http://www.youtube.com/watch?v=HIRNdveLnJI">Ouvir</a></td>
                        </tr>
                        <tr>
                            <td>Nightmare</td>
                            <td>Nightmare</td>
                            <td class="download"><a href="http://www.youtube.com/watch?v=94bGzWyHbu0">Ouvir</a></td>
                        </tr>
                        <tr>
                            <td>Welcome to the Family</td>
                            <td>Nightmare</td>
                            <td class="download"><a href="http://www.youtube.com/watch?v=1OZs7IoWTvc">Ouvir</a></td>
                        </tr>
                        </table>
                    </div>
                </div>
                <div class="clear"></div>
            </div>
        </div>
    </div>
    <div align="right">
        <font size="2">text</font>
    </div>
</div>
</body>
</html>

And here's the CSS code:

这是 CSS 代码:

.head{
    width: 150px;
    height: 160px;
}
body {
    font-family: tahoma, helvetica, arial, sans-serif;
    font-size: 12px;
    text-align: center;
    background: #000;
    color: #ddd4d4;
    padding-top: 12px;
    line-height: 2;
    background-image: url('images/background.jpg');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: 100%;
    opacity: 0.8;
    filter:alpha(opacity=80);
}
td, th {
    font-size: 12px;
    text-align: left;
    line-height: 2;
}
#wrapper {
    margin: auto;
    text-align: left;
    width: 832px;
    position: relative;
    padding-top: 27px;
}
#body {
     background: url(images/body_bg_1.gif) repeat-y;
     width: 832px;
}
#bodyi {
     background: url(images/body_top_1.gif) no-repeat;
     width: 832px;
}
#bodyj {
     background: url(images/body_bot_1.gif) bottom no-repeat;
     padding: 1px 0;
     }
#body2 {
     background: url(images/body_bg_2.gif) repeat-y;
     width: 832px;
}
#bodyi2 {
     background: url(images/body_top_2.gif) no-repeat;
     width: 832px;
}
#bodyj2 {
     background: url(images/body_bot_2.gif) bottom no-repeat;
     padding: 1px 0;
}
h1, h2, h3, #nav, #nav li {
    margin: 0; padding: 0;
}
#nav {
    font-size: 10px;
    position: absolute;
    right: 0;
    top: 12px;
    line-height: 1.2;
    padding-left: 120px;
}
#nav li {
    float: left;
    width: 108px;
    list-style: none;
    margin-left: 2px;
    border-bottom: 1px solid black;
}
#nav a {
    background: #738d09;
    color: #2e3901;
    font-weight: bold;
    text-decoration: none;
    display: block;
    text-align: center;
    padding: 1px 0;
}
#sidebar {
    float: left;
    width: 250px;
    padding-left: 4px;
}
#sidebar .content {
    padding-left: 24px;
}
#sidebar .content img {
    float: left;
    clear: left;
    margin: 5px 5px 5px 0;
}
#sidebar .divider {
    background: url(images/left_splitter.gif) center no-repeat;
    height: 5px;
    width: 169px;
}
#content {
    float: right;
    width: 462px;
}
#content1 {
    float: left;
    width: 800px;
}
#content1 .content {
    margin: 7px 35px 7px 20px;
    padding-left: 20px;
}
#content .content {
    margin: 7px 55px 7px 17px;
}
#content .content table {
    width: 310px;
    margin-right: 0px;
}
#content .content table td,
#content .content table th {
    padding-right: 10px;
}
#content .content table td.download {
    text-align: right;
    padding-right: 0px;
}
#content .divider {
    background: url(images/right_splitter.gif) repeat-x;
    height: 5px;
}
h1 {
    position: absolute;
    left: 0;
    top: 0;
}
h2 {
    font-size: 10px;
    color: #cf9118;
    margin: 1em 0;
}
h3 {
    font-size: 10px;
    margin: 1em 0;
}
h5 {
    font-size: 20px;
    color: #cf9118;
    margin: 1em 0;
    text-align: center;
}
h6 {
    font-size: 18px;
    margin: 1em 0;
}
p {
    margin: 1em 0;
}
img {
    border: 0;
}
img.left  { float: left; margin-right: 14px; }
img.right { float: right; margin-left: 14px; }
.readmore {
    text-align: right;
}
.hidden {
    visibility: hidden;
}
.clear {
    clear: both;
}
a {
    color: #f0b33c;
    font-weight: bold;
    text-decoration: none;
}
a:visited {
    color: #cf9118;
}
a:hover {
    text-decoration: underline;
}
table a {
    text-decoration: underline;
    font-weight: normal;
    color: #7f7c79;
}
#power {color:#fff;text-align:center;}
#power a {color:#fff;}

Any help's appreciated... Thanks in advance.

任何帮助表示赞赏......提前致谢。

回答by justisb

I think the simplest solution, rather than making the bodyelement partially transparent, would be to add an extra divto hold the background, and change the opacity there, instead.

我认为简单的解决方案不是让body元素部分透明,而是添加一个额外的东西div来保持背景,并改变那里的不透明度。

So you would add a divlike:

所以你会添加一个div喜欢:

<div id="background"></div>

And move your bodyelement's background CSS to it, as well as some additional positioning properties, like this:

并将body元素的 CSS 背景移动到它,以及一些额外的定位属性,如下所示:

#background {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('images/background.jpg');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: 100%;
    opacity: 0.8;
    filter:alpha(opacity=80);
}

Here's an example: http://jsfiddle.net/nbVg4/4/

这是一个例子:http: //jsfiddle.net/nbVg4/4/

回答by Charming Prince

I think what's happening, is that, since the wrapper id is relatively position, it just appears on the same position with the body tag, what you should do, is that you can add a Z-index to the wrapper id.

我认为发生的事情是,由于包装器 id 是相对位置,它只是出现在与 body 标签相同的位置,您应该做的是,您可以向包装器 id 添加 Z-index。

#wrapper {
margin: auto;
text-align: left;
width: 832px;
position: relative;
padding-top: 27px;
z-index: 99; /* added this line */
 }

This should make layers above the transparent body tag.

这应该在透明 body 标签上方创建图层。

回答by Charming Prince

I would agree with @evillinux, It would be best to make your background image semi transparent so it supports < ie8

我同意@evillinux,最好使您的背景图像半透明,以便它支持 < ie8

The other suggestions of using another div are also a great option, and it's the way to go if you want to do this in css. For example if the site had such features as selecting your own background color. I would suggest using a filter for older IE. eg:

使用另一个 div 的其他建议也是一个不错的选择,如果您想在 css 中执行此操作,这是一种可行的方法。例如,如果该站点具有选择您自己的背景颜色等功能。我建议对较旧的 IE 使用过滤器。例如:

filter:Alpha(opacity=50)

回答by evilunix

Make the background image transparent/semi-transparent. If it's a solid coloured background just create a 1px by 1px image in fireworks or whatever and adjust its opacity...

使背景图像透明/半透明。如果它是纯色背景,只需在烟花或其他任何东西中创建一个 1px x 1px 的图像并调整其不透明度......

回答by Gabe

background:rgba(0,0,0,0);
opacity:1;

回答by Gautam Siwach

Just include following in your code

只需在您的代码中包含以下内容

<body background="C:\Users\Desktop\images.jpg"> 

if you want to specify the size and opacity you can use following

如果要指定大小和不透明度,可以使用以下

<p><img style="opacity:0.9;" src="C:\Users\Desktop\images.jpg" width="300" height="231" alt="Image" /></p> 

回答by Matthew Vines

There is a css3 solution hereif that is acceptable. It supports the graceful degradationapproach where css3 isn't supported. you just won't have any transparency.

如果可以接受,这里有一个css3 解决方案。它支持不支持 css3的优雅降级方法。你只是不会有任何透明度。

body {
    font-family: tahoma, helvetica, arial, sans-serif;
    font-size: 12px;
    text-align: center;
    background: #000;
    color: #ddd4d4;
    padding-top: 12px;
    line-height: 2;
    background-image: url('images/background.jpg');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: 100%;
    background: rgb(0, 0, 0); /* for older browsers */
    background: rgba(0, 0, 0, 0.8); /* R, G, B, A */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#CC000000, endColorstr=#CC0000); /* AA, RR, GG, BB */

}

to get the hex equivalent of 80% (CC) take (pct / 100) * 255 and convert to hex.

获得相当于 80% (CC) 的十六进制取 (pct/100) * 255 并转换为十六进制。

回答by Matthew Vines

You probably want an extra wrapper. use a div for the background and position it below your content..

您可能需要一个额外的包装器。使用 div 作为背景并将其放置在您的内容下方..

http://jsfiddle.net/pixelass/42F2j/

http://jsfiddle.net/pixelass/42F2j/

HTML

HTML

<div id="background-image"></div>
<div id="content">
    Here is the content at opacity 1
    <img src="http://lorempixel.com/100/50/fashion/1/">

</div>

CSS

CSS

#background-image {
    background-image: url(http://lorempixel.com/400/200/sports/1/);
    opacity:0.4;
    position:absolute;
    top:0;
    left:0;
    height:200px;
    width:400px;
    z-index:0;
}
#content {
    z-index:1;
    position:relative;
}