HTML5 and CSS: 31-40

31 - Use HTML5 to Require a Field

  • 使用這個 input required 屬性,使用者就不能以空白提交資料

<form action="/submit-cat-photo">
  <input type="text" placeholder="cat photo URL" required>
  <button type="submit">Submit</button>  
</form>

Submit

32 - Create a Set of Radio Buttons

  • radio button 單選按鈕

  • 單選按鈕只是input輸入框的一種類型。

  • 每一個單選按鈕都應該嵌套在它自己的label(標簽)元素中。

  • 注意:所有關聯的單選按鈕應該使用相同的name屬性。

  • 這一題我覺得怪怪的 orz

參考資料

應該 <label></label> 像是單純顯示的文字,但和 input 又有點關係。看其他的範例,都是 label 裡面要再設定 for=""。因此我推測,如果將 input 放到 label 裡面 nesting,就可以省去這些設定

<form action="/submit-cat-photo">

    <label> <input type="radio" name="indoor-outdoor"/>indoor</label>
    <label><input type="radio" name="indoor-outdoor">outdoor</label>

  <button type="submit">Submit</button>
</form>

33 - Create a Set of Checkboxes

<form action="/submit-cat-photo">
  <label><input type="checkbox" name="personality"> 內向</label>
  <label><input type="checkbox" name="personality"> 外向</label>
  <label><input type="checkbox" name="personality"> 神經</label>
  <input type="text" placeholder="cat photo URL" required>
  <button type="submit">Submit</button>
</form>

34 - Check Radio Buttons and Checkboxes by Default

  • 在 input 新增 checked 屬性,就會默認被選中

    <form action="/submit-cat-photo">
    <label><input type="radio" name="indoor-outdoor" checked> Indoor</label>
    <label><input type="radio" name="indoor-outdoor"> Outdoor</label>
    <label><input type="checkbox" name="personality" checked> Loving</label>
    <label><input type="checkbox" name="personality"> Lazy</label>
    <label><input type="checkbox" name="personality"> Energetic</label>
    <input type="text" placeholder="cat photo URL" required>
    <button type="submit">Submit</button>
    </form>

35 - Nest Many Elements within a Single Div Element

  • div 竟然這麼後面才提到 @@

  • 主要用途是,包裹內容用的容器,方便之後排版

<div>
  <p>Things cats love:</p>
  <ul>
    <li>cat nip</li>
    <li>laser pointers</li>
    <li>lasagna</li>
  </ul>
  <p>Top 3 things cats hate:</p>
  <ol>
    <li>flea treatment</li>
    <li>thunder</li>
    <li>other cats</li>
  </ol>
</div>

36 - Give a Background Color to a Div Element

<style>
  .silver-background {
    background-color: silver;
  }
</style>

<div class="silver-background">
  <p >Things cats love:</p>
  <ul>
    <li>cat nip</li>
    <li>laser pointers</li>
    <li>lasagna</li>
  </ul>
  <p>Top 3 things cats hate:</p>
  <ol>
    <li>flea treatment</li>
    <li>thunder</li>
    <li>other cats</li>
  </ol>
</div>

37 - Set the ID of an Element

  • id 在 html 元素中是唯一的

  • <h2 id="cat-photo-form">

38 - Use an ID Attribute to Style an Element

  • id 竟然這麼後面才說

<style>
  #cat-photo-form {
    background-color: green;
  }
</style>  

<h2 id="cat-photo-form">

39 - Adjusting the Padding of an Element

  • padding 內邊距

  • border 邊框

  • margin 外邊距

<style>
  .green-box {
    background-color: green;
    padding: 20px;
  }
</style>

40 - Adjust the Margin of an Element

<style>
  .injected-text {
    margin-bottom: -25px;
    text-align: center;
  }

  .box {
    border-style: solid;
    border-color: black;
    border-width: 5px;
    text-align: center;
  }

  .yellow-box {
    background-color: yellow;
    padding: 10px;
  }

  .red-box {
    background-color: red;
    padding: 20px;
    margin: 20px;
  }

  .green-box {
    background-color: green;
    padding: 20px;
    margin: 20px;
  }
</style>
<h5 class="injected-text">margin</h5>

<div class="box yellow-box">
  <h5 class="box red-box">padding</h5>
  <h5 class="box green-box">padding</h5>
</div>

.injected-text { margin-bottom: -25px; text-align: center; }

.box { border-style: solid; border-color: black; border-width: 5px; text-align: center; }

.yellow-box { background-color: yellow; padding: 10px; }

.red-box { background-color: red; padding: 20px; margin: 20px; }

.green-box { background-color: green; padding: 20px; margin: 20px; } </style>

margin

padding

padding

Last updated

Was this helpful?