首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >居中翻转图像

居中翻转图像
EN

Stack Overflow用户
提问于 2018-08-20 07:52:53
回答 1查看 49关注 0票数 0

我在Stack Overflow上找到了以下代码。

但是,代码不会使图像居中。有人能帮帮我吗?

HTML:

代码语言:javascript
复制
<div class="photo">
<a href="#">
    <img src="https://www.weebly.com/editor/uploads/2/9/9/6/29968995/custom_themes/185017182709687634/files/img/beforeafter/s6.jpg" alt="before" class="before">
    <img src="https://www.weebly.com/editor/uploads/2/9/9/6/29968995/custom_themes/185017182709687634/files/img/beforeafter/s1.jpg" alt="after" class="after">
</a>
</div>

CSS:

代码语言:javascript
复制
    img {
width: 300px;
}
.photo {

position: relative;
overflow-x: hidden;
}
.photo .after {
position: absolute;
top: 0;
left: 0; 
display: none;
}
.photo .before:hover + .after, .after:hover {
display: block;
}
EN

回答 1

Stack Overflow用户

发布于 2018-08-20 12:23:51

有多种方法可以实现最简单的flex布局,你需要编辑CSS for photo类,添加下面几行代码:

代码语言:javascript
复制
.photo {
  display: flex;
  justify-content: center; //horizontal align to center
 /* align-items: center*/ -->  this will align it vertically.  
}

flex的默认方向是行(水平方向),属性的行为也相应。如果我们改变flex-direction: columnjustify-contentalign-items根据方向的不同表现不同。

有关 flexbox 的更多信息:关于CSS flexbox的最佳文章:https://css-tricks.com/snippets/css/a-guide-to-flexbox

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51922706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档