CSSスタイルシートの基本概念と実装手法

CSSの記述方法と適用方式

インラインスタイルの実装

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>インラインスタイル例</title>
</head>
<body>
    <section style="height: 250px; width: 250px; background: #ff6b6b;">
        <h2>セクション1</h2>
    </section>
    
    <section>
        <h2>セクション2</h2>
    </section>
</body>
</html>

内部スタイルシートの利用

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>内部スタイル例</title>
    <style>
        section {
            height: 280px;
            width: 280px;
            background: #4ecdc4;
            color: #2d3436;
        }
    </style>
</head>
<body>
    <section>
        <h2>セクションA</h2>
    </section>
    
    <section>
        <h2>セクションB</h2>
    </section>
</body>
</html>

外部スタイルシートのリンク

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>外部スタイル例</title>
    <link rel="stylesheet" href="styles/main.css">
</head>
<body>
    <section>
        <h2>セクションX</h2>
    </section>
    
    <section>
        <h2>セクションY</h2>
    </section>
</body>
</html>

main.cssファイルの内容

section {
    height: 400px;
    width: 400px;
    background: #ffe66d;
    color: #2d3436;
}

CSSセレクタの種類と使用方法

要素セレクタ

<style>
    article {
        height: 320px;
        width: 320px;
        background: #ff7675;
    }
</style>

<article><h3>記事1</h3></article>
<article><h3>記事2</h3></article>

IDセレクタ

<style>
    #specialBox {
        height: 350px;
        width: 350px;
        background: #74b9ff;
        color: #ffffff;
    }
</style>

<article id="specialBox"><h3>特別ボックス</h3></article>

クラスセレクタ

<style>
    .highlight {
        height: 300px;
        width: 300px;
        background: #a29bfe;
    }
</style>

<article class="highlight"><h3>ハイライト1</h3></article>
<article class="highlight"><h3>ハイライト2</h3></article>

複数セレクタの競合解決

<style>
    article {
        height: 300px;
        width: 300px;
        background: #2d3436;
    }
    
    .featured {
        background: #fdcb6e;
    }
    
    #uniqueItem {
        background: #e17055;
    }
</style>

<article id="uniqueItem" class="featured"><h3>優先アイテム</h3></article>

属性セレクタ

<style>
    div[data-position="left"] {
        background: #fdcb6e;
    }
    div[data-position="right"] {
        background: #0984e3;
    }
</style>

<div data-position="left">左配置</div>
<div data-position="right">右配置</div>

疑似クラスセレクタ

<style>
    .nav-link {
        text-decoration: none;
    }
    .nav-link:link {
        color: #00b894;
    }
    .nav-link:hover {
        color: #2d3436;
        text-decoration: underline;
    }
    .nav-link:active {
        color: #636e72;
    }
    .nav-link:visited {
        color: #6c5ce7;
    }
</style>

<a class="nav-link" href="#">ナビゲーションリンク</a>

子孫セレクタ

<style>
    #container .link-item {
        color: #d63031;
    }
</style>

<nav id="container">
    <a class="link-item">内部リンク</a>
</nav>
<a class="link-item">外部リンク</a>

CSSプロパティの詳細設定

テキスト関連プロパティ

<style>
    .text-content {
        background: #81ecec;
        font-family: "メイリオ", sans-serif;
        font-size: 24px;
        color: #e84393;
        text-decoration: none;
        text-align: center;
        font-weight: bold;
        line-height: 60px;
    }
</style>

背景設定

<style>
    .page-background {
        background-color: #fd79a8;
        background-image: url('images/pattern.png');
        background-repeat: repeat-x;
    }
</style>

リストスタイル

<style>
    .custom-list li {
        list-style-type: none;
        list-style-image: url('images/icon.png');
    }
</style>

<ul class="custom-list">
    <li>項目1</li>
    <li>項目2</li>
</ul>

表示モードの変更

<style>
    .inline-block {
        height: 200px;
        width: 200px;
        background: #e17055;
        display: inline-block;
    }
    
    .horizontal-list li {
        list-style-type: none;
        display: inline;
    }
</style>

フロートレイアウト

<style>
    .float-left {
        float: left;
        width: 250px;
        height: 250px;
        background: #00cec9;
    }
    
    .float-right {
        float: right;
        width: 250px;
        height: 250px;
        background: #fab1a0;
    }
    
    .clear-float {
        clear: both;
    }
</style>

ボックスモデルの理解

パディングとボーダー

<style>
    .box-model {
        width: 280px;
        height: 280px;
        background: #55efc4;
        padding: 40px;
        border: 3px solid #e84393;
        margin: 20px;
    }
</style>

角丸ボーダー

<style>
    .rounded-corners {
        border-radius: 25px;
    }
    
    .circle {
        border-radius: 50%;
    }
    
    .semi-circle-top {
        border-radius: 200px 200px 0 0;
    }
</style>

ボックスシャドウ

<style>
    .shadow-box {
        box-shadow: 5px 5px 15px rgba(0,0,0,0.3);
    }
</style>

CSSポジショニング

相対位置指定

<style>
    .relative-pos {
        position: relative;
        left: 80px;
        top: 30px;
    }
</style>

絶対位置指定

<style>
    .absolute-pos {
        position: absolute;
        left: 150px;
        top: 200px;
    }
</style>

固定位置指定

<style>
    .fixed-banner {
        position: fixed;
        top: 0;
        width: 100%;
        background: #fd79a8;
    }
</style>

重なり順の制御

<style>
    .layer-1 { z-index: 10; }
    .layer-2 { z-index: 5; }
    .layer-3 { z-index: 1; }
</style>

透明度の設定

<style>
    .transparent-element {
        opacity: 0.7;
    }
</style>

タグ: CSS スタイルシート セレクタ ボックスモデル ポジショニング

10月16日 12:23 投稿