0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

html,cssのこつ

0
Posted at

こつ

3つのポイント

1.インライン要素、ブロック要素
2.グルーピング
3.float

インライン要素ブロック要素

  • タグで見るとブロック要素もインライン要素も、縦に並んでいる
  • インライン要素は高さを持つことができない
<!DOCTYPE html>
<html>
  <head>
    <title>hello</title>
    <meta charset="utf-8">
  </head>
  <body>
    <h2>ブロック要素</h2>
      <p>hello</p>
      <p>hello</p>
      <p>hello</p>
    <h2>インライン要素</h2>
      <span>hello</span>
      <span>hello</span>
      <span>hello</span>
  </body>
</html>

表示結果

結果はブロック要素は縦に、インライン要素は横になる、
これらの特性を利用してレイアウトを作成していく

スクリーンショット 2020-01-11 13.34.18.png

グルーピング

特定の部分だけに適応したいcssは普通にあるその場合にグルーピングの考え方ができると良い

  • body要素の中にhtmlがただある状態

スクリーンショット 2020-01-11 13.34.18.png

  • グループ分けをしてみる

goup1のクラスがついたdiv要素で、くくってみる

<!DOCTYPE html>
<html>
  <head>
    <title>hello</title>
    <meta charset="utf-8">
    <style>
      .group1{
        padding: 20px;
      }
    </style>
  </head>
  <body>
  <div class="group1">
    <h2>ブロック要素</h2>
    <p>hello</p>
    <p>hello</p>
    <p>hello</p>
   </div>
    <h2>インライン要素</h2>
      <span>hello</span>
      <span>hello</span>
      <span>hello</span>
  </body>
</html>
  • 結果

group1のエリアだけにpadding適応される

スクリーンショット 2020-01-11 13.44.18.png

  • インライン要素も、ブロックの中に入れてあげる

<!DOCTYPE html>
<html>
  <head>
    <title>hello</title>
    <meta charset="utf-8">
    <style>
      .group1{
        padding: 20px;
      }
    </style>
  </head>
  <body>
  <div class="group1">
    <h2>ブロック要素</h2>
    <p>hello</p>
    <p>hello</p>
    <p>hello</p>
    <h2>インライン要素</h2>
      <span>hello</span>
      <span>hello</span>
      <span>hello</span>
   </div>
    
  </body>
</html>

両方に適応された

スクリーンショット 2020-01-11 13.49.41.png

float

ブロック要素を横並びにできる。

<!DOCTYPE html>
<html>
  <head>
    <title>hello</title>
    <meta charset="utf-8">
    <style>
      .group1{
        float: left;
      }
      .group2{
        float: left;
      }
    </style>
  </head>
  <body>
  <div class="group1">
    <h2>ブロック要素</h2>
    <p>hello</p>
    <p>hello</p>
    <p>hello</p>
   </div>
  <div class="group2">
    <h2>インライン要素</h2>
    <span>hello</span>
    <span>hello</span>
    <span>hello</span>
   </div>  
  </body>
</html>

横並びになった

スクリーンショット 2020-01-11 13.55.37.png

  • 問題
<!DOCTYPE html>
<html>
  <head>
    <title>hello</title>
    <meta charset="utf-8">
    <style>
      .group1{
        float: left;
        background-color:red;
      }
      .group2{
        float: left;
        background-color: blue;
      }
      .group3{
        background-color: green;
      }
    
    </style>
  </head>
  <body>
  <div class="group1">
    <h2>ブロック要素</h2>
    <p>hello</p>
    <p>hello</p>
    <p>hello</p>
   </div>
  <div class="group2">
    <h2>インライン要素</h2>
    <span>hello</span>
    <span>hello</span>
    <span>hello</span>
   </div>  
   <div class="group3">
      <h2>インライン要素</h2>
      <span>hello</span>
      <span>hello</span>
      <span>hello</span>
     </div>  
  </body>
</html>

緑色の要素がしたに入り込む

スクリーンショット 2020-01-11 13.59.58.png

  • 回り込みの解除
<!DOCTYPE html>
<html>
  <head>
    <title>hello</title>
    <meta charset="utf-8">
    <style>
      .group1{
        float: left;
        background-color:red;
      }
      .group2{
        float: left;
        background-color: blue;
      }
      .group3{
        background-color: green;
        clear: both;
      }
    
    </style>
  </head>

結果

スクリーンショット 2020-01-11 14.03.55.png

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?