How to make background-image responsive? - css

I am trying to make background-image of .panda_section_wrapper responsive. While it finally looks good on smaller devices, it is wrong on bigger ones. It's okey that it is stretched but I would like it to also stretch the height in this case, so Image looks good on small and big screens. How can I do this?
My site:
https://wandawix.github.io/online-zoo/index.html
My code:
https://github.com/WandaWix/online-zoo
<main class="main">
<!-- Panda section ---------------------------------------------------------- -->
<div class="panda_section_wrapper">
<!-- <div class="panda_circle">
</div>
<div class="writting">Watch<br>your<br><span style="color: #FFEE2E">favorite</span><br>animal<br>online</div>
</div>
<div class="btn btn_watch">
<button class="btn">Watch online</button>-->
</div>
.panda_section_wrapper {
/* max-width: 1600px;
background-color: pink;
background-image: url(img/GiantPanda.png);
height: 69vh;
background-size: 93vw; */
max-width: 1600px;
background-color: pink;
background-image: url(img/GiantPanda.png);
height: 25vh; /*min(max(16px, 80vw), 60vh);*/
background-size: 93vw;
max-width: 1600px;
background-color: pink;
background-image: url(img/GiantPanda.png);
background-size: 93vw;
background-repeat: no-repeat;
background-size: contain;
background-position: center;
background-size: 100%;
background-size: 100% 100%;
}
.panda_section_container {
max-width: 1160px;
height: 100%;
margin: 0 auto;
}
.panda_circle {
position: relative;
width: 40vw;
height: 50vh;
left: 33%;
top: 0%;
background: linear-gradient(113.96deg, #F9804B 1.49%, #FE9013 101.44%);
border-radius: 50%;
}

Try using this:
.panda_section_wrapper {
max-width: 1600px;
background-color: pink;
background-image: url(img/GiantPanda.png);
height: 25vh;
background-color: pink;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
#media only screen and (min-width: 968px) {
height: 75vh;
}
}
And see if this works

Related

How can I fix parallax images causing whitespace at bottom of page?

I'm making a website for myself as practice. I'm attempting to have a parallax banner then content below that but for some reason I need to set the minimum height of the inner content to be at least the same size as the banner itself, otherwise you can scroll past the end of the inner content and see the parallax images that are supposed to be hidden. Can anyone tell me why this is happening and how I could fix it? I tried inspecting the page and the space at the bottom seems to just be the background and not any parallax layers. The background is fixed and set to 100% so I'm not sure why that would cause this behavior.
HTML:
<body>
<div id="scrollContainer" class="parallax">
<div class="parallax__group">
<div class="parallax__layer parallax__layer--0">
</div>
<div class="parallax__layer parallax__layer--1">
</div>
<div class="parallax__layer parallax__layer--2">
</div>
<div class="parallax__layer parallax__layer--3">
</div>
<div class="parallax__layer parallax__layer--banner">
</div>
<div class="parallax__layer parallax__layer--4">
</div>
<div class="parallax__layer parallax__layer--5">
</div>
<div class="parallax__layer parallax__layer--6">
</div>
<div id="parBody" class="parallaxBody">
<div id="circleContainer" class="container topSec">
<div id="smallCircle" class="smCircle1"></div>
<div class="smCircle2"></div>
<div class="circle"></div>
</div>
</div>
</div>
</div>
CSS:
html{
height: 100%;
}
body {
font: 50% / 1.5 Arial;
font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
color: black;
height: 100%;
}
/*parallax styles for 1000px width
Do something different for less than 1000px width*/
.parallax__group {
position: relative;
transform-style: preserve-3d;
transition: transform 0.5s;
}
.parallaxBody{
position: absolute;
background: rgb(25, 25, 75);
background-size: 1300px auto;
top: 1800px;
width: 100%;
min-height: 1000px;
background-size: 100% 100%;
}
.parallax {
background-image: url("../images/layer1.png");
background-position-x: center;
background-position-y: 0;
background-size: 1300px 1300px;
perspective: 1px;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
font-size: 200%;
}
.parallax__layer {
position: absolute;
top: 0;
right: 0;
left: -12px;
background-size: 1300px 1300px;
background-repeat: no-repeat;
}
.parallax__layer--6 {
top:-100px;
height: 3200px; /*adjust for screen sizes*/
background-size: 1300px 3200px;
background-image: url("../images/layer7.png");
}
.parallax__layer--5 {
top:-200px;
height: 2000px;
background-size: 1300px 1500px;
transform: translateZ(-0.5px) scale(1.5);
background-image: url("../images/layer6.png");
}
.parallax__layer--4 {
top:-200px;
height: 1200px;
transform: translateZ(-1.5px) scale(2.5);
background-image: url("../images/layer5.png");
background-size: 1300px 1300px;
}
.parallax__layer--3 {
top: -200px;
height: 1200px;
transform: translateZ(-2px) scale(3);
background-size: 1300px 1300px;
background-image: url("../images/layer4.png");
}
.parallax__layer--banner {
height: 300px;
background-size: 50vw 15vw;
left: 32%;
top: 400px;
-webkit-transform: translate(-32%, -70%);
transform: translate(-32%, -70%);
background-repeat: no-repeat;
transform: translateZ(-1px) scale(2);
background-image: url("../images/banner.png");
}
.parallax__layer--2 {
top: -500px;
height: 1200px;
transform: translateZ(-4px) scale(5);
background-size: 1300px 1300px;
background-image: url("../images/layer3.png");
}
.parallax__layer--1 {
top: -400px;
height: 1000px;
transform: translateZ(-5px) scale(6); /*Try moving z-index up and scaling down*/
background-image: url("../images/layer2.png");
background-size: 1300px 1300px;
}
#media screen and (min-width: 1300px){
.parallax__layer--6 {
top:-200px;
height: 3400px;
background-size: 1600px 4200px;
}
.parallax__layer--5 {
top:-500px;
height: 2200px;
background-size: 1600px 2000px;
}
.parallax__layer--4 {
top:-200px;
height: 1500px;
background-size: 1600px 1600px;
}
.parallax__layer--3 {
top: -200px;
height: 1500px;
background-size: 1600px 1600px;
}
.parallax__layer--banner {
height: 300px;
background-size: 50vw 15vw;
top: 0;
}
.parallax__layer--2 {
top: -700px;
background-size: 1600px 1600px;
}
.parallax__layer--1 {
top: -1200px;
height: 1200px;
background-size: 1600px 1600px;
}
}
#media screen and (min-width: 1600px){
.parallax__layer--6 {
top:-200px;
height: 2000px;
background-size: 2000px 5000px;
}
.parallax__layer--5 {
top:-700px;
height: 3000px;
background-size: 2000px 2500px;
}
.parallax__layer--4 {
top:-400px;
height: 1800px;
background-size: 2000px 2000px;
}
.parallax__layer--3 {
top: -700px;
height: 1500px;
background-size: 2000px 2000px;
}
.parallax__layer--banner {
height: 300px;
background-size: 50vw 15vw;
top: 0;
left: 12vw;
}
.parallax__layer--2 {
top: -1000px;
background-size: 2000px 2000px;
height: 1300px;
}
.parallax__layer--1 {
top: -1000px;
height: 1300px;
background-size: 2000px 2000px;
}
}
#media screen and (min-width: 2000px){
.parallax__layer--6 {
top:-800px;
height: 5000px;
background-size: 2600px 6500px;
}
.parallax__layer--5 {
top:-1300px;
height: 4000px;
background-size: 2600px 3250px;
}
.parallax__layer--4 {
top:-1100px;
height: 2200px;
background-size: 2600px 2600px;
}
.parallax__layer--3 {
top: -1100px;
height: 2400px;
background-size: 2600px 2600px;
}
.parallax__layer--banner {
height: 500px;
background-size: 35vw 10vw;
top: 0;
}
.parallax__layer--2 {
top: -2500px;
background-size: 2600px 2600px;
height: 1800px;
}
.parallax__layer--1 {
top: -2000px;
height: 1500px;
background-size: 2600px 2600px;
}
}
* {
margin: 0;
padding: 0;
}

Background image gradient not applying

I'm attempting to apply a background image gradient over an image by setting it on its parent div, but it's having no effect, which is strange so I've done this before.
<header id="hero">
<div id="hero-image">
<img src="http://placeholdit.imgix.net/~text?txtsize=44&txt=Hero%20Image&w=1920&h=500" />
</div>
</header>
header#hero {
margin: 0 auto;
width: 100%;
font-size: 1rem;
}
header#hero #hero-image {
background-image: linear-gradient(transparent 50%, black 100%);
}
header#hero #hero-image img {
display: block;
width: 100%;
max-height: 500px;
object-fit: cover;
}
Demo: http://codepen.io/ourcore/pen/xgqNZJ
I was able to achieve this by applying position: relative and z-index: -1 to #hero-image img:
header#hero {
margin: 0 auto;
width: 100%;
font-size: 1rem;
}
header#hero #hero-image {
background-image: linear-gradient(transparent 50%, black 100%);
}
header#hero #hero-image img {
display: block;
position: relative;
width: 100%;
max-height: 500px;
object-fit: cover;
z-index: -1;
}

Automatically reduce the size of a background-image set via CSS when there is not enough width

The header on my website contains an image set as the background, with the HTML output as below -
<div id="header-container">
<div class="inner">
<h1>
<a title="Go home..." href="http://home_url">Blog title</a>
</h1>
</div>
</div>
The method works just fine, but I'm having issues with mobile devices as my header image is 432px wide.
Because of this I need to amend the code below so that the image reduces in size should the available width of #header-container .inner be less than the width ofthe background image.
I've tried several things, including using background-size: cover; and max-width, but I cannot seem to find a working combination.
How can I overcome this issue?
Here is a JS Fiddle showing the issue. Just reduce the size of the rendered view to see that the image does not shrink.
And here is the CSS that I am using -
#header-container .inner h1{
background: transparent url(res/title-white.png) no-repeat center left;
-moz-background-size: 432px auto;
-webkit-background-size: 432px auto;
background-size: 432px auto;
height: 85px;
margin: 0;
width: 432px;
}
#header-container .inner h1 a{
display: block;
height: 85px;
text-indent: -9999px;
width: 432px;
}
You should use background-size: contain, not background-size: cover, in combination with min/max-width.
Change your CSS like this.
#header-container .inner h1{
background: transparent url(http://apps.gwtrains.co.uk/apklibrary/wp-content/themes/apklibrary/images/logo-white.png) no-repeat center left;
background-size: contain;
height: 85px;
margin: 0;
max-width: 432px;
}
Sample snippet
#header-container{
background-color: #053525;
border-bottom: 1px solid #4DC386;
padding: 10px 20px;
position: relative;
width: 100%;
box-sizing: border-box;
}
#header-container .inner{
margin: 0 auto;
max-width: 1000px;
position: relative;
}
#header-container .inner h1{
background: transparent url(http://apps.gwtrains.co.uk/apklibrary/wp-content/themes/apklibrary/images/logo-white.png) no-repeat center left;
background-size: contain;
max-height: 85px;
margin: 0;
max-width: 432px;
position: relative;
}
#header-container .inner h1:before {
display: block;
content: "";
width: 100%;
padding-top: 20%;
}
<div id="header-container">
<div class="inner">
<h1>
</h1>
</div>
</div>
#media query might help here:
#header-container .inner h1{
background: transparent url(res/title-white.png) no-repeat center left;
background-size: 432px auto;
height: 85px;
margin: 0;
width: 432px;
}
#media (max-width: 432px){
#header-container .inner h1{
background-size: cover;
}
}
You can use media queries for mobile devices. Like:
#media all and (max-width: 540px){
#header-container .inner h1{
-moz-background-size: 260px auto;
-webkit-background-size: 260px auto;
background-size: 260px auto;
width: 280px;
}
#header-container .inner h1 a{
width: 260px;
}
}
You can force this by avoiding the background image altogether as you replace it with a regular image in the <img> tag. Layer containers of content (one with the image and another with text) on top of each other with absolute position and z-index. It's a workaround to the background image problems I've experienced similar to yours and the layers provide nice flexibility for managing content.
#z1 {
z-index: 1;
}
#z2 {
z-index: 2;
padding: 5%;
color: white;
}
#z1, #z2 {
position: absolute;
left: 0px;
top: 0px;
}
<div id="outer">
<div id="z1">
<img width="100%" src="http://www.verolago.com/blog/wp-content/uploads/2013/11/sailboat.jpg" />
</div>
<div id="z2">
<p>I'm on a boat!</p>
</div>
</div>

How to allow scroll on mobile

Good morning,
I have a problem with scrolling my website on mobiles. I have set div height to 100%, but on mobile, it has height 'auto'. But still, when text is longer than the screen height, I am unable to read it. How should I edit it please? Thank you!
HTML:
<div id="about">
<div id="onas">
<div id="obsah">
<p> text text text....</p>
</div>
</div>
</div>
CSS:
#about {
background:url('images/bg-about.png') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
display: block;
min-height: 100%;
width: 100%;
color: white;
position: fixed;
top: 0;
left: 0;
margin: 0 auto;
z-index: 1;
}
#obsah {
margin-left: 10%;
margin-right: 10%;
margin-top: 5%;
color: black;
font-size: 21px;
}
#onas {
position: absolute;
font-family: cond;
bottom: 0;
left: 0;
width: 100%;
background: rgba(255, 255, 255, .5);
min-height: 40%;
}
#media screen and (max-width: 1024px){
#onas {
position: static;
margin-top: 200px;
padding-top: 15px;
width: 100%;
height: auto;
min-height: 40%;
}
}
Your problem is that you have:
position: fixed;
In your about, you were probably trying to apply it to the background.
Delete that line and it will scroll.
docs

Change background image size with media query

.logo {
background-image: url(https://ofbuckleyandbeatles.files.wordpress.com/2011/01/testpattern.gif);
background-size: 100%;
}
#media(max-width:767px) {
.logo {
background-image: url(https://ofbuckleyandbeatles.files.wordpress.com/2011/01/testpattern.gif);
background-size: 50%;
background-repeat: no-repeat;
}
}
<div class="logo"> </div>
I can't work out why this doesn't work - any ideas why the image doesn't render?
HTML:
<div class="logo"> </div>
CSS:
.logo {
background-image: url(../img/logo_white.png);
background-size: 100%;}
#media(max-width:767px) {
.logo {
background-image: url(../img/logo_white.png);
background-size: 50%;
background-repeat: no-repeat;}
}
Any help greatly appreciated
Try this...
#media screen and (max-width:767px)
First of all, you syntax was wrong. Secondly, at least in the code you provided, your div was zero size. See how it works:
.logo {
display: block;
width: 99%;
height: 400px;
border: solid 1px red;
background-image: url('http://phandroid.s3.amazonaws.com/wp-content/uploads/2014/10/mountains.jpg');
background-size: 100%;
}
#media only screen and (max-width: 767px) {
.logo {
background-size: 50%;
background-repeat: no-repeat;
}
}
<div class="logo"></div>
But is it that you want for responsive design? Probably you misunderstood the meaning of background-size: 50%; property?
Uhm mate, heres a simple one: give your wrapper some height.
.logo {
background-image: url(https://ofbuckleyandbeatles.files.wordpress.com/2011/01/testpattern.gif);
background-size: 100%;
width: 100%;
height: 100px;
}
#media(max-width:767px) {
.logo {
background-image: url(https://ofbuckleyandbeatles.files.wordpress.com/2011/01/testpattern.gif);
background-size: 50%;
background-repeat: no-repeat;
}
}
<div class="logo"> </div>

Resources