基于移动或桌面的背景图像

时间:2016-09-09 03:23:15

标签: android jquery html css twitter-bootstrap

我创建的网页只在页脚上显示背景图片和文字,非常适合桌面用户访问者,但对于移动用户访问者而言,图片不会自动调整为屏幕大小。

这是我的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分辨率

2 个答案:

答案 0 :(得分: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)

  1. 从不使用html元素来设置任何内容
  2. 向身体申请保证金以移动您的内容是不可重复的
  3. 如果你对一个元素应用100%宽度并且还填充不要忘记添加(box-sizing:border-box;)样式
  4. 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>

相关问题