首页 HTML/CSS css文字放在img上

css文字放在img上

作者:胡同里的砖头 围观群众:199 更新于:2023-11-20

.img-text {
position: relative;
}
.img-text img {
display:block;
margin:0 auto;
}
.img-text p {
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
color: #fff;
font-size: 20px;
text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.5);
padding:10px 0;
background-color: rgba(0,0,0,.5);
}
以上代码中,先将图片所在的容器设为相对定位(position: relative)。接着设置img标签为块级元素(display: block),并水平居中(margin:0 auto)。最后使用绝对定位(position: absolute)将文字容器放到图片底部,使用宽度100%(width:100%)使其与图片同宽,使用文本居中(text-align:center)、文字颜色(color)、字体大小(font-size)、阴影效果(text-shadow)和背景(background-color)属性来设置文字样式。可以根据需要调整属性值,以达到不同的效果。

  • 本文标题: css文字放在img上
  • 文章分类:【HTML/CSS】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8