How to position a button centered on top of an image - html

I have been trying to make a simple site like this. The button never does show up as intended. I want #play_button to show up exactly on the play button image in the background. How can it be done?
My CSS code:
body {
background: url('http://oi44.tinypic.com/33tjudk.jpg') no-repeat center center fixed;
background-size:cover; /*For covering full page*/
}
#play_button {
position:relative;
transition: .5s ease;
top: 191px;
left: 420px;
right: -420px;
bottom: -191px;
}
#play_button:hover {
-webkit-transform: scale(1.05);/*Grows in size like Angry Birds button*/
-moz-transform: scale(1.05);
-ms-transform: scale(1.05);
-o-transform: scale(1.05);
}
Just one thing more, problem occurring is that if I resize the browser window, then the image moves to a new position.
UPDATE:
Problem solved :) Here, in this example, you can see how the button remains in the center of the page even if you resize the browser window.As always, you can tweak the left and top offsets to get the desired results. Here's the code.

Try using absolute positioning, rather than relative positioning
this should get you close - you can adjust by tweaking margins or top/left positions
#play_button {
position:absolute;
transition: .5s ease;
top: 50%;
left: 50%;
}
http://jsfiddle.net/rolfsf/9pNqS/

I'd use absolute positioning:
#play_button {
position:absolute;
transition: .5s ease;
left: 202px;
top: 198px;
}

It seems some what center of the screen. So I would like to do like this
body {
background: url('http://oi44.tinypic.com/33tjudk.jpg') no-repeat center center fixed;
background-size:cover;
text-align: 0 auto; // Make the play button horizontal center
}
#play_button {
position:absolute; // absolutely positioned
transition: .5s ease;
top: 50%; // Makes vertical center
}

So, the trick here is to use absolute positioning calc like this:
top: calc(50% - XYpx);
left: calc(50% - XYpx);
where XYpx is half the size of your image, in my case, the image was a square. Of course, in this now obsolete case, the image must also change its size proportionally in response to window resize to be able to remain at the center without looking out of proportion.

Related

Pure CSS Parallax - Firefox translateZ issue? Only half of content is visible on load

I am fiddling around with making a parallax website, have been following the steps from Keith Clark to slowly get to know parallax. However, I stumble upon an issue that looks like a Firefox issue? On load, the first layer and the darkseagreen background layer are cut in half. If I change the translateZ property from -1px to 0, everything is loaded correctly but then the parallax effect isn't working anymore.
If I scroll down or adjust the size of my browser, the rest is visible, but I would like to have it visible on the initial load.
Link to codepen I'm using Firefox 80.0.
.parallax {
perspective: 1px;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
}
.parallax__layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
padding: 100vh 0;
width: 100%;
}
.parallax__layer--base {
transform: translateZ(0);
}
.parallax__layer--back {
transform: translateZ(-1px);
background-color: darkseagreen;
width: 100%;
}
.title {
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: #fff;
text-align: center;
}
In my case it was an inherited 'overflow: hidden' that clipped the content in FireFox unnecessarily.
Reading through the original docs I found:
One important rule to keep in mind when grouping elements is, we
cannot clip the content of a group. Setting overflow: hidden on a
parallax__group will break the parallax effect. Unclipped content will
result in descendant elements overflowing, so we need to be creative
with the z-index values of the groups to ensure content is correctly
revealed/hidden as the visitor scrolls through the document.
With this you have to deal with the z-index cleverly, which is kind of an issue in my approach, but I can handle it.
In particular by using intersection observers. Once a certain intersection is reached the z-index is flipped. This makes the entire code more complex, which is what I'm afraid of, but it seems to work.

Using CSS to zoom on an image on hover but maintain the image size. I got it to work, but it's only displaying one corner of the image

I have an image near the top of a webpage. I've made it so that when I hover on the image, it zooms in slightly. However, in doing so, I've messed something up that causes the image to only display one portion whether hover is activated or not. I've tried removing portions of the code I added, but can't seem to fix it without completely removing the hover animation. I've also tried changing margin, padding, and position. I'm using Bootstrap 4 if that makes a difference. I'm sure it's something simple, I just can't seem to figure out what needs to be changed.
Here's a link to the Codepen: https://codepen.io/amandathedev/pen/zyEyze
Here's the relevant portion of the CSS:
.imgBox {
float: left;
position: relative;
width: 640px;
height: 360px;
margin: 0 auto;
/* justify-content: center;
display: inline-block; */
overflow: hidden;
}
.imgCard {
position: absolute;
top: 0;
left: 0;
}
.imgCard img {
-webkit-transition: 0.4s ease;
transition: 0.4s ease;
}
.imgBox:hover .imgCard img {
transform: scale(1.05);
-webkit-transform: scale(1.05);
}
You need to set transform-origin to center so that it will scale from center on, so your css must look like this:
..other css
.imgBox:hover .imgCard img {
transform: scale(1.05);
transform-origin: center;
-webkit-transform: scale(1.05);
}
Looking at your code example on codepen, the solution looks to be making the width of the img 100%. So in your example you would do something like:
.photo {
width:100%
}
However, this cuts off the bottom of the image. You're going to need to adjust the height of the imgBox that contains the imgCard. It's currently set to 360px. Because of the way your example is written, it will probably be best for you to just choose a number so that the resulting image will have the same aspect ratio as the original image (playing around with it, 478px looks like the magic number to show the entire image).

global wrapper div min-height 100vh breaks children fixed on mobile

In html i'm globally wrapping content with a div.wrapper min-height:100vh; for :target push-left menu, on mobile it breaks all childrens position:fixed; and stick it to the top of page and not to the visible part.
If you have any idea i tried many many solutions..
You can see this at : http://test.lelabdigital.fr where nav-button-open on the top left corner is position fixed but stick to the top when you scroll bottom.
Thanks
In your main.css 1422 line add position: fixed; .
body:target aside.mobile-nav {
z-index: 9999;
overflow-y: hidden;
position: fixed;
}
In main.css line 1409 try to remove this style it will wprk
.wrapper {
transform: translateX(0) translateZ(0);
transition: transform .2s;
will-change: transform;
}
You also need to remove one more style from your main.css
body:target .wrapper {
transform: translateX(50vw);
}
If you not romve this , then nav-button-open will not fix when you sroll

Moving an div from the top left corner to bottom right using css

I am trying to move a div from top left corner to bottom right corner using only CSS (this is required for my assignment). Also, I need to see the transition happening (the div sliding to the bottom). I tried using transform:translate() but i can't get my div to go to the bottom corner! Here is what I've done until now: http://codepen.io/ascii-hero/pen/JXEXVB
Tldr;
Solution 1: Using Left/Right/Top/Bottom positioning
In order for your div to move, you will need to set a parent element to relative, and the div to absolute positioning. Note, since the html element is the 'top most' element of the html tree, it is automatically assumed this relative position.
div {
background: red;
height: 50px;
width: 50px;
position: absolute;
top: 0;
left: 0;
}
html:hover div {
top: auto;
left: auto;
bottom: 0;
right: 0;
}
<div></div>
Solution 2: Transforms
Using transforms is great, as you can also add transitions for a smooth effect. Just note you'll need to add just a slight alteration to solution 1.
div {
background: red;
height: 50px;
width: 50px;
position: absolute;
top: 0;
left: 0;
transition: all 0.4s;
}
html:hover div {
top: 100%;
left: 100%;
transform: translate(-100%, -100%);
}
<div></div>
Explanation of Solution 2
To allow for transforms, the DOM needs to know the start point, the end point, and the duration explicitly. So hence, the start is set to
top:0; left:0;
To represent the top and left vales.
The 'duration' can be set using the transition property. Here I have set this to 0.4s(econds), but this can be altered to any suitable value.
Lastly, and most crucially, you need to set a definitive end point to your transform. Here you will notice the
top:100%;left:100%;
However, as I am sure you are aware in doing that it will put the very top left corner at this position, (100%,100%) so to speak. It is hence the reason for the inclusion for the translate to 'move the box back onto the screen'. The translate property takes two values, the X and Y disposition. By using a % as the unit, it will move a % of the size of either the width or height of the box, depending on the axis you are moving it. In other words, using
transform:translate(-100%, -100%);
will move the box 100% (of itself) to the left, and 100% (of its height) up, hence it can be seen in the bottom right of the page.
try this
#block:hover {
left: 100%;
top: 100%;
-webkit-transition-property: left, top, background, -webkit-transform;
-webkit-transition-duration: 2s, 2s, 1s, 1s;
transform:translate(-100%, -100%);
}
I'm on mobile so I hav t tested it but it /should/ work
So you want to move the div to bottom right corner. In your code you are doing
#block {
background: cornflowerblue;
width: 100px;
height: 100px;
left: 0;
top: 0;
}
so instead of top to be 0, you want bottom to be 0, and instead of left you want right.

Fade background image moves in Firefox

I am using the method outlined here to fade in a background image on hover of an element.
My codepen example:
http://codepen.io/anon/pen/vqtjf
HTML:
<div><span></span></div>
CSS:
div {
position: relative;
width: 219px;
height: 218px;
background: url(https://dl.dropboxusercontent.com/u/3454522/home-option-icon-off.png) no-repeat;
}
span {
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background: url(https://dl.dropboxusercontent.com/u/3454522/home-option-icon-energy.png) no-repeat;
opacity: 0;
-webkit-transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
-o-transition: opacity 0.5s;
}
div:hover span {
opacity: 1;
}
The problem I'm having is that in Firefox (Mac) the background image of the span is not quite aligned with the background image of the span so when it fades in you can see a small movement (In the codepen the shift is vertical but in my project where the code is amongst a whole lot of other junk I actually had a horizontal shift). If you resize the Firefox window the problem is fixed.
A screencast of the effect can be seen here:
https://dl.dropboxusercontent.com/u/3454522/firefox-fadebg-problem.mp4
(View at 100% to see the problem which is subtle).
Any idea on whats causing this or how to fix?
I think it's a regression in how Firefox renders images with opacity animation, especially when the images has been resized with HTML width/height attributes (usually by more than half).
The effect can be very subtle like a slight off-setting (~1 px) or a kind of antialiasing.
STR:
1. Open the testcase I joined
2. Move the mouse over the images to animate the opacity
3. Try at different zoom levels to observe the off-setting/antialiasing
WORKAROUND: adding "box-shadow: #000 0em 0em 0em;" to images fixes the bad rendering.
source: https://bugzilla.mozilla.org/show_bug.cgi?id=745549
I had the same problem. Solved it by adding the following to the images css.
transform: translate3d(0,0,0);