1

I'm using multiple backgrounds in CSS (one being a kind of corporate background, and the other is a member of staff).

This is my code :

background: url("../../images/andy.png"),url("../../images/background.png") no-repeat top;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

This works fine, but I want to animate and do other CSS options specifically to the first background parameter (url("../../images/andy.png")).

Is there any possible way to reference that? It's not it's own element so how could it be possible?

Here is my HTML

<section id="intro" class="main style1 dark fullscreen">
    <div class="content container 100% 12u(mobile)">
        <header>
            <div style="width:40%;margin-right:auto;margin-left:auto;"><h2 style="font-size:50px;">a title</h2></div>
        </header>
        <div style="width:40%;margin-right:auto;margin-left:auto;">
            <p style="width:100%; margin-right:0; margin-left:0;"><i>“sometext”.</i></p>
        </div>
        <footer>
            <a href="#work" class="button style2 down">More</a>
        </footer>
    </div>
</section>
Geoff James
  • 3,122
  • 1
  • 17
  • 36
Gaz Smith
  • 1,100
  • 1
  • 16
  • 30
  • I think the ideas for using pseudo `:before` and `:after` elements are great. But, is there anything stopping you from using 2 different `div`s in a container (to size them), and then position them over one another - then you can control the background and animation for each individually? (If the animation isn't working with `:before` and `:after`) – Geoff James Oct 07 '16 at 08:46
  • Gaz -any chance you could make a JSFiddle/CodePen of your issue? Also, it is unclear where in your HTML that your CSS is being used - is it possible you could add the necessary CSS in question (the class that's got the backgrounds and animation and its name), so we can see where you're using it? Thanks – Geoff James Oct 07 '16 at 08:56
  • @GeoffJames https://jsfiddle.net/6nstfftn/ – Gaz Smith Oct 07 '16 at 09:11
  • i just pasted the whole css but line 2875 is the intro class.I am rying to make the image of the employee animate from the left and the background fade in – Gaz Smith Oct 07 '16 at 09:12
  • HOLY MOLY! I don't think you need *ALL* that CSS in there - any chance you could just put in the bits that you need? Also, could you post in your original question when you've done that for everyone to use? :) – Geoff James Oct 07 '16 at 09:13
  • Gaz - just a thought: Do *really* need Andy as a background image for something? What's wrong with including him as a normal image, and then animating that element in? Obviously you can keep the other background as-is and animate that as required – Geoff James Oct 07 '16 at 09:15
  • and i thought it would be better for responsiveness to have them as background images – Gaz Smith Oct 07 '16 at 09:21
  • I can see what you're thinking - but you could still use responsive values to animate Andy's position in from the left – Geoff James Oct 07 '16 at 09:22
  • Is this something like what you're after? : https://jsfiddle.net/6nstfftn/4/ – Geoff James Oct 07 '16 at 10:03
  • Yeah thats perfect, what did you change? – Gaz Smith Oct 07 '16 at 10:08
  • I'll add an answer ;) – Geoff James Oct 07 '16 at 10:08
  • thanks mate, much appreciated. – Gaz Smith Oct 07 '16 at 10:09
  • Answer added below. Hope it explains and makes sense what you need to do. Happy coding! ;) – Geoff James Oct 07 '16 at 10:23

3 Answers3

1

Firstly:

Your JSFiddle was a little over-complicated and animations were on wrong elements etc.

I'd removed unnecessary elements in the HTML for clarity.

Here is an updated JSFiddle that shows your desired effect https://jsfiddle.net/6nstfftn/4/

Read up on CSS Animations!:

I would highly recommend looking into CSS animations. There's a guy I follow on YouTube and he's super-awesome. He has some great tutorials - well worth a watch.

Find him at : https://www.youtube.com/devtipsfordesigners

He's done a series very recently with some videos on CSS transition/animation/effects etc. Check it out.


Steps to take:

  1. Make your #index element's position: relative;
  2. Add pseudo-elements for :before and :after, setting their position: absolute; and their top, bottom, left and right to 0 (basically making them fill the width and height of their "parent" element). Make sure to set the content: '', otherwise they will have no size!
  3. Then, for the pseudo-elements, set the background-image, and set the z-index to layer them properly - you can have them in whatever order you like
  4. Set the "default" style for each "background" - so, if you want it to start at 0 opacity (to fade in), do that; likewise if you want it to move, move it to the "start" position: TL;DR - you may end up seeing the elements appear and flash before the animation has started if you set them to the "end" position - also, if the attribute is not defined in the CSS, the animation might not know what it was to begin with
  5. Create the animations using @keyframes animationName, using from and to to set the starting and ending properties of the animation
  6. Apply the transition attribute to each of the "backgrounds" to allow smoothness
  7. Apply the animation attribute to each of the backgrounds, setting the name of each @keyframes to use. Set a duration, and use forwards to keep the end-state of the animation
  8. Sit back and enjoy a rewarding hot beverage

Here's a snippet of the CSS:

#intro {
  position: relative;
}

#intro:after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: url("http://i65.tinypic.com/2woc2o5.png") no-repeat top;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  z-index: -2;
  transition: all 0.5s ease-in-out;
  animation: background 0.5s forwards;
  opacity: 0;
}

#intro:before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: url("http://i65.tinypic.com/2mn0w8j.png") no-repeat top;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  z-index: -1;
  transition: all 0.5s ease-in-out;
  animation: andy 1s forwards;
  transform: translateX(-100%);
}

@keyframes andy{
  from { transform: translateX(-100%); }
  to { transform: translateX(0%); }
}

@keyframes background {
  from { opacity: 0; }
  to { opacity: 1; }
}

In my example I've not included the vendor-specific extensions for transition, animation, transform etc. Obviously, you can add these under your own steam for compatibility.


I would highly recommend looking into CSS Animations to get the desired effect.

There are literally hundreds (if not thousands) of them out there.

Any questions, just ask!

Hope this helps :)

Geoff James
  • 3,122
  • 1
  • 17
  • 36
  • Just another point, which I hadn't noted - don't forget to add the supported browser/vendor "kit" extensions for the `transition`, `animation` and `transform` attributes (and any others I might not have mentioned) – Geoff James Oct 07 '16 at 10:24
  • 1
    I have quite alot going on in the page, i just tried to strip all the parts that wasn't relevant to the question on the fiddle - i think thats why it was a little much. That aside, its working perfectly and thats very informative answer, thank you. – Gaz Smith Oct 07 '16 at 10:26
  • @GazSmith - no problem. Hope it helps you get a grasp of how to get what you wanted. I've edited answer to include note about the vendor-specific extensions for `transform`, `animation` etc. Also added to the note about the "default" positions of elements before they're animated. Happy coding! :) – Geoff James Oct 07 '16 at 10:32
  • Also (and I'm rambling now) - thinking about it: you wouldn't even need both the `:before` *and* `:after` pseudo-element - you could just use the one, and move the other background into the `#intro` element's CSS itself. Entirely up to you :) – Geoff James Oct 07 '16 at 10:35
  • Could i ask another quick question Geoff? – Gaz Smith Oct 07 '16 at 16:17
  • @GazSmith You can - my answer might probably be "ask another question and link me in", but go ahead..? – Geoff James Oct 07 '16 at 16:17
  • I want to add a third layer its obviously not #intro:after:after, but is this possible? – Gaz Smith Oct 07 '16 at 16:21
  • I would say to have the `#intro` as one layer, a `#intro:before` as a second and a `#intro:after` as the third. Like I mentioned before, I would try a similar concept with just normal elements, rather than backgrounds - as you could have unlimited elements and animations! Hope this helps : – Geoff James Oct 07 '16 at 16:22
0

The easiest solution would be to add the background.png in the :after or :before pseudo element, ale change the stuff member with this approach How do I change the background image using jQuery animation?

I don't think there is a way to change only one bg via css. You would have to add/change both.

Community
  • 1
  • 1
Zdenek Hatak
  • 1,135
  • 14
  • 23
0

You can do like this,

.my-class:before{
     background: url("../../images/andy.png");
     -webkit-background-size: cover;
     -moz-background-size: cover;
     -o-background-size: cover;
     background-size: cover;
    /* Do the Animation for this Background image*/
}
.my-class:after{
     url("../../images/background.png") no-repeat top;
     -webkit-background-size: cover;
     -moz-background-size: cover;
     -o-background-size: cover;
     background-size: cover;
    /* Do the Animation for this Background image*/
}

Here is HTML

<section id="intro" class="main style1 dark fullscreen">
    <div class="content container 100% 12u(mobile)">
        <header>
            <div style="width:40%;margin-right:auto;margin-left:auto;"><h2 style="font-size:50px;">a title</h2></div>
        </header>
        <div style="width:40%;margin-right:auto;margin-left:auto;">
            <p style="width:100%; margin-right:0; margin-left:0;">
                <i>“sometext”.</i>
            </p>
        </div>
        <footer>
            <a href="#work" class="button style2 down">More</a>
        </footer>
    </div>
</section>
Lukasz Koziara
  • 4,274
  • 5
  • 32
  • 43
Samir
  • 1,312
  • 1
  • 10
  • 16
  • Didn't work that, just leaves it blank for some reason. – Gaz Smith Oct 07 '16 at 08:39
  • Can you please add some HTML ? – Samir Oct 07 '16 at 08:40
  • Are you adding the background image to the `section`? – Samir Oct 07 '16 at 08:47
  • I can't see the usage of `.my-class` in your example @Samir - is it meant to be on the `section` element? – Geoff James Oct 07 '16 at 08:49
  • i have not added any HTML @GeoffJames, The HTMl is added by the @Gaz smith. I am trying for the soultion. As you mention in your comment, to use another `HTML element` for another image will be preferable. – Samir Oct 07 '16 at 08:52
  • Thanks @Samir - but it looks like your HTML is just copy/pasted from Gaz's - I would suggest giving your HTML example with the usage of the `.my-class` to see where it needs to go in the original code to provide the solution :) – Geoff James Oct 07 '16 at 08:54
  • Ahhhh - that would explain it @GazSmith! Haha – Geoff James Oct 07 '16 at 09:03
  • @Samir - just re-looking at your code - don't the `:before` and `:after` elements need a `content: ''` attribute, otherwise they have no size... and it would need to be positioned absolute with top, bottom, left, right to fill? Just off the top of my head... – Geoff James Oct 07 '16 at 09:44
  • @GeoffJames sorry i dont understand? can you edit on your post? – Gaz Smith Oct 07 '16 at 09:51