概要:このチュートリアルでは、styleプロパティを使用してHTML要素のインラインスタイルを操作する方法を学習します。
インラインスタイルの設定
要素のインラインスタイルを設定するには、その要素のstyleプロパティを使用します。
element.styleCode language: CSS (css)styleプロパティは、CSSプロパティのリストを含む読み取り専用のCSSStyleDeclarationオブジェクトを返します。たとえば、要素の色をredに設定するには、次のコードを使用します。
element.style.color = 'red';Code language: JavaScript (javascript)CSSプロパティにハイフン(-)が含まれる場合(例:-webkit-text-stroke)、配列のような表記([])を使用してプロパティにアクセスできます。
element.style.['-webkit-text-stock'] = 'unset';Code language: JavaScript (javascript)次の表は、一般的なCSSプロパティを示しています。
| CSS | JavaScript |
|---|---|
| background | background |
| background-attachment | backgroundAttachment |
| background-color | backgroundColor |
| background-image | backgroundImage |
| background-position | backgroundPosition |
| background-repeat | backgroundRepeat |
| border | border |
| border-bottom | borderBottom |
| border-bottom-color | borderBottomColor |
| border-bottom-style | borderBottomStyle |
| border-bottom-width | borderBottomWidth |
| border-color | borderColor |
| border-left | borderLeft |
| border-left-color | borderLeftColor |
| border-left-style | borderLeftStyle |
| border-left-width | borderLeftWidth |
| border-right | borderRight |
| border-right-color | borderRightColor |
| border-right-style | borderRightStyle |
| border-right-width | borderRightWidth |
| border-style | borderStyle |
| border-top | borderTop |
| border-top-color | borderTopColor |
| border-top-style | borderTopStyle |
| border-top-width | borderTopWidth |
| border-width | borderWidth |
| clear | clear |
| clip | clip |
| color | color |
| cursor | cursor |
| display | display |
| filter | filter |
| float | cssFloat |
| font | font |
| font-family | fontFamily |
| font-size | fontSize |
| font-variant | fontVariant |
| font-weight | fontWeight |
| height | height |
| left | left |
| letter-spacing | letterSpacing |
| line-height | lineHeight |
| list-style | listStyle |
| list-style-image | listStyleImage |
| list-style-position | listStylePosition |
| list-style-type | listStyleType |
| margin | margin |
| margin-bottom | marginBottom |
| margin-left | marginLeft |
| margin-right | marginRight |
| margin-top | marginTop |
| overflow | overflow |
| padding | padding |
| padding-bottom | paddingBottom |
| padding-left | paddingLeft |
| padding-right | paddingRight |
| padding-top | paddingTop |
| page-break-after | pageBreakAfter |
| page-break-before | pageBreakBefore |
| position | position |
| stroke-dasharray | strokeDasharray |
| stroke-dashoffset | strokeDashoffset |
| stroke-width | strokeWidth |
| text-align | textAlign |
| text-decoration | textDecoration |
| text-indent | textIndent |
| text-transform | textTransform |
| top | top |
| vertical-align | verticalAlign |
| visibility | visibility |
| width | width |
| z-index | zIndex |
既存のインラインスタイルを完全に上書きするには、styleオブジェクトのcssTextプロパティを設定します。例えば
element.style.cssText = 'color:red;background-color:yellow';
Code language: JavaScript (javascript)または、setAttribute()メソッドを使用することもできます。
element.setAttribute('style','color:red;background-color:yellow');Code language: JavaScript (javascript)インラインスタイルを設定したら、1つ以上のCSSプロパティを変更できます。
element.style.color = 'blue';Code language: JavaScript (javascript)既存のCSSプロパティを完全に上書きしたくない場合は、次のように新しいCSSプロパティをcssTextに連結できます。
element.style.cssText += 'color:red;background-color:yellow';Code language: JavaScript (javascript)この場合、+=演算子は新しいスタイル文字列を既存の文字列に追加します。
次のcss()ヘルパー関数は、キーと値のペアのオブジェクトから要素の複数のスタイルを設定するために使用されます。
function css(e, styles) {
for (const property in styles)
e.style[property] = styles[property];
}Code language: JavaScript (javascript)このcss()関数は、idが#contentの要素に対して複数のスタイルを設定するために使用できます。
let content = document.querySelector('#content');
css(content, { background: 'yellow', border: 'solid 1px red'});Code language: JavaScript (javascript)次の例では、styleオブジェクトを使用して、idがcontentの段落のCSSプロパティを設定しています。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Style Demo</title>
</head>
<body>
<p id="content">JavaScript Setting Style Demo!</p>
<script>
let p = document.querySelector('#content');
p.style.color = 'red';
p.style.fontWeight = 'bold';
</script>
</body>
</html>Code language: HTML, XML (xml)仕組み
- まず、
querySelector()メソッドを使用して、idがcontentである段落要素を選択します。 - 次に、
styleオブジェクトのcolorプロパティとfontWeightプロパティを設定することで、段落の色とフォントウェイトプロパティを設定します。
インラインスタイルの取得
styleプロパティは、要素のインラインスタイルを返します。styleプロパティは、外部スタイルシートなどの他の場所からのルールを返さないため、実際にはあまり役に立ちません。
要素に適用されているすべてのスタイルを取得するには、window.getComputedStyle()メソッドを使用する必要があります。
まとめ
element.styleオブジェクトのプロパティを使用して、HTML要素のインラインCSSプロパティを設定します。