I'm trying to make thumbnails. Here's the HTML code:
<div class="product-preview">
<div data-img-url="images/image7.jpg" class="p-thumbnail">
<img src="images/image7.jpg" />
</div><div data-img-url="images/image8.jpg" class="p-thumbnail">
<img src="images/image8.jpg" />
</div><div data-img-url="images/image9.jpg" class="p-thumbnail">
<img src="images/image9.jpg" />
</div><div data-img-url="images/image10.jpg" class="p-thumbnail">
<img src="images/image10.jpg" />
</div>
</div>
And css
.product-preview {
margin-left: -5px;
margin-right: -5px;
}
.product-preview .p-thumbnail {
height: 120px;
width: 25%;
display: inline-block;
padding: 10px 5px;
overflow: hidden;
}
.p-thumbnail img {
height: 100%;
display: block;
margin: 0 auto;
}
How to fix so that the the img will not exceed to the padding area of .p-thumbnail? I attach a photo below, hope I make clear enough so that someone can understand what I'm trying to say! Thanks!
It's because overflow property will only hide the child of the element from the border and outwards. If you want to limit your image not to exceed the content box, you have to put some inner container or make the image to contain.
First Option
<div data-img-url="images/image7.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div><div data-img-url="images/image8.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div><div data-img-url="images/image9.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div><div data-img-url="images/image10.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div>
With additional CSS
div.imgcover{
width: 100%;
overflow: hidden;
}
If you want your image to be centered on the imgcover div you can use
.p-thumbnail{
position: relative;
}
.p-thumbnail img {
max-height: 100%;
max-width: 100%;
display: block;
margin: 0 auto;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
Second Option
Change the style of the image to this to emulate object-fit: contain
.product-preview .p-thumbnail {
height: 120px;
width: 25%;
display: inline-block;
padding: 10px 5px;
overflow: hidden;
text-align: center;
}
.p-thumbnail img {
max-height: 100%;
max-width: 100%;
display: block;
margin: 0 auto;
}
what you want is
.product-preview img{
max-width: 100%;
height: auto; /* instead of height: 100%; */
}
and try to used images with same width and height so you dont have any problem
The very first simple solution that comes to mind is that you can use a overflow: hidden; on the parent element.
But if you want your image to fully be visible on the thumbnail you can use this codes to your image tag
{
width: 100%;
object-fit: cover;
}
Related
This website I'm coding has a header with a portfolio. I want the persons "avatar" to be halfway onto the portfolio. Basically I want the avatar image to always be 50% down on to the portfolio div. The page is responsive so it shrinks accordingly.
The avatar image shrinks/resizes accordingly, however; the margin-bottom doesn't keep the same proportion. I always want it to be 50% below, onto the next div.
Here's the GIF. the start of it is how I want it, watch when I resize. Thanks.
https://imgur.com/a/nL6m9ow
here's my code
body {
width: 95%;
max-width: 1200px;
margin: 0 auto;
}
.avatar img {
width: 100%;
margin-bottom: -100px;
}
<div class="avatar">
<img src="images/portfolio-avatar.png" class="banner">
</div>
<div class="portfolio">
<img src="images/banner.png" class="banner">
</div>
body{
width: 95%;
max-width: 1200px;
margin: 0 auto;
}
.avatar {
position: relative;
padding-top: 100px;
}
.avatar img{
max-width: 100px;
position:absolute;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
.portfolio img {
max-width: 100%;
}
<div class="avatar">
<img src="https://www.ienglishstatus.com/wp-content/uploads/2018/04/Sad-Profile-Pic-for-Whatsapp.png" class="banner">
</div>
<div class="portfolio">
<img src="https://about.canva.com/wp-content/uploads/sites/3/2015/02/Etsy-Banners.png" class="banner">
</div>
check this code, or you can check this here also.
find the link for codepen.
https://codepen.io/atulraj89/pen/qQQBMm
Resize the window and enjoy
If you use relative units (%) for the width of your avatar, as well as the padding-top, it will grow/shrink accordingly.
body {
width: 95%;
max-width: 1200px;
margin: 0 auto;
}
.avatar {
position: relative;
padding-top: 7.5%;
}
.avatar img {
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
width: 15%;
border-radius: 50%;
border: 6px solid white;
}
.portfolio img {
max-width: 100%;
}
<div class="avatar">
<img src="https://picsum.photos/120/120
" class="banner">
</div>
<div class="portfolio">
<img src="https://picsum.photos/1200/400" class="banner">
</div>
How to make multiple images with a tag name as a link on top of each one?
I can make only one image with a tag name on top of the image, but not other ones. Somehow when I copy the whole div (contains the img and the tag's div), it doesn't show the tags on other images.
I've tried: position: relative for the parent div, position: absolute for the tag (child div), make the image float: left.
I've also tried to "stick" the child div with its parent.
I've tried adding div and class for each item: image, image container, tag, a tag...
.container {
position: relative;
width: 400px;
height: 400px;
}
.container img {
width: 100%;
/*I've tried with px, doesn't make a difference*/
float: left;
/*I've also tried adding a class for img and position: absolute*/
}
.nameTag {
width: 100%;
height: 50px;
position: absolute;
background-color: gray;
}
.nameText {
color: white;
}
<div class="container">
<img src="assets/images/img1.png" />
<div class="nameTag">Mobile App</div>
</div>
<div class="container">
<img src="assets/images/img2.png" />
<div class="nameTag">Mobile App</div>
</div>
<div class="container">
<img src="assets/images/img3.png" />
<div class="nameTag">Mobile App</div>
</div>
Not sure if this is exactly what you are looking for but this is what I've changed in your CSS:
.container {
position: relative;
display: inline-block; //allowed images to float
width: 400px;
height: 400px;
}
.nameTag {
width: 100%;
height: 50px;
position: absolute;
background-color: gray;
top: 100px; //positioned about half way down the image
text-align: center; //center the link text
}
Here is a link to the CodePen for you to view. Let me know!
Try changing your float to "none" or copy/paste this into your html/css files.
Hope this helps.
.container {
position: relative;
width: 400px;
height: 400px;
}
.container img {
width: 100%;
/*I've tried with px, doesn't make a difference*/
float: none;
/*I've also tried adding a class for img and position: absolute*/
}
/* ^^^ Change float to "none"...seems to work here */
.nameTag {
width: 100%;
height: 50px;
position: relative;
background-color: gray;
}
.nameText {
color: white;
}
<div class="container">
<div class="nameTag">Mobile App</div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlUiyrmhTXFppqk4aYzqTOU9nimCQsYibukwAV8rstsDkAVQT-mA" />
</div>
<div class="container">
<div class="nameTag">Mobile App</div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlUiyrmhTXFppqk4aYzqTOU9nimCQsYibukwAV8rstsDkAVQT-mA" />
</div>
<div class="container">
<div class="nameTag">Mobile App</div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlUiyrmhTXFppqk4aYzqTOU9nimCQsYibukwAV8rstsDkAVQT-mA" />
</div>
I created a image link:-
.post-image {
margin: 20px auto;
display: block;
max-width: 100%;
height: auto;
}
<a href="example.com/link">
<img class="post-image" src="https://i.stack.imgur.com/gKSXt.jpg" />
</a>
But the link is clickable outside the image also.
How can I center-align the image and also make the link clickable only on the image not outside the image (overflowing area).
Centered image
Not sure if this is acceptable, but I use {display: inline-block}
Edit: Just noticed you want the image to be centered too. Edited code to reflect
body {
text-align: center
}
.post-image {
margin: 0 auto;
display: inline-block;
max-width: 100%;
height: auto;
}
<body>
<a href="example.com/link">
<img class="post-image" src="https://i.stack.imgur.com/gKSXt.jpg" />
</a>
</body>
Add display: inline; to image .post-image to get it centered and add text-align: center; to tha parent of a tag
div {
margin: 0 auto;
text-align: center;
}
a {
margin: 0px auto;
}
.post-image {
margin: 20px auto;
display: inline;
max-width: 100%;
height: auto;
}
<div>
<a href="https://i.stack.imgur.com/gKSXt.jpg">
<img class="post-image" src="https://i.stack.imgur.com/gKSXt.jpg" />
</a>
</div>
Another Way:
Simply use Position:Absolute. And if you have any other elements in the page. position:absolute may affect their visibility.
.post-image {
max-width: 100%;
position: absolute;
margin: 20px auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
<a href="example.com/link">
<img class="post-image" src="https://i.stack.imgur.com/gKSXt.jpg" />
</a>
You can add a div before href tag and add text-align: center; on this div. like:
.post-image {
margin: 20px auto;
display: inline-block;
max-width: 100%;
height: auto;
}
<div style="text-align: center;">
<a href="example.com/link">
<img class="post-image" src="https://i.stack.imgur.com/gKSXt.jpg" />
</a>
</div>
This question already has answers here:
center image in a div
(3 answers)
Closed 6 years ago.
How can I ensure that the img within the container is centered and scaling correctly from mobile to desktop? Here is a demo on Codepen. Scale to mobile to better understand the problem
HTML
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
CSS
.image-container{
width: 100%;
height: 500px;
max-height: 500px;
background: red;
overflow: hidden;
text-align: center;
position: relative;
display: block;
}
img{
height: auto;
width: 100%;
max-width: 100%;
position: absolute;
top: -50%;
left: 0;
}
I ask this because the image loses it height on mobile and looks incorrect. I sort of what this to work like `background-size: cover. I'd like the image to completely fill the container
You can add margin:0 auto; to align center horizontally. Remove width:100% as this will stretch width of an image unless you want it to. Keep height:auto to adjust with width.
.image-container{
width: 100%;
height: 500px;
max-height: 500px;
background: red;
overflow: hidden;
text-align: center;
position: relative;
display: block;
}
img{
height: auto;
max-width: 100%;
margin:0 auto;
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
To center image horizontally and vertically inside div you can set top: 50% and left: 50% and then just add transform: translate(-50%, -50%).
To make image responsive you can use max-width: 100% and by default height is auto.
.image-container {
width: 100%;
height: 500px;
background: red;
overflow: hidden;
position: relative;
}
img {
max-width: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
Other option is to use Flexbox if you don't want to use relative/absolute position but to keep img responsive you can use object-fit: contain with height: 100%
.image-container {
width: 100%;
height: 500px;
background: red;
display: flex;
align-items: center;
justify-content: center;
}
img {
max-width: 100%;
object-fit: contain;
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
If you want img to behave like background-size: cover one way to that is to use object-fit: cover with height: 100% and width: 100%
body,
html {
margin: 0;
padding: 0;
}
.image-container {
width: 100%;
height: 500px;
background: red;
display: flex;
align-items: center;
justify-content: center;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="image-container">
<img src="https://placeimg.com/470/310/people" alt="" />
</div>
I would like to create a full-screen (so to fill the visible area) 2x2 table, where every cell contains a center aligned image, which size is adjusted to the cell (shrink/stretched), that way that it's ratio is kept.
Maybe an image is better:
What I found so far:
2x2 full-screen table HTML:
<table style="height:100%;width:100%; position: absolute; top: 0; bottom: 0; left: 0; right: 0;border:1px solid">
<tr style="height: 50%;">
<td style="width: 50%; text-align: center; vertical-align: middle;"></td>
<td style="width: 50%; text-align: center; vertical-align: middle;"></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%; text-align: center; vertical-align: middle;"></td>
<td style="width: 50%; text-align: center; vertical-align: middle;"></td>
</tr>
</table>
I tried to add image in the cells, like:
<img style="width: 100%;max-height: 100%" src="...">
This is almost ok:
It stretches horizontally to fill the cell, but the height it too large this case, so table won't fit on the screen, vertical scrollbar appears.
Also it shrinks horizontally to fill the cell, but the height it too large this case, so table won't fit on the screen, vertical scrollbar appears.
So it is ok for width changes, but not considering the situation when height should be the leader.
Basically, I would like a full-screen 2x2 gallery viewer, that every image fills as much as possible the cell, keeping the ratio.
I'm assuming that using a <table> is not compulsory? I have created an example using 4 divs. Clean, simple.
The markup is reduced to:
<div class="section">Section 1 image</div>
<div class="section">Section 2 image</div>
<div class="section">Section 3 image</div>
<div class="section">Section 4 image</div>
The basic CSS
Sections are display: inline-block with 50% height and width.
The images have 100% height and will keep the height/width ratio correct
box-sizing: border-box incorporates the borders into the width / height of the sections
html, body { height: 100%; } allows the sections to have a 50% height
The body is given an appropriate min-width to prevent the sections from becoming too small along with a max-width
To prevent double borders, the appropriate sections are targeted with nth-child / first-child and have a border removed
New Example
max-width and max-height maintains the aspect ratio of the image when resized vertically and horizontally
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 100%;
}
body {
min-width: 500px;
max-width: 800px;
margin: 0 auto;
}
.section {
width: 50%;
height: 50%;
border: solid 1px #000;
display: inline-block;
vertical-align: middle;
position: relative;
}
.section:first-child,
.section:nth-child(3) {
border-right: none;
}
.section:nth-child(1),
.section:nth-child(2) {
border-bottom: none;
}
.section img {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
max-height: 90%;
max-width: 90%;
}
<div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div>
Archived - broken - Examples
"Show code snippet" and then run it.
Note how the closing and opening div tags have no whitespace between them. This is to prevent the gap that occurs between inline elements.
Example 1
The images are position: absolute and position themselves in relation to their section container which has position: relative.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 100%;
}
body {
min-width: 500px;
max-width: 800px;
margin: 0 auto;
}
.section {
width: 50%;
height: 50%;
border: solid 1px #000;
display: inline-block;
vertical-align: middle;
position: relative;
}
.section:first-child,
.section:nth-child(3) {
border-right: none;
}
.section:nth-child(1),
.section:nth-child(2) {
border-bottom: none;
}
.section img {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
height: 100%;
padding: 10px;
}
<div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div>
Example 2
Images are not position:absolute and are centered with padding and text-align: center
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 99%;
}
body {
min-width: 500px;
max-width: 800px;
margin: 0 auto;
}
.section {
width: 50%;
height: 50%;
border: solid 1px #000;
display: inline-block;
vertical-align: top;
text-align: center;
}
.section:nth-child(1),
.section:nth-child(2) {
border-bottom: none;
}
.section:first-child,
.section:nth-child(3) {
border-right: none;
}
.section img {
height: 100%;
padding: 10px;
}
<div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div><div class="section">
<img src="http://placehold.it/200" />
</div>