【问题标题】:how do i fit background image to any screen?我如何使背景图像适合任何屏幕?
【发布时间】:2017-03-09 12:26:41
【问题描述】:

HTML

<!doctype html>

<html>

<head>
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Coming Soon</title>
  <link href="css/Main.css" rel="stylesheet">

</head>


<body>

  <section class="content">

    <div style="width:500px;
                    margin:0 auto;
                    top:25%;
                    position:relative">

      <img src="img/logo.png">
      <img src="img/line.png">
      <p class="header large-2 white padding-top-triple">Coming This Fall 2015</p>
      <p class="white padding-top"><span class="header">Email: </span>
        <a href="mailto:Jethwa96@hotmail.co.uk">
          <my-email data-user="Jethwa" data-domain="jedesigns.uk"></my-email>
        </a>
      </p>

    </div>

  </section>
</body>

</html>

CSS

/* Typography */

.header {
  font-family: "futura-pt", Arial, Helvetica, sans-serif;
  font-weight: 700;
  font-size: 1.0em;
  margin: 0;
  padding: 0;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
p {
  font-family: "futura-pt", Arial, Helvetica, sans-serif;
  font-weight: 400;
  font-size: 1.0em;
  margin: 0;
  padding: 0;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
/* Sizes */

.large-5 {
  font-size: 5.0em;
}
.large-4 {
  font-size: 4.0em;
}
.large-3 {
  font-size: 3.0em;
}
.large-25 {
  font-size: 2.5em;
}
.large-2 {
  font-size: 1.5em;
}
.large-15 {
  font-size: 1.3em;
}
@media screen and (min-width: 768px) {
  .large-5 {
    font-size: 5.0em;
  }
  .large-4 {
    font-size: 4.0em;
  }
  .large-3 {
    font-size: 3.0em;
  }
  .large-25 {
    font-size: 2.5em;
  }
  .large-2 {
    font-size: 2.0em;
  }
  .large-15 {
    font-size: 1.5em;
  }
}
/* Colours */

.white {
  color: #fff;
}
.black {
  color: #000;
}
/* Spacing */

.padding-top {
  padding-top: 2em;
}
.padding-top-double {
  padding-top: 2em;
}
.padding-top-triple {
  padding-top: 1em;
}
.padding-bottom {
  padding-bottom: 1em;
}
/* Links */

a:link,
a:visited,
a:active {
  color: #fff;
  text-decoration: none;
}
a:hover {
  color: #fff;
  text-decoration: underline;
}
/* General */

body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  text-align: center;
  background: #fff;
}
*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
/* Structure */

.content {
  width: 100%;
  position: relative;
  height: 1200px;
  margin: 0 auto;
  border: 3px solid #fff;
  background: url(http://jedesigns.uk/img/hd-sunset-river-HD-1200x1920.jpg) no-repeat;
}
.content img {
  max-width: 100%;
}
my-email::after {
  content: attr(data-domain);
}
my-email::before {
  content: attr(data-user)"\0040";
}

我需要知道如何使背景图像完全适合任何屏幕设备,以便它响应任何设备。

我尝试了很多方法,但都没有成功,所以希望堆栈溢出的人可以提供帮助:)

【问题讨论】:

  • 用户属性“background-size:cover”
  • 只需在 css 中执行 img{background-size:cover;}
  • 试过这个没用,

标签: html css


【解决方案1】:

您需要正确使用background-size:cover。这意味着基本上给你的.content 100% 的高度(并将其添加到包括html 在内的所有父母):

html, section {height:100%;}
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    text-align: center;
    background: #fff;
}

*, *:before, *:after {
  -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
 }

/* Structure */


.content{
    width: 100%;
    position: relative;
    height: 100%;
    margin: 0 auto;
    border: 3px solid #fff;
    background:url(http://jedesigns.uk/img/hd-sunset-river-HD-1200x1920.jpg) no-repeat;
    background-size:cover;
    background-position:center, bottom;
}

.content img {
   /* max-width: 100%;*/

}

我还删除了您添加的内联样式。 .content img 是错误的 css,因为您在 html 中没有任何 &lt;img&gt; 可供调用。

JSFIDDLE

【讨论】:

  • 非常感谢 :) 解决了问题
猜你喜欢
  • 2014-11-04
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-24
  • 2018-01-11
  • 2021-02-28
  • 2021-02-15
相关资源
最近更新 更多