完整的背景图片响应内部CSS

时间:2017-01-27 09:27:47

标签: html css image

我正在寻找一种方法来使整个背景图像响应,但使用内部CSS。我尝试了很多其他代码,但似乎没有任何工作(在Chrome和Firefox中测试过)。我拥有的断点是320px,480px,768px,1366px和1920px。这就是我在代码方面所拥有的。那么我应该如何标记这些媒体查询以给出相应宽度的背景图像?最重要的是,在设计图像时,应该选择合适的高度?



<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Naamloos document</title>
<style>

body {

  /* Location of the image */
 background-image: url(Version1/dr-muller-landing-page_1920x1080px.png);
   
  /* Image is centered vertically and horizontally at all times */
  background-position: center center;
  
  /* Image doesn't repeat */
  background-repeat: no-repeat;
  
  /* Makes the image fixed in the viewport so that it doesn't move when 
     the content height is greater than the image height */
  background-attachment: fixed;
  
  /* This is what makes the background image rescale based on its container's size */
  background-size: cover;
  
  /* Pick a solid background color that will be displayed while the background image is loading */
  background-color: rgb(236,146,189);
  
  /* SHORTHAND CSS NOTATION
   * background: url(background-photo.jpg) center center cover no-repeat fixed;
   */
}
  <style>
/* Three website */

* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}

.photo-grid {
	margin: 290px auto;
	margin-bottom: 0px;
	max-width: 1920px;
	text-align: center;
}

.photo-grid li {
	display: inline-block;
	margin: 15px;
	width: 500px;
	height: 500px;
	text-align: center;
}

.photo-grid img {
	display: block;
	max-height: 100%;
	max-width: 100%;
}

.photo-grid figure {
	height: 400px;
	overflow: hidden;
	position: relative;
	width: 400px;
}

.photo-grid figcaption {
	background: rgba(0,0,0,0.5);
	color: white;
	display: table;
	height: 100%;
	left: 0;
	opacity: 0;
	position: absolute;
	right: 0;
	top: 0;
	z-index: 100;
}

.photo-grid figcaption p {
	display: table-cell;
	font-size: 1.5em;
	position: relative;
	top: -40px;
	width: 500px;
	vertical-align: middle;
}


.photo-grid li:hover figcaption {
	opacity: 1;
}

.photo-grid img {
	display: block;
	height: 500px;
	width: 500px;
	-webkit-transition: all 300ms;
	-moz-transition: all 300ms;
	transition: all 300ms;
	max-width: 100%;
}

.photo-grid li:hover img {
	-webkit-transform: scale(1.4);
	-moz-transform: scale(1.4);
	transform: scale(1.4);
}

.photo-grid figcaption p {
	display: table-cell;
	color: white;
	font-size: 2em;
	position: relative;
	top: -40px;
	width: 500px;
	-webkit-transition: all 300ms ease-out;
	-moz-transition: all 300ms ease-out;
	transition: all 300ms ease-out;
	vertical-align: middle;
}

.photo-grid li:hover figcaption p {
	-moz-transform: translateY(40px);
	-webkit-transform: translateY(40px);
	transform: translateY(40px);
}


.photo-grid figcaption {
	background: rgba(192,192,192,0.3);
	color: white;
	display: table;
	height: 100%;
	left: 0;
	opacity: 0;
	position: absolute;
	right: 0;
	top: 0;
	-webkit-transition: all 300ms;
	-moz-transition: all 300ms;
	transition: all 300ms;
	-webkit-transition-delay: 100ms;
	-moz-transition-delay: 100ms;
	transition-delay: 100ms;
	z-index: 100;
}

@font-face {font-family:Arial, Helvetica, sans-serif;
src:url(fonts/ITCAvantGardeStd-Demi.otf)
}
div{font-family:Arial, Helvetica, sans-serif;
}
}
  </style>
</head>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

试试这个css代码:

html {
  background: url(images/BG.jpg) no-repeat center center fixed;
  background-size: cover;
}

答案 1 :(得分:0)

无需使用媒体查询来设置&#34; 响应式图像&#34;。
我用来制作一个完整背景的块(但这不是唯一的方法):

#background {
    width: 100%;
    height: 100vh;
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
    background: url(my/image.jpeg) center center no-repeat;
    background-size: cover;
}

我在#background标记后写div body

<body>
    <div id="background"></div>
    <!-- the content of the page here -->

<小时/> 我不想在body上使用它,以防我需要在其上做一些事情(我喜欢挑战自己并尝试一些事情),但这是我的观点和一种方法。

< / p>

答案 2 :(得分:0)

我认为你应该在所有重置CSS之后列出你的背景标签,只是为了确保重置不会杀死你的任何风格。没有任何冲突,但仍然,最好在重置后设置样式。此外,您看起来有两个开放式样式标签,重置前的标签不需要在那里。