Less-nested-rules

提供:Dev Guides
移動先:案内検索

LESS-ネストされたルール

説明

これは、あるクラスのプロパティを別のクラスに使用できるようにするCSSプロパティのグループであり、そのプロパティとしてクラス名が含まれています。 LESSでは、クラスまたはIDセレクターを使用してCSSスタイルと同じ方法でmixinを宣言できます。 複数の値を保存でき、必要に応じてコードで再利用できます。

次の例は、LESSファイルでのネストされたルールの使用を示しています-

<html>
   <head>
      <title>Nested Rules</title>
      <link rel = "stylesheet" type = "text/css" href = "style.css"/>
   </head>

   <body>
      <div class = "container">
         <h1>First Heading</h1>
         <p>LESS is a dynamic style sheet language that extends the capability of CSS.</p>
         <div class = "myclass">
            <h1>Second Heading</h1>
            <p>LESS enables customizable, manageable and reusable style sheet for web site.</p>
         </div>
      </div>
   </body>
</html>

次に、_style.less_ファイルを作成します。

style.less

.container {
   h1 {
      font-size: 25px;
      color:#E45456;
   }
   p {
      font-size: 25px;
      color:#3C7949;
   }

   .myclass {
      h1 {
         font-size: 25px;
         color:#E45456;
      }
      p {
         font-size: 25px;
         color:#3C7949;
      }
   }
}

次のコマンドを使用して、_style.less_ファイルを_style.css_にコンパイルできます-

lessc style.less style.css

上記のコマンドを実行します。それは次のコードで自動的に_style.css_ファイルを作成します-

style.css

.container h1 {
   font-size: 25px;
   color: #E45456;
}

.container p {
   font-size: 25px;
   color: #3C7949;
}

.container .myclass h1 {
   font-size: 25px;
   color: #E45456;
}

.container .myclass p {
   font-size: 25px;
   color: #3C7949;
}

出力

上記のコードがどのように機能するかを確認するには、次の手順に従ってください-

  • 上記のHTMLコードを nested_rulesl ファイルに保存します。
  • このHTMLファイルをブラウザで開くと、次の出力が表示されます。

ネストされていないルール