--- title: content slug: Web/CSS/content tags: - CSS - CSS プロパティ - CSS 生成コンテンツ - Reference translation_of: Web/CSS/content ---
CSS の content プロパティは、要素を生成された値で置き換えます。 content プロパティを使用して挿入されたオブジェクトは、無名の置換要素になります。
/* 他の値と組み合わせることができないキーワード */
content: normal;
content: none;
/* <image> 値 */
content: url("http://www.example.com/test.png");
content: linear-gradient(#e66465, #9198e5);
/* 生成コンテンツの代替テキスト、第3水準の仕様書で追加 */
content: url("http://www.example.com/test.png") / "This is the alt text";
/* 以下の値は ::before および ::after を使用して生成されたコンテンツにのみ適用 */
/* <string> 値 */
content: "prefix";
/* <counter> 値 */
content: counter(chapter_counter);
content: counters(section_counter, ".");
/* HTML 属性値にリンクした attr() 値 */
content: attr(value string);
/* 言語や位置に依存したキーワード */
content: open-quote;
content: close-quote;
content: no-open-quote;
content: no-close-quote;
/* normal と none を除き、複数の値が同時に使用可 */
content: open-quote chapter_counter;
/* グローバル値 */
content: inherit;
content: initial;
content: unset;
{{cssinfo}}
nonenormal:before および :after 擬似要素では none として計算されます。\000A9 は著作権記号を表します。{{cssxref("counter")}} 関数には、 'counter(名前)' または 'counter(名前, スタイル)' の二つの形式があります。生成されるテキストは、その擬似要素のスコープにおけるその名前の最も内側のカウンターです。指定されたスタイルで整形されます (decimal が既定値です)。
{{cssxref("counters")}} 関数も、 'counters(名前, 文字列)' または 'counters(名前, 文字列, スタイル)' の二つの形式があります。生成されるテキストは、その擬似要素のスコープにおけるその名前のすべてのカウンターの値であり、外側から内側に向けて、指定された文字列で区切られます。カウンターは指定されたスタイルで表示されます(decimal が既定値です)。
attr(x)open-quote | close-quoteno-open-quote | no-close-quoteこの例では引用部分の周りに引用符を挿入し、見出しの前に "Chapter" の語を追加します。
<h1>5</h1>
<p>According to Sir Tim Berners-Lee,
<q cite="http://www.w3.org/People/Berners-Lee/FAQ.html#Internet">I was
lucky enough to invent the Web at the time when the Internet
already existed - and had for a decade and a half.</q>
We must understand that there is nothing fundamentally wrong
with building on the contributions of others.
</p>
<h1>6</h1>
<p>According to the Mozilla Manifesto,
<q cite="http://www.mozilla.org/en-US/about/manifesto/">Individuals
must have the ability to shape the Internet and
their own experiences on the Internet.</q>
Therefore, we can infer that contributing to the open web
can protect our own individual experiences on it.
</p>
q {
color: blue;
}
q::before {
content: open-quote;
}
q::after {
content: close-quote;
}
h1::before {
content: "Chapter "; /* 最後の空白は、追加コンテンツと
残りのコンテンツの間を隔てる
ものです */
}
{{EmbedLiveSample('Headings_and_quotes', '100%', 200)}}
この例はリンクの前に画像を挿入します。画像が見つからなければ、代わりにテキストを挿入します。
<a href="http://www.mozilla.org/en-US/">Mozilla Home Page</a>
a::before {
content: url("https://mozorg.cdn.mozilla.net/media/img/favicon.ico") " MOZILLA: ";
font: x-small Arial, sans-serif;
color: gray;
}
{{EmbedLiveSample('Image_combined_with_text', '100%', 60)}}
この例はリストの特定の項目の後に追加のテキストを挿入します。
<h2>Paperback Best Sellers</h2> <ol> <li>Political Thriller</li> <li class="new-entry">Halloween Stories</li> <li>My Biography</li> <li class="new-entry">Vampire Romance</li> </ol>
.new-entry::after {
content: " New!"; /* 先頭の空白は、追加コンテンツと
残りのコンテンツの間を隔てる
ものです */
color: red;
}
{{EmbedLiveSample('Targeting_classes', '100%', 160)}}
この例はそれぞれのリンクの前に画像を挿入し、後に id 属性を追加します。
<ul>
<li><a id="moz" href="http://www.mozilla.org/">
Mozilla Home Page</a></li>
<li><a id="mdn" href="https://developer.mozilla.org/">
Mozilla Developer Network</a></li>
</ul>
a {
text-decoration: none;
border-bottom: 3px dotted navy;
}
a::after {
content: " (" attr(id) ")";
}
#moz::before {
content: url("https://mozorg.cdn.mozilla.net/media/img/favicon.ico") ;
}
#mdn::before {
content: url("https://mdn.mozillademos.org/files/7691/mdn-favicon16.png") ;
}
li {
margin: 1em;
}
{{EmbedLiveSample('Images_and_element_attributes', '100%', 160)}}
この例は要素の内容を画像で置き換えます。要素の内容を {{cssxref("<url>")}} または {{cssxref("<image>")}} の値のどちらかで置き換えることができます。 ::before または ::after で追加された内容は、要素の中身が置き換えられるときは生成されません。
<div id="replaced">Mozilla</div>
#replaced {
content: url("https://mdn.mozillademos.org/files/12668/MDN.svg");
}
#replaced::after { /* 要素の置換に対応している場合は表示されない */
content: " (" attr(id) ")";
}
{{EmbedLiveSample('Element_replacement', '100%', 200)}}
CSS の生成コンテンツは DOM に含まれません。そのため、アクセシビリティツリーに現れず、支援技術とブラウザーの組み合わせによってはアナウンスされません。コンテンツがページの目的を理解する上で重要な情報を持つのであれば、文書本体に含めた方が適切です。
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName("CSS3 Content", "#content-property", "content")}} | {{Spec2("CSS3 Content")}} | 代替テキストの対応を追加 |
| {{SpecName("CSS2.1", "generate.html#content", "content")}} | {{Spec2("CSS2.1")}} | 初回定義 |
このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 https://github.com/mdn/browser-compat-data をチェックアウトしてプルリクエストを送信してください。
{{Compat("css.properties.content")}}