Slideshow not centering? - html

I have a slideshow that I wanted to center, but I can't center it properly, the left part of the image stands exacly in the middle, so the image is not centered, anyone has an idea why?
Here's the following code and images:
CSS
.slider {
width: 100%;
margin: auto;
height:400px;
background-color: rgba(0,0,0,0.1);
}
.slider-wrapper{
width: 100%;
height: 400px;
}
.slide {
position: absolute;
max-width: 100%;
max-height: 400px;
margin:auto;
opacity: 0;
transition: opacity 500ms linear;
}
.slider contains: .slider-wrapper and .slide, .slide is the class I use for each image.
HTML
<div class="slider" id="main-slider">
<div class="slider-wrapper">
<img src="images/eventos/image1.png" class="slide" />
<img src="images/eventos/image2.png" class="slide" />
<img src="images/eventos/image3.png" class="slide" />
<img src="images/eventos/image4.png" class="slide" />
</div>
</div>
This image contains text-align:center and margin: 0 auto
And this one doens't
My code contains jquery so the slideshow works, if needed, please ask so I put it in too.
UPDATE
I discovered the problem, the problem is the position: absolute in those images, I deleted every css and any div I had and left the the .slide and just the images, in .slide I just left position: absolute and it still didn't work, so what can I do?

You can't apply text-align:center alignment on a absolute positioned element. Remove the absolute position and the images should center when using text-align in your wrapper:
.slider {
width: 100%;
margin: auto;
height:400px;
background-color: rgba(0,0,0,0.1);
}
.slider-wrapper{
text-align:center;
width: 100%;
height: 400px;
}
.slide {
width: 100%;
height: 400px;
opacity: 1;
transition: opacity 500ms linear;
border:1px solid black;
}
However, It is unclear from your code whether the absolute position is required. If so, you may need to resort to js calculations.
UPDATE
From OP comment, position absolute is required. Therefore, it is necessary to set the left attribute on slides. If the slide width is known, you could calculate by subtracting half of the viewport width from half of the slide width. For instance, on a 200px wide slide:
left:calc(50vw - 100px);
Assuming slides have variable width, resort to js:
var myElement = document.getElementById('foo');
var width = myElement.offsetWidth;
myElement.style.left = 'calc(50vw - ' + width / 2 + 'px)';
In lieu of finding elements by id, requiring individual ids for each slide, you may want to store all slides in an object array using getElementsByClassName('slide') and retrieving the width according to the slide currently displayed.

If you want to center an element that is set to position: absolute, you need to set the left and right properties to 0. I.e. right: 0 and left: 0. Check out the code below:
.slide {
position: absolute;
max-width: 100%;
max-height: 400px;
width: 200px;
height: 200px;
margin: 0 auto;
left: 0;
right: 0;
/* opacity: 0; */
transition: opacity 500ms linear;
border: 1px solid #000;
}
.slider {
width: 100%;
margin: auto;
height:400px;
background-color: rgba(0,0,0,0.1);
}
.slider-wrapper{
width: 100%;
height: 400px;
}
.slide {
position: absolute;
max-width: 100%;
max-height: 400px;
width: 200px;
height: 200px;
margin: 0 auto;
left: 0;
right: 0;
/* opacity: 0; */
transition: opacity 500ms linear;
border: 1px solid #000;
}
<div class="slider" id="main-slider">
<div class="slider-wrapper">
<img src="images/eventos/image1.png" class="slide" />
<img src="images/eventos/image2.png" class="slide" />
<img src="images/eventos/image3.png" class="slide" />
<img src="images/eventos/image4.png" class="slide" />
</div>
</div>

Maybe try position: fixed; and then top: 50%; left: 50%;

The answer for me was controlling the width of the of the direct children of the class uk-slider-items through uk-child-width-1-3#m as follows:
<div class="uk-container">
<h1 class="uk-heading-line uk-text-center uk-padding-large"><span>Similar Products</span></h1>
<div uk-slider="center: true">
<div class="uk-position-relative uk-visible-toggle uk-light">
<ul class="uk-slider-items uk-child-width-1-2 uk-child-width-1-3#s uk-child-width-1-3#m uk-grid uk-grid-match" >
<li v-for="(pro,index) in category" :key="index">
<div class="uk-card uk-card-default">
<div class="uk-card-media-top">
<img src="/product.jpg" alt="">
</div>
<div class="uk-card-body">
<h3 class="uk-card-title">{{pro.name}}</h3>
<p>{{pro.details}}</p>
</div>
</div>
</li>
</ul>
<a class="uk-position-center-left uk-position-small uk-background-secondary" href="#" uk-slidenav-previous uk-slider-item="previous"></a>
<a class="uk-position-center-right uk-position-small uk-background-secondary" href="#" uk-slidenav-next uk-slider-item="next"></a>
</div>
<ul class="uk-slider-nav uk-dotnav uk-flex-center uk-margin"></ul>
</div>

Related

How do I align a div element onto an image in a responsive matter?

I'm working on my portfolio site now. My projects are listed as clickable images, arranged into two columns. I want it that when a user hovers over an image, a solid block of color covers it with the project title in the center. Basically like this: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade.
I had it working perfectly when I used row/column css classes, but since I've found that to not be very responsive (on mobile, the first column gets stacked on top of the other, which makes sense but that's not the order I want), I aligned the images with float and padding instead. Now, the hover effect/link stretches across the entire page, instead of being contained in the image area.
Here's my code for reference:
CSS
.container {
position: relative;
}
.image {
display: block;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
height: 100%;
width: 100%;
transition: .5s ease;
}
.container:hover .overlay {
opacity: 1;
}
.text {
color: white;
font-size: 30px;
font-family: "Lato-Bold";
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
#media screen and (min-width: 1025px) {
.image {
float: left;
width: 45%;
padding-left: 40px;
padding-right: 15px;
padding-bottom: 30px;
}
}
HTML
<div class = "container">
<img src="image name" class = "image">
<div class="overlay" style = "background-color: #color;">
<div class="text">project title</div>
</div>
</div>
How can I fix this? I've tried adding display: block to the .overlay class. I've tried making the .overlay class the same size as the image. I've also tried wrapping the link around the container class instead of the other way around like it is now. Nothing happens at all. I've also tried adjusting the container size, but that shrunk the images and stacked them into 1 column :(
Read about Responsive Images and Flexbox
Try the following code.
Note: I changed the HTML structure slightly.
.container {
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.thumb {
position: relative;
width: 50%;
}
.thumb img {
width: 100%;
}
.thumb:hover .overlay {
opacity: 1;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
height: 100%;
width: 100%;
transition: .5s ease;
color: #000;
background: #fff;
}
.overlay .text {
position: absolute;
top: 50%;
transform: translate(-50%);
left: 50%;
text-align: center;
}
<div class="container">
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
</div>

CSS - How to keep a fixed point on a resizeable image?

i am making a map with points. My project uses bootstrap, if that helps. As my map scales due to the screen size, my points move away, and aren't fixed in the same position in regards to their parent.
I presume i also need to scale down their size as the map gets smaller or larger, in order to keep it accurate. Is there a simple way of doing this?
.pin{
position: absolute;
width: 1em;
height: 1em;
margin: 0.5em 0 0 0.5em;
border-radius: 50%;
background: #F06449;
animation-name: bounce;
animation-fill-mode: both;
animation-duration: 1s;
box-sizing:border-box;
transition: box-shadow 0.3s linear;
}
.map{
width: 40%;
height: 25%;
position: relative;
left: 56%;
background-color: #E5F9E0;
}
and my HTML:
<div>
<img src="img/output-onlinepngtools.png" class="map animated fadeIn slower" alt="Responsive image">
<div id="mapPins">
<span onclick="openOverlay()" class="pin" style="left: 60em; bottom: 30em"><p>Test</p></span>
</div>
</div>
Thanks you! :)
When a element have position: absolute you can use the top and left css properties. They also support percentage so you could do top: 45% and left: 15% to position the pin.
I have corrected it. I should've done the following:
Create a div holder for just the image and additional information. Set both that and image to be relative, and the pin to be absolute. Eg:
<div class = "mapContainer">
<img src="img/output-onlinepngtools.png" class="animated fadeIn slower">
<span onclick="openOverlay()" class="pin" style="top: 66.5%; left: 40%;"><p>Test</p></span>
</div>
Then for my css:
.mapContainer{
position: relative;
width: 50%;
box-sizing: border-box;
margin: 0 auto;
left: 30%;
}
.mapContainer > img{
height: 60%;
width: 80%;
position: relative;
margin: 0;
padding: 0;
background-color: #E5F9E0;
}
With my pin now being set with absolute positioning, as well as percentages for position.
Setting the position of both the pin and the map a position that's in percentages achieves the effect you want. Like this:
html:
<div class="test-background">
<img src="img/output-onlinepngtools.png" class="map animated fadeIn slower" alt="Responsive image"/>
<div id="mapPins">
<span onclick="openOverlay()" class="pin"><p>Test</p></span>
</div>
</div>
css:
.pin{
position: absolute;
width: 1em;
height: 1em;
border-radius: 50%;
background-color: yellow;
animation-name: bounce;
animation-fill-mode: both;
animation-duration: 1s;
box-sizing:border-box;
transition: box-shadow 0.3s linear;
/* give relative positioning */
left: 40%;
top: 20%;
}
.map{
width: 40%;
height: 250px;
position: relative;
display: block;
margin: auto;
background-color: #E5F9E0;
}
Check it out here (added some background colors for clarity and remoevd some of the animation attributes that cluttered up the css): https://codepen.io/jasperdg/pen/oNNVNro

Modify position after transition ends

I'm trying to force changing property position to change after certain part of time (after all animations will end). For this I've created sample codepen so you can see what I've already achieved (more-less):
transition: width 0.5s ease-in, height 0.5s, position 0s ease 0.6s;
https://codepen.io/anon/pen/xpQdZm
As you can see hovering on the green container triggers transition of inside container grow (from right to left). What I would like to do is to make the container decrease when we remove the hover effect but to keep the position set to absolute while transition still lasts but set position to static after all transitions ends.
The more illustrative example can be see here: https://codepen.io/anon/pen/VyVzed
Because of the changing of position from absolute to static when hover is missing, the images are flickering and it looks so ugly.
.container {
display: flex;
flex-direction: row;
width: 800px;
}
.container div {
position: relative;
flex: 1;
}
.container div img {
width: 200px;
transition: width 0.5s ease-out;
z-index: 100;
}
.container div img:hover {
height: 200%;
position: absolute;
width: 200%;
left: 0;
right: auto;
top: 0;
bottom: auto;
z-index: 200;
transition: width 0.5s ease-out;
}
.container:nth-of-type(even) div img:hover {
bottom: 0;
top: auto;
}
.container div:nth-of-type(even) img:hover {
left: auto;
right: 0;
}
<div class="container">
<div>
<img src="https://media.giphy.com/media/pqwPJPgR6qCB2/giphy.gif">
</div>
<div>
<img src="https://img00.deviantart.net/f0f4/i/2017/182/c/6/pvz_heroes_random_colored_doodle_of_nc_by_crystilialance-dbeqssx.png">
</div>
<div>
<img src="http://images4.fanpop.com/image/answers/177000/177769_1287459785835_400_300.jpg">
</div>
<div>
<img src="http://1.bp.blogspot.com/-6BwaeUjaDnM/TZSPiw4YeoI/AAAAAAAAAf4/YlxYyxxu6nE/s400/2.jpg">
</div>
</div>
<div class="container">
<div>
<img src="http://images4.fanpop.com/image/answers/177000/177769_1287459785835_400_300.jpg">
</div>
<div>
<img src="http://1.bp.blogspot.com/-6BwaeUjaDnM/TZSPiw4YeoI/AAAAAAAAAf4/YlxYyxxu6nE/s400/2.jpg">
</div>
<div>
<img src="https://media.giphy.com/media/pqwPJPgR6qCB2/giphy.gif">
</div>
<div>
<img src="https://img00.deviantart.net/f0f4/i/2017/182/c/6/pvz_heroes_random_colored_doodle_of_nc_by_crystilialance-dbeqssx.png">
</div>
</div>
I know I can set div to fixed height but the problem is I want images to be scaled by its width.
Instead of thinking about changing position why not changing how the animation works. You may try the use of scale and adjust transform-origin to decide how the image should move (and also optimize your markup)
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 400px;
}
.container img {
width: 100px;
height: 100px;
transition: 0.5s ease-out;
transform-origin: top left;
}
.container img:nth-child(even) {
transform-origin: top right;
}
.container img:nth-child(2n+5) {
transform-origin: bottom left;
}
.container img:nth-child(2n+6) {
transform-origin: bottom right;
}
.container img:hover {
position: relative;
z-index: 2;
transform: scale(2);
}
<div class="container">
<img src="https://lorempixel.com/100/100/">
<img src="https://lorempixel.com/150/150/">
<img src="https://lorempixel.com/200/200/">
<img src="https://lorempixel.com/300/300/">
<img src="http://1.bp.blogspot.com/-6BwaeUjaDnM/TZSPiw4YeoI/AAAAAAAAAf4/YlxYyxxu6nE/s400/2.jpg">
<img src="http://1.bp.blogspot.com/-6BwaeUjaDnM/TZSPiw4YeoI/AAAAAAAAAf4/YlxYyxxu6nE/s400/2.jpg">
<img src="https://media.giphy.com/media/pqwPJPgR6qCB2/giphy.gif">
<img src="https://img00.deviantart.net/f0f4/i/2017/182/c/6/pvz_heroes_random_colored_doodle_of_nc_by_crystilialance-dbeqssx.png">
</div>

Use img tag inside a div as the divs background image with text over

I have the following html:
<div class="article">
<img src="..." class="article-bg">
<h1 class="heading">Article Heading</h1>
<h2 class="author">Author Name</h2>
</div>
The article divs background image gets set dynamically, so setting the divs background in css is out, I have to use an image tag. I'm not too sure though how to use an img as the divs background, and at the same time have text over the img.
Also the height of the article div should always be 180px, I only have the following simple CSS:
.article {
height: 180px;
padding: 10px;
background-color: blue;
}
Thanks in advance for any tips!
You can do it by this way:
<div class="article">
<img src="http://www.bdembassyusa.org/uploads/images/beautiful-Bangladesh-23.jpg" class="article-bg">
<h1 class="heading">Article Heading</h1>
<h2 class="author">Author Name</h2>
</div>
Ad some more css below:
.article{
height: 180px;
padding: 10px;
background-color: blue;
overflow:hidden;
}
.article img{
position:absolute;
z-index:0;
width: 100%; // make the img fluid
height:200px;
margin:-10px;
object-fit: contain; // similar to `background-size: contain;`
}
.article h1,.article h2{
position:relative;
z-index:1;
}
Test it on jsfiddle:
http://jsfiddle.net/sarowerj/o9L72do0/
What you're looking for in z-index.
Using Z-index allows you to position one element above of the other. But do keep in mind that z-index does only work with positioned elements such as absolute or relative positioning.
You do specify a z-index as follows in the CSS:
.heading { position: absolute; top: 10px; left: 10px; z-index: 900; color: #fff; }
See this jsFiddle for a demo on how to use it:
You can use the CSS property object-fit for this.
However it is worth noting that this property has very little to no support on IE and Edge browser.
.conainer{
position: relative;
width: 500px;
height: 300px;
color: #ffffff;
overflow: hidden;
margin: auto;
}
.conainer img{
position: absolute;
top: 0;
left: 0;
transition: all 1s ease;
}
.conainer:hover img{
transform: scale(1.2);
}
.conainer .content{
position: relative;
z-index: 1;
}
.conainer .content h2{
color: white;
text-shadow: 3px 2px 10px #545454;
text-align: center;
}
<div class="conainer">
<div><img src="https://placeimg.com/640/480/nature" alt=""></div>
<div class="content">
<h2>Here's an example</h2>
</div>
</div>
You can use this code, to make <img> behave like a background image:
<img src="..." class="background-image" />
.background-image {
position: absolute;
z-index: -1;
min-width: 100%;
min-height: 100%;
left: 0;
top: 0;
pointer-events: none;
}
use
<div class="article" style="background: url(imageurl)">
</div>

rotated text that is aligned to the top left corner outside of the image?

I was wondering what would be the most efficient way to place a rotated text directly outside the image's top left corner? Please keep in mind that I would like it if the height of the text box aligns with the height of the image, and the image scale will vary (tall images, short image, wide images, etc.)
Here is a visual of what I would like to achieve:
How might I do this? Thank you in advance!
Whoops, forgot to add the jsfiddle. You can view it here!
.image.information {
display:block;
position:absolute;
margin:0;
top:45%;
left:100%;
height:100%;
-webkit-transition:all 250ms linear;
-o-transition:all 250ms linear;
transition:all 250ms linear;
transform:rotate(-90deg);
-webkit-transform:rotate(-90deg);
-moz-transform:rotate(-90deg);
-ms-transform:rotate(-90deg);
-o-transform:rotate(-90deg);
filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}
.image-wrap {
height: 100%;
width: 100%;
display: block;
}
.image-inner img {
width: 500px;
height: auto;
display: block;
}
<div class="image-wrap">
<img class="image-inner" src="http://i.stack.imgur.com/YyMHW.jpg" alt="" />
<div class="image information">
information<br/>
informa<br/>
info
</div>
</div>
This is possible using CSS3 transforms, but requires an extra element in your DOM:
<div class="image-wrap">
<img class="image-inner" src="http://media.creativebloq.futurecdn.net/sites/creativebloq.com/files/images/2013/08/korea5b.jpg" alt="" />
<div class="image information">
<div class="info-inner">
information
<br/>informa
<br/>info
</div>
</div>
</div>
Now with some absolute-positioning magic:
.image.information {
position:absolute;
left: 0;
top: 0;
bottom: 0;
width: 75px;
background: #ccc;
}
.info-inner {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
margin-top:-75px;
margin-left: -75px;
height:55px;
width: 150px;
vertical-align: bottom;
transform: rotate(90deg);
}
.image-wrap {
height: 100%;
width: 100%;
position: relative;
}
img.image-inner {
width: 300px;
margin-left: 75px;
}
Note that there's no simple way to have vertically-centered text without specifying an explicit text height. In this case I have chosen 55px. It will stretch to match the height of any image you specify.
JSFiddle: http://jsfiddle.net/zephod/ckqcLsbv/2/