How to create fixed background scrolling effect without background image? - css

I have a horizontally and vertically centred image on a page. I would like it so when the user scrolls down, the content below it actually comes up, as if the top content is fixed. Like this effect here... http://tympanus.net/codrops/2013/05/02/fixed-background-scrolling-layout/
Only problem is for that effect they use the background-attachment: fixed property. I cannot use this as I need the image to be content (it will actually be changed to HTML5 video).
My code is here... http://jsfiddle.net/5jphd/1/
HTML
<div class="image">
<img src="http://upload.wikimedia.org/wikipedia/commons/3/36/Hopetoun_falls.jpg">
<div class="text">Scroll down</div>
</div>
<div class="wrap">
Here is some content
</div>
CSS
html, body {height: 100%}
body {
padding: 0;
margin:0;
font-family: sans-serif;
}
.image {
position: relative;
left: 0px;
height: 100%;
background-position: 50% 50%;
background-size: cover;
background-attachment: scroll;
text-align: center;
}
img {
max-width: 90%;
max-height: 70%;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
.text {
position: absolute;
bottom: 20px;
left:50%;
margin-left: -44px;
}
.wrap {
background-color: lightblue;
text-align: center;
padding: 100px;
font-size: 30px;
min-height: 1000px;
}
Is this possible to do with this markup? So when you scroll down the content will rise up and overlap the image.

Ok i have done it, this is what I wanted to achieve. I simply made the video and scroll text position:fixed, and made the main body content position:relative - http://jsfiddle.net/5jphd/4/
html, body {height: 100%}
body {
padding: 0;
margin:0;
font-family: sans-serif;
}
.image {
position: relative;
left: 0px;
height: 100%;
background-position: 50% 50%;
background-size: cover;
background-attachment: scroll;
text-align: center;
}
img {
max-width: 90%;
max-height: 70%;
margin: auto;
position: fixed;
top: 0; left: 0; bottom: 0; right: 0;
}
.text {
position: fixed;
bottom: 20px;
left:50%;
margin-left: -44px;
}
.wrap {
background-color: lightblue;
text-align: center;
padding: 100px;
font-size: 30px;
min-height: 1000px;
position:relative;
}

JSFiddle
I think this is what you're essentially asking for. Keep aspect ratios in mind, this is what an hd vieo would probably look like on a 320x480 or so.
#bg {
position:absolute;
position:fixed;
top:0;
width:100%;
height:100%;
margin:auto;
overflow:hidden;
z-index:-1;
}
#bg img { max-width:100%; }

Related

Position Sticky does not work inside container [duplicate]

This question already has an answer here:
Why position:sticky is not working when the element is wrapped inside another one?
(1 answer)
Closed 1 year ago.
I want to make the image caption stick to the top of the viewport once a user scrolls the page, but it's not working. I would greatly appreciate the help, here is my HTML and CSS code:
<div id="img-div">
<img src="https://static.stacker.com/s3fs-public/styles/sar_screen_maximum_large/s3/Audrey%20Lead.png" id="image">
<p id="img-caption"><strong>For beautiful eyes, look for the good in others; for beautiful lips, speak only words of kindness; and for poise, walk with the knowledge that you are never alone.<br><br>-Audrey Hepburn</strong></p>
</div>
html{
width: 100vw;
}
body{
width: 100%;
margin: auto;
}
h1{
position: absolute;
z-index: 5;
}
#img-div{
position: relative;
height: 778px;
width: 100%;
}
#image{
background-repeat: no-repeat;
height: 774px;
width: 100%;
object-fit: cover;
}
#img-caption{
position: sticky;
font-size: 19px;
height: 9.5em;
width: 21em;
padding: 23px 13px 20px 23px;
margin: 0;
background-color: white;
opacity: 70%;
top: 0px;
z-index: 5;
}
The problem is that the stickiness position occurs in the given code in relation to the containing element, not in relation to the viewport.
This snippet takes the caption out of that element and then it sticks to the top of the viewport. (Note, to make things scrollable body has been given height 500vh for this demo).
html {
width: 100vw;
}
body {
width: 100%;
margin: auto;
height: 500vh;
}
h1 {
position: absolute;
z-index: 5;
}
#img-div {
position: relative;
height: 778px;
width: 100%;
}
#image {
background-repeat: no-repeat;
height: 774px;
width: 100%;
object-fit: cover;
}
#img-caption {
position: sticky;
font-size: 19px;
height: 9.5em;
width: 21em;
padding: 23px 13px 20px 23px;
margin: 0;
background-color: white;
opacity: 70%;
top: 0px;
z-index: 5;
}
<div id="img-div">
<img src="https://static.stacker.com/s3fs-public/styles/sar_screen_maximum_large/s3/Audrey%20Lead.png" id="image">
</div>
<p id="img-caption"><strong>For beautiful eyes, look for the good in others; for beautiful lips, speak only words of kindness; and for poise, walk with the knowledge that you are never alone.<br><br>-Audrey Hepburn</strong></p>

How to add an image on top of a fixed position container?

I would like to create a following shaped notice bar on the bottom of my webpage (sticky).
Here is my HTML
<div class="notice-container">
<div class="wave"></div>
<div>
<p>Content here</p>
</div>
</div>
And here is my CSS, I tried several things, but here is the latest:
.notice-container {
display: block;
height: auto;
background-color: #ccc;
position: fixed;
bottom: 0;
left: 0;
right: 0;
width: 100%;
}
.wave:after {
content: "";
background-image: url('../wave.png');
background-repeat: repeat-x;
position: absolute;
top: 0;
margin: -30px;
width: 100%;
height: auto;
}
Since the container has a position: fixed, how can I get the repeat-x work on the wave? I would like to display the background-image on top of the container div.
Your pseudo element needs display: block; and also a specified height attribute. Since the value auto would just tell it to extend to fit its contents (and it has none), then the height value would remain 0.
.wave:after {
content: "";
display: block; /* <- Add this */
background-image: url('../wave.png');
background-repeat: repeat-x;
position: absolute;
top: 0;
margin: -30px;
width: 100%;
height: 60px; /* Or whatever your wave.png requires */
}
Place your url and justice the sizes of image in background-size. Also do not forget to change needed height of pseudo element which is also needs to configure margin-top and top
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
footer {
width: 100%;
height: 70px;
border: 5px solid red;
border-top: 0;
margin-top: 20px;
position: relative;
}
footer:after {
width: 100%;
display: block;
content: '';
height: 20px;
position: absolute;
left: 0;
top:-20px;
background-image: url(https://i.stack.imgur.com/z4HMY.png);
background-size: 10% 20px;
}
<footer></footer>

Resize image keeping both sizes on ratio

I have a photo blog on the very final stages and this is the last problem I'm struggling with.
The layout has fixed menu bar on the right and the content flows on the right. When photo is opened from gallery page to it's own, it is supposed to be resized so that either side is not over (for example) 70% of the free space. But not just that. It should also stay in horizontal and vertical center of the right content div.
At this point everything works except when the image is a portrait it goes over the screen.
Is this possible to achieve with only CSS to please the few percent that has JS not activated?
If not, well thats not a deal breaker.
The HTML:
<div id="wrapper">
<div id="left_column">
</div>
<div id="right_column_post">
<div id="post_container">
<img src="img.jpg" width="1000" height="750"/>
</div>
</div>
</div>
The CSS:
#left_column {
position: fixed;
top: 0;
bottom: 0;
left: 0;
z-index:100;
width: 240px;
height: 100%;
overflow: hidden;
}
#right_column_post{
height: 100%;
width:auto;
margin-left:240px;
position: relative;
}
#post_container {
width:100%;
height:100%;
display: block;
vertical-align: middle;
text-align:center;
}
#post_container:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
#post_container img{
max-width:70%;
height:auto;
display: inline-block;
vertical-align: middle;
margin:auto;
position:relative;
}
This is close: http://jsfiddle.net/jcAGj/ but it'll break the layout if implemented as is.
I am not sure what you are actually looking for, but if you want to resize image in aspect ratio then do not give max-width and max-height both, if u do so then at a point it will loose its aspect ratio.
u can give width:100%;
Here i am giving width in percentage
#left_column {
position: fixed;
top: 0;
bottom: 0;
left: 0;
z-index:100;
width: 40%;
height: 100%;
overflow: hidden;
background:red;
}
#right_column_post{
height: 100%;
width:50%;
margin-left:45%;
position: relative;
}
#post_container {
width:90%;
height:100%;
display: block;
vertical-align: middle;
text-align:center;
}
#post_container:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
#post_container img{
width:100%;
max-width:100%;
display: block;
vertical-align: middle;
margin:0 auto;
position:relative;
}
Check this link:
/NEW LINK/
http://jsfiddle.net/madhuri2987/jcAGj/2/
Hope this is what u were looking for.
I got it working ...kind of. I did a stripped down test page and it works:
http://tomiphotography.com/wp-content/themes/Photomi/Test/post-2.html
But when I use the same exact css in the main WP page the image ratio goes haywire and the image isn't centered????:
http://tomiphotography.com/?p=176
:(
Any idea what might be in WP that overrides some css?
HTML:
<body>
<div id="wrapper">
<div id="left_column">
</div>
<div id="right_column_post">
<div id="post_container">
<img src="http://tomiphotography.com/wp-content/uploads/2013/04/Ruka-viimeinen.jpg"/>
</div>
</div>
</div>
</body>
CSS:
html {
height: 100%;
padding:0;
margin:0;
}
body {
background-color: #000000;
height: 100%;
padding: 0;
margin: 0;
background: url(img/bg.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom left;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#wrapper {
float: left;
height: 100%;
width:100%;
padding:0;
margin:0;
}
#left_column {
position: fixed;
top: 0;
bottom: 0;
left: 0;
z-index:100;
width: 240px;
height: 100%;
overflow: hidden;
background-color:#ff0000;
}
#right_column_post{
height: 100%;
width:auto;
margin-left:240px;
position: relative;
}
#post_container {
position:fixed !important;
position:absolute;
margin:auto;
text-align:center;
top:8%;
right:80px;
bottom:10%;
left:320px;
}
#post_container img {
max-width:100%;
max-height:100%;
padding:4px;
background-color:#fff;
}

Proper css div positioning?

I tried, but totally failed at positioning an interface that will go above a google maps layout. What I tried to do is this:
But I ended up with this
The format I have is like this:
<div id="GoogleMap"> // the containing google maps layer
<!-- map will go here <div id="GoogleMapCanvas"></div> --> //eventual map
<div class="map-topmenu"></div> //my interface
<div class="map-leftmenu"></div> //my interface
<div class="map-rightmenu"></div> //my interface
<div class="map-bottommenu"></div>//my interface
</div>
I succeeded in making the top menu horizontally centered, and 10px from the top of the screen.
I have been unable to center vertically the left and right menu's, and I started noticing that the code needed to center the left menu was becoming weird.
And as for the bottom menu, I utterly failed - however many different methods I tried.
Can anyone please look at my code and let me know where I messed up? Thanks so much!
body {
border: 0 none;
margin: 0;
padding: 0;
height:100%;
}
#GoogleMap {
position:absolute;
background-color: grey;
background-position: 50% 50%;
background-repeat: repeat;
height: 100%;
overflow: hidden;
width: 100%;
}
.map-topmenu {
height: 52px;
width: 353px;
background-image: url(http://i.imgur.com/KlyKR.png);
margin-top: 10px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
.map-leftmenu {
height: 263px;
width: 77px;
margin-left: 10px;
top:50%;
position: absolute;
margin-top: -150px;
background-image: url(http://i.imgur.com/Q3d1r.png);
}
.map-rightmenu {
background-image: url(http://i.imgur.com/si6dl.png);
height: 147px;
width: 280px;
margin-right: 10px;
float: right;
top:50%;
}
.map-bottommenu {
background-image: url(http://i.imgur.com/iDmuP.png);
height: 52px;
width: 312px;
margin-right: auto;
margin-bottom: 10px;
bottom: 0;
margin-left: auto;
}
Use absolute positioning with negative margins:
http://jsfiddle.net/PJTDy/3/
#GoogleMap {
position:absolute;
background-color: grey;
height: 100%;
overflow: hidden;
width: 100%;
}
.map-topmenu, .map-leftmenu, .map-rightmenu, .map-bottommenu {
z-index:1;
position:absolute;
background:blue;
}
.map-topmenu {
top:0;
left:50%;
margin-left:-176px;
height: 52px;
width: 353px;
}
.map-leftmenu {
height: 263px;
width: 77px;
left:0px;
margin-top:-131px;
top:50%;
}
.map-rightmenu {
height: 147px;
width: 280px;
right:0;
top:50%;
margin-top:-73px;
}
.map-bottommenu {
height: 52px;
width: 312px;
bottom: 0;
left:50%;
margin-left:-156px;
}
Any reason why you are positioning the main container div (id="GoogleMap") absolutely? Your bottom menu does not have a position property set, otherwise you can position it exactly same way as the top one with bottom: 0.

Background of div gets cut off at viewport

I know that this is SIMILAR to a few questions already out there, but it's different in that it's not my main body background that's causing the problem, and so I'm lost.
The website is at http://www.thesweet-spot.com/test77
The problem is that when you shrink your viewport to be smaller than the content and then scroll down, the wavy line on the left stops at where the bottom of your viewport originally was. The tricky part is that I want the wavy line on the left to scroll WITH the content when the content is too long.
The relevant CSS looks like this:
body {
background: url('images/background.jpg');
margin: 0;
padding: 0;
min-width: 1000px;
}
#container {
width: 100%;
position: absolute;
top: 105px;
bottom: 0;
margin: 0;
padding: 0;
}
#sidebarbg {
background: url('images/chocolate.jpg');
width: 300px;
height: 100%;
position: fixed;
left: 0;
z-index: 11;
background-attachment:fixed;
}
#sidebar {
background: url('images/sidebar.png');
width: 300px;
height: 100%;
position: absolute;
left: 0;
z-index: 12;
}
#contentnest {
position: absolute;
top: 50px;
left: 365px;
right: 0;
z-index: 14;
}
#content {
background: url('images/contentbg.png');
-moz-border-radius: 30px;
-webkit-border-radius: 30px;
border-radius: 30px;
-khtml-border-radius: 30px;
padding: 20px;
border: #f062a4 3px solid;
width: 80%;
min-width: 350px;
margin-left: auto;
margin-right: auto;
font-size: 22px;
line-height: 150%;
font-family: QuicksandBook, Tahoma, Arial, sans-serif;
color: #905131;
}
and the HTML looks like this:
<body>
<div id="sidebarbg"></div>
<div id="sidebar"></div>
<div id="container">
<div id="contentnest">
<div id="content">
<! -- content goes here -->
</div>
</div>
</div>
</body>
What am I missing?
in #sidebar try removing height:100% and add bottom:-99999em
the other way is to make the sidebar position:fixed.
I was able to get the BG of my absolute container to extend beyond the viewport by adding this to my div style that has the BG.
min-height: 100%;
height: 100%;
overflow-y: scroll;
This will cause a double scroll bar so add this to your body style
overflow-y: hidden;

Resources