CSSのbackground-clipプロパティとは?使い方とサンプルコードを解説
CSSのbackground-clipプロパティは、要素の背景(色や画像)をどの範囲まで描画するかを指定するためのプロパティです。ボーダー(枠線)の内側まで背景を表示するか、パディング領域までにするか、コンテンツ領域のみにするかなどを細かく制御できます。
background-clipで指定できる主な値
- border-box(初期値):ボーダー領域まで背景を描画します。
- padding-box:パディング領域まで背景を描画し、ボーダーの下には背景が表示されません。
- content-box:コンテンツ領域のみに背景を描画します。
- text:テキストの形に合わせて背景をクリップします(ベンダープレフィックス -webkit-background-clip: text; の併用が必要な場合があります)。
使用例
以下のコードでは、background-clip: padding-box; を指定することで、破線のボーダー部分に背景色が表示されない様子を確認できます。
<html>
<head>
<style>
#demo {
border: 5px dashed red;
padding: 10px;
background: orange;
background-clip: padding-box;
}
</style>
</head>
<body>
<div id="demo">
<h1>www.tutorialspoint.com</h1>
<p>Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more.</p>
</div>
</body>
</html>
この例では、オレンジ色の背景がパディング領域までしか描画されないため、赤い破線のボーダーと背景の間に隙間があるように見えます。background-clipの値を border-box や content-box に変更して、描画範囲の違いを実際に試してみると理解が深まります。
-
CSSのwhite-spaceプロパティとは?要素内の空白・改行の扱い方を徹底解説
CSSのwhite-spaceプロパティを使うと、HTML要素内の半角スペース、タブ、改行などの空白文字をどのように表示するかを細かく制御できます。デフォルトでは連続する空白は1つにまとめられますが、このプロパティを指定することで、ソースコード上の空白や改行をそのまま反映させたり、逆に折り返しを禁止したりすることが可能です。 white-spaceプロパティの基本例 以下は、normalとpreという2つの値を使って、空白の扱いの違いを比較するサンプルコードです。 <!DOCTYPE html> <html> <head> <style>
-
CSSのwriting-modeプロパティ徹底解説!テキストを縦書き・横書きにする方法
writing-modeプロパティとは CSSのwriting-modeプロパティは、テキストの行を水平方向にレイアウトするか、垂直方向にレイアウトするかを指定するためのプロパティです。日本語の縦書きのように、上から下へ文字を読ませるレイアウトも簡単に実現できます。 基本構文 writing-mode: horizontal-tb | vertical-rl | vertical-lr; 指定できる主な値 horizontal-tb … 行が上から下へ流れる標準的な横書き表示(初期値) vertical-rl … 行が右から左へ流れる縦書き表示。日本語や中国語などの縦組みレイアウトに適し