Html+Css網頁設計(二)

Html+Css網頁設計(二)

本章概要

  • 學習雙欄佈局
  • 使用媒體查詢 @media 實現響應式佈局
  • 要在手機中瀏覽,字體自動變大,得添加 meta viewport
    <html lang="zh-Hant">
     <head>
       ...
       <meta name="viewport" content="width=device-width, initial-scale=1">
     </head>
    </html>
    

效果

PC端

首頁

News

手機端

要在手機中查看效果,可以在 Visual Code 中安裝插件 Live Server (如何安裝和操作見上一章),
只要手機和電腦在同一個局域網內就能用手機插件效果了。

首頁

News

繼續往下拉,側邊欄被移動到了下面

項目結構

首頁

index.html

沒做修改,與上一章一樣

<!DOCTYPE html>
<html lang="zh-Hant">

<head>
    <meta charset="UTF-8" />
    <title>WCB cofe</title>
    <meta name="description" content="提供綜合咖啡與健康有機食物的咖啡店" />
    <link rel="icon" type="image/png" href="images/favicon.png"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://unpkg.com/ress/dist/ress.min.css" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/css?family=Philosopher" rel="stylesheet" />
    <link href="css/style.css" rel="stylesheet" />
    <link href="css/index.css" rel="stylesheet" />
</head>

<body>
    <div id="home" class="big-bg">
        <header class="page-header wrapper">
            <h1>
                <a href="index.html"><img src="images/logo.svg" alt="WCB cafe 首頁" class="logo" /></a>
            </h1>
            <nav>
                <ul class="main-nav">
                    <li><a href="news.html">News</a></li>
                    <li><a href="menu.html">Menu</a></li>
                    <li><a href="contact.html">Contact</a></li>
                </ul>
            </nav>
        </header>

        <div class="home-content wrapper">
            <h2 class="page-title">We'll Make Your Day</h2>
            <p>想不想待在時尚咖啡店裡放鬆身心?用無人工添加物的食材讓身體由內而外煥然一新。</p>
            <a class="button" href="menu.html">菜單介紹</a>
        </div><!-- /.home-content -->
    </div> <!-- / #home -->
</body>

</html>

index.css

新增 index.css,把index.html的css分離出來

#home {
    background-image: url(../images/main-bg.jpg);
    min-height: 100vh;
    /* background-image: linear-gradient(#c9ffbf,#ffafbd);
    background-color: #0bd;
    background-blend-mode: hard-light; */
}

.home-content {
    text-align: center;
    margin-top: 10%;
}

.home-content p {
    font-size: 1.125rem;
    margin: 10px 0 42px;
}

#home .page-title {
    text-transform: none;
}


/* 媒體查詢:手機版本
--------------------------------------------------*/
 /*600px 及其以下的界面使用的樣式*/
 @media (max-width: 600px) {
    /* HOME */
    .home-content {
        margin-top: 20%;
    }
}

News

news.html

<!DOCTYPE html>
<html lang="zh-Hant">

<head>
    <meta charset="UTF-8">
    <title>WCB Cafe - NEWS</title>
    <meta name="description" content="提供綜合咖啡與健康有機食物的咖啡店">
    <link rel="icon" href="images/favicon.png">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://unpkg.com/ress/dist/ress.min.css" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/css?family=Philosopher" rel="stylesheet" />
    <link href="css/style.css" rel="stylesheet" />
    <link href="css/news.css" rel="stylesheet" />
</head>

<body>
    <div id="news" class="big-bg">
        <header class="page-header wrapper">
            <h1>
                <a href="index.html"><img src="images/logo.svg" alt="WCB cafe - NEWS" class="logo" /></a>
            </h1>
            <nav>
                <ul class="main-nav">
                    <li><a href="news.html">News</a></li>
                    <li><a href="menu.html">Menu</a></li>
                    <li><a href="contact.html">Contact</a></li>
                </ul>
            </nav>
        </header>

        <div class="wrapper">
            <h2 class="page-title">News</h2>
        </div>
    </div><!-- /#news -->

    <div class="news-contents wrapper">
        <article>
            <header class="post-info">
                <h2 class="post-title">店內製作全面更新</h2>
                <p class="post-date">3/30 <span>2022</span></p>
                <p class="post-cat">類別: 店內介紹</p>
            </header>
            <img src="images/wall.jpg" alt="站內圖片">
            <p>
                WCB CAFE提供有益健康的自然食物,主要的特色是菜單選用了無人工添加物的食材。 
                請用好喝的綜合咖啡與健康的有機食物由體內開始療癒身心。
                WCB CAFE提供有益健康的自然食物,主要的特色是菜單選用了無人工添加物的食材。 
                請用好喝的綜合咖啡與健康的有機食物由體內開始療癒身心。
            </p>
            <p>
                WCB CAFE提供有益健康的自然食物,主要的特色是菜單選用了無人工添加物的食材。 
                請用好喝的綜合咖啡與健康的有機食物由體內開始療癒身心。
                WCB CAFE提供有益健康的自然食物,主要的特色是菜單選用了無人工添加物的食材。 
                請用好喝的綜合咖啡與健康的有機食物由體內開始療癒身心。
                WCB CAFE提供有益健康的自然食物,主要的特色是菜單選用了無人工添加物的食材。 
                請用好喝的綜合咖啡與健康的有機食物由體內開始療癒身心。
            </p>
            <p>WCB CAFE提供有益健康的自然食物。</p>
        </article>

        <aside>
            <h3 class="sub-title">品類</h3>
            <ul class="sub-menu">
                <li><a href="#">店內介紹</a></li>
                <li><a href="#">期間限定菜單</a></li>
                <li><a href="#">優惠活動</a></li>
                <li><a href="#">與顧客對話</a></li>
            </ul>

            <h3 class="sub-title">關於本店</h3>
            <p>
                WCB CAFE提供有益健康的自然食物,主要的特色是菜單選用了無人工添加物的食材。 
                請用好喝的綜合咖啡與健康的有機食物由體內開始療癒身心。
            </p>
        </aside>
    </div>
    <!--news.contents-->

    <footer>
        <div class="wrapper">
            <div class="copyright">
                <p><small> &copy; 2022 WCB Cafe</small> </p>
            </div>
        </div>
    </footer>
</body>

</html>

news.css

#news {
    background-image: url(../images/news-bg.jpg);
    height: 270px;
    margin-bottom: 40px;
}

#news .page-title {
    text-align: center;
}


/* news contents 
-----------------------------------------------------*/
.news-contents {
    display: flex;
    justify-content: space-between;
    margin-bottom: 50px;

}

/* 報道部分
-----------------------------------------------------*/
article {
    width: 74%;
}

.post-info {
    position: relative; /*爲了給 .post-date 在配置位置時有個的定位點*/
    padding-top: 4px;
    margin-bottom: 40px;
}

.post-date {
    background: #0bd;
    border-radius: 50%;
    color: #fff;
    width: 100px;
    height: 100px;
    font-size: 1.625rem;
    text-align: center;
    position: absolute; /*設置基於.post-info的位置*/
    top: 0;
    padding-top: 10px;
}

.post-date span {
    font-size: 1rem;
    border-top: 1px rgba(255, 255, 255, .5) solid;
    padding-top: 6px;
    display: block;
    width: 60%;
    margin: 0 auto;
}

.post-title {
    font-family: "Yu Mincho", "YuMincho", serif;
    font-size: 2rem;
    font-weight: normal;

}

.post-title, .post-cat {
    margin-left: 120px;
}

article img {
   margin-bottom: 20px;
}

article p {
    margin-bottom: 1rem;
}

/* 邊欄
-----------------------------------------------------*/
aside {
    width: 22%;
}

.sub-title {
    font-size: 1.375rem;
    padding: 0 8px 8px;
    border-bottom: 2px #0bd solid;
    font-weight: normal;
}

.sub-menu {
    margin-bottom: 60px;
    list-style: none;
}

.sub-menu li {
    border-bottom: 1px #ddd solid;
}

.sub-menu a {
    color: #432;
    padding: 10px;
    display: block;
}

.sub-menu a:hover {
    color: #0bd;
}

aside p {
    padding: 12px 10px;
}
/* 尾部
-----------------------------------------------------*/
footer {
    text-align: center;
    padding: 26px 0;
}

footer p {
    color: #fff;
    font-size: 0.875rem;
}


/* 媒體查詢:手機版本
--------------------------------------------------*/
 /*600px 及其以下的界面使用的樣式*/
 @media (max-width: 600px) {
    /* NEWS */
    .news-contents {
        flex-direction: column;
    }
    
    article, aside {
        width: 100%;
    }

    .post-info {
        margin-bottom: 30px;
    }

    .post-date {
        width: 70px;
        height: 70px;
        font-size: 1rem;
    }
    .post-date span {
        font-size: 0.875rem;
        padding-top: 2px;
    }

    .post-title {
        font-size: 1.375rem;
    }

    .post-cat {
        font-size: 0.875rem;
        margin-top: 5px;
    }

    .post-title, .post-cat {
        margin-left: 80px;
    }
}

style.css

@charset "UTF-8";

/* 公共部分
---------------------------------------*/
html {
    font-size: 100%;  /* 根據瀏覽器預設的文字大小,設置相對值*/
   
}

body {
    font-family: "Yu Gothic Medium", YuGothic, sans-serif;
    line-height: 1.7;  /* 行高 */
    color: #432;
}

a {
    text-decoration: none;  /* 去掉底部的下劃線 */
}

img {
    max-width: 100%;  /* 寬度爲父元素的100% */
}

/* HEADER
------------------------------- */
.logo {
    width: 210px;
    margin-top: 14px;
}

.main-nav {
    display: flex;
    font-size: 1.25rem;
    text-transform: uppercase; /* 英文字母全部大寫*/
    margin-top: 36px;
    list-style: none;  /* 無列表圖標*/
}

.main-nav li {
    margin-left: 36px;
}

.main-nav a {
    color: #432;
}

.page-header {
    display: flex;
    justify-content: space-between; /* 水平對齊方式:左右對齊 */
}

.wrapper {
    max-width: 1100px; /* 設定最大寬度 */
    margin: 0 auto; /* 居中對齊 */
    padding: 0 4%; /* 設定內側留白空間,以配合窄界面*/
}

/*標題*/
.page-title {
    font-size: 5rem;
    font-family: 'Philosopher', serif;
    text-transform: uppercase;
    font-weight: normal;
}

/* 按鈕 */
.button {
    font-size: 1.375rem;
    background: #0bd;
    color: #fff;
    border-radius: 5px;
    padding: 18px 32px;
}
.button:hover {
    background: #0090aa;
}


.big-bg {
    background-size: cover; /* 圖片鋪面界面 */
    background-position: center top;
    background-repeat: no-repeat;
}


/* 媒體查詢:手機版本
--------------------------------------------------*/
 /*600px 及其以下的界面使用的樣式*/
@media (max-width: 600px) {
    .page-title {
        font-size: 2.5rem;
    }

    /* Header */
    .main-nav {
        font-size: 1rem;
        margin-top: 10px;
    }
    .main-nav li {
        margin: 0 20px;
    }

    .page-header {
        flex-direction: column;
        align-items: center;
    }
}

資源文件

News頁面: news-bg.jpg

文章中的圖片:wall.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章