LoginSignup
1
0

labelとinputタグについて

Last updated at Posted at 2023-11-17

labelタグとinputタグは紐付けが必要

紐付け方法1: labalのfor属性とinputのid属性を同じにする

<label for="cheese"> 
    Do you like peas? 
</label> 
<input type="checkbox" name="cheese" id="cheese" /> 

メリット: cssで横並びにしやすい
デメリット: labalのfor属性とinputのid属性を指定する必要がある

紐付け方法2: labelタグでinputタグを囲む

<label> 
  Do you like peas? 
  <input type="checkbox" name="peas" /> 
</label> 

メリット: labalのfor属性とinputのid属性を指定する必要がない
デメリット: cssの組み方が限られてしまう(flexboxでの横並びができないなど)

参考: https://developer.mozilla.org/ja/docs/Web/HTML/Element/label

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0