The images I'm working with are 350 x 525 px, but the top and bottom 90ish pixels of the image are transparent, which makes the actual content roughly 350 x 350 px in the center of the png. I want to show the 350x350 content with rounded corners, so I have the following CSS which does the trick:
img {
width: 100%;
height: 60%;
object-fit: cover;
border-radius: 2rem;
}
The code basically removes the top and bottom bezels and gets me a 1:1 aspect ratio image with rounded corners. The problem now is that the <div> housing this image stays the same height, so when I arrange these images in a grid, I get a lot of empty space that I don't know how to fix.
I am working with React and styled-components, but this is the equivalent HTML and CSS:
This is the main grid container.
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));
grid-gap: 2rem;
}
And each element in the grid is called a card. Here's how each card is defined.
<div class="card">
<a href="#">
<img src="image-path" alt="" />
<h4>some text</h4>
</a>
</div>
img {
width: 100%;
height: 60%;
object-fit: cover;
border-radius: 2rem;
}
a {
text-decoration: none;
}
h4 {
text-align: center;
padding: 1rem;
}
I've attached a few images of the issue. How do I fix this?
image 1
image 2
As per Максим Мовчан's answer
try this
.card {
margin-bottom: -40%;
}
Set height to max-content for your a tag.
CSS:
a {
height: max-content;
}
Related
I'm using a grid layout. I used grid-template-column and grid-template-rows to define its structure. I want the first cell to contain an image. Since I want to center it horizontally, my idea was to place it inside a div which itself can be a flex or grid container.
I'm having problems scaling the image. When not using the div, I'm basically placing the image directly inside the grid, I can use height: 100%; and it scales down so that the height fits the cell.
But when using an extra div container that does not work anymore. Using height: 100% seems to reference the height of the whole grid layout, not just the div. Google says I should use max-height and max-width, but I can't get it to work. Below is a simple test project (HTML and CSS) to show the problem. When using width: 25px; you can see, how big the sections of the grid should be.
I appreciate any help. Keep in mind, this is a simple test project, so don't mind the naming of my classes and IDs.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body,
html {
height: 100%;
}
.class {
background-color: blue;
}
.container {
height: 100vh;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 3fr 3fr;
gap: 10px;
background-color: green;
}
#image-container>img {
max-height: 100%;
}
<div class="container">
<div class="class" id="image-container">
<img src="https://www.industrialempathy.com/img/remote/ZiClJf-1920w.jpg" alt="">
</div>
<div class="class" id="2">Good</div>
<div class="class" id="3">Day</div>
</div>
Check out the overflow on the child element class and max-height on the parent to achieve the desired result.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body,
html {
min-height: 100%;
}
.parent {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr repeat(2, 3fr);
gap: 1rem;
background: orchid;
max-width: 100%;
max-height: 100vh;
}
.class{
background: teal;
overflow: hidden;
}
img{
display:block;
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="parent">
<div class="class">
<img src="https://www.industrialempathy.com/img/remote/ZiClJf-1920w.jpg" alt="">
</div>
<div class="class">
<p>Good</p>
</div>
<div class="class">
<p>Day</p>
</div>
</div>
My preferred method is setting it as background image, with the background-size:contain (or cover). This applies logic from the browser instead of trying to get it right yourself and is fully responsive.
.exampleA, .exampleB{
width: 34%;
height: 150px;
background-position: center;
background-repeat: no-repeat;
background-color: pink; /* Just for this demo: to display canvas size */
border: 1px solid black; /* Just for this demo */
display: inline-block;
}
.exampleA {
background-size: contain;
}
.exampleB {
background-size: cover;
}
<div class="exampleA"
style="background-image: url('https://www.industrialempathy.com/img/remote/ZiClJf-1920w.jpg');"
></div>
<div class="exampleB"
style="background-image: url('https://www.industrialempathy.com/img/remote/ZiClJf-1920w.jpg');"
></div>
I am making card design in cakephp,
but when it comes to show longer image than 600px width,
It extended insanely!!!
I am using placeholder or dummyimage but it has no problem.
Why this is happening? What should I do to show smaller images?
html inline width height constraint did not work.
<div class="cards">
<div class="image">
<img src="https://dummyimage.com/600x150/000/fff" >
</div>
</div>
* {
margin: 0;
padding: 0;
}
.cards {
width: 30%;
background: #999;
}
.image img {
width: 100%;
}
.title {
text-align: center;
}
.des {
text-align: center;
}
You've set your image to take 100% of the container size, which is 30% of the screen width. Set
.image img {
width: auto;
}
Important, if your screen is too small, then the background will only cover a portion of the image, since you set the card to be 30% of the size of the screen, but the image is being automatically sized. Try adding max-width: 100%; to the image CSS.
I have a CSS grid layout with a bunch of areas and a photo container.
I don't know how to:
control the size of the photo, so that it is contained within the area of the grid
keep the width of the photo at 100% (thus equal to its container) and scale the height of the container to fit the photo. Basically, when the window becomes smaller, the width of the photo becomes smaller, and so does the height - pulling the tag, album, and rotate elements up.
.container {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: 40vh 30vh 30vh;
grid-gap: 10px;
grid-template-areas:
"info photo photo photo photo"
"comment photo photo photo photo"
"comment tag tag album rotate"
}
.photo {
grid-area: photo;
background: yellow;
}
.photo>img {
object-fit: cover;
width: 100%
}
.info {
grid-area: info;
background: pink;
}
.tag {
grid-area: tag;
background: teal;
}
.comment {
grid-area: comment;
background: green;
}
.album {
grid-area: album;
background: red;
}
.rotate {
grid-area: rotate;
background: blue;
}
<div class="container">
<div class="photo">
<img src="http://wallpaper-gallery.net/images/image/image-13.jpg" />
</div>
<div class="info">info</div>
<div class="tag">tag</div>
<div class="comment">comment</div>
<div class="album">album</div>
<div class="rotate">rotate</div>
</div>
You have two different problems here.
I'll address the first one, which is to contain the image in its container. You almost had it.
Your code:
.photo > img {
object-fit: cover;
width: 100%;
}
You just needed to specify a maximum height on the image so it could not overflow the container:
.photo > img {
object-fit: cover;
width: 100%;
max-height: 100%;
}
JSFiddle demo
The second problem, which is to scale the size of the image container, and drag up the grid items below when the image gets smaller, is significantly more complex.
This isn't an issue relating to the image. In fact, you can remove the image altogether when trying to solve this problem.
This is an issue of dynamically sizing a grid item (the image container). Why would this grid item change size in relation to the image size, when its size is being controlled by grid-template-columns and grid-template-rows?
In addition, why would the bottom row of grid items (tag, album, rotate) follow the image container either up or down? They would have to exit their row.
Scaling the entire grid container wouldn't be a problem. But it seems like you only want to scale one grid item (the image container). It's tricky. You're probably looking at additional containers, auto values for lengths, and possibly scripting.
Here's what happens if you give the image rows an auto value: JSFiddle demo
What if you use a background-image inside one of the grid cells rather than an IMG tag (haven't used display: grid yet b/c it's so new - cool!)
Then using background-size and background-position, you can size it properly within that cell.
.container {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: 40vh 30vh 30vh;
grid-gap: 10px;
grid-template-areas: "info photo photo photo photo" "comment photo photo photo photo" "comment tag tag album rotate"
}
.photo {
grid-area: photo;
background: yellow;
height: 100%;
background-size: cover; /* <-- background size */
background-position: center; /* <-- background position */
}
.info {
grid-area: info;
background: pink;
}
.tag {
grid-area: tag;
background: teal;
}
.comment {
grid-area: comment;
background: green;
}
.album {
grid-area: album;
background: red;
}
.rotate {
grid-area: rotate;
background: blue;
}
<div class="container">
<div class="photo" style="background-image: url('https://thumbs.web.sapo.io/?epic=YmIzAEUIMb3pue+Zz8qV8QS/WuKmq0vrL/lWiluSn7SstKeeh7/UTTBAuDlhHFD6YC9+vaCHBQuNOXeVaHkjzTSE8tF3hMBev6512ha92Yc4kRw=&W=1200&H=627&crop=center&delay_optim=1')">
</div>
<div class="info">info</div>
<div class="tag">tag</div>
<div class="comment">comment</div>
<div class="album">album</div>
<div class="rotate">rotate</div>
</div>
I have a slight correction which works for any combination of aspect ratio of both css-grid cell and src img. (As the accepted solution only works if the aspect ratio of the cell is greater than the aspect ratio of the image). It's very simple:
.photo > img {
object-fit: cover;
width: 100%;
height: 100%;
}
You probably need to add explicit width: 100% to the image
I need to overlap two images that I centred using the following CSS:
display: block;
margin: auto;
The current situation is this-
The current situation
I want the bigger image (the one at the bottom right now) to be under the smaller image and the text without losing the already centred positions. I've tried with z-index and setting the positions to relative and absolute but that just messes up with the alignment of the images.
HTML:
<img id="logo" src="Pics/logo1.png" class="animated bounceInUp">
<img id="wall" src="Pics/Ywall1.png">
CSS:
#logo
{
height: 200px;
width: 200px;
display: block;
margin: auto;
padding-top: 15%;
}
#wall
{
height: 500px;
display: block;
margin: auto;
}
Tried this answer also but again I got issues with alignment. Adjusting the top and left values is not only a big headache but it also doesn't give perfect centring.
This: Overlap Images In Center [css]
P.S- Stackoverflow won't let me embed images and embed 2 links until I get 10 rep. Sorry for that.
Would it be possible to have the larger image as a background?
#wall {
height: 413px;
width: 620px;
display: block;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
background-image: url('http://cdn2.spectator.co.uk/files/2016/07/wall.jpg');
text-align: center;
}
<div id="wall">
<div>
<img id="logo" src="https://logopond.com/avatar/1862/logoholik-icon-logopond2.png" class="animated bounceInUp">
<div>Logo Text!</div>
</div>
</div>
I have an image in a container div below which is a div containing text that can scroll. The image can vary in size so the height of the image will vary depending on the image being displayed i.e. it isn't always 400 x 200 as in my example.
My problem is that when the text scrolls there is a space between the image and the point where the text should scroll behind the image. This seems to be because the image container div is not the same size as the image.
This JSFiddle shows the problem https://jsfiddle.net/t0ag2z5k/50/
Can anyone tell me both why this is happening and how to fix it please?
CSS code below...
#plotdetails {
display: flex;
flex-direction: column;
float: left;
width: 400px;
z-index: 5;
position: fixed;
height: 100%;
}
#plotdetails #plot-img-container {
display: inline-block;
}
#plotdetails #plot-img-container img{
width: 400px;
display: inline-block;
}
#details-text {
padding: 0 20px 0 20px;
overflow-y: scroll;
flex: 1;
}
HTML here
<div id="plotdetails">
<div id="plot-img-container">
<img src="http://placehold.it/400x200">
</div>
<div id="details-text">
<h1>Lot's of words here....</h1>
</div>
</div>
Why do you want your image to be display as an inline-block when it's not utilized as such? Try (https://jsfiddle.net/t0ag2z5k/52/):
#plotdetails #plot-img-container img{
width: 400px;
display: block;
}