<div> Background image transparent but NOT text [duplicate] - html

This question already has answers here:
Can I set an opacity only to the background image of a div?
(8 answers)
Closed 4 years ago.
I am trying to create a <div> that has a slow panning transparent background, as part of an HTML module in opencart. It being a module, styling has to be self contained.
What I have so far:
<style>
#keyframes backgroundScroll {
0% { background-position: 50% 50%; }
33% { background-position: 1% 50%; }
40% { background-position: 1% 50%; }
66% { background-position: 99% 50%; }
75% { background-position: 99% 50%; }
100% { background-position: 50% 50%; }}
/* added pauses to each edge of the image */
#mtn-scroll {
width: 800px;
height: 100%;
background: url(coolpanoramapicture.jpg) no-repeat; opacity:0.1;
background-size: 250%;
background-position: 50% 50%;
animation: backgroundScroll 60s ease-in-out 1s infinite; }
</style>
<div id="mtn-scroll">
<div>
Content text goes here!
</div>
</div>
What I want is for opacity to NOT affect the text as well. Ive heard the bell ring about using ::before, but I have no idea where the hammer hangs. Was I onto something?
Does anyone know how to make ONLY the background image go transparent, without having to resort to finagling with a transparent PNG file?

You were on the right track with the ::before pseudo-element. Here's how to implement it:
html,
body {
height: 100%;
}
#mtn-scroll {
width: 800px;
height: 100%;
position: relative;
}
#mtn-scroll::before {
background: url('https://i.imgur.com/4HLnakJ.jpg') no-repeat;
opacity: 0.1;
background-size: 250%;
background-position: 50% 50%;
animation: backgroundScroll 60s ease-in-out 1s infinite;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 0;
content: '';
}
#mtn-scroll div {
position: relative;
}
#keyframes backgroundScroll {
0% {
background-position: 50% 50%;
}
33% {
background-position: 1% 50%;
}
40% {
background-position: 1% 50%;
}
66% {
background-position: 99% 50%;
}
75% {
background-position: 99% 50%;
}
100% {
background-position: 50% 50%;
}
}
<div id="mtn-scroll">
<div>
Content text goes here!
</div>
</div>

Related

How to fit div with background-image into a parent container, without causing scrolling, given the child div is rotating?

I have this super basic minimal demo, which shows a square image with border radius to make it look like a circle. It then rotates slowly. However, because it is a square, as it rotates, because it goes into a diagonal, the horizontal width grows and shrinks back, repeatedly. This is causing the parent to resize. I don't want the parent to resize, I want the parent to stay 100% width, and have the content fit inside. See the problem here (image courtesy of this):
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.container {
display: flex;
flex-shrink: 0;
padding: 64px;
width: 100vw;
}
.bg-image {
background-image: url(https://i.imgur.com/9mhwcLH.png);
background-size: contain;
-webkit-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;
transform-origin: 50% 50%;
background-position: center center;
background-repeat: no-repeat;
-webkit-animation: spin 512s linear infinite;
animation: spin 512s linear infinite;
height: 0;
padding-top: 100%;
background-size: contain;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border-radius: 50%;
}
<div class="container">
<div class="bg-image"></div>
</div>
There are two ways this could work, I don't have a preference how it is solved, either way would be fine for me.
Since it is carved into a circle, the parent width should never change (because it's a circle..). Even though the underlying div is still a square... This would mean that the change in the width of the square div should have no effect on the parent.
Or alternatively, if it is at all possible, you could potentially shrink down the child so that at a 45 degree angle, that fits perfectly into the parent, and as it rotates it just gets smaller than the parent. Either way works, knowing how to do both would be very advantageous and helpful, so I can choose when the situation is right, but knowing just one way is also enough to solve my problem.
Any ideas how to fix this and get it to work, not resizing the parent (so the parent width is constant, or so the child doesn't have an impact on the parent width)?
I made some changes to inherit the parent height and width.
Note parent height and width will be in ratio of 1:1 so the child will be in radius as you expected
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.container {
display: flex;
flex-shrink: 0;
padding: 64px;
height:300px;
width: 300px; /* parent height and with will be in ratio of 1:1*/
}
.bg-image {
background-image: url(https://i.imgur.com/9mhwcLH.png);
background-size: contain;
-webkit-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;
transform-origin: 50% 50%;
background-position: center center;
background-repeat: no-repeat;
-webkit-animation: spin 512s linear infinite;
animation: spin 512s linear infinite;
height: 0;
/*padding-top: 100%;*/
background-size: contain;
/*position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;*/
border-radius: 50%;
width:100%;
height:100%;/* inherits the parent's height and width */
}
<div class="container">
<div class="bg-image"></div>
</div>
You may clip the overflowing part of the container as follows
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.container {
display: flex;
flex-shrink: 0;
padding: 64px;
width: 100vw;
overflow-x: clip;
}
.bg-image {
background-image: url(https://i.imgur.com/9mhwcLH.png);
background-size: contain;
-webkit-transform-origin: 50% 50%;
-ms-transform-origin: 50% 50%;
transform-origin: 50% 50%;
background-position: center center;
background-repeat: no-repeat;
-webkit-animation: spin 512s linear infinite;
animation: spin 512s linear infinite;
height: 0;
padding-top: 100%;
background-size: contain;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border-radius: 50%;
}
<div class="container">
<div class="bg-image"></div>
</div>

CSS Positioning: Content that should be below the fold is appearing above the fold

I am trying to build a page that is divided into above-the-fold and below-the-fold sections to give the viewer an impression of going undersea. I've run into a stumbling block, because something just refuses to click in my brain when it comes to the concepts of CSS positioning.
Ultimately, I am trying to add another section below the fold where I can add content, but want the above water scene to stay full width. Currently, the content I want to appear below the fold is rendering in the above the fold section.
body {
background: #90caf9;
}
.above-water {
height: 100vh;
width: 100vw;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: 0;
}
.wave {
position: absolute;
width: 100%;
height: 220px;
bottom: 0;
left: 0;
background: radial-gradient(circle farthest-corner at 50% 0%, transparent 30%, #2179ea 30%, #2179ea 45%, #1d86ea 45%, #1d86ea 60%, #2a9eea 60%, #2a9eea 75%, #02b0ea 75%);
-webkit-animation: anim-h-wave 4s linear infinite, anim-v-wave 2s infinite alternate;
-moz-animation: anim-h-wave 4s linear infinite, anim-v-wave 2s infinite alternate;
animation: anim-h-wave 4s linear infinite, anim-v-wave 2s infinite alternate;
}
.container {
width: 100%;
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
#-webkit-keyframes anim-h-wave {
0% {
background-position: -100px 0;
}
100% {
background-position: 100px 0;
}
}
#keyframes anim-h-wave {
0% {
background-position: -100px 0;
}
100% {
background-position: 100px 0;
}
}
#-webkit-keyframes anim-v-wave {
0% {
background-size: 100px 220px;
}
100% {
background-size: 100px 270px;
}
}
#keyframes anim-v-wave {
0% {
background-size: 100px 220px;
}
100% {
background-size: 100px 270px;
}
}
<section id="above-the-water">
<div class="above-water">
<div class="wave"></div>
</div>
</section>
<section id="underwater">
<div class="container">
<h1>test</h1>
</div>
</section>
If needed, the full code can be found on my pen at https://codepen.io/sabey-dc/pen/zYOdNOv
You've got to add margin-top: 100vh to the underwater section because since the above water has absolute positioning, it won't push the underwater content down on its own.

Animation Width Compared to Background Image

I have a background image that resizes automatically based on screen size. I have an animation that I'm trying to get to do the same thing for consistency. However, the animation doesn't appear to be the same width or won't stretch across the entire screen.
body {
height: 100%;
margin: 0;
padding: 0;
background:url(../img/Ex.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
padding-top: 3rem;
outline-style: solid;
outline-color: #ffcd11;
outline-width: thick;
background-color: #ffcd11;
}
.rain {
height: 100%;
margin: 0;
padding: 0;
background:url(../img/snowfall2.png) center center
fixed,url(../img/snowfall3.png) center center fixed;
animation: rain 1s linear infinite;
}
.rain:before {
content: '';
position: absolute;
height: 100%;
background: white;
animation: lighting 4s linear infinite;
opacity: 0;
}
#keyframes lighting {
0%
{
opacity: 0;
}
10%
{
opacity: 0; position: 0% 0%;
}
11%
{
opacity: 1; position: 20% 100%;
}
14%
{
opacity: 0;
}
20%
{
opacity: 0;
}
21%
{
opacity: 1;
}
24%
{
opacity: 0;
}
104%
{
opacity: 0;
}
}
#keyframes rain {
0%
{
background-position: 0% 0%;
}
100%
{
background-position: 20% 100%;
}
}
<div class="carousel-caption rain">
<!-- <h1>Machine Parts Intelligence</h1> -->
<h1 class="lead">THE NEW MACHINE MODEL INFORMATION EXPERIENCE</h1>
</div>
Keyframes have been added. I haven't quite gotten the lighting effect to work as I want to, but I'm not really concerned about that at the moment. I just want the rain/snowfall effect to be the same width as the background image itself.
I just need to add the following code in my CSS:
.rain {
height: 100%;
margin: 0;
padding: 0;
left: 0;
right: 0;
background:url(../img/snowfall2.png) center center fixed,url(../img/snowfall3.png)
center center fixed;
animation: rain 1s linear infinite;
}
A left and right function took care of my issue and it expanded the width of my website.
It's difficult to know what you were going for without actually having the images or a visual representation of your goal, but this is my best guess as to what you wanted.
The main change I made was to adjust the background-position property within your keyframe to have 50% as the first value, effectively positioning it in the center of the element. Since it's named rain, I also made it fall from above to below.
body {
height: 100%;
margin: 0;
padding: 0;
background:url('https://images-na.ssl-images-amazon.com/images/I/51IwmuOPQyL._SL1052_.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
padding-top: 3rem;
outline-style: solid;
outline-color: #ffcd11;
outline-width: thick;
background-color: #ffcd11;
}
.rain {
height: 100%;
margin: 0;
padding: 0;
background:url('https://www.ikea.com/gb/en/images/products/euphorbia-acrurensis-potted-plant__0654026_pe708251_s5.jpg') center center
fixed, url('https://media.istockphoto.com/photos/cactus-in-pot-picture-id486466536?k=6&m=486466536&s=612x612&w=0&h=3CJO4XNOcS3WtAdMU4A9TuCbcwxnE9Rr6lHU4GDSwhE=') center center fixed;
animation: rain 1s linear infinite;
}
.rain:before {
content: '';
position: absolute;
height: 100%;
background: white;
animation: lighting 4s linear infinite;
opacity: 0;
}
#keyframes lighting {
0%
{
opacity: 0;
}
10%
{
opacity: 0; position: 0% 0%;
}
11%
{
opacity: 1; position: 20% 100%;
}
14%
{
opacity: 0;
}
20%
{
opacity: 0;
}
21%
{
opacity: 1;
}
24%
{
opacity: 0;
}
104%
{
opacity: 0;
}
}
#keyframes rain {
0%
{
background-position: 50% 100%;
}
100%
{
background-position: 50% 0%;
}
}
<div class="carousel-caption rain">
<!-- <h1>Machine Parts Intelligence</h1> -->
<h1 class="lead">THE NEW MACHINE MODEL INFORMATION EXPERIENCE</h1>
</div>
Let me know if this wasn't what you intended and I'll see if I can adjust it.

Animate a Sprite grid using CSS3?

I've got this sample sprite grid sheet that I need to run through and animate. I am able to reach a certain point but struggling to make it perfect. The animation is not that smooth and additionally, the image is not aligned properly. During the animation, you can see image elements not centered with other elements in the view. Here is my HTML and CSS3 code so far.
.hi {
width: 910px;
height: 340px;
background-image: url("https://simba-heroku.imgix.net/animation-homepage-tablet-retina.jpg?auto=format,compress");
position: relative;
-webkit-animation: playv 12s steps(6) infinite, playh 2s steps(4) infinite;
}
#-webkit-keyframes playv {
0% { background-position-y: 0px; }
100% { background-position-y: 100%; }
}
#-webkit-keyframes playh {
0% { background-position-x: 0px; }
100% { background-position-x: 100%; }
}
<div class="hi">
</div>
Fiddle: http://jsfiddle.net/bf5ckdv9/
I have added a background dimension style, and rearranged some of your properties
the result is almost ok; but your sprite grid seems to be out of order
.hi {
width: 910px;
height: 340px;
background-image: url("https://simba-heroku.imgix.net/animation-homepage-tablet-retina.jpg?auto=format,compress");
position: relative;
animation: playh 2s steps(5) infinite, playv 10s steps(5) infinite;
border: solid 1px blue;
background-size: 500% 500%;
background-repeat: no-repeat;
}
#keyframes playv {
0% { background-position-y: 0px; }
100% { background-position-y: 125%; }
}
#keyframes playh {
0% { background-position-x: 0%; }
100% { background-position-x: 125%; }
}
<div class="hi">
</div>

Is there a way to use CSS to make a background image act like "cover" but at 120%?

The CSS value background-size: cover will stretch an image but retain its width to height ratio. However, it will only stretch it until the smallest side reaches 100% of the parent node's same side.
/* This is an image of 100px Wide x 50px Tall, so it'll
stretch to 100% of the window height and then scale the
width larger to retain its width-to-height ratio.
*/
body
{
background-image: url( "/images/test.png" );
background-size: cover;
background-position: center;
}
I want something that will do a cover but to some larger percentage, for example 130%. (I will be using this in a CSS keyframes animation to make the background image grow/shrink)
How would I do this?
By using a pseudo element you can achieve something like that
CSS animation has a browser support at +90%: http://caniuse.com/#feat=css-animation
.bkg {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: hidden;
}
.bkg::before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-image: url(http://lorempixel.com/500/400/nature/1);
background-size: cover;
background-position: center;
animation: growme 5s forwards;
}
#keyframes growme {
from {transform: scale(1);}
to {transform: scale(1.3);}
}
<div class="bkg"></div>
Grow and shrink
.bkg {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: hidden;
}
.bkg::before {
content: '';
position: absolute;
left: -20%;
top: -20%;
width: 140%;
height: 140%;
background-image: url(http://lorempixel.com/500/400/nature/1);
background-size: cover;
background-position: center;
animation: shrinkme ease-in-out 10s infinite;
}
#keyframes shrinkme {
0% {transform: scale(1);}
50% {transform: scale(.72);}
100% {transform: scale(1);}
}
<div class="bkg"></div>
Update based on comment where some browsers have issues animation pseudo elements, where one simply use a child element instead.
.bkg {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: hidden;
}
.bkg div {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-image: url(http://lorempixel.com/500/400/nature/1);
background-size: cover;
background-position: center;
animation: growme 5s forwards;
}
#keyframes growme {
from {transform: scale(1);}
to {transform: scale(1.3);}
}
<div class="bkg"><div></div></div>
I've done this before by applying the background-image to a psuedo element such as ::before that is positioned absolutely, and applying a scale transform to it.