I have an image gallery. I want to display a piece of content (# of likes the photo has on instagram)in the center of the image when the image is hovered over. This content and the image are siblings under the same parent element, so I'm trying to use position: relative; and bottom: 50%; on the content. The result is that the content (class of p.likes) does not move up 50% of it's parent element. Why is this not working?
.photo-box {
width: 100%;
display: inline-block;
margin: 0 0 .75em;
background-color: lightgrey;
/*border: 1px solid pink;*/
}
.photo-box .image-wrap {
width: 100%;
height: auto;
max-height: 100%;
padding: none;
background-color: lightgrey;
/*border: 1px solid red;*/
}
.image-wrap img {
width: 100%;
display: inline-block;
margin-bottom: 0px;
padding: none;
}
.image-wrap:hover img {
opacity: .8;
}
.image-wrap a {
height: 100%;
width: 100%;
margin: 0px;
padding: none;
}
p.likes {
width: 100%;
visibility: hidden;
position: relative;
bottom: 50%;
text-align: center;
font-family: 'Open Sans', sans-serif;
color: #fff;
/*border: 1px solid red;*/
}
.photo-box:hover p.likes {
visibility: visible;
}
<div class="photo-box">
<div class="image-wrap wow fadeIn">
<a href="{{link}}" target="_blank">
<img src="{{image}}">
</a>
<p class="likes"><i class="fa fa-heart" aria-hidden="true"></i> {{likes}}</p>
</div>
</div>
The parent element needs a set positioning in order for the child elements to move in relation to the container. One way you can do this is first adding position: relative on the .image-wrap parent div. Then, on the .likes class, replace the relative positioning with position: absolute.
I added your code to a pen to test this out (and modified it slightly), but is this what you're going for? I threw a transition in there but you can remove that and play with the bottom properties to get it where you want. Also note that I added a static height to the parent.
Pen
Related
What is the best way to have an image on top of a card with content underneath it?
I have tried negative margins but I have had no luck on with this approach.
I attached an image of what the look and feel I am going for:
Here is my attempt on code
.card {
text-decoration: none;
margin-right: 20px;
text-align: center;
}
.card__section {
overflow: hidden;
padding-bottom: 50px;
display: block;
position: relative;
}
.card__inner {
background: black;
padding: calc(35px + 30%) 35px 35px;
color: white;
width: 100%;
display: block;
position: relative;
}
.card__image {
width: 80%;
height: auto;
margin: 0 auto -30%;
position: relative;
z-index: 1;
}
<a href="#" class="card">
<section class="card__section">
<img
class="card__image"
src="http://via.placeholder.com/340x220"
alt=""
/>
<div class="card__inner">
<h1>
This is a static template, there is no bundler or bundling
involved!
</h1>
</div>
</section>
</a>
you need to add below css
.card__section {
overflow: visible;
}
.card__image {
margin-bottom: 20px;
margin-top: -100px;
}
section.card__section {
margin-top: 60px;
}
and put your image in card__inner div
see https://codesandbox.io/s/card-hover-5n0yf?file=/index.html:450-461
hope this helps
You're almost there! There are two things that need to be updated:
1. Remove extra % in .card__image
.card__image {
width: 80%;
height: auto;
margin: 0 auto -30%%; // <-- Remove extra % at the end.
position: relative;
z-index: 1;
}
2. Adjust padding in .card__inner due to negative margin on previous element.
.card__inner {
background: black;
// Update padding to below. Negative margin on previous element needs to be added to the top padding.
padding: calc(35px + 30%) 35px 35px;
color: white;
width: 100%;
display: block;
position: relative;
}
Sandbox: https://codesandbox.io/s/card-hover-wiilf
I'm trying to get the red border that contains the text to stretch to the bottom of the parent div. Height 100% won't do it. It essentially needs to be the same height as the image.
Note: when viewing the demo, it might be worth shrinking your browser window.
How can I get this to size properly when the 3:2 image is setting the height?
Many thanks for any help!
.the-window {
width: 100%;
height: auto;
display: block;
float: left;
}
.the-image img {
display: block;
width: 100%;
height: auto;
margin: 0;
}
.the-image {
float: left;
background-size: cover !important;
width: 45%;
background-position: center center !important;
display: block;
background: pink;
box-sizing: border-box;
border: 1px solid #ccc;
border-right: none;
}
.the-details {
display: block;
float: left;
width: 55%;
height: 80px;
box-sizing: border-box;
border: 1px solid red;
padding: 10px;
overflow: hidden;
}
.the-header {
padding: 0;
font-size: 14px;
color: #333;
}
<div class="the-window">
<a href="#">
<div class="the-image" style="background:url('#')">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Aspect_ratio_-_3x2.svg/270px-Aspect_ratio_-_3x2.svg.png" alt="">
</div>
</a>
<div class="the-details">
<h4 class="the-header">Some text will be going in here</h4>
</div>
</div>
You can do it with the Flexbox:
/* commented out the unnecessary styles */
* {box-sizing:border-box} /* use * {box-sizing:border-box;margin:0;padding:0} to keep the same height of both all the way up when resized vertically */
html, body {margin:0}
.the-window {
/*width: 100%;*/
/*height: auto;*/
/*float: left;*/
display: flex; /* displays flex-items (children) inline, where flex-items have the same height by default, which is dictated by the height of the "tallest" one / can also use the display: inline-flex if you only want it to take the contents width */
}
.the-image img {
display: block;
max-width: 100%; /* modified */
max-height: 100vh; /* vertically responsive */
margin: 0;
}
.the-image {
/*float: left;*/
background-size: cover !important;
/*width: 45%;*/
background-position: center center !important;
/*display: block;*/
background: pink;
border: 1px solid #ccc;
border-right: none;
}
.the-details {
/*flex: 1; uncomment if you want it to take the remaining horizontal space*/
/*display: block;*/
/*float: left;*/
/*width: 55%;*/
/*height: 80px;*/
border: 1px solid red;
padding: 10px;
overflow: hidden;
}
.the-header {
padding: 0;
font-size: 14px;
color: #333;
}
<div class="the-window">
<a href="#">
<div class="the-image" style="background:url('#')">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Aspect_ratio_-_3x2.svg/270px-Aspect_ratio_-_3x2.svg.png" alt="">
</div>
</a>
<div class="the-details">
<h4 class="the-header">Some text will be going in here</h4>
</div>
</div>
One way is to use flexbox, as already shown in another answer. Another way is to put the image inside the "details" container (which has 100% width, or any desired width), float the image to the left and add height: auto and overflow: auto to its container to make it wrap the complete height of the image.
I erased the unnecessary CSS in the following snippet and added a margin-right to the image to create some distance to the text:
.the-details {
width: 100%;
height: auto;
box-sizing: border-box;
border: 1px solid red;
overflow: auto;
}
.the-details img {
float: left;
margin-right: 20px;
}
.the-header {
font-size: 14px;
color: #333;
}
<div class="the-window">
<a href="#">
<div class="the-details">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Aspect_ratio_-_3x2.svg/270px-Aspect_ratio_-_3x2.svg.png" alt="">
<h4 class="the-header">Some text will be going in here</h4>
</div>
</div>
try to remove the height from .the-details then add display:flex; align-items:stretch; to the style of .the-window and give .the-image width:100%
There is a block 225px. Inside you insert a picture large size (850px). And she goes outside.
It looks like this:
.content {display: inline-block;}
.column {float:right; width:225px;}
.slider {
border: 1px solid;
width: 220px;
padding: 5px;
}
.single-slide img {
width: auto;
}
<div class="content">
<div class="column">
<div class="slider single-slide">
<div><img src='https://s-media-cache-ak0.pinimg.com/736x/2c/d0/19/2cd0197c5eb8c1f84e81734f97e80cd3.jpg' /></div>
<div>to place the center of the image</div>
</div>
</div>
</div>
Using slick slider. I want to add dots to control the slider.
When I add the dots they are placed in the middle of the block. It's okay, I understand. But I want to place them in the center of the picture. How to do it?
UPD: The image goes beyond the block, that's right! And I need to do to the dots were at the center of the image, not the block
I see that your dots are in the center of the window.
To do this, your dots list must be inside a container with the image's width.
I think you should enlarge your slider or limit image's width with a max-width: 100%;
if you want to need image in box then change
.single-slide img { width: auto;}
to
.single-slide img { max-width: 100%;}
Updated: The inherit keyword specifies that a property should inherit its value from its parent element [ Source ]. and image can't be a parent element. see this example.
Here is a solution on based on inherit and position on your latest update.
See the html section I have added a class "img-holder" on div which hold the img and add a class "div_center" on div which contain text's.
img inherit width from its parent div class "img-holder". "img-holder" and "div_center" inherit width from parent div class slider.
N.B: if you set the img width auto text will always center of the div class slider.
.content {
display: inline-block;
}
.column {
float: right;
width: 225px;
}
.slider {
border: 1px solid #000;
width: 220px;
padding: 5px;
position:relative;
}
.img-holder {
width: inherit;
}
.img-holder img {
width: inherit;/*width auto default value. The browser calculates the width*/
}
.div_center {
position: absolute;
top: 50%;
left: 0;
right: 0;
text-align: center;
}
<div class="content">
<div class="column">
<div class="slider single-slide">
<div class="img-holder">
<img src='https://s-media-cache-ak0.pinimg.com/736x/2c/d0/19/2cd0197c5eb8c1f84e81734f97e80cd3.jpg' />
</div><!--./ img-holder -->
<div class="div_center">
to place the center of the image
</div><!--end of txt_center -->
</div><!--./ slider single-slide-->
</div><!--./ column -->
</div><!--./ content -->
Previous: Position absolute for slick-dots class but you dont set any relative position for that. so you need to add position relative to your slide div.
And make the image responsive. I have added responsive property for your image on css section and added border on li for clear view.
.content {display: inline-block;}
.column {float:right; width:225px;}
.slider {
border: 1px solid;
width: 220px;
padding: 5px;
position: relative;
}
.single-slide img {
max-width:100%;
display:block;
height:auto;
margin:0 auto;
}
.slick-dotted.slick-slider
{
margin-bottom: 30px;
}
.slick-dots
{
position: absolute;
top:50%;
left:0;
right:0;
display: block;
width: 100%;
padding: 0;
margin: 0;
list-style: none;
text-align: center;
}
.slick-dots li
{
position: relative;
display: inline-block;
width: 20px;
height: 20px;
margin: 0 5px;
padding: 0;
border:2px solid red;
cursor: pointer;
}
.slick-dots li button
{
font-size: 0;
line-height: 0;
display: block;
width: 20px;
height: 20px;
padding: 5px;
cursor: pointer;
right: 0;
bottom: 0;
left: 0;
color: transparent;
border: 0;
outline: none;
background: transparent;
}
.slick-dots li button:hover,
.slick-dots li button:focus
{
outline: none;
}
.slick-dots li button:hover:before,
.slick-dots li button:focus:before
{
opacity: 1;
}
.slick-dots li button:before
{
font-family: 'slick';
font-size: 12px;
line-height: 20px;
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 20px;
content: '•';
text-align: center;
opacity: .25;
color: black;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.slick-dots li.slick-active button:before
{
opacity: .75;
color: black;
}
<div class="content">
<div class="column">
<div class="slider single-slide">
<div><img src='https://s-media-cache-ak0.pinimg.com/736x/2c/d0/19/2cd0197c5eb8c1f84e81734f97e80cd3.jpg' /></div>
<ul class="slick-dots">
<li class="active"><button></button></li>
<li><button></button></li>
<li><button></button></li>
</ul>
</div>
</div>
</div>
I have 3 images in a div placed horizontally. When I resize the page the images changes their position and float in a vertical alignment.I want the page responsive and when i resize the page i want that the whole div is adapted and resized. I need also to put in the page others div with others images. I want the images align in the bottom side.Sameone help me please?
This is the code example.
.box{
display: inline-block;
position: relative;
bottom:100%;
width: 100%;
margin: 5% 5% 0 0;
font-size: 12px;
}
.box:before{
content: "";
display: block;
padding-top: 100%;
}
img {
max-width:100%;
height:auto;
border:1px solid black;
text-align:center;
}
.content{
/* Positioning */
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
/* Styling */
border:1px solid;
padding: 30px;
text-align: center;
vertical-align: center;
text-transform: uppercase;
color: #fff;
}
<div class="box">
<div class="content">
<img src="http://fantapr.altervista.org/paul.jpg" width="350px">
<img src="http://fantapr.altervista.org/paul.jpg" width="350px">
<img src="http://fantapr.altervista.org/paul.jpg" width="350px">
</div>
</div>
strong text
Just add this to the img:
See fiddle https://jsfiddle.net/16q2xr8k/27/
img {
width: calc((100% / 3) - 2px);
height: auto;
border: 1px solid black;
text-align: center;
}
I'm trying to show off the best hairstyles that I've done on a web site.
When you hover over the images, it gives a little information about each of the styles. The hover works fine and shows up, but even though I have the height set as 100% on the information div, it only takes up enough space needed for the text. I'm using display: table to get the text centered vertically "on the image". When I change it to display block, it works fine, but the vertical centering is a must. This is extremely annoying & after 2 hours of fiddling with it I simply cannot get it to work. Please help! http://jarahairandmakeup.tumblr.com/
div.post {
width: 50%;
height: auto;
position: relative;
float: left;
}
div.information {
display: table;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.65);
text-align: center;
z-index: 2;
opacity: 0;
}
div.post:hover .information { opacity: 1 !important; }
div.info {
color: #fff;
display: table-cell;
vertical-align: middle;
font: 11px/1.4 'Gudea';
letter-spacing: .3em;
text-transform: uppercase;
padding: 0 5%;
}
div.info h3 {
font: 100 20px/1 'Gudea';
margin: 0; padding: 0;
letter-spacing: 6px;
}
div.info hr { color: #fff; background: #fff; margin: 20px auto;}
div.image {
float: left;
position: relative;
z-index: 1;
}
div.image img.hairstyle {
width: 100%;
height: 100%;
display: block;
}
<div class="post">
<a href="/ciara">
<div class="information">
<div class="info">
<h3>CURLED UPDO</h3>
<hr />
A romantic style updo with braided accents for prom
</div>
</div>
<div class="image">
<img src="http://static.tumblr.com/ww0u3rz/BRjn5dotq/img_0919.jpg" class="hairstyle">
</div>
</a>
</div>
I'd like to make it so the black background of the information div takes up the entire length of the photo.
use display:block instead of table.
Edit:
Just wrap the info div with a wrapper and have the following css:
display: table;
position: absolute;
height: 100%;
width: 100%;
it works fine: http://cloud.kerim.me/image/2p3W1x011m0I