在响应式容器内垂直对齐图像
挑战
您有一个 HTML 结构,其中一个容器保持方形纵横比作为浏览器窗口已调整大小。在此容器内,您想要添加图像,但需要确保它保持垂直对齐。挑战的出现是因为图像的高度是可变的,并且容器的高度无法固定。
解决方案
使用 CSS 内联元素
- 创建一个内联块伪元素作为容器元素的第一个(或最后一个)子元素,并将其高度设置为100%以占据整个容器的高度。
- 为伪元素和图像设置vertical-align: middle,使元素垂直居中。
- 通过设置 font-size 删除元素之间的任何空白: 0;在容器元素上删除字符(空格)占用的空间。
HTML:
<div class="container"> <div> <p>CSS:</p> <pre class="brush:php;toolbar:false">.container { height: 300px; text-align: center; /* align the inline(-block) elements horizontally */ font: 0/0 a; /* remove the gap between inline(-block) elements */ } .container:before { /* create a full-height inline block pseudo=element */ content: ' '; display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ height: 100%; } #element { display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ font: 16px/1 Arial sans-serif; /* <h3 id="制作容器响应式">制作容器响应式</h3><p>要创建高度相对于宽度调整大小的响应式容器,您可以使用百分比值对于顶部/底部填充属性:</p><pre class="brush:php;toolbar:false">.responsive-container { width: 60%; padding-top: 60%; /* 1:1 Height is the same as the width */ padding-top: 100%; /* width:height = 60:100 or 3:5 */ padding-top: 45%; /* = 60% * 3/4 , width:height = 4:3 */ padding-top: 33.75%; /* = 60% * 9/16, width:height = 16:9 */ }
包装图像内容
为了避免容器顶部或底部出现过多空间,请将图像包装在包装元素中并将其定位绝对在容器内以填充其整个空间:
.responsive-container { width: 60%; position: relative; } .responsive-container .wrapper { position: absolute; top: 0; right: 0; bottom: 0; left: 0; }
包括图像和 CSS对齐
HTML:
<div class="responsive-container"> <div class="dummy"></div> <div class="img-container"> <img src="/static/imghwm/default1.png" data-src="http://placehold.it/150x150" class="lazy" alt=""> </div> </div>
用于图像对齐的CSS:
.img-container { text-align: center; /* Align center inline elements */ font: 0/0 a; /* Hide the characters like spaces */ } .img-container:before { content: ' '; display: inline-block; vertical-align: middle; height: 100%; } .img-container img { vertical-align: middle; display: inline-block; }
浏览器兼容性的替代方案
为了更好地跨浏览器兼容性,您可以使用 div 元素作为图像容器的第一个子元素而不是伪元素:
HTML:
<div class="img-container"> <div class="centerer"></div> <img src="/static/imghwm/default1.png" data-src="http://placehold.it/150x150" class="lazy" alt=""> </div>
CSS:
.img-container .centerer { display: inline-block; vertical-align: middle; height: 100%; }
使用 max-* 属性进行图像控制
保留图像当容器内宽度较小时,可以使用 max-height 和 max-width 属性图片:
.img-container img { max-height: 100%; /* Set maximum height to 100% of its parent */ max-width: 100%; /* Set maximum width to 100% of its parent */ }
以上是如何在响应式容器内垂直对齐图像?的详细内容。更多信息请关注PHP中文网其他相关文章!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

我知道,我知道:有大量的内容管理系统选项可用,而我进行了几个测试,但实际上没有一个是一个,y&#039;知道吗?怪异的定价模型,艰难的自定义,有些甚至最终成为整个&

链接CSS文件到HTML可以通过在HTML的部分使用元素实现。1)使用标签链接本地CSS文件。2)多个CSS文件可通过添加多个标签实现。3)外部CSS文件使用绝对URL链接,如。4)确保正确使用文件路径和CSS文件加载顺序,优化性能可使用CSS预处理器合并文件。

选择Flexbox还是Grid取决于布局需求:1)Flexbox适用于一维布局,如导航栏;2)Grid适合二维布局,如杂志式布局。两者在项目中可结合使用,提升布局效果。

包含CSS文件的最佳方法是使用标签在HTML的部分引入外部CSS文件。1.使用标签引入外部CSS文件,如。2.对于小型调整,可以使用内联CSS,但应谨慎使用。3.大型项目可使用CSS预处理器如Sass或Less,通过@import导入其他CSS文件。4.为了性能,应合并CSS文件并使用CDN,同时使用工具如CSSNano进行压缩。

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重构自己的代码看起来是什么样的?约翰·瑞亚(John Rhea)挑选了他写的一个旧的CSS动画,并介绍了优化它的思维过程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3汉化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具