Chen-AMao / Web-development-learning

Recording the learning route for web development.
0 stars 0 forks source link

CSS笔记:实现图片点击放大,居中显示 #5

Open Chen-AMao opened 6 years ago

Chen-AMao commented 6 years ago

html代码: image

css代码:

.img-view {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: auto;
}

/*遮罩层样式*/
.img-view .img-layer {
  position: fixed;
  z-index: 999;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.7);
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.img-view .img {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  margin: auto;
  z-index: 1000;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.img-view .img img {
  position: absolute;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
}

关键点:

  1. 图片居中:用flex布局
  2. 图片自适应:设置width和height为auto