圖片撐大到全螢幕

 

我想要將一幅圖片作全幅版面就是上下左右都不會有間隔出現

目前有三種做法

 

一、使用100vh值來表示該畫面100%的高度

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        img {
            height: 100vh;
            width: 100%;
        }

        body {
            margin: 0 0 0 0;
        }
    </style>
</head>
<body>
    <div>
        <a><img src="http://i.imgur.com/77tyOkH.jpg" title="my home page" /></a>
    </div>
    <div>
        <a><img src="http://i.imgur.com/77tyOkH.jpg" title="my home page" /></a>
    </div>
    <div>
        <a><img src="http://i.imgur.com/77tyOkH.jpg" title="my home page" /></a>
    </div>
</body>
</html>

JS Bin on jsbin.com

注意右邊會出現卷軸與下面會有一小行空白,

可以對body使用overflow:hidden;以取消卷軸,

但對於多幅圖案的捲動反而感覺正常。

 

二、使用div配合100vh與background-size:cover技術來撐大全圖

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        body {
            margin: 0 0 0 0;
        }

        div {
            height: 100vh;
            background: url(http://i.imgur.com/77tyOkH.jpg) no-repeat;
            background-size: cover;
        }
    </style>
</head>
<body>
    <div id="one">
    </div>
    <div id="two">
    </div>
    <div id="three">
    </div>
</body>
</html>

JS Bin on jsbin.com

這個Demo不會像第一個一樣圖片隨著瀏覽器寬度調整而變形,

反而是一比一呈現,超出畫面會截掉。

 

另外也有另一個做法使用對div tag的寬高撐大成100%,

但先決條件是該div所有上層tag的寬高都要設定100%才行。

 

以下兩項Demo我認為參考用就好

三、使用bootstrap framework搭配padding-right padding-left為0

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Template</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        img {
            height: 100vh;
            width: 100%;
        }

        .container-fluid {
            padding-left: 0px;
            padding-right: 0px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <a><img src="http://i.imgur.com/77tyOkH.jpg" title="my home page" /></a>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>

Bootstrap Template on jsbin.com

這個呈現的結果是最好的,但必須在Bootstrap底下。

 

四、對body使用background

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Template</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        body {
            background: url("http://i.imgur.com/77tyOkH.jpg") no-repeat center center;
            background-size: cover;
            height: 100%;
        }
    </style>

</head>
<body>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>

Bootstrap Template on jsbin.com

這個效果也不錯,須注意因圖片比例的關係會忽略外圍圖邊,

另外他也必須在Bootstrap theme底下才能呈現。

 

參考資料:

CSS3 background-size Property

Full screen background cover page

Create Fullscreen HTML5 Page Background Video

bootsnipp