将文本中心放在flexbox中的图像上

编程入门 行业动态 更新时间:2024-10-25 10:31:37
本文介绍了将文本中心放在flexbox中的图像上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用FlexBox将文本中心对齐到< img> ?

How can I centre align text over an <img> preferably using FlexBox?

body { margin: 0px; } .height-100vh { height: 100vh; } .center-aligned { display: box; display: flex; box-align: center; align-items: center; box-pack: center; justify-content: center; } .background-image { position: relative; } .text { position: absolute; }

<section class="height-100vh center-aligned"> <img class="background-image" src="vignette2.wikia.nocookie/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" /> <div class="text">SOME TEXT</div> </section>

推荐答案

布局可以通过flexbox和定位属性来实现。没有对HTML的更改。

The layout can be achieved with flexbox and positioning properties. No changes to HTML.

.height-100vh { height: 100vh; display: flex; /* establish flex container */ flex-direction: column; /* stack flex items vertically */ position: relative; /* establish nearest positioned ancestor for absolute positioning */ } .text { position: absolute; left: 50%; /* horizontal alignment */ top: 50%; /* vertical alignment */ transform: translate(-50%, -50%); /* horizontal, vertical alignment fine tuning; explanation: stackoverflow/q/36817249 */ }

修订的Codepen

上面的代码将文本的垂直和水平方向置于图片的中心:

The code above centers the text both vertically and horizontally over the image:

更多推荐

将文本中心放在flexbox中的图像上

本文发布于:2023-11-09 15:57:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:放在   图像   文本   中心   flexbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!