I am trying to accomplish placing two parallax background-images side by side while keeping their aspect ratio. The issue I am running into is that each image appears to be getting cut in half vertically. I have tried using different values in both background-attachment and background-size to no avail. Removing background-attachment: fixed; from the code below fixes the aspect-ratio issue but loses the parallax effect. Does anyone know how to accomplish both simultaneously?
.image-left {
width: 50%;
float: left;
height: 500px;
background-attachment: fixed;
background-position: center;
background-repeat: none;
background-size: cover;
background-image: url('https://www.gstatic.com/webp/gallery/1.webp');
}
.image-right {
width: 50%;
float: left;
height: 500px;
background-attachment: fixed;
background-position: center;
background-repeat: none;
background-size: cover;
background-image: url('https://www.gstatic.com/webp/gallery/2.webp');
}
.content {
text-align: center;
padding: 100px 30px;
font-size: 1.25rem;
color: #fff;
background-color: orange;
}
<div class="content">
<h1>Lorem</h1>
</div>
<div class="image-left"></div>
<div class="image-right"></div>
<div class="content">
<h1>Ipsum</h1>
</div>
Fiddle for above code here.
I have also attempted to use the jQuery function from this post but was unable to get it to work with side by side images. (see fiddle)
$(window).scroll(function() {
var scrolledY = $(window).scrollTop();
$('#container').css('background-position', 'left ' + ((scrolledY)) + 'px');
});
As others pointed out, I don't think you can achieve your goal via background images...
So I tried another approach that consists basically on:
- Having two sections: one for the images, another for the content.
- As for the images, wrap them into an element and use position fixed. They are positioned at the very top of the element, should you want to change this, you can play with top property.
- As for the content, both regions are also wrapped in a container with position absolute.
- The content at the bottom will be responsible for the 'breathing' space in the images, so, you need to play with margin-top to achieve desired results.
Some considerations:
- The given example works at the moment only on desktop, tested on fulls screen laptop (around 1680px width).
- If you shrink the screen, everything goes really bad, thus, you will need to adjust all measures for mobile via media queries.
- The bottom element have a min-height attribute just for demonstration purposes.
All given, I'm not quite sure if this is something I would recommend.
Can you actually merge both images into one? This way, you can just use the background approach, and this development would not be needed.
What I don't like about my approach, is that it contains a lot of fixed values on positions, and eventually, this would introduce maintainability issues.
I hope it helps!
.image-wrapper {
position: fixed;
top: 0;
width: 100%;
display: flex;
flex-flow: row nowrap;
}
.image {
width: 100%;
}
.content-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
}
.content {
text-align: center;
padding: 100px 30px;
font-size: 1.25rem;
color: #fff;
background-color: orange;
}
.content-bottom {
margin-top: 300px;
min-height: 1000px;
}
<div class="image-wrapper">
<img class="image" src="https://www.gstatic.com/webp/gallery/1.webp">
<img class="image" src="https://www.gstatic.com/webp/gallery/2.webp">
</div>
<div class="content-wrapper">
<div class="content">
<h1>Lorem</h1>
</div>
<div class="content content-bottom">
<h2>Ipsum</h2>
</div>
</div>
As avcajaraville pointed out, the best approach is to have a container for the images with position fixed.
Here is my solution, using this idea, but working without needing size adjustments
Note that since now the images cover only half the width of the screen, they will cover also half the height. This could be fixed having images in portrait mode. To get a more nice result with the current images, I have added another row of images, now with the order reversed (visible only for some screen ratios)
.images {
position: fixed;
width: 100%;
z-index: -1;
}
.images div {
display: inline-block;
width: 49%;
margin: 0;
height: 500px;
background-position: center;
background-size: cover;
}
.image-left {
background-image: url('https://www.gstatic.com/webp/gallery/1.webp');
}
.image-right {
background-image: url('https://www.gstatic.com/webp/gallery/2.webp');
}
.content {
text-align: center;
padding: 100px 30px;
font-size: 1.25rem;
color: #fff;
background-color: orange;
}
.filler {
height: 500px;
}
<div class="images">
<div class="image-left"></div>
<div class="image-right"></div>
<div class="image-right"></div>
<div class="image-left"></div>
</div>
<div class="content">
<h1>Lorem</h1>
</div>
<div class="filler"></div>
<div class="content">
<h1>Ipsum</h1>
</div>
.flexrow {
display: flex;
flex-flow: row wrap;
}
.flexrow > .image {
flex: 0 1 50%;
min-height: 350px;
background-size: 100%;
}
.img1 {
background-size: cover;
background: url('https://www.gstatic.com/webp/gallery/1.webp') no-repeat center center fixed; ;
}
.img2 {
background-size: cover;
background: url('https://www.gstatic.com/webp/gallery/2.webp') no-repeat center center fixed; ;
}
.content {
text-align: center;
padding: 100px 30px;
font-size: 1.25rem;
color: #fff;
background-color: orange;
}
<div class="content">
<h1>Lorem</h1>
</div>
<div class="flexrow">
<div class="image img1"></div>
<div class="image img2"></div>
</div>
<div class="content">
<h1>Ipsum</h1>
</div>
think this is what you're looking for, remember that min-height property on images may break this aspect ratio when no space are available (on resize, low res).
Related
I created a css sprite to combine several images appeared in my homepage. However I now have issues displaying those images.
You see that the images (store logos) are not displayed centrally. Here is my html code:
<div class="slider-slick">
<?php foreach($stores as $store){?>
<div class="slide">
<div class="client">
<div class="sprite sprite-<?php echo $store->_storeName?>"></div>
</div>
</div>
<?}?>
</div>
The css for the sprite is:
.sprite {
background-image: url(../images/spritesheet-logos.png);
background-repeat: no-repeat;
margin: auto;
}
.sprite-store1 {
width: 149px;
height: 71px;
background-position: -5px -5px;
}
.sprite-store2 {
width: 148px;
height: 23px;
background-position: -164px -5px;
}
and the parent div is:
.client {
padding: 70% 0 0;
background: #ffffff;
}
After removing the padding they look like:
I ve been trying all different options with margin but couldnt really make it. Any ideas how to make them look like this:
Try using flexbox:
.client {
display: flex;
justify-content: center;
align-items: center;
}
Don't use padding to center the inner elements with different height.
Give height to the parent and fill the bg color
.client {
padding: 0;
background: #ffffff;
height: 71px;
}
and position the inner div using transform, so it will be center with any height.
.client > div {
position: relative;
top: 50%;
transform: translateY(-50%);
}
Try this in your .sprite css:
background-position: center;
(I found it here)
Most likely a css problem, the images I'm using as backgrounds for each page are repeating & varying in size. The homepage is the only completely functional section.
Here's a live example: http://athenatestingwebsite.tumblr.com/
And here's my html:
<div id="PROJECTSP">
<a id="projects" class="smooth"></a>
</div>
<div id="CONTACTP">
<a id="contact" class="smooth"></a>
</div>
and here's some of my css:
body {
height: 1000px;
}
#projectsp {
height: 1000px;
width: 100%;
background-color: #0a0b38;
display: inline-block;
text-align: center;
background: url("http://i.imgur.com/rrmPP7E.png");
}
#contactp {
height: 1000px;
width: 100%;
height: 100%;
background-color: #0a0b38;
display: inline-block;
background: url("http://i.imgur.com/s9gGzHO.png");
}
background-repeat-y: no-repeat;
add this line after every background image you set. remove
body height,width remove all #id height 1000px or 2000px you set..set height auto.
add this class
div.slogan h1 {
margin: auto;
}
hope all will be fix. without the nav overlay.
Happy Coding . Good luck
I'm a newbie, and I've quickly gotten in over my head:
I'm trying to create a pattern I can re-use throughout my site:
two photos, side by side, each with watercolor splashes peeking out from behind them.
They should scale appropriately down to the smallest screens (I'm pretty agnostic about whether they wrap or not on tiny screens).
Here's my code:
.two-pics {
width: 100%;
}
.wc-pic-blue {
width: 40%;
background-image: url('http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c4007c65e4c37e086e54/1464452096489/_watercolor-splash-blue.jpg');
background-size: contain;
background-repeat: no-repeat;
float: left;
padding: 4%;
}
.wc-pic-pink {
width: 40%;
background-image: url('http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c4077c65e4c37e086e6d/1464452103387/_watercolor-splash-magenta.jpg');
background-size: contain;
background-repeat: no-repeat;
float: right;
padding: 4%;
}
<div class="two-pics">
<div class="wc-pic-blue pic">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c529b09f953f29724252/1464452394022/8248798.jpg">
</div>
<div class="wc-pic-pink pic">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749ca6e37013bafc39f394d/1464453743501/parade-2.jpg">
</div>
<br style="clear: left;" />
</div>
My instinct was to wrap two Divs (identical but for their source images) with background images (the watercolor splashes) inside a parent Div, then stick images within each of the child Divs — and I tried to center the images (both vertically and horizontally) within each of the child Divs, so the watercolor splashes would be equally visible on all sides;
By some miracle this actually worked — mostly — but I was finding weird phantom space when I inspected the page; the inner images were never quite centering correctly within their watercolor Divs.
There's also weird stuff happening upon scaling :(
I'm desperately confused — should I be using Flexbox? Nested Divs with background-images?
Here's my Fiddle if anyone is feeling brave and generous :)
Any help would be so appreciated!
Here's a solution with the following features:
flex layout
viewport percentage units for sizing the images
absolute positioning to center one image over the other
media query for vertical alignment on smaller screens
.two-pics {
display: flex;
justify-content: space-around; /* 1 */
}
.pic {
position: relative;
}
img:first-child {
height: 30vw; /* 2 */
}
img:last-child {
height: 25vw;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* 3 */
}
#media (max-width: 600px) {
.two-pics {
flex-direction: column;
align-items: center;
}
}
<div class="two-pics">
<div class="pic">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c4007c65e4c37e086e54/1464452096489/_watercolor-s.jpg" alt="">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c529b09f953f29724252/1464452394022/8248798.jpg" alt="">
</div>
<div class="pic">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c4077c65e4c37e086e6d/1464452103387/_watercolor-splash-magenta.jpg" alt="">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749ca6e37013bafc39f394d/1464453743501/parade-2.jpg" alt="">
</div>
</div>
https://stackoverflow.com/a/33856609/3597276
https://stackoverflow.com/a/32174347/3597276
https://stackoverflow.com/a/36817384/3597276
Revised Fiddle
I centered the images on the screen by aligning the left div right and solved the scaling problem. I also added a #media query for smaller screens, it looks very fine.
Improved Fiddle
.two-pics {
width: 100%;
}
.wc-pic-blue {
width: 49%; /* decrease for more space between images */
box-sizing: border-box;
background-image: url('http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c4007c65e4c37e086e54/1464452096489/_watercolor-splash-blue.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: top right;
float: left;
padding: 4%;
text-align: right;
}
.wc-pic-pink {
width: 49%; /* decrease for more space between images */
box-sizing: border-box;
background-image: url('http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c4077c65e4c37e086e6d/1464452103387/_watercolor-splash-magenta.jpg');
background-size: contain;
background-repeat: no-repeat;
float: right;
padding: 4%;
}
.two-pics .pic img {
max-width: 100%;
}
#media (max-width: 500px) {
.two-pics .pic {
width: 100%;
padding: 8%;
}
.two-pics .pic img {
width: 100%;
}
}
<div class="two-pics">
<div class="wc-pic-blue pic">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749c529b09f953f29724252/1464452394022/8248798.jpg">
</div>
<div class="wc-pic-pink pic">
<img src="http://static1.squarespace.com/static/57476d871bbee069994f419a/t/5749ca6e37013bafc39f394d/1464453743501/parade-2.jpg">
</div>
<br style="clear: left;" />
</div>
I have a problem with my site. I am using Skrollr to display one image and a text on this image. The text fades out and the image has some nice effect. First I used Foundations as a framework, but this doesn't work for me. So I switched to Bootstrap. Now I have the issue that my body height is set to 2157px. I don't know why, but on pages without Skrollr this doesn't occur. For me it seems like Skrollr is the problem. I used the following code:
<section id="slide-1" class="homeSlide">
<div class="bcg"
data-0="background-position: 0px 0px;"
data-300="background-position:0px -200px;"
>
<div class="hsContainer">
<div class="hsContent container"
data-0="opacity:1;"
data-500="opacity:0;"
>
Lorem ipsum
</div>
</div>
</div></section>
My Css is:
.hsContainer {
display: table;
table-layout: fixed;
width: 100%;
max-height: 50%;
overflow: hidden;
position: relative;
opacity: 1;
}
.hsContent {
font-size:5vw;
max-width: 450px;
margin: -150px auto 0 auto;
display: table-cell;
vertical-align: middle;
color: #ebebeb;
padding: 13% 8%;
text-align: center;
text-shadow: 0 0 5px #333;
}
.bcg {
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
height: 45vw;
width: 100%;
}
I don't see, where the problem comes from. Does anybody have an idea?
Yes, it is skrollr that is causing that issue as an effect of its parallax calculations. You can pass in forceHeight=false as an option when you initialize skrollr, like so:
var s = skrollr.init({
forceHeight: false
});
And it will then leave your <body> height alone.
New to CSS styling. I have a responsive background image on my page, and I want to have a circular logo positioned at the bottom center of it. I want this image to stay in the same position (in relation to the bottom of the background image) no matter how small/big the view is.
I've been playing around with the images padding and positioning, but neither are responsive. When the screen is smaller, the image moves up. When it's bigger, the image moves down. I want it to stay in the same position.
Here's a quick fiddle of my most recent attempt:
https://jsfiddle.net/ybeuvn9m/
And the code:
<div class="container-fluid">
<div class="row">
<div class="splash col-sm-12">
<div class="photo">
<img class="img-responsive img-circle logo" src="http://i.imgur.com/Dum5A8J.png">
</div>
</div>
</div>
</div>
.logo {
padding-top: 200px;
width: 10%;
margin: 0 auto;
display: block;
}
.splash {
background: url('http://s169923.gridserver.com/images/IntelligentsiaMocha.jpg');
background-repeat: no-repeat;
background-size: cover;
height: 50vh;
background-color: rgba(0, 0, 0, 0.2);
background-blend-mode: overlay;
}
If I am understanding correctly you want an .photo to always be in the center of .splash. For this, you should use Flex-box.
add the following to your code:
.parentDiv{
display: flex;
justify-content: center;
align-items: center:
}
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ ... for reference.
You probably want to absolutely position it inside the main image div. Here's a modified version of your JSFiddle: https://jsfiddle.net/jameson5555/ybeuvn9m/1/
Here are the updated .logo styles. You'll also need to add position: relative to your container to make the .logo's position be relative to it.
.logo {
position: absolute;
bottom: 0;
left: 50%;
width: 40px;
margin-left: -20px;
display: block;
}
.logo {
padding-top: 150px;
width: 110px;
margin: 0 auto;
display: block;
}
Your logo should have the constant width of your logo.