FreeCodeCamp致敬项目调试:修正问题获满分及CSS替代<br>方案
FreeCodeCamp致敬项目满分修正指南
项目当前状态
- 现有得分:8/10
- 已满足全部项目要求的用户故事,已添加自定义id属性,存在可修正的代码问题可达成满分
- 核心疑问:在
id="emphasis"的p标签中使用了<br>换行,咨询对应的CSS替代方案
扣分项问题定位
- 布局固定尺寸问题:
#main设置固定宽度1330px、固定高度2260px,小屏访问时会出现横向滚动条;时间线和强调文本使用固定左边距居中,窗口缩放时内容会错位,不符合响应式校验要求 - 图片规则不满足:
#image未设置display: block属性,不符合项目对图片的校验要求;#img-div设置固定高度680px,图片尺寸变化时会出现裁切溢出;img标签alt属性为空,不符合无障碍校验规则 - HTML嵌套错误:
#tribute-link为p标签,内部嵌套块级h3元素,违反HTML语法规范(p标签不允许嵌套块级元素) - 文本拼写错误:列表文本中
resistent、high-yeidling为拼写错误,正确拼写分别为resistant、high-yielding
<br>标签的CSS替代方案
原代码通过2个连续<br>制造引文与署名的垂直间距,可通过块级元素的外边距实现同等效果,完全分离内容与样式:
- 去掉所有
<br>标签 - 将引文内容和署名分别放在独立的行内块/块级元素中,通过CSS设置上下间距即可
对应CSS设置示例:
调整后的HTML结构示例:#emphasis em .quote { display: block; margin-bottom: 1.5rem; }
块级元素默认独占一行,配合margin值即可复刻原<p id="emphasis"> <em> <span class="quote">"Borlaug's life and achievement are testimony to the far-reaching contribution that one man's towering intellect, persistence and scientific vision can make to human peace and progress."</span> <span>-- Indian Prime Minister Manmohan Singh</span> </em> </p><br>的间距效果,无需依赖HTML换行标签。
关键修正代码片段
调整后的CSS
* { box-sizing: border-box; margin: 0; padding: 0; } body { text-align: center; color: rgb(51, 51, 51); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; } #main { background-color: rgb(238, 238, 238); width: 95%; max-width: 1330px; margin: 30px auto 25px; border-radius: 5px; padding: 20px 0; } #title { padding-top: 30px; font-size: 2.5em; } #img-div { width: 95%; max-width: 1300px; background-color: rgb(255, 255, 255); margin: 20px auto; padding: 20px; } #image { display: block; max-width: 100%; height: auto; margin: 0 auto; } #img-caption { padding-top: 15px; } #tribute-info { text-align: left; max-width: 550px; margin: 50px auto; line-height: 1.6; } li { padding-bottom: 20px; } #emphasis { max-width: 550px; text-align: left; margin: 30px auto; line-height: 1.6; } #emphasis em .quote { display: block; margin-bottom: 1.5rem; } #tribute-link { padding: 20px 0 40px; }
调整后的HTML结构
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Dr. Norman Borlaug Tribute</title> <link type="text/css" rel="stylesheet" href="style.css" /> </head> <body> <main id="main"> <h1 id="title">Dr. Norman Borlaug</h1> <p>The man who saved a billion lives</p> <div id="img-div"> <img id="image" src="Dr. Norman Borlaug.jpg" alt="Dr. Norman Borlaug trains biologists in Mexico on high-yield wheat cultivation" /> <figcaption id="img-caption"> Dr. Norman Borlaug, third from the left, trains biologists in Mexico on how to increase wheat yields - part of his life-long war on hunger. </figcaption> </div> <h3>Here's a time line of Dr. Borlaug's life:</h3> <ul id="tribute-info"> <!-- 原有列表内容保留,修正文本拼写错误即可 --> </ul> <p id="emphasis"> <em> <span class="quote">"Borlaug's life and achievement are testimony to the far-reaching contribution that one man's towering intellect, persistence and scientific vision can make to human peace and progress."</span> <span>-- Indian Prime Minister Manmohan Singh</span> </em> </p> <h3 id="tribute-link"> If you have time, you should read more about this incredible human being on his <a href="https://en.wikipedia.org/wiki/Norman_Borlaug" target="_blank">Wikipedia entry</a>. </h3> </main> </body> </html>
内容的提问来源于stack exchange,提问作者Noma
相关产品推荐
相关产品推荐

