Home > Article > Web Front-end > Beginner's article: How to use CSS to create image and text layout (code sharing)
In the previous article "Teach you step by step how to use css to create table border setting effects (with code)", I introduced you how to use css to create table border setting effects. The following article will introduce to you how to use CSS to create image and text layout. Let’s take a look at how to do it.
There are often such CSS image and text layouts in web pages. Let me share with you the renderings and see the effect. Let’s study how to achieve it. Let us explain the basic process of html css image text layout.
Mainly use CSS attributesvisibility: hidden;
Hide the p
label text, and then use hover
Selector to change the height of the class card
and display the p
label text visibility: visible;
.
1. First, create a new HTML file and define three div
tags.
<body> <div class="container"> <div class="card"> <div class="img"> <img src="54545454.jpg" alt="Beginner's article: How to use CSS to create image and text layout (code sharing)" > <!-- one --> </div> <div class="top-text"> <div class="name"> 第一次班级聚会 </div> </div> <div class="bottom-text"> <div class="text"> 还记的,2018年,大一下学期,开学我们第一次班级聚会,相聚在北海园博园假山, 一起动手、齐力快乐的一起烧烤,虽然天色黑的伸手不见五指,让人害怕,但我们相聚在一起, 有说有笑,彼此相知,却一点感觉不到害怕,那刻,仿佛时间停住了,只剩下快乐相伴。 </div> </div> </div> <!-- two --> <div class="card"> <div class="img"> <img src="54545454.jpg" alt="Beginner's article: How to use CSS to create image and text layout (code sharing)" > </div> <div class="top-text"> <div class="name"> 优秀班级评比 </div> <!-- <p>Apps Developer</p> --> </div> <div class="bottom-text"> <div class="text"> 还记得,大二上学期,一次晚点名辅导员说,每个班级要拍出最美的班级照, 参加最美班级的摄影评比,我们大家一起在群里齐思广议,每个人把自己觉得好的想法分享出来, 争取拍几张最美的班级照,很想说,我们大家认真付出的样子真的帅呆了。 </div> </div> </div> <!-- three --> <div class="card"> <div class="img"> <img src="54545454.jpg" alt="Beginner's article: How to use CSS to create image and text layout (code sharing)" > </div> <div class="top-text"> <div class="name"> 团日活动 </div> </div> <div class="bottom-text"> <div class="text"> 还记得,大二下学期,大家为了完成辅导员下发了“最美北海”我为北海做的那些事志愿活动, 我们大家来到北海美丽的海滩公园,齐心志愿动手去捡垃圾,保护海滩,大家人认真捡着垃圾, 看到旁边的人举起大拇指,感觉此刻值了。 </div> </div> </div> </div> </body>
2. The class
of the div
box is set to container
to avoid floating layout. The bottom is not aligned.
3. Add style settings to container
: display: flex
Flexible layout; align-items: center
Align in the center along the vertical axis;justify-content: left
Just align the axis direction to the left.
<style type="text/css"> .container{ width: 100%; height: 500px; padding: 0px 40px; display: flex; align-items: center; justify-content: left; }
Code effect
4. Add style settings to card
: transition
attribute mouseover; box-shadow
Set the shadow effect; background-color
The background color of the attribute element.
.card{ height: 270px; max-width: 350px; margin: 0px 20px; background-color: white; transition: 0.4s; box-shadow: 2px 2px 5px rgba(0,0,0,0.2); }
5. Select the mouse movement style for the hover
selector.
.card:hover{ height:400px; box-shadow:5px 5px 10px rgba(0,0,0,0.2); }
6. Use the img
tag to process the image size width and height, object-fit: cover
to cut the image and retain the original proportion of the image.
.card .img{ height: 200px; width: 100%; } .card .img img{ height: 100%; width: 100%; object-fit: cover; }
7, visibility: hidden;
Hide the p
label text and add the transition
attribute on mouseover.
.card .bottom-text{ text-indent: 2em; padding: 0 20px 10px 20px; margin-top: 5px; background-color: white; visibility: hidden; transition: 0.5s;
8, hover
selector to change the height of class card
, and p
label textvisibility: visible;
display.
.card:hover .bottom-text{ opacity: 1; visibility: visible;
ok, done! !
Complete code
CSS图片文字排版 <style type="text/css"> .container{ width: 100%; height: 500px; padding: 0px 40px; display: flex; align-items: center; justify-content: left; } .card{ height: 270px; max-width: 350px; margin: 0px 20px; background-color: white; transition: 0.4s; box-shadow: 2px 2px 5px rgba(0,0,0,0.2); } .card:hover{ height:400px; box-shadow:5px 5px 10px rgba(0,0,0,0.2); } .card .img{ height: 200px; width: 100%; } .card .img img{ height: 100%; width: 100%; object-fit: cover; } .card .top-text{ padding-top: 5px; } .card .top-text .name{ font-size: 25px; font-weight:600; color: #202020; } .card .top-text p{ font-size: 20px; font-weight:600; color: #e74c3c; line-height: 20px; } .card .bottom-text{ text-indent: 2em; padding: 0 20px 10px 20px; margin-top: 5px; background-color: white; visibility: hidden; transition: 0.5s; } .card:hover .bottom-text{ opacity: 1; visibility: visible; } .card .bottom-text .text{ text-align: justify; }第一次班级聚会还记的,2018年,大一下学期,开学我们第一次班级聚会,相聚在北海园博园假山, 一起动手、齐力快乐的一起烧烤,虽然天色黑的伸手不见五指,让人害怕,但我们相聚在一起, 有说有笑,彼此相知,却一点感觉不到害怕,那刻,仿佛时间停住了,只剩下快乐相伴。优秀班级评比还记得,大二上学期,一次晚点名辅导员说,每个班级要拍出最美的班级照, 参加最美班级的摄影评比,我们大家一起在群里齐思广议,每个人把自己觉得好的想法分享出来, 争取拍几张最美的班级照,很想说,我们大家认真付出的样子真的帅呆了。团日活动还记得,大二下学期,大家为了完成辅导员下发了“最美北海”我为北海做的那些事志愿活动, 我们大家来到北海美丽的海滩公园,齐心志愿动手去捡垃圾,保护海滩,大家人认真捡着垃圾, 看到旁边的人举起大拇指,感觉此刻值了。
Recommended learning: CSS video tutorial
The above is the detailed content of Beginner's article: How to use CSS to create image and text layout (code sharing). For more information, please follow other related articles on the PHP Chinese website!