2016-09-09 34 views
0

我創建了一個網頁,只在頁腳上顯示背景圖片和文字,這對桌面用戶訪問者非常有用,但對於移動用戶訪問者而言,圖像不會自動調整爲屏幕大小。基於手機或臺式機的背景圖像

這裏,這是我的CSS

body { 
    margin-top: 50px; 
    margin-bottom: 50px; 
    background: none; 
} 

.full { 
    background: url('../1_v3.png') no-repeat center center fixed; 
    -webkit-background-size: cover; 
    -moz-background-size: cover; 
    -o-background-size: cover; 
    background-size:cover; 
} 
.navbar-header { 
    float: left; 
    padding: 15px; 
    text-align: center; 
    width: 100%; 
} 
.navbar-brand {float:none;} 

這是HTML文件:

<!DOCTYPE html> 
<html class="full" lang="en"> 
<head> 

    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 

    <title>TEXT HERE/title> 

    <!-- Bootstrap Core CSS --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 

    <!-- Custom CSS --> 
    <link href="css/the-big-picture.css" rel="stylesheet"> 

    <!-- 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/libs/html5shiv/3.7.0/html5shiv.js"></script> 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> 
    <![endif]--> 

</head> 

<body> 

    <!-- Navigation --> 
    <nav class="navbar navbar-inverse navbar-fixed-bottom" role="navigation"> 

     <div class="container"> 
      <!-- Brand and toggle get grouped for better mobile display --> 
      <div class="navbar-header"> 
       <a class="navbar-brand"> TEXT HERE</a> 
      </div> 
      <!-- Collect the nav links, forms, and other content for toggling --> 
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
      </div> 
      <!-- /.navbar-collapse --> 
     </div> 
     <!-- /.container --> 
    </nav> 
    <!-- /.container --> 

    <!-- jQuery --> 
    <script src="js/jquery.js"></script> 

    <!-- Bootstrap Core JavaScript --> 
    <script src="js/bootstrap.min.js"></script> 

</body> 

</html> 

是否有任何CSS技巧來調整基於用戶設備屏幕大小的背景圖像?

P/S:我的形象是尺寸2536x1420分辨率

+0

你的背景大小有含有蓋的,而不是! –

+0

opps @RudiUrbanek,問題一與蓋標籤 –

回答

1

在媒體查詢,您可以指定背景位置,這樣,你想要的圖像顯示。

這裏是一個工作示例http://hubenterprises.com.mx/stackOverflow/index.html

隨意在我的網頁檢查員發揮它。

或者這裏是你的代碼(在Chrome瀏覽)

<html> 
<head> 
<link rel="stylesheet" type="text/css" href="index.css"> 
</head> 
<body> 
    <div class='stretch'> 

    </div> 
</body> 
</html> 

CSS

.stretch { 
    background-image: url("http://hubenterprises.com.mx/github/bg.jpg"); 
    background-repeat: no-repeat; 
    background-size: cover; 
    background-position: 100% 100%; 
    width: 100vw; 
    height: 100vh; 
    outline: 1px solid; 
} 
body { 
    margin: 0; 
} 

@media only screen and (max-width: 500px) { 
    .stretch { 
     background-position:50% 50% ; 
    } 
} 
1
  1. 從不使用HTML元素的樣式什麼
  2. 申請保證金,以身體來移動你內容不可推薦
  3. 如果您將100%的寬度應用於元素,並且填充不要忘記添加(盒大小:邊界盒;)造型

body { 
 
    margin:0; 
 
    background: url('https://pixabay.com/static/uploads/photo/2016/01/12/19/16/painting-1136443_1280.jpg') no-repeat center center fixed; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
} 
 
.navbar-header { 
 
    float: left; 
 
    padding: 15px; 
 
    text-align: center; 
 
    width: 100%; 
 
    background: rgba(255,255,255,0.4); 
 
    box-sizing: border-box; 
 
} 
 
.navbar-brand { 
 
    float: none; 
 
}
<!-- Navigation --> 
 
<nav class="navbar navbar-inverse navbar-fixed-bottom" role="navigation"> 
 

 
    <div class="container"> 
 
    <!-- Brand and toggle get grouped for better mobile display --> 
 
    <div class="navbar-header"> 
 
     <a class="navbar-brand"> TEXT HERE</a> 
 
    </div> 
 
    <!-- Collect the nav links, forms, and other content for toggling --> 
 
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
 
    </div> 
 
    <!-- /.navbar-collapse --> 
 
    </div> 
 
    <!-- /.container --> 
 
</nav> 
 
<!-- /.container --> 
 

 
<!-- jQuery --> 
 
<script src="js/jquery.js"></script> 
 

 
<!-- Bootstrap Core JavaScript --> 
 
<script src="js/bootstrap.min.js"></script>