Centering an image as a link within a div - html

Easy problem: Centering an img within a div:
I have div with a given width and height, and an img with an unknown width and height within the div:
<style>
div {
width: ...;
height: ...;
}
</style>
<div>
<img src="...">
</div>
Centering the img while keeping it contained is easy:
div {
display: flex;
align-items: center;
justify-content: center;
}
img {
max-width: 100%;
max-height: 100%;
}
Here's a couple examples of centering a 500x300 img using this approach:
In a 400x200 div: https://jsfiddle.net/thejonwithnoh/f9evLpx0/
In a 200x400 div: https://jsfiddle.net/thejonwithnoh/9dndr9r8/
Hard problem: Centering an img within an a within a div
I have div with a given width and height, and an img with an unknown width and height within an a within the div :
<style>
div {
width: ...;
height: ...;
}
</style>
<div>
<a href="...">
<img src="...">
</a>
</div>
A solution which gets pretty close is simply to style the a as such:
a {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
Here's a couple examples of centering a 500x300 img using this approach:
In an a in a 400x200 div: https://jsfiddle.net/thejonwithnoh/udo957d9/
In an a in a 200x400 div: https://jsfiddle.net/thejonwithnoh/k0nj571f/
The problem here is that the space outside of the img but within the div is now clickable as a link.
The question (tl;dr friendly)
How can I center an img (of unknown size) within a div (of known size), but also have the image be a link?
Edit for clarifications/requirements based on comments:
The answer should only be in html and css (i.e. no javascript)
The answer should not depend on any framework (though framework dependent answers are still interesting in their own right, and I would be curious to see them, but they are not allowed for the "accepted" answer)
The image should maintain its aspect ratio
The image should be contained by the div
If the image is bigger than the div in either dimension, then the image should be scaled so that it fits that dimension
Otherwise, if the image is not bigger than the div in either dimension, then the image should simply be its natural size centered within the div (though I'd be curious to see a solution where the image is scaled up to snugly fit within the div, but they are not allowed for the "accepted" answer)
The area outside the image and inside the div should not be part of the link

A simple way for centering an image (even it is wrapped by <a></a>) within a fixed width/height div container is this:
div {
width: 200px;
height: 150px;
line-height: 150px;
font-size: 0px;
background-color:red;
text-align: center;
}
img {
max-width:100%;
max-height:100%;
vertical-align: middle;
}
<div>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAEs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usYuuou7AR87wv5XXaevAAAAAElFTkSuQmCC"/>
</div>
Setting the divs' ...
- line-height equal to its height combined with an image that has vertical-align set to center, centers the image vertically.
- text-align to center, centers the image horizontally.

Related

inline div not resizing for image but works for other div

I need to fit an image into a fixed width/height div (say 80%/80%), but I also need the image to be wrapped in another div so that I can place an absolutely positioned element on top of the image (using that wrapper div as the anchor). I have accomplished the first point by just setting the max-height and max-width of the image to 100% so that it will always take up 100% of one dimension and won't exceed the other while maintaining aspect ratio, but I am unable to figure out a way to wrap the image in a div such that there is no extra space in the wrapper. I was under the impression that using display: inline or display: inline-block on the .wrapper div should shrink to the size of it's content (the image in my case), but that does not appear to be the case. When I replace the image with a test div with a defined width and height, the wrapper works as expected, i.e. there is no excess yellow background from the wrapper, it is exactly the same size as the div. How can I achieve the same behavior with the image? I've tried using all sorts of combinations of different display modes (flexbox/inline/block) and various min/max heights/widths but none have worked.
I've put an example of what my HTML looks like now, and what I would like it to look like if I could get this to work below. The .window element is a stand in for whatever the parent of the container is. The .container element is where I'd like to fit the image. In the example with the image, the inline wrapper is still larger than the image (which can be seen by the yellow overflowing on the sides). In the example after that with just a fixed size div (colored green), the wrapped properly shrinks to exactly the size of the div. Can this be accomplished with just css without knowing anything about the size of the image itself?
.window {
height: 400px;
width: 600px;
display: flex;
justify-content: center;
align-items: center;
background: red;
}
.container {
height: 80%;
width: 80%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: blue;
}
.wrapper {
display: inline;
max-height: 100%;
max-width: 100%;
background: yellow;
}
img {
max-height: 100%;
max-width: 100%;
}
.test {
width: 64px;
height: 128px;
background: green;
}
<div class='window'>
<div class='container'>
<div class='wrapper'>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/ReceiptSwiss.jpg/1920px-ReceiptSwiss.jpg" />
</div>
</div>
</div>
<div class='window'>
<div class='container'>
<div class='wrapper'>
<div class='test' />
</div>
</div>

How to get img tag to fit to scale inside a css grid cell without object-fit?

This is a vue component, its parent renders 8 of these components in a 2 x 4 even layout. The image is larger than its grid container. I would like it to shrink to fit the container while maintaining its aspect ratio. However object-fit is having no effect on the img tag and the image is rendered full size, drastically warping the grid and messing up my layout.
When the img tag is commented out, its parent div formats to the correct size and fills its grid container.
<template>
<div class="camera-wrapper">
<img class="camera" src="../assets/tsLogo.png" />
</div>
</template>
<style scoped>
.camera-wrapper {
position: relative;
border: 1px solid black;
width: 100%;
height: 100%;
overflow: hidden;
}
.camera {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
Would swapping to background-image on the div and using background-size: contain be possible instead of an img element?
I am sorry I cannot put up a snippet as I’m confined to a touch device at the moment

How: scrollable large images, centered small image,

No matter what I try, I cannot manage to find the proper CSS for the behaviour below:
A: Larger-than-window images show the upper left of the image and allow scrolling to see the rest of it.
(Important: the parent's DIV background is covered, but should be visible on images with transparency — even if you scroll.)
B: Smaller-than-window images are horizontally and vertically centered, the parent DIV covers the whole window.
Thanks for any help!
In order to center the smaller image inside the parent div, you can use display:flex for the parent div. Then set the justify-content and align-items to center. Here is a workaround,
#mydiv {
overflow: auto;
max-width: 500px;
max-height:500px;
}
.mainContainer{
width:500px;
height:500px;
background-color:#000;
display: flex;
justify-content: center;
align-items: center;
background-size:cover;
}
Replace the small image with this to check how it works with a large image.
<div class="mainContainer">
<div id='mydiv'>
<img src='https://i1.wp.com/www.rceshop.com/wp-content/uploads/2016/12/samples.png?fit=480%2C400' />
</div>
</div>
PS: Change the width and height of the parent div to the required screen size of yours. Hope this will help!
The line-height property turned out to be the base for solving the problem. Not sure if this is a hack, but it works.
.mainContainer {
text-align: center;
}
#mydiv {
line-height: 100vH;
margin: 0;
}
img {
vertical-align: middle;
}

Centering in the unknown

I would like to center and clamp the dimensions of a child div inside its parent.
<style type='text/css'>
.parent {
width: 100%;
height: 100%;
}
.child {
max-width: 100%;
max-height: 100%;
}
</style>
<div class="parent">
<div class="child">
<img src='dog.jpg' />
</div>
</div>
Here are the constraints:
The parent div is set to occupy the entire screen (of unknown size), so width:100% and height:100%.
The width and height of the child div are unknown. In one use case, the child div contains an image. In another, it contains a video.
The width and height of the child div must be constrained to the size of the parent, so max-width: 100% and max-height: 100%.
The child div must be vertically and horizontally centered inside the parent.
Ideally, this should work without javascript.
IE can be left unsupported :)
I've tried all the techniques listed in this excellent article, 'Absolute Centering in CSS' , and none of them pan out. Here's why:
Absolute centering: In order for this technique to work with a child of unknown size, you must set display:table on the child. You can then constrain the max-width of the child's contents, but not the max-height, because by CSS 2.1 rules, tables render to fit their contents.
Negative margins: Doesn't allow for variable height.
Transforms: Undesirable because it can result in blurry rendering.
Table-cell: Fails for the same reason that absolute centering fails, i.e. table rendering.
Inline-block: Doesn't work in the important case where the child is 100% width.
Flexbox: Works well until a window resize occurs, at which point you have to force a Webkit redraw to propagate the centering changes. This hack does the job, but it's still a hack. I want to believe there's a more elegant solution to this.
Best solution here is to use :before pseudo element. Check out this article on centering the unknown, http://css-tricks.com/centering-in-the-unknown/
SEE THE DEMO HERE
body,html {
width: 100%;
height: 100%;
margin: 0;
}
.container {
text-align: center;
background: #ccc;
width: 100%;
height: 100%;
}
.container:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
.image {
display: inline-block;
max-width: 100%;
max-height: 100%;
vertical-align: middle;
padding: 10px 15px;
border: #a0a0a0 solid 1px;
background: #f5f5f5;
}
You could use display:table and display:table-cell like so Jsfiddle. Though this will just center the image of the child div.
If you need to have a centered div around the image you could always add another div inside of the child div with the style display: inline-block example

Horizontally centering a child element inside a parent narrower than the child element

I've been trying to horizontally center a wide <img> element inside a <div> which is not as wide as the image itself. And the <div> has overflow: hidden set on it.
For instance, the image is 500px wide and the DIV is 250px wide. Is there any clean way (such that it works for images of any dimension) to center the image such that only the center portion shows up inside the div.
<div class="Container">
<img class="Thumb">
</div>
And :
.Container {
position: /* anything but static */
width: 250px;
}
.Thumb {
position: relative;
width: 500px;
margin: 0 auto;
}
you can add your image as a background using css and give background position to center center.
Since you're expecting a responsive behavior, I would suggest to have (for a simple one)
div {
text-align: center;
}
But using CSS3 and media queries it is possible.
Check this out How to Create a Responsive Centered Image in CSS3
Quick demo