技能测试:背景和边框

此技能测试的目的是帮助您评估是否理解 CSS 中框的背景和边框

注意: 如需帮助,请阅读我们的“技能测试”使用指南。您也可以通过我们的沟通渠道与我们联系。

任务 1

在此任务中,我们要为页面标题添加背景、边框和一些基本样式。

完成任务

  1. 为该框添加 5px 的黑色实线边框,边框圆角为 10px。

  2. <h2> 添加半透明黑色背景色,并将文本设置为白色。

  3. 添加一个背景图像,并调整其大小以覆盖整个框。您可以使用以下图像:

    https://mdn.github.io/shared-assets/images/examples/balloons.jpg
    

您的最终结果应如下面的图片所示

Images shows a box with a photograph background, rounded border and white text on a semi-transparent black background.

html
<div class="box">
  <h2>Backgrounds & Borders</h2>
</div>
css
body {
  padding: 1em;
  font: 1.2em / 1.5 sans-serif;
}

* {
  box-sizing: border-box;
}

.box {
  padding: 0.5em;
}

.box {
  /* Add styles here */
}

h2 {
  /* Add styles here */
}
点击此处显示解决方案

您应该使用 borderborder-radiusbackground-imagebackground-size,并理解如何使用 RGB 颜色使背景色部分透明。

css
.box {
  border: 5px solid black;
  border-radius: 10px;
  background-image: url("https://mdn.github.io/shared-assets/images/examples/balloons.jpg");
  background-size: cover;
}

h2 {
  background-color: rgb(0 0 0 / 50%);
  color: white;
}

任务 2

在此任务中,我们要为装饰性框添加背景图像、边框和其他一些样式。

完成任务

  1. 为该框添加 5px 的浅蓝色边框,并将左上角圆角设置为 20px,右下角圆角设置为 40px。

  2. 标题使用 star.png 图像作为背景图像,左侧为单个居中显示的星星,右侧为重复的星星图案。您可以使用以下图像:

    https://mdn.github.io/shared-assets/images/examples/star.png
    
  3. 确保标题文本不覆盖图像,并且文本居中——您需要使用先前课程中学到的技巧来实现这一点。

您的最终结果应如下面的图片所示

Images shows a box with a blue border rounded at the top left and bottom right corners. On the left of the text is a single star, on the right 3 stars.

html
<div class="box">
  <h2>Backgrounds & Borders</h2>
</div>
css
body {
  padding: 1em;
  font: 1.2em / 1.5 sans-serif;
}
* {
  box-sizing: border-box;
}
.box {
  width: 300px;
  padding: 0.5em;
}

.box {
  /* Add styles here */
}

h2 {
  /* Add styles here */
}
点击此处显示解决方案

您需要为标题添加内边距,使其不覆盖星形图像——这与先前 盒子模型课程 中的学习内容相关。文本应使用 text-align 属性对齐。

css
.box {
  border: 5px solid lightblue;
  border-top-left-radius: 20px;
  border-bottom-right-radius: 40px;
}

h2 {
  padding: 0 40px;
  text-align: center;
  background:
    url("https://mdn.github.io/shared-assets/images/examples/star.png")
      no-repeat left center,
    url("https://mdn.github.io/shared-assets/images/examples/star.png") repeat-y
      right center;
}