gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
分析并优化如下代码,使其文字区域的高度一直,要有title,并保证文字要对齐 HTML: [code] CSS: [code]
- User
分析并优化如下代码,使其文字区域的高度一直,要有title,并保证文字要对齐
HTML:
<div class="card-list"> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/3fa18f62ec5316bc1315.jpg" alt="Comprehensive Security"> <div class="card-title"> <h3>Comprehensive Security</h3> </div> <div class="card-desc"> <p>Build an all-around security and safety system by harnessing fully-functional security cameras and diversified sensors. 24/7 monitoring and live alerts provide users with peace of mind.</p> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/12996f67d7eb412f7a33.jpg" alt="Device Automation"> <div class="card-title"> <h3>Device Automation</h3> </div> <div class="card-desc"> <p>Achieve appliance automation, the core of home automation, to let users discover the magic of a smart home and fully enjoy a smart life.</p> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/6c796362ca346acc25aa.jpg" alt="Health Data Record"> <div class="card-title"> <h3>Health Data Record</h3> </div> <div class="card-desc"> <p>Our powerful and interconnected health monitoring products enable health tracking and analysis, allowing users to gain a deeper insight into their health.</p> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/b7707f97fd2f246f8d1a.jpg" alt="Home Entertainment"> <div class="card-title"> <h3>Home Entertainment</h3> </div> <div class="card-desc"> <p>Our innovative smart devices in the areas of lighting, video, and audio help users easily embrace a theater or party atmosphere at home anytime.</p> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/67bf77c85a9e6106619f.jpg" alt="Fitness Life"> <div class="card-title"> <h3>Fitness Life</h3> </div> <div class="card-desc"> <p>Smart fitness devices with in-app fitness service features empower you to provide more essential and popular home fitness scenes.</p> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/fdf40edf73ff916939da.png" alt="24/7 Energy Monitoring"> <div class="card-title"> <h3>24/7 Energy Monitoring</h3> </div> <div class="card-desc"> <p>Working with top energy companies, Tuya provides 24/7 energy monitoring capabilities and AI technologies to promote efficient energy use.</p> </div> </div> </div> </div>CSS:
.card-list { display: flex; flex-wrap: wrap; justify-content: space-between; } .card-item { position: relative; width: 30%; margin-bottom: 20px; cursor: pointer; } .card-img { position: relative; width: 100%; height: 200px; overflow: hidden; } .card-img img { width: 100%; height: auto; display: block; transition: transform 0.3s ease-in-out; } .card-img:hover img { transform: scale(1.1); } .card-title, .card-desc { position: absolute; bottom: 0; left: 0; right: 0; background-color: #fff; padding: 20px; transform: translateY(100%); transition: transform 0.3s ease-in-out; } .card-img:hover .card-title, .card-img:hover .card-desc { transform: translateY(0%); } .card-title h3, .card-desc p { font-size: 14px; line-height: 1.5; margin: 0; } .card-title h3 { font-size: 18px; font-weight: bold; margin-top: 0; margin-bottom: 5px; } - Assistant
优化代码如下:
HTML:
<div class="card-list"> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/3fa18f62ec5316bc1315.jpg" alt="Comprehensive Security"> <div class="card-content"> <div class="card-title"> <h3>Comprehensive Security</h3> </div> <div class="card-desc"> <p>Build an all-around security and safety system by harnessing fully-functional security cameras and diversified sensors. 24/7 monitoring and live alerts provide users with peace of mind.</p> </div> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/12996f67d7eb412f7a33.jpg" alt="Device Automation"> <div class="card-content"> <div class="card-title"> <h3>Device Automation</h3> </div> <div class="card-desc"> <p>Achieve appliance automation, the core of home automation, to let users discover the magic of a smart home and fully enjoy a smart life.</p> </div> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/6c796362ca346acc25aa.jpg" alt="Health Data Record"> <div class="card-content"> <div class="card-title"> <h3>Health Data Record</h3> </div> <div class="card-desc"> <p>Our powerful and interconnected health monitoring products enable health tracking and analysis, allowing users to gain a deeper insight into their health.</p> </div> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/b7707f97fd2f246f8d1a.jpg" alt="Home Entertainment"> <div class="card-content"> <div class="card-title"> <h3>Home Entertainment</h3> </div> <div class="card-desc"> <p>Our innovative smart devices in the areas of lighting, video, and audio help users easily embrace a theater or party atmosphere at home anytime.</p> </div> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/67bf77c85a9e6106619f.jpg" alt="Fitness Life"> <div class="card-content"> <div class="card-title"> <h3>Fitness Life</h3> </div> <div class="card-desc"> <p>Smart fitness devices with in-app fitness service features empower you to provide more essential and popular home fitness scenes.</p> </div> </div> </div> </div> <div class="card-item"> <div class="card-img"> <img src="//seastatic1bhugtk.cdn5th.com/static/portal-intl-2019/_next/static/img/fdf40edf73ff916939da.png" alt="24/7 Energy Monitoring"> <div class="card-content"> <div class="card-title"> <h3>24/7 Energy Monitoring</h3> </div> <div class="card-desc"> <p>Working with top energy companies, Tuya provides 24/7 energy monitoring capabilities and AI technologies to promote efficient energy use.</p> </div> </div> </div> </div> </div>CSS:
.card-list { display: flex; flex-wrap: wrap; justify-content: space-between; } .card-item { width: calc(33.33% - 10px); /* 添加间距 */ margin-bottom: 20px; cursor: pointer; } .card-img { position: relative; width: 100%; height: 200px; overflow: hidden; } .card-img img { width: 100%; height: auto; display: block; transition: transform 0.3s ease-in-out; } .card-img:hover img { transform: scale(1.1); } .card-content { position: absolute; bottom: 0; left: 0; right: 0; background-color: #fff; padding: 20px; transform: translateY(100%); transition: transform 0.3s ease-in-out; } .card-img:hover .card-content { transform: translateY(-100%); } .card-title h3, .card-desc p { margin: 0; } .card-title { margin-bottom: 5px; /* 让标题和描述之间有一定的距离 */ } .card-title h3 { font-size: 18px; font-weight: bold; } .card-desc p { font-size: 14px; line-height: 1.5; } .card-desc { height: 80px; /* 设置描述文字区域高度 */ overflow: hidden; } .card-img::before { /* 防止文字区域高度不一时造成的卡顿 */ content: ""; display: block; padding-top: 75%; }优化点:
- 统一在一个容器
.card-content中加入标题和描述以实现文字居中且垂直对齐。 - 使用
calc()函数和添加margin-right的方式,实现卡片间的间距,并避免出现最后一张卡片在一行的情况。 - 将
margin、font-size等属性设置为0,然后根据需求再逐个设置,避免浏览器默认样式的影响。 - 设置
height和overflow属性,以确保文字区域高度一致且超出后能够省略显示。 - 添加卡片图片的宽高比,防止因文字区域高度不一时造成的卡顿。
- 统一在一个容器