html - 在图像中放置文本
问题描述
<head>
<style>
.relative{position:relative; width:600px;}
.absolute-text{position:absolute; bottom:1; font-size:12px; font-family:"vedana"; background:rgba(251,251,251,0.5);
padding:10px 20px; width:10%; text-align:center;}
</style>
</head>
<body>
<div class="relative">
<img src="Chttps://i.stack.imgur.com/Ss3PX.jpg">
<p class="absolute-text">16<br>august</p>
</div>
</body>
我有一张图片,并希望使用 html 和 css 在该图片的按钮左角中添加一些文本......
这是一个看起来像这样的图片:
我试了很多次,还是没有。。
解决方案
您必须使用 css position absolute 和 position relative。使父相对和子相对。
.container {
position: relative;
text-align: center;
color: white;
}
.bottom-left {
position: absolute;
bottom: 8px;
left: 16px;
}
.top-left {
position: absolute;
top: 8px;
left: 16px;
}
.top-right {
position: absolute;
top: 8px;
right: 16px;
}
.bottom-right {
position: absolute;
bottom: 8px;
right: 16px;
}
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<h2>Image Text</h2>
<p>How to place text over an image:</p>
<div class="container">
<img src="https://www.w3schools.com/w3images/hamburger.jpg" alt="Snow" style="width:100%;">
<div class="bottom-left">Bottom Left</div>
<div class="top-left">Top Left</div>
<div class="top-right">Top Right</div>
<div class="bottom-right">Bottom Right</div>
<div class="centered">Centered</div>
</div>
推荐阅读
- django-rest-framework - Django,在序列化程序中使用上下文,many=True
- r - 在多项式 GAM (mgcv) 中使用权重
- swift - 使用 Swift Package Manager、Cocoa Pods 或其他工具将最小的 GRDB 添加到 Xcode 项目
- python - 在 jupyter notebook 中保存 pandas 的情节
- java - Tic-Tac-Toe AI 使用 Minimax 算法产生堆栈溢出错误 Java
- html - 让图像贴在 DIV 的底部?不工作
- php - PHP Symfony 使用 Doctrine 实体属性和关联实体属性对实体进行排序
- javascript - 停止绑定子 div,将其与另一个模型绑定
- tensorflow - Tensorflow的逆变换到squeeze
- video - FFMpeg 默认比特率值