
想法是 焦点 内 DIV, 与图像 绝对定位 { 位置: 绝对; }. 要做到这一点, 首先是 分配, 到 div 中将包含图像, 定位 相对, 从而防止绝对定位是形象的有相对定位的元素父容器 下一个, 或是不及格, 到 身体.
假设我们有下面的代码与 div 和内相同的图像:
1 2 3 | <div class="contenedor"> <img class="imagen" src="" /> </div> |
应用于容器及图像的样式如下:
1 2 3 4 5 6 7 8 9 10 11 | .contenedor { position: relative; } .imagen { position:absolute; left:0px; right:0px; top:0px; margin:auto; } |
若要查看的一些结果 示例, 我们要去 修改 通过将指定的 HTML 代码 边缘 和 大小 到 div 中, 以及添加 路线 和 大小 对图像. 它会看起来像这样:
1 2 3 4 5 | <div class="contenedor" style="border:1px solid orange;width:100%;max-width:500px;height:400px;"> <img class="imagen" src="https://professor-falken.com/wp-content/uploads/2016/04/10-fondos-de-pantalla-para-que-uses-en-WhatsApp-Telegram-o-tu-aplicacion-de-mensajeria-favorita-Image-5-professor-falken.com_.jpg" style="width:200px;" /> </div> |
和, 然后, 结果:


我们的留言或输入到论坛