0

CSS を使用して、次の HTMLのテキスト ( "Word 1 Word2 Word3 Word4" ) を画像に置き換えたいと考えています。テキストと同じように、画像も水平方向にインラインで表示したいと思います。

<div id="aSentence">
    <p>
        <span id="word1">Word1&nbsp </span>
        <span id="word2">Word2&nbsp </span>
        <span id="word3">Word3&nbsp </span>
        <span id="word4">Word4&nbsp </span>
    </p>                    
</div>

私はmezzoblueや他のいくつかのサイトからいくつかのテクニックを読んで試してみましたが、これらのテクニックでは、テキストのスパンが単語を作成する 1 つまたは別の要素タイプで囲まれている必要があるように思われるという結論に達しています。横に並ばない。

この目標を達成する方法についてのアイデアはありますか? (可能であれば)スクリーンリーダーなどにソリューションを「アクセス可能」にしたいと思います。

どうもありがとう、
プレンボ。

PS: いくつかの手法を実装しようとしましたが、どれも正しく機能しませんでした: TextReplacement.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title>Text Replacement by Image using CSS</title>
        <style type="text/css" title="currentStyle" media="screen">
            @import "TextReplacement.css";
        </style>
    </head>

    <body id="bodyId">
        <div id="centerContainer">
            <h1><span>Comparison of Various Techniques</span></h1>
            <p class="noteText">(NOTE: only text "Word1" is being replaced by image)</p>
            <div id="mainBodyContainer">
                <div class="techniqueContainer">
                    <h2>Technique 1: FIR</h2>
                    <ul id="t1List">
                        <li id="t1w1"><span>Word1.&nbsp;</span></li>
                        <li id="t1w2"><span>Word2.&nbsp; </span></li>
                        <li id="t1w3"><span>Word3.&nbsp; </span></li>
                        <li id="t1w4"><span>Word4.&nbsp; </span></li>
                    </ul>                    
                    <p class="techniqueComment">Word 1 and corresponding image disappear completely.</p>
                </div>

                <div class="techniqueContainer">
                    <h2>Technique 2: Radu</h2>
                    <ul id="t2List">
                        <li id="t2w1"><span>Word1.&nbsp;</span></li>
                        <li id="t2w2"><span>Word2.&nbsp; </span></li>
                        <li id="t2w3"><span>Word3.&nbsp; </span></li>
                        <li id="t2w4"><span>Word4.&nbsp; </span></li>
                    </ul>                    
                    <p class="techniqueComment">All disapppear completely.</p>
                </div>                

                <div class="techniqueContainer">
                    <h2>Technique 3: Rundle</h2>
                    <ul id="t3List">
                        <li id="t3w1"><span>Word1.&nbsp;</span></li>
                        <li id="t3w2"><span>Word2.&nbsp; </span></li>
                        <li id="t3w3"><span>Word3.&nbsp; </span></li>
                        <li id="t3w4"><span>Word4.&nbsp; </span></li>
                    </ul>                    
                    <p class="techniqueComment">No effect - image and text visible.</p>
                </div>    

                <div class="techniqueContainer">
                    <h2>Technique 4: Leahy/Langridge</h2>
                    <ul id="t4List">
                        <li id="t4w1"><span>Word1.&nbsp;</span></li>
                        <li id="t4w2"><span>Word2.&nbsp; </span></li>
                        <li id="t4w3"><span>Word3.&nbsp; </span></li>
                        <li id="t4w4"><span>Word4.&nbsp; </span></li>
                    </ul>                    
                    <p class="techniqueComment">Image appears above Word1.</p>
                </div>    

                <div class="techniqueContainer">
                    <h2>Technique 5: Dwyer</h2>
                    <ul id="t5List">
                        <li id="t5w1"><span>Word1.&nbsp;</span></li>
                        <li id="t5w2"><span>Word2.&nbsp; </span></li>
                        <li id="t5w3"><span>Word3.&nbsp; </span></li>
                        <li id="t5w4"><span>Word4.&nbsp; </span></li>
                    </ul>                    
                    <p class="techniqueComment">Word 1 and corresponding image disappear completely.  Same as Technique 1.</p>
                </div>                        
                <div class="techniqueContainer">
                    <h2>Technique 6: Jason</h2>
                    <div id="t6List" class="borderContainer">
                        <span id="t6w1">Word1.&nbsp; </span>
                        <span id="t6w2">Word2.&nbsp; </span>
                        <span id="t6w3">Word3.&nbsp; </span>
                        <span id="t6w4">Word4.&nbsp; </span>
                    </div>                    
                    <p class="techniqueComment">Same as Technique 3 - Rundle.</p>
                </div>     
            </div>
        </div>
    </body>
</html>

CSS ファイル: TextReplacement.css

* {
    margin: 0;
    padding: 0;
}

body {
    margin: 0;
    padding: 0; 
    font-family: verdana, "trebuchet MS", helvetica, sans-serif; 
}

#centerContainer {
    margin-left: auto;     /* centres container */
    margin-right: auto; /* centres container */ 
    margin-top: 18px;
}

#mainBodyContainer {
    width: 900px;
    margin-left: auto; 
    margin-right: auto; 
    margin-bottom: 20px;
    font-size: 1.8em;
    font-weight: bold;    
}

#mainBodyContainer h2{
    font-size: 1.2em;
    font-weight: bold;    
    font-style: italic;    
    color: green;
}

div.techniqueContainer{
    text-align: center;
    margin-bottom: 30px;    
}

p.techniqueComment{
    font-size: 0.5em;
    font-weight: normal;    
    font-style: italic;    
    color: red;
}

p.noteText{
    font-size: 1em;
    font-weight: normal;    
    font-style: italic;    
    color: blue;
}

#mainBodyContainer ul{
    list-style: none;
    border: 1px solid #820000;
}

#mainBodyContainer ul li{
    display: inline;
}

/* TECHNIQUE 1: FIR */
li#t1w1 {
    width: 250px;
    height: 61px;
    background-image: url(http://stackoverflow.com/content/img/so/logo.png);
}

li#t1w1 span {
    display: none;
}

/* TECHNIQUE 2: Radu */
li#t2w1 {
    width: 2250px;
    height: 61px;
    background: url(http://stackoverflow.com/content/img/so/logo.png) top right;
    margin: 0 0 0 -2000px;
}

/* TECHNIQUE 3: Rundle */
li#t3w1 {
    width: 250px;
    height: 61px;
    background: url(http://stackoverflow.com/content/img/so/logo.png);
    text-indent: -9999px;
}

/*TECHNIQUE 4: Leahy/Langridge. */
li#t4w1 {
    width: 250px;
    padding: 61px 0 0 0;
    height: 0;
    background: url(http://stackoverflow.com/content/img/so/logo.png) no-repeat;
    overflow: hidden;
}

/*TECHNIQUE 5: Dwyer. */
#t5{

}

#t5List{
    list-style: none;
    border: 1px solid #820000;
}

#t5List li{
    display: inline;
}

li#t5w1 {
width: 250px;
    height: 61px;
    background-image: url(http://stackoverflow.com/content/img/so/logo.png);
}

li#t5w1 span{
    display: block;
    width: 0;
    height: 0;
    overflow: hidden;
}

/* TECHNIQUE 6: Jason */
li#t6w1 {

}

#t6w1{
    text-indent: -5000px; 
    background: url(http://stackoverflow.com/content/img/so/logo.png) no-repeat 0 0; 
    overflow: hidden;
}
4

8 に答える 8

4

それは簡単です:

span {text-indent: -5000px; background: url(../images/yourimage.jpg) no-repeat 0 0; overflow: hidden;}

やってみて。動作することがわかります。

何をするにしても、この効果を得るために JAVASCRIPT を使用しないでください。CSSで簡単にできる

順序なしリストを選択することもできます。

<ul>
  <li>word</li>
  ...
</ul>

ul {list-style-type: none; margin: 0; padding: 0;}
li {list-style-type: none; float: left;}
于 2009-05-21T08:59:09.647 に答える
4

HTML はそのままにして、テキストの代わりに画像を表示するということですか? もしそうなら、それは次のように行うことができます:

#word1 {
   display:block;
   width:100px; /* width of image */
   height:80px; /* height of image */
   background:url(image.jpg) top left no-repeat; /* sets image as background of span */
   text-indent:-999999px; /* this hides the text so the image is not obstructed */
}

テキストはそのままでSEOフレンドリーですが、派手な画像で印象的に見えることがあります.

于 2009-05-22T00:36:47.837 に答える
2

こんにちは、私はこの問題について 1 年前にテスト ページcca を用意しましたが、タグ スープが発生し、「画像がオフになっている」問題に直面する可能性があるため、興味があるだけでこれをリンクします。その純粋な CSS。

更新: 繰り返し画像用に設計されているため、これは 1 つの画像を置き換える手法ではありません。

于 2009-05-22T14:46:58.763 に答える
1

個人的には、余分なマークアップがなく、シンプルでエレガントなRundleテキストインデント手法が好きです。表示をインラインブロックに変更すると、機能します。インラインブロックをサポートしていないブラウザの場合は、次のいずれかの手法を使用してください:http: //blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/(コメントも確認してください) 、そこにいくつかのより簡単な提案があります)。私は@Jasonに同意します。これは、cssが有効で、画像が無効になっている人々をカバーするためだけに、このための複雑なソリューションを作成するのはおそらくやり過ぎです。

于 2010-11-20T16:59:55.703 に答える
1

あなたのオリジナルのアイデアは何ですか?何を達成したいですか?訪問者に画像を表示したい場合は、そのための適切なタグを使用して、単語を属性IMGに添付してください。ALT

それ以外の場合、これらの画像が実際にはコンテンツ画像ではなく、プレゼンテーション レイヤーの一部である場合は、間違ったマークアップを選択していると思います。SPAN意味的な意味はありません。好きなように設計できる順序なしリストを選択することをお勧めします。

編集

さて、あなたの回答によると、プレゼンテーション層でのソリューションが本当に必要です。text-shadow影の効果には、標準の CSS2 プロパティを選択できます。Safari、Opera、Konqueror で動作し、残りのブラウザーではJavaScriptを使用できます。

(SPANは、スタイルを適用できる単純なコンテナー要素です。)

于 2009-05-21T07:11:33.047 に答える
1

セマンティックな意味を持たない: スパンは (div と同様に) コンテンツに意味を追加しないため、目的に使用するのに適した要素である可能性があります。

スパンをブロックとして表示し、フロートします。

span {
  display: block;
  float: left;
}
于 2009-05-21T08:55:29.357 に答える
1

私が使用して大きな効果を上げたテクニックの 1 つは、基本的にテキストを表示可能領域の外に押し出すことです。名前があるかどうかはわかりません。常に調整が必要ですが、基本的には次のとおりです。

height: {someHeight};
line-height: {someHeight x 3};
overflow: hidden;
letter-spacing: -1.1em; /* roughly */

最初の行は要素の高さを残し、2 番目の行はその高さを超えてテキストを押し込み (フォント サイズによって異なりますが、x3 が適切な出発点です)、3 番目の行は表示から非表示にし、最後の行はテキストの幅を縮小します。

于 2009-05-21T09:00:11.423 に答える
-2

jQuery を使用して、関連する要素のテキストを画像に置き換えます。画像はインラインなので流れに沿っていきます。

于 2009-05-21T08:58:06.437 に答える