2009-07-28 3 views

答えて

62

あなたの相対的位置要素の内側に絶対位置要素持つことができます。

<div id="container"> 
    <div id="background"> 
    Text to have as background 
    </div> 
    Normal contents 
</div> 

をそしてCSS:

#container { 
    position: relative; 
} 

#background { 
    position: absolute; 
    top: 0; 
    left: 0; 
    bottom: 0; 
    right: 0; 
    z-index: -1; 
    overflow: hidden; 
} 

はここan example of itです。

+4

がelementName:{ \tコンテンツ後: "\ BB"。 \t float:right; } – Jazzy

1

bgテキストを含む要素のスタッキング順序(z-インデックス、位置)を低くし、不透明度を設定することもできます。したがって、上に必要な要素は、より高い積み重ね順序(z-index:5; position:relative; exの場合)を必要とし、背後の要素は何か低い値を必要とします(デフォルトまたは3やposition:relative ;)。

34

それは使用してのみCSSで可能な(しかし非常にハック)であってもよい:擬似要素の後に::前または

<style type="text/css"> 
    .bgtext { 
    position: relative; 
    } 
    .bgtext:after { 
    content: "Background text"; 
    position: absolute; 
    top: 0; 
    left: 0; 
    z-index: -1; 
    } 
</style> 

<div class="bgtext"> 
    Foreground text 
</div> 

これが動作しているようですが、あなたはおそらくそれを少し微調整する必要があります。また、それは:afterをサポートしていないので、IE6では動作しません。

+0

更新:現代版のブラウザーはすべて擬似要素をサポートしています。たとえば、FontAwesomeがCSSアイコンのためにどのように動作するか(インライン要素のbefore:を使用)です。 –

82

SVGテキストの背景画像

body { 
    background-image:url("data:image/svg+xml;utf8, 
    <svg xmlns='http://www.w3.org/2000/svg' version='1.1' 
     height='50px' width='120px'> 
    <text x='0' y='15' fill='red' font-size='20'>I love SVG!</text> 
    </svg>"); 
} 

ないか確認してください:あなたは(これは、あなたが単一のライナーSVGを使用するない作業必要ない)よりよく理解できるように、CSSの

body { 
 
    background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='50px' width='120px'><text x='0' y='15' fill='red' font-size='20'>I love SVG!</text></svg>"); 
 
}
<p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p> 
 
<p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p><p>I hate SVG!</p>

インデントバージョンポータブルです(これはFirefox 31とChrome 36で動作します)、技術的には画像ですが、ソースはインラインでプレーンテキストであり、無限に拡大します。

@senectusは、あなたがそれをコード化するbase64であれば、それはIEでうまく機能することを発見:テキストを含むSVGデータURIの背景についてhttps://stackoverflow.com/a/25593531/895245

+0

興味深い。私はこれをFirefox 31では動作させることしかできませんでしたが、Chrome 36やSafari 7では動作させることができませんでした。 –

+0

@JPRichardson True、ここで同じです。クローム36では、背景はそこにあるという印象を持っていますが、非常に小さな文字になります。たぶん、背景/ SVGサイズのパラメータを設定するのを忘れているのでしょうか? –

+0

ええと、私は現時点でそれを試しています...おそらく "viewBox"のように見えますか?私はまだそれを使いこなしている。 –

13

チロのソリューションは非常に賢いです。

ただし、単純なSVGソースをデータURIに追加するだけでIEでは動作しません。

これを回避してIE9以降で動作させるには、SVGをbase64にエンコードします。 This is a great tool.

だから、この:

background:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"><text x="5%" y="5%" font-size="30" fill="red">I love SVG!</text></svg>'); 

はこのようになります。

background:url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjx0ZXh0IHg9IjUlIiB5PSI1JSIgZm9udC1zaXplPSIzMCIgZmlsbD0icmVkIj5JIGxvdmUgU1ZHITwvdGV4dD48L3N2Zz4='); 

はテストの結果、それがIE9-10-11、WebKitの(クロム37、オペラ23)とのGecko(Firefoxの31)で動作します。

http://jsfiddle.net/qapp5dLn/

+0

これは良いツールです:https://jpillora.com/base64-encoder/ simpile、エラーなし、自動入力、画像プレビューうん、それは飛躍的に優れている。 –

1

@Ciro

あなたはバックスラッシュクローム54とFirefox 50

で以下のコード
body { 
    background: transparent; 
    background-attachment:fixed; 
    background-image: url(
    "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='170px' height='50px'> \ 
    <rect x='0' y='0' width='170' height='50' style='stroke:white; fill:gray; stroke-opacity: 0.3; stroke-width: 3px; fill-opacity: 0.7; stroke-dasharray: 10 5; stroke-linecap=round; '/> \ 
    <text x='85' y='30' style='fill:lightBlue; text-anchor: middle' font-size='16' transform='rotate(10,85,25)'>I love SVG!</text></svg>"); 
} 

Iでテスト"\"

とインラインSVGコードを破ることができますテスト済みでさえ、

background-image: url("\ 
data:image/svg+xml;utf8, \ 
    <svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='170px' height='50px'> \ 
    <rect x='0' y='0' width='170' height='50'\ 
     style='stroke:white; stroke-width: 3px; stroke-opacity: 0.3; \ 
      stroke-dasharray: 10 5; stroke-linecap=round; \ 
      fill:gray; fill-opacity: 0.7; '/> \ 
    <text x='85' y='30' \ 
     style='fill:lightBlue; text-anchor: middle' font-size='16' \ 
     transform='rotate(10,85,25)'> \ 
     I love SVG! \ 
    </text> \ 
    </svg>\ 
"); 

、それが動作します(少なくともChromeで54 & Firefoxの50 ==> NWjs &電子guarenteedでの使用)、純粋なCSSの使用

0

(ただし、まれにこれを使用し、HTML方式理由はるか優先されています。

.container{ 
 
\t position:relative; 
 
} 
 
.container::before{ 
 
\t content:""; 
 
\t width: 100%; height: 100%; position: absolute; background: black; opacity: 0.3; z-index: 1; top: 0; left: 0; 
 
\t background: black; 
 
} 
 
.container::after{ 
 
\t content: "Your Text"; position: absolute; top: 0; left: 0; bottom: 0; right: 0; z-index: 3; overflow: hidden; font-size: 2em; color: red; text-align: center; text-shadow: 0px 0px 5px black; background: #0a0a0a8c; padding: 5px; 
 
\t animation-name: blinking; 
 
\t animation-duration: 1s; 
 
\t animation-iteration-count: infinite; 
 
\t animation-direction: alternate; 
 
} 
 
@keyframes blinking { 
 
\t 0% {opacity: 0;} 
 
\t 100% {opacity: 1;} 
 
}
<div class="container">here is main content, text , <br/> images and other page details</div>