您的位置:

CSS 图片放大的使用技巧指南

一、CSS 图片放大缩小

CSS 中可以通过设置 transform: scale() 属性实现图片的放大和缩小,这个属性的值为需要缩放的倍数,例如设置 transform: scale(2) 将会把图片放大到原图的两倍。我们也可以通过媒体查询来为不同分辨率设备设置不同的缩放比例:

@media screen and (min-width: 992px) {
  .image-container {
    transform: scale(1.5);
  }
}
@media screen and (max-width: 991px) {
  .image-container {
    transform: scale(1);
  }
}

以上代码将会在大屏幕设备上为图片容器设置 1.5 倍大小,小屏幕上保持原始尺寸。如果需要在移动端设备上禁用图片的缩放,可以使用下面的代码:

@media screen and (max-width: 640px) {
  .image-container {
    transform: none;
  }
}

二、CSS 图片大小调整

CSS 中的 widthheight 属性可以用来调整图片大小,例如:

img {
  width: 200px;
  height: 200px;
}

以上代码将会把图片的宽高设置为 200px。如果需要等比例缩放图片,可以只指定其中一个尺寸,并保持另一个尺寸自适应:

img {
  width: 200px;
  height: auto;
}

三、CSS 图片放大隐藏

有时候我们需要在鼠标悬停时将图片放大并显示相关的信息,这种效果可以通过 CSS 的 :hover 伪类实现。为了避免图片挤压其他元素,我们可以将图片容器的尺寸设为固定值,然后在放大时使用 transform-origin 属性将图片放大中心点调整到容器中心:

.image-container {
  width: 300px;
  height: 200px;
  overflow: hidden;
}
.image-container img {
  transition: transform .5s ease;
}
.image-container:hover img {
  transform: scale(1.2);
  transform-origin: center center;
}

四、CSS 图片放大不失真

如果我们通过 transform: scale() 将图片放大,有可能出现图片失真的情况。为了解决这个问题,我们可以将图片放大前的尺寸设置为图片容器的尺寸,并将图片的 object-fit 属性设为 cover:

.image-container {
  width: 300px;
  height: 200px;
  overflow: hidden;
}
.image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform .5s ease;
}
.image-container:hover img {
  transform: scale(1.2);
  transform-origin: center center;
}

五、CSS 图片放大效果

CSS 中的 transition 属性以及 transform: scale()opacity 属性可以结合使用,实现图片在鼠标悬停时缓慢放大并半透明显示的效果:

.image-container {
  width: 300px;
  height: 200px;
  overflow: hidden;
}
.image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform .5s ease, opacity .5s ease;
}
.image-container:hover img {
  transform: scale(1.2);
  transform-origin: center center;
  opacity: .5;
}

六、CSS 图片放大代码

下面是将图片作为按钮,在鼠标悬停时放大的简单示例:

.button-image {
  width: 200px;
  height: 200px;
  overflow: hidden;
}
.button-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform .3s ease;
}
.button-image:hover img {
  transform: scale(1.2);
  transform-origin: center center;
}

七、CSS 图片放大镜

可以通过在图片容器上叠加一个放大镜容器,然后根据鼠标位置计算放大镜的位置及显示区域,实现图片放大镜的效果。

.image-container {
  position: relative;
  width: 400px;
  height: 300px;
  overflow: hidden;
}
.image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.magnifier {
  position: absolute;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  border: 5px solid #FFF;
  box-shadow: 0 0 5px rgba(0, 0, 0, .5);
  background-size: 100% 100%;
  visibility: hidden;
  pointer-events: none;
}
.image-container:hover .magnifier {
  visibility: visible;
}

八、CSS 图片放大超框代码

下面是一个简单的示例,将图片放在容器中,并可以通过按钮在不打乱页面布局的情况下将图片放大到超出容器的范围:

.container {
  position: relative;
  width: 400px;
  height: 300px;
  border: 1px solid #DDD;
  overflow: hidden;
}
.container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.overlay {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: rgba(0, 0, 0, .5);
  display: none;
  justify-content: center;
  align-items: center;
  text-align: center;
  color: #FFF;
}
.overlay.active {
  display: flex;
}
.overlay img {
  max-width: 95%;
  max-height: 95%;
  object-fit: contain;
}
.button {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px;
  background: #333;
  color: #FFF;
  cursor: pointer;
}

HTML 部分:

  
放大

JavaScript 部分:

var container = document.querySelector('.container');
var overlay = container.querySelector('.overlay');
var button = container.querySelector('.button');

button.addEventListener('click', function() {
  overlay.classList.toggle('active');
});

九、HTML 图片放大缩小

HTML5 中可以使用 <img> 元素的 widthheight 属性来调整图片的大小,例如:


 

如果需要等比例缩放图片,可以只指定其中一个尺寸,并保持另一个尺寸自适应:


 

十、CSS 背景图片放大

CSS 中可以使用 background-size 属性控制背景图片的大小,例如:

div {
  background-image: url(image.jpg);
  background-size: cover;
}

以上代码将会让背景图片完全覆盖容器,并且不会出现过度拉伸或缩放的情况。如果需要将背景图片缩小到容器内平铺,可以将 background-size 属性的值设为 contain

div {
  background-image: url(image.jpg);
  background-size: contain;
  background-repeat: repeat;
}

总结

本文详细介绍了 CSS 图片放大的多种实现方式,包括缩放、尺寸调整、悬停效果、放大镜、超框放大等,希望能够帮助开发人员更好地掌握这一技术。

CSS 图片放大的使用技巧指南

2023-05-20
印象笔记记录java学习(Java成长笔记)

2022-11-12
使用CSS在Word中移动图片的技巧

2023-05-12
python技巧笔记(python自学笔记)

2022-11-12
使用CSS为网页设置背景图片的技巧

2023-05-12
优化图片显示的CSS技巧

2023-05-12
jsp程序开发学习笔记2,jsp程序设计题库

本文目录一览: 1、《JSP&Servlet学习笔记》pdf下载在线阅读,求百度网盘云资源 2、林信良编著jsp&servlet学习笔记第2版课后答案吗 3、jsp有没有快速掌握的办法呀? 4、要学J

2023-12-08
CSS设置背景图片技巧详解

2023-05-12
优化网站背景图片展示的CSS技巧

2023-05-12
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)