best fit img with caption into element - html

Building an image slideshow, I have a container div of arbitrary size and aspect ratio into which I have to best fit an image, centred, with a caption overlayed at the bottom of the image and fitting its width. My best solution to date is to contain the image and the caption in a child element of the container but I'm having trouble centring it. This should be such a simple thing that I can't believe it's not staring me in the face but I can't see it. The code below uses a portrait format image but I need it to handle landscape also. I'm using React so jQuery is out.
.container {
position: relative;
width: 80%;
height: 48vw;
/* 4:3 */
margin: 0 auto;
display: flex;
justify-content: center;
}
.img-wrap {
background-color: #efe;
}
img {
position: absolute;
max-width: 100%;
max-height: 100%;
}
.caption {
position: absolute;
bottom: 0;
color: #fff;
background-color: rgba(153, 153, 153, 0.541)
}
<div class="container">
<div class="img-wrap">
<img src="https://png.pngtree.com/thumb_back/fw800/background/20190223/ourmid/pngtree-full-aesthetic-aurora-night-sky-background-skystarry-skystarnight-viewbackgroundstar-image_87582.jpg" height="1600px">
<div class="caption">Caption Content</div>
</div>
</div>

Update your code like below:
.container {
width: 80%;
height: 48vw;
/* 4:3 */
margin: 5px auto;
text-align: center;
}
.img-wrap {
background-color: #efe;
height: 100%;
display: inline-block;
position: relative;
}
img {
max-width: 100%;
height: 100%;
display: block;
object-fit: contain; /*or cover if you want to cover all the area*/
object-position: bottom;
}
.caption {
position: absolute;
left: 0;
right: 0;
bottom: 0;
color: #fff;
background-color: rgba(153, 153, 153, 0.541)
}
<div class="container">
<div class="img-wrap">
<img src="https://i.picsum.photos/id/10/400/600.jpg">
<div class="caption">Caption Content</div>
</div>
</div>
<div class="container">
<div class="img-wrap">
<img src="https://i.picsum.photos/id/10/600/300.jpg">
<div class="caption">Caption Content</div>
</div>
</div>

don't position absolute the image,
also if the caption is the sibling of the image,
set the size of the parent and set the image as 100% if the parent's width and height
then you can simply use the text-align: center on the caption to center it.
edit :
keep the existing style of a caption for positioning
fiddle : https://jsfiddle.net/hellooutlook/6ep4Lofz/4/

Related

Fit images to parent and add nameplates over them

I am trying to insert some images and add nameplates over them. Images can be vertical, horizontal, and square. They are static and added manually.
What I expect:
long images are scaled to fit max-750px wide parent
tall images are scaled to be no more than 80vh, otherwise fit parent width
nameplates always cover 100% of image width
Exactly how it should look. Blue is article, parent, 1000px wide. Red frames are containers .art, orange is img, black is span with plain text.
What I made so far.
Html:
<div class="art">
<img src="./some/path">
<span>Image name</span>
</div>
Css (scss):
.art {
max-width: 75%;
margin: 0 auto;
position: relative;
img {
object-fit: contain;
width: 100%;
max-height: 80vh;
}
span {
box-sizing: border-box;
position: absolute;
left: 0;
width: 100%;
bottom: 1rem;
padding: 1rem;
background-color: rgba(0,0,0,0.85);
}
}
Problem: works for long images, does not for tall. Nameplates stick outside of image.
Please, check this code. Here is your solution.
add a DIV for Image parent and also add CSS for this.
.art {
max-width: 75%;
margin: 0 auto;
position: relative;
text-align:center;
}
.art-container{
display:inline-block;
position:relative;
}
img {
object-fit: cover;
height: 80vh;
max-width:100%;
}
span {
box-sizing: border-box;
position: absolute;
color:white;
left: 0;
width: 100%;
bottom: 1rem;
padding: 1rem;
background-color: rgba(0,0,0,0.85);
}
}
<div class="art">
<div class="art-container">
<img src="https://i.pinimg.com/originals/af/8d/63/af8d63a477078732b79ff9d9fc60873f.jpg">
<span>Image name</span>
</div>
</div>
<!-- https://cdn57.androidauthority.net/wp-content/uploads/2015/11/00-best-backgrounds-and-wallpaper-apps-for-android.jpg -->

Attach Div to the corner of Fullscreen Centered Image with object-fit: contain

I'm trying to attach an "information div" to the left bottom edge of a fullscreen/centered/fitted image.
The image has to fit into the screen size (just like css property object-fit: contain does) and the information div should be attached to the bottom left side of the image.
Here is my css try (not even close) and here are three images to describe the wanted behavior!
Note: I'm looking for a CSS-only solution, so, please, no JS!
Edit: Attach div to a fullscreen image to the corner of another div with no predefined size / fullscreen.
* {
margin: 0;
padding: 0;
}
#out {
}
#in {
}
#info {
background-color: red;
color: white;
display: inline-block;
position: absolute;
bottom: 10px;
left: 10px;
}
img {
width: 100vw;
height: 100vh;
object-fit: contain;
}
<div id="out">
<div id="in">
<img src="https://images.unsplash.com/photo-1501630834273-4b5604d2ee31?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" alt="">
<div id="info">This is the image info!</div>
</div>
</div>
If I understand you correctly, you're looking for something like this? Open it fullscreen, resize the page, throw different images in there, cheers.
div {
border: red 1px dashed;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
}
div:after {
content: attr(data-overlay);
position: absolute;
bottom: 1rem;
left: 1rem;
color: white;
font-size: 2rem;
}
img {
object-fit: contain;
height: 100vh;
width: 100wh;
}
<div data-overlay="TEXT TEXT TEXT">
<img src="https://images.pexels.com/photos/1498923/pexels-photo-1498923.jpeg"/>
</div>
Wrap the image in a div that will be the relative parent of your info div.
section {
display: flex; /* centers everything in the section */
flex-direction: column;
align-items: center;
}
.image-wrap {
display: inline-block; /* keeps the wrapper div the same size as the child image */
position: relative; /* for positioning the info div */
}
.image-info {
position: absolute;
left: 10px;
bottom: 10px;
font-size: 18px;
text-transform: uppercase;
color: white;
}
<section>
<div class="image-wrap">
<img src="https://picsum.photos/200/300" />
<div class="image-info">Nice image</div>
</div>
<div class="image-wrap">
<img src="https://picsum.photos/300/200" />
<div class="image-info">This one is cool</div>
</div>
<div class="image-wrap">
<img src="https://picsum.photos/450/180" />
<div class="image-info">Pretty!</div>
</div>
</section>
you need to use Viewport unit
for that you need to set the size of your div.#out
which is width: 100vw; height: 100vh

Centering a Relative Div

Tried a few things(margin-auto, text align:center etc) to centre this relative div - which is the header in my responsive layout with no luck. Any other ways to try?
The problem is keeping it centered as the page expands/contracts
Its CSS properties are
#header {
height: 170px;
width: 100%;
overflow: visible;
padding: 10px;
margin-bottom: 7px;
position: relative;
z-index: 99;
}
How can a div appear visually centered when it's 100% width of its parent?
Check out this fiddle: https://jsfiddle.net/w6332ytc/
HTML:
<div class="wrapper">
<div class="inner">
Content
</div>
</div>
CSS:
.wrapper {
width: 100%;
background: #000;
height: 300px;
}
.inner {
width: 50%;
background: red;
margin: 0 auto;
}

Getting text over a image

Have created a full screen image, that is filling the full site when you enter my website. But I can't make text over the image so that I can have a read more button and a welcome to name.
This is my code:
<div class="row">
<div class="col-md-12-">
<img class="img-responsive" style="min-height: 100%; min-width: 1024px; width: 100%; height: auto; position: fixed; top: 0; left: 0; " src="~/Content/img/maxresdefault%20(1).jpg" />
</div>
</div>
Any suggestions on how I add text over an image?
It needs to look like this:
There are a few ways:
div with text inside and style="background: url('my_img.png');".
a div with 'position: absolute; z-index: -1;' behind it that contains the img or background img.
Just add position property value of absolute, and a positive z-index property value to the text container only. See the example below and adjust as needed.
.row {
position: relative;
width: 100%;
height: 200px;
overflow: hidden;
}
.col-md-12 {
width: 100%;
height: 100%;
}
.col-md-12 img {
width: 100%;
height: auto;
}
.text {
position: absolute; /* Make stack-ability possible */
z-index: 3;
top: 100px;
left: 50px;
color: white;
font-size: 36px;
font-weight: bold;
background: black;
opacity: 0.70;
padding: 10px;
;
<div class="row">
<div class="col-md-12">
<img src="http://i.stack.imgur.com/xvCoo.jpg">
</div>
<div class="text"> Whatever your text is goes here </div>
<div>
There are a couple of ways to do it, the second option IMO is the simplest.
Positioned absolute
Offset from top with margin and center aligned button/content
http://codepen.io/anon/pen/jbBVeB
body{
background: url(http://placehold.it/1600x900);
}
.welcome{
margin-top: 50px;
text-align: center;
}
with the background-image method your CSS code would be:
body{
background-image: url('~/Content/img/maxresdefault%20(1).jpg');
background-position: center;
background-attachment: fixed;
}
put that on CSS and you're done!

CSS display-table width 100% - missing pixel?

I have a few div containers with overlays inside of them:
<div class="container">
<div class="overlay"></div>
</div>
<div class="container">
<div class="overlay"></div>
</div>
<div class="container">
<div class="overlay"></div>
</div>
<div class="container">
<div class="overlay"></div>
</div>
The problem is, when I set overlay display to table (it has to be table as I'm centering stuff both vertically & horizontally there - just simplified this example for SO) like so:
.container {
position: relative;
display: inline-block;
background: #fed900;
outline: solid 5px #000;
width: 25%;
height: 200px;
}
.overlay {
position: absolute;
top: 0;
display: table;
background: rgba(0,0,0,0.4);
width: 100%;
height: 100%;
}
I'm getting weird glitch - although developer tools tell me the overlay has the same width as container - in some cases the overlay width equals container's width minus 1 pixel. Why? What am I missing? And how to fix that? :)
http://jsfiddle.net/v13mdq57/
Depending on the width, the calculation is giving you a half pixel (which can't be rendered). We can achieve this without display: table. I'm not quite sure why this only occurs with display: table, but leaving the overlay as a block element fixes the problem.
In this example:
.overlay:before brings inline elements into the middle. It is an invisible element that is lined up on the left hand side inside the overlay.
The closing and opening div tags have no white space between them, which prevents the inline gap.
Read more about removing the gap between inline elements.
CSS / HTML / Demo
body {
margin: 0;
}
.container {
position: relative;
display: inline-block;
vertical-align: middle;
background: #fed900;
width: 25%;
height: 200px;
}
.overlay:before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.overlay {
position: absolute;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.4);
width: 100%;
height: 100%;
text-align: center;
}
<div class="container">
<div class="overlay">Centered</div>
</div><div class="container">
<div class="overlay">Centered</div>
</div><div class="container">
<div class="overlay">Centered</div>
</div><div class="container">
<div class="overlay">Centered</div>
</div>
* {
margin:0px;
padding:0px;
}
.container {
position: relative;
display: inline-block;
background: #fed900;
width: 25%;
height: 200px;
margin-right: -3px;
}