gpt4 book ai didi

css - 在 div 中居中响应图像

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:50 24 4
gpt4 key购买 nike

我正在尝试将由 anchor 标记包裹的图像置于 div 中。我尝试使用 display flexbox 和 justify-content:center 在一定程度上起作用的图像,但是图像的高度保持不变,而宽度相应地变化,所以像我一样挤压图像从桌面缩小到移动......从而失去其质量。

如何让这张图片居中。

注意:我用的是bourbon neat

这是我的

HTML

<div class="project">
<a class="project__thumb" href="{{ project.url }}" target="_blank">
<img src="{{ site.baseurl }}img/{{ project.thumbnail }}">
</a>
</div>

SCSS

.project{
width: 80%;
margin: 0 auto; /*center whole block for mobile*/
height: auto;
margin-bottom: 60px;
border-radius: 2px;
position: relative;

a{
display: flex;
justify-content: center;
}

img{
max-width: 100%; /*make image responsive*/
height: auto; /*make image responsive*/
border-radius: 2px;
transition: opacity 0.25s ease-in-out;
}
}

@include media ($tablet){
.main__projects{
@include span-columns(12);
}

.project{
@include span-columns(6 of 12);
@include omega(2n);
margin-bottom: 50px;
}
}

@include media ($desktop){
.main__projects{
@include span-columns(12);
}

.project{
@include span-columns(4 of 12);
@include omega(3n);
margin-bottom: 100px;
}

}

最佳答案

这里有几个问题。第一个问题是您对 CSS 使用了错误的注释。对于 CSS,您需要使用 /* */ 包裹注释。 // 用于注释 JavaScript。因为您使用的是 Javascript 注释,所以 imgheightborder-radius 属性将被忽略。

另一个问题是在 anchor 标记上使用 display:flex;,这将阻止响应式图像大小调整。这应该让你开始:

.project{
width: 80%;
margin: 0 auto; /* center whole block for mobile */
height: auto;
margin-bottom: 60px;
border-radius: 2px;
position: relative;
text-align:center;
}
.project a {
display:inline-block;
}
.project img{
max-width: 100%; /* make image responsive */
height: auto; /* make image responsive - THIS IS NOT NECESSARY */
border-radius: 2px;
transition: opacity 0.25s ease-in-out;
}
<div class="project">
<a class="project__thumb" href="{{ project.url }}" target="_blank">
<img src="http://placehold.it/250x250">
</a>
</div>

关于css - 在 div 中居中响应图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42911745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com