I have a series of images, which have different captions. each of these captions needs to appear to float halfway over the bottom of the image. Here is a mockup of how this looks and also how I have structured it in divs. because there needs to be space between the bottom of the caption and whatever div is beneath, the caption cannot be absolutely positioned.
I am aware this could be done in javascript but this would be a hack so I'd prefer to find a CSS only solution.
...and here is the code so far: JS FIDDLE
.outer {
position: relative;
width: 320px;
margin-bottom:24px // this needs to be the space between the bottom of the caption and the next image or whatever div is beneath.
}
.image {
background-color: blue;
width: 320px;
height: 380px;
position: relative;
border-radius: 5px;
}
.caption {
position: relative;
margin-top: -24px;
/* THIS NEEDS TO BE 50% of the Captions variable height */
}
.card {
margin-right: 25%;
margin-left: 5%;
position: relative;
background-color: #FFFFFF;
padding: 24px;
line-height: 24px;
}
<div class="outer">
<div class="image">
</div>
<div class="caption">
<div class="card">
Lorem ipsum dolar sit amet consectetur idipiscine ipsumd lorem ipsum dolar sit amet. a
</div>
</div>
</div>
Negative margin-left and -right work the same, provided the element has a width. Here we apply margin-left: -10px and margin-right: 10px. First paragraph with margin-left: -10px . Second paragraph with margin-right: -10px .
Negative paddings are not allowed (and don't work) in css, only negative margins.
Positive margin values move the content towards the inner side of its position or page. But if we take negative margin values, it moves the content towards outside of its position or page. The margin property in html gives space around the outermost element's content of the box-like structure.
You can combine fixed value and percentage values in the CSS margin property. Negative values are allowed in the CSS margin property. When the value is provided as a percentage, it is relative to the width of the containing block. See also margin-top, margin-bottom, margin-left, and margin-right.
Just use position: absolute
and transform
property to do the trick.
Here is an example: https://jsfiddle.net/nawLywc9/1/
The .block
classs must be like this:
.block {
position:relative; // This is important
background-color:#FFFFFF;
padding:24px;
line-height:24px;
bottom: -50%; // This is important
transform: translateY(-50px); // This is important
width: 60%;
border-radius: 5px;
margin: 0 auto; // Only if you need to be centered
}
Here is the preview: https://jsfiddle.net/nawLywc9/1/
I've achieved this with:
/* Required to expand element to height required. */
position: relative;
/* Move up by 50% of own Height. */
transform: translateY(-50%);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With