练习 | CSS 浮动模型 清除模型 伪类

本文通过四个题目详细介绍了CSS中的浮动模型和清除模型,包括使用after伪类清除浮动,额外标签法,以及:before和:after伪元素在绘制日期输入对象和聊天对话框中的应用。同时,文章提及了在实现对话框时遇到的挑战,并推荐了纯CSS实现对话框的补充阅读资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题1:清除浮动(使用after伪类)

请用浮动模型和清除模型的实现下面版面
在这里插入图片描述

在这里插入图片描述
index.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>EX01</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>
	<div class="container">
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
  </div>
</body>
</html>

index.css

.container {
  border:1px solid #333;
  width: 280px;
}

.container:after {
  content: "."; 
  height: 0; 
  visibility: hidden; 
  display: block; 
  clear: both; 
}

.content {
  float: left;
  margin: 3px;
  background: #eee;
  width: 50px;
  height: 50px;
}

题2:清除浮动(额外标签法)

请用浮动模型和清除模型的实现下面版面
在这里插入图片描述

在这里插入图片描述
index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>EX02</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>
  <div class="container">
    <div class="content first"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <div class="content"></div> 
    <br style="clear:both;" />
  </div>
</body>
</html>

index.css

.container {
  border:1px solid #333;
  width: 280px;
}

.content {
  float: left;
  margin: 3px;
  background: #eee;
  width: 50px;
  height: 50px;
}

.content.first {
  height: 103px;
  width: 103px;
}

题3:使用伪类

请用:before伪元素和:after伪元素的绘制日期输入对象
在这里插入图片描述

在这里插入图片描述
日历图标为从 iconfont 下载的图片

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>EX03</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>
  <div class="container">
    <div class="content">2016-09-12</div>
  </div>
</body>
</html>

index.css

.container {
  border: 1px solid #eee;
  height: 30px;
  width: 220px;
}

.content {
  margin: 0 10px;
  line-height: 30px;
  color: #666;
}

.content:before {
  content: "开始日期:";
  color: #999;
}

.content:after {
  content: url(date.png);
  color: #999;
  margin-left: 5px;
}

题4:对话框

请用:before伪元素和:after伪元素的绘制聊天对话框对象
在这里插入图片描述

在这里插入图片描述
没有弄出来小三角形的小弧度 orz

index.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>EX04</title>
	<link rel="stylesheet" href="index.css">
</head>
<body>
  <div class="container"></div>
</body>
</html>

index.css

.container {
  background-color: green;
  height: 100px;
  width: 200px;
  border-radius: 10px;
}

.container:after {
  content: "";
  border-top: 13px solid transparent;
  border-left: 20px solid green;
  border-bottom: 13px solid transparent;
  position: absolute;
  left: 200px;
  top: 25px;
  color: green;
}

补充阅读:(码)
纯CSS实现各类气球泡泡对话框效果
翻译:关于完美圆角实现的研究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值