Centring an image in a viewport regardless of viewport width - html

I have an image slideshow that I am trying to centre in the viewport, regardless of the viewport's width. This is easily achieved if the viewport is wider than the image by using margin: auto;, however, when the viewport is narrower than the image, like on a tablet in portrait mode, the image butts up against the left margin and does not centre.
Centred in landscape mode:
Centred in portrait mode:
This is the HTML:
<div id="slidebound">
<div class="slider">
<img src="images/slider/slide04.png" alt=""/>
<img src="images/slider/slide03.png" alt=""/>
<img src="images/slider/slide02.png" alt=""/>
<img src="images/slider/slide01.png" alt=""/>
</div>
</div>
This is the CSS:
#slidebound {
width: 100%;
height: 300px;
overflow: hidden;
}
.slider {
width: 1024px;
height: 300px;
overflow: hidden;
position: relative;
margin: 0 auto;
}
.slider img {
position: absolute;
animation: slider 32s infinite;
opacity: 0;
width: 100%;
height: auto;
}
#keyframes slider {25% {opacity: 1;} 40% {opacity: 0;}}
.slider img:nth-child(4) {animation-delay: 0s;}
.slider img:nth-child(3) {animation-delay: 8s;}
.slider img:nth-child(2) {animation-delay: 16s;}
.slider img:nth-child(1) {animation-delay: 24s;}

As per i understand, you want your image to appear in center, no-matter what the screen size is. For this you can try below code:
#slidebound {
width: 100%;
height: 300px;
overflow: hidden;
display: flex;
justify-content: center;
}
.slider {
width: 1024px;
height: 300px;
overflow: hidden;
position: relative;
margin: 0 auto;
}
.slider img {
position: absolute;
animation: slider 32s infinite;
opacity: 0;
width: 100%;
height: auto;
}
#keyframes slider {25% {opacity: 1;} 40% {opacity: 0;}}
.slider img:nth-child(4) {animation-delay: 0s;}
.slider img:nth-child(3) {animation-delay: 2s;}
.slider img:nth-child(2) {animation-delay: 3s;}
.slider img:nth-child(1) {animation-delay: 4s;}
<div id="slidebound">
<div class="slider">
<img src="http://abload.de/img/a6aawu.jpg" alt=""/>
<img src="https://nxworld.net/example/css-image-hover-effects/pic01.jpg" alt=""/>
<img src="http://abload.de/img/a6aawu.jpg" alt=""/>
<img src="https://nxworld.net/example/css-image-hover-effects/pic01.jpg" alt=""/>
</div>

You can use flexbox. But as a alternative, as you know the height and width you can use position and negative margins:
position:absolute;
left:50%;
margin-left:-512px;
top:50%;
margin-top:-150px;
See it in action.

Are you ok in using flex for your case? It depends on what browsers you need to support. You can check it out using caniuse.
In the meantime, here is a sample
https://codepen.io/hellouniverse/pen/QxwNWv
where I have centred one image both vertically and horizontally to show the way
#slidebound {
height: 100vh;
}
.slider {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
margin: 0 auto;
height: inherit;
}
<div id="slidebound">
<div class="slider">
<img src="https://naomisimson.com/wp-content/uploads/image-placeholder.jpg" alt="" />
</div>
</div>

Related

How to make a div fit the width of its contents without causing them to wrap, while being wider than is parent

See the code in this jsfiddle: https://jsfiddle.net/frpL3yr1/
The idea is that I want a bar of images at the top of the screen. The img-wrapper div will later be animated via javascipt to move to the left when you mouse over. For an example of what I am ultimately attempting to accomplish, see this page. The difference is that in mine, the animation will only run when moused-over.
The issue is that in my jsfiddle and the linked example, the width of the div containing the images is hard-coded. In my case, the css hard-codes the width of img-wrapper to 200%. I need my page to support an arbitrary number of images, so I need its width to be equal to that of the contents. The way my jsfiddle is implemented, if there are more images that can fit in img-wrapper, they will wrap to a new line.
What is the best way to go about fixing this?
Approach using flexbox and animation:
html, body {
margin:0;
padding:0;
overflow: hidden;
}
.demo-ribbon {
width: 100%;
height: 70vmin;
margin-top: 2rem;
overflow: hidden;
}
.img-wrapper {
height: 70vmin;
width: 100%;
display: flex;
flex-flow: row nowrap;
justify-content: stretch;
}
img {
flex: 1;
object-fit: content;
margin: 0 .2rem;
width: 100vmin;
height: 100%;
}
.lead {
animation: bannermove 12s linear 320ms infinite paused alternate;
}
.img-wrapper:hover .lead {
animation-play-state: running;
}
#keyframes "bannermove" {
0% {
margin-left: 0%;
}
100% {
margin-left: -230%;
}
}
You will need to add prefixes in order to work in all browsers especially animation
Further reading: https://devdocs.io/css/animation
working pen: https://codepen.io/manAbl/pen/KROvjx ;
Aspect Ratio: https://www.w3schools.com/howto/howto_css_aspect_ratio.asp & https://css-tricks.com/aspect-ratio-boxes/
Hope helps! :)
Use flexbox and animation with translate :)
.demo-ribbon {
width: 100%;
overflow: hidden;
}
.demo-ribbon .img-wrapper {
display: flex;
justify-content: stretch;
margin-right: 0;
position: relative;
width: 100%;
}
.demo-ribbon .img-wrapper img {
transition: all 0.5s ease;
margin: 2px;
}
.demo-ribbon .img-wrapper img:first-child {
animation: lefttoRight 25s linear 320ms infinite paused alternate;
}
.demo-ribbon .img-wrapper img:hover {
transform: scale(1.1);
cursor: pointer;
box-shadow: 0px 3px 5px rgba(0, 0, 0, 0.2);
}
.demo-ribbon .img-wrapper:hover img {
animation-play-state: running;
}
#keyframes lefttoRight {
0% {
margin-left: 0;
}
50% {
margin-left: -200%;
}
100% {
margin-left: 0%;
}
}
<html>
<body>
<div class="demo-ribbon">
<div class="img-wrapper">
<img class="lead" src="https://media-cdn.tripadvisor.com/media/photo-s/0e/9a/e3/1d/freedom-tower.jpg">
<img src="http://static.asiawebdirect.com/m/phuket/portals/www-singapore-com/homepage/pagePropertiesImage/singapore.jpg.jpg">
<img class="" src="https://www.s-ge.com/sites/default/files/cserver/styles/sge_header_lg/streamy/company/images/Hongkong-Fotolia-48687313-rabbit75-fot-282451.jpg?itok=ANpJxrgW">
<img class="" src="https://media-cdn.tripadvisor.com/media/photo-s/0e/9a/e3/1d/freedom-tower.jpg">
<img src="http://static.asiawebdirect.com/m/phuket/portals/www-singapore-com/homepage/pagePropertiesImage/singapore.jpg.jpg">
<img class="" src="https://www.s-ge.com/sites/default/files/cserver/styles/sge_header_lg/streamy/company/images/Hongkong-Fotolia-48687313-rabbit75-fot-282451.jpg?itok=ANpJxrgW">
<img class="" src="https://media-cdn.tripadvisor.com/media/photo-s/0e/9a/e3/1d/freedom-tower.jpg">
<img src="http://static.asiawebdirect.com/m/phuket/portals/www-singapore-com/homepage/pagePropertiesImage/singapore.jpg.jpg">
<img class="" src="https://www.s-ge.com/sites/default/files/cserver/styles/sge_header_lg/streamy/company/images/Hongkong-Fotolia-48687313-rabbit75-fot-282451.jpg?itok=ANpJxrgW">
<img class="" src="https://media-cdn.tripadvisor.com/media/photo-s/0e/9a/e3/1d/freedom-tower.jpg">
</div>
</div>
</body>
</html>

Best way to create animation form left to right with css

I wan to make animation from left to right (animation start in invisible area like left: -100px) and end in invisible area too (right: -100px) I am using this code which works but not correctly on different sizes of screens becuase is in %. And i need to remake it but i dont know how.
.ip_first_block {
width: 100%;
height: 100%;
}
section {
position: relative;
overflow: hidden;
}
.ip_welcome_text {
width: 100%;
height: 70%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.astronaut1 {
position: relative;
animation: lefttoright 10s;
animation-fill-mode: forwards;
}
#keyframes lefttoright {
from {
transform: translateX(-1500%);
}
to {
transform: translateX(2200%);
}
}
<section style="height:100%;">
<div class="ip_first_block" id="ifb">
<div class="ip_welcome_text">
<div class="astronaut1">
<img src="images/astronaut.svg" height="70px" ; width="70px;" />
</div>
</div>
</div>
</section>
It's easier if you animate the position, e.g. left property:
body {margin: 0}
.astronaut1 {
overflow: hidden;
}
img {
position: relative;
left: -70px; /* starting point; needs to be at least the img width to hide it */
animation: lefttoright 10s forwards;
}
#keyframes lefttoright {
to {left: 100%} /* cover the entire parent width */
}
<div class="astronaut1">
<img src="http://placehold.it/70x70" alt="" height="70" width="70">
</div>

How to prevent images from being cropped

I'm trying to create an animated picture which contains a lot of layers.
In order to have images with right proportions on different screens I use cover css property value(I've tried it for both object-fit for images and background-size for background images). That's why my images on wide screen are croped by the browser.
The problem is that my layers are transformed(mostly rotated and moved) during the animation so there are moments when it is seen the cropped image.
Please see my example below.
How it can be prevented? Or it there some other technique?
body {
margin: 0;
padding: 0;
/*Just to imitate wide screen*/
width: 1000px;
height: 450px;
}
#container {
width: 100%;
height: 100vh;
/*Just to imitate wide screen*/
height: 100%;
overflow: hidden;
position: relative;
}
.layer {
height: 100%;
position: absolute;
width: calc(100% + 20px);
}
.layer img {
height: 100%;
width: 100%;
object-fit: cover;
}
.gulls {
animation: gulls ease-in-out 13s infinite alternate;
}
#keyframes gulls {
from {
transform: rotate(3deg) scaleX(0.95) skew(-10deg, -10deg);
}
to {
transform: rotate(-3deg) scaleX(1.05) skew(10deg, 10deg);
}
}
<div id="container">
<div class="layer">
<img src="https://firebasestorage.googleapis.com/v0/b/wedding-42174.appspot.com/o/animation%2Fsky.png?alt=media&token=25033588-d58c-4616-94e9-4974ec4157a4" alt="">
</div>
<div class="layer gulls">
<img src="https://firebasestorage.googleapis.com/v0/b/wedding-42174.appspot.com/o/animation%2Fgulls5.png?alt=media" />
</div>
</div>
Currently I have this: https://jsfiddle.net/koljada/c08qdw1m/
first of all i have to say that your birds image is a lot bigger then the birds therself (many padding around) as i see the whole image is 2048/1934...
anyway, when you use
object fit:cover he crop;
the image for save the proportion. you can using
object-fit:scale-down;
for save the proportion by scale down the image until he come inside the parent space. i add a quick exemple about how it works down here:
hope it is what you search for..
Images can be cropped with a container div with overflow: hidden; position: relative, So you can position the image inside with position: absolute. The top and left css attribute of the image has to be set a negative value.
The image exact values can be found by using sine and cosine functions.
body {
margin: 0;
padding: 0;
/*Just to imitate wide screen*/
width: 200px;
height: 100px;
}
#container {
width: 100%;
height: 100vh;
/*Just to imitate wide screen*/
height: 100%;
overflow: hidden;
position: relative;
}
.layer {
height: 100%;
position: absolute;
top: -20px;
left: -20px;
width: calc(100% + 20px);
}
.layer img {
height: 150%;
width: 150%;
}
.gulls {
animation: gulls ease-in-out 3s infinite alternate;
}
#keyframes gulls {
from {
transform: rotate(3deg) scaleX(0.95) skew(-10deg, -10deg);
}
to {
transform: rotate(-3deg) scaleX(1.05) skew(10deg, 10deg);
}
}
<div id="container">
<div class="layer gulls">
<img src="http://via.placeholder.com/350x150/000000" />
</div>
</div>
You can use overflow: hidden; position: relative to crop your images after using container div, and using position: absolute at the images to place the image.

CSS keyframe slideshow images not showing

I am creating a slideshow using keyframes.
My issue lies in the fact that only the first image actually shows, the other 4 are just ignored as you can see in my snippet.
I have looked at my CSS and don't think any of it is "useless" (correct me if there is anything unnecessary) and otherwise, I am unsure why the images wouldn't show.
Here is a working snippet of what I have done so far:
carousel {
max-width: 100%;
height: 500px;
max-height: 500px;
margin-bottom: 20px;
position: relative;
overflow: hidden;
display: block;
}
carousel img {
max-width: 100%;
width: 1920px;
height: auto;
max-height: 500px;
position: absolute;
left: 0;
top: 0;
animation: slide 20s ease-in-out infinite;
}
#keyframes slide {
0% {
left: 0;
}
100% {
left: -500%;
}
}
<carousel>
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
</carousel>
You need to manually push one image after anothe, because you have positin: absolute they are each on another , because left: 0
carousel {
max-width: 100%;
height: 500px;
max-height: 500px;
margin-bottom: 20px;
position: relative;
overflow: hidden;
display: block;
}
carousel img {
max-width: 100%;
width: 1920px;
height: auto;
max-height: 500px;
position: absolute;
left: 0;
top: 0;
animation: slide 20s ease-in-out infinite;
}
carousel img:nth-child(2) {
margin-left: 100%;
}
carousel img:nth-child(3) {
margin-left: 200%;
}
carousel img:nth-child(4) {
margin-left: 300%;
}
carousel img:nth-child(5) {
margin-left: 400%;
}
#keyframes slide {
0% {
left: 0;
}
100% {
left: -500%;
}
}
<carousel>
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080" alt="carousel-img">
</carousel>
The problem is because you are positioning all the images absolutely at top: 0px, left: 0px of the container (meaning, they are all one over the other) and also adding the same animation to them at the same time. Because of this all images move from left: 0% to left: -500% at the same time and so you get to see only one image.
I am not quite sure what effect you are wanting to achieve and so I'll give you two options. If what you want is a film-roll like effect then remove position: absolute oncarousel img, get them to display on the same line using white-space: nowrap on the parent and then animate parent's margin-left.
carousel {
max-width: 100%;
height: 500px;
max-height: 500px;
margin-bottom: 20px;
position: relative;
overflow: hidden;
display: block;
white-space: nowrap;
animation: slide 20s ease-in-out infinite;
}
carousel img {
height: auto;
max-height: 500px;
}
#keyframes slide {
0% {
margin-left: 0;
}
100% {
margin-left: -500%;
}
}
<carousel>
<img src="http://www.lorempixel.com/1920/1080/nature/1" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/2" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/3" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/4" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/5" alt="carousel-img">
</carousel>
Or, if you want to achieve an effect where each image is stacked one below the other but they start to move only after the previous image (the one above it) has completely moved out then add animation-delay to each image element such that it is equal to the amount of time required for the images above it to complete their animation. Also, change animation-duration to the total amount of time required for all 5 images to complete their animation.
carousel {
max-width: 100%;
height: 500px;
max-height: 500px;
margin-bottom: 20px;
position: relative;
overflow: hidden;
display: block;
}
carousel img {
max-width: 100%;
width: 1920px;
height: auto;
max-height: 500px;
position: absolute;
left: 0;
top: 0;
animation: slide 100s ease-in-out infinite;
}
carousel img:nth-child(1){
animation-delay: 80s;
}
carousel img:nth-child(2){
animation-delay: 60s;
}
carousel img:nth-child(3){
animation-delay: 40s;
}
carousel img:nth-child(4){
animation-delay: 20s;
}
#keyframes slide {
0% {
left: 0;
}
20% {
left: -100%;
}
}
<carousel>
<img src="http://www.lorempixel.com/1920/1080/nature/1" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/2" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/3" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/4" alt="carousel-img">
<img src="http://www.lorempixel.com/1920/1080/nature/5" alt="carousel-img">
</carousel>

Max-height of div content

Take a look at this fiddle: https://jsfiddle.net/cz1gusj6/1/
What i want to do is this bars to fit the screen size height, always.
This is the page: http://cl.ly/image/0o0e1W0X0n2Z/1-browser.jpg
This is what happens when i change the window height: http://cl.ly/image/410z3E0l420K/2-browser.jpg
This is what i want it to happen when i change the window height: http://cl.ly/image/0P3u1r0F2P1r/3-browser.jpg
I tried to assign a max-height to the div but that does not work because the content it's not images:
#container{
width: 100%;
height: 100%;
}
.container2{
max-height: 100%;
}
<div id="container">
<div class="container2">CONTENT (the bars)</div>
</div>
jsBin demo
Simply set your bars to a vh (viewport height) unit size.
Also you cannot have multiple ID inside a single page! id="progressbar" should be unique! So use class ..
Since you use vh now, don't use <br> tags, rather a margin (also in vh).
Set the Child of your progressbars to height:100%; (to fill the parent height)
If you have fixed number of bars, read on (I'm using 10 bars as an example).
DEMO: http://jsfiddle.net/8yrp8d9u/
HTML
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
<div class="progressbar"><span></span></div>
CSS
html, body {
height: 100%;
}
body {
background: #4086a4;
margin: 0;
}
.progressbar {
margin: 0 auto;
width: 30%;
height: 10%;
box-sizing: border-box;
border: 10px solid #4086a4;
border-width: 5px 0;
background: #7aabbf;
}
.progressbar span {
display: block;
width: 0;
max-width: 100%;
height: 100%;
background: white;
-webkit-animation: progress 2s 1 forwards;
-moz-animation: progress 2s 1 forwards;
-ms-animation: progress 2s 1 forwards;
animation: progress 2s 1 forwards;
}
#-webkit-keyframes progress {
from {} to {width: 100%}
}
#-moz-keyframes progress {
from {} to {width: 100%}
}
#-ms-keyframes progress {
from {} to {width: 100%}
}
#keyframes progress {
from {} to {width: 100%}
}
change container2 to:
.container2{
max-width: 100%;
max-height: 100%;
bottom: 0;
left: 0;
margin: auto;
overflow: auto;
position: fixed;
right: 0;
top: 0
}