概要:このチュートリアルでは、styleプロパティを使用してHTML要素のインラインスタイルを操作する方法を学習します。
インラインスタイルの設定
要素のインラインスタイルを設定するには、その要素のstyle
プロパティを使用します。
element.style
Code 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プロパティを設定します。