【隐藏和裁剪:overflow与clip】CSS实现一字两色/多色(左白右黑/上白下黑)

起因:经常在一些APP、网站、公众号、海报等等途径看到两色组成(左右或上下)的一个字,希望自己去通过所学知识用CSS实现

(当然,一些CSS3特效网一堆源码,但是,为何不自己亲手尝试呢?这样不更容易加深对知识点的理解?是否和专业源码一样先不考虑,只要视觉上达到了,就成功了一步。)

例:句读app(还原仿写放在最下面,含源码)

在这里插入图片描述



一、刚开始我能想到的方法思路是: 分别做两个图层(两个同级元素),通过overflow(溢出隐藏)

  1. html部分,创建两个同级元素,内容要一样,不然怎么重叠,颜色不同就行了:List item
  2. 虽然定义 line-height 等于元素高来垂直居中,但是因为“字”有点长,下面一点就超出了元素底边,后面需要调整 line-height :在这里插入图片描述
  3. 设置文字颜色:在这里插入图片描述
  4. 移位重叠(这里我用 margin-top,当然,absolute+top也是可以的,第二种方法我才用absolute):
    (额之前加边是为了说明“字”即使设置了行高,也会超出底边,忘了去掉)在这里插入图片描述
    在这里插入图片描述
  5. 给处于最上层的元素 overflow,配合 width 来隐藏掉一部分:List item
  6. 由于文字小部分超出了元素的高(底边),所以line-height做了微调来移动文字在元素盒子内的垂直位置:在这里插入图片描述
  7. 移动整个部分到粉色盒子(box)的中间(把做好的一字两色用wrap包裹成一个整体,只需移动wrap):在这里插入图片描述
  8. 父元素忘了给 overflow: hidden; 导致margin-top/bottom无效(带着整个父元素一起跑了): 在这里插入图片描述
    在这里插入图片描述
  9. 最后去背景色完成(当然,我举例步骤说明才加的背景,实际不需要的):List item
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!--
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/index.css" />
-->
<style type="text/css">
/*reset.css就不需要了,此处练习用不上*/

/*index.css*/
.box {
	width: 500px;
	height: 500px;
	margin: 200px;
	overflow: hidden;
	background: pink;
}

.wrap {
	width: 200px;
	height: 200px;
	margin: 150px auto;
}

.content {
	/*width: 200px;		这里就不定宽了,因为两者的宽都不同,在另一个类分别定义宽*/
	height: 200px;
	font-size: 200px;
	text-align: center;
	line-height: 185px;
}

.color-white {
	width: 200px;
	color: #fff;
}

.color-black {
	width: 100px;
	margin-top: -200px;
	color: #000;
	overflow: hidden;
}
</style>
</head>

<body>
	<div class="box">
		<div class="wrap">
			<div class="content color-white"></div>
			<div class="content color-black"></div>
		</div>
	</div>
</body>
</html>


二、后来我发现,overflow 缺乏灵活性,不能任意调整隐藏显示部分,于是我尝试了img的裁剪属性 clip 发现,可行且更灵活,你可以任意裁剪显示想要的部分


  1. 前面的步骤与上面是一样的:List item
  2. 将下方元素移动到上方,我这用position: absolute;,如果跟上面一样用margin-top也可以,看你喜欢:在这里插入图片描述
  3. 然后给最上层的元素,设置clip:(关于clip的用法,我会另外写一篇博客详细介绍)在这里插入图片描述
  4. 灵活的clip裁剪属性:在这里插入图片描述
    在这里插入图片描述
  5. 最后让该部分居中于粉色盒子(box):在这里插入图片描述
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!--
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/index.css" />
-->
<style type="text/css">
/*reset.css不需要*/

/*index.css*/
.box {
	width: 500px;
	height: 500px;
	margin: 200px;
	overflow: hidden;
	background: pink;
}

.wrap {
	width: 200px;
	height: 200px;
	margin: 150px;
	position: relative;
}

.content {
	width: 200px;
	height: 200px;
	font-size: 200px;
	line-height: 180px;
}

.color-white {
	color: #fff;
}

.color-black {
	color: #000;
	clip: rect(0px, 100px, 200px, 0px);
	position: absolute;
	top: 0;
}
</style>
</head>

<body>
	<div class="box">
		<div class="wrap">
			<div class="content color-white"></div>
			<div class="content color-black"></div>
		</div>
	</div>
</body>
</html>



最后,来还原(仿)“句读”app的页面【未兼容】(用的是overflow:hidden;

第一次练习:

在这里插入图片描述
源代码复制粘贴即可查看效果:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!--
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/index.css" />
-->
<style type="text/css">
/*reset.css*/
body, h1, h2, h3, h4, h5, h6, dl, dt, dd, ol, ul, li, p, a, input, form, textarea {
	padding: 0;
	margin: 0;
	font-family: "Microsoft YaHei";
}

ol, ul, li {
	list-style: none;
}

img {
	display: block;
	border: none;
}

a {
	display: block;
	text-decoration: none;
}

.clearfix {
	zoom: 1;
}

.clearfix:after {
	display: block;
	content: '';
	clear: both;
	visibility: hidden;
	height: 0;
}

/*--------------------------------------------------------------------*/

/*index.css*/
.bg {
	min-width: 100%;
	min-height: 100%;
	position: absolute;
	overflow: hidden;
	background: pink;
}

.main-container {}

.main-wrap {
	width: 476px;
	height: 802px;
	background: #fff;
	margin: 100px auto 0;
}

.main-container .date {
	width: 120px;
	font-size: 120px;
	line-height: 120px;
}

/*header*/
.main-container .main-header {
	height: 62px;
}

.main-header .string-logo {
	display: inline-block;		/*block会占行,把它行内化或者行内块级化*/
	font-size: 34px;
	line-height: 34px;
	padding-top: 12px;
	padding-left: 32px;
}

.main-header .list-button {
	float: right;
}

.main-header .list-button .list-button-item {
	float: left;
	width: 73px;
}

.main-header .list-button-item .item-link {
	display: inline-block;
	width: 30px;
	height: 30px;
	float: left;
	margin-top: 18px;
}

.main-header .list-button-item .item-num {
	font-size: 16px;
	color: #747474;
	float: left;
	margin-top: 12px;
	margin-left: 5px;
}

.main-header .list-button-item .item-comments {
	background: url("https://img-blog.csdnimg.cn/20200207204823196.png") no-repeat;
	background-size: 100%;
}

.main-header .list-button-item .item-like {
	background: url("https://img-blog.csdnimg.cn/20200207204830919.png") no-repeat;
	background-size: 100%;
}

.main-header .list-button-item .item-share {
	background: url("https://img-blog.csdnimg.cn/20200207204840140.png") no-repeat;
	background-size: 90%;
	margin-left: 13px;
}

/*banner*/
.main-container .main-banner {
	width: 476px;
	height: 318px;
	overflow: hidden;
	position: relative;
}

.main-banner .photo-txt {
	font-size: 16px;
	line-height: 30px;
	color: #fff;
	writing-mode: vertical-rl;	/*非IE浏览器可识别*/
	writing-mode: tb-rl;		/*IE浏览器只识别这条*/
	text-decoration: underline;
	letter-spacing: 5px;
	position: absolute;
	top: 30px;
	right: 20px;		/*这里需要考虑 line-height 带来的偏差*/
}

.main-banner .photo img{
	width: 476px;
	height: 318px;
}

.main-banner .color-white {
	color: #fff;
	margin-top: -80px;
	margin-left: 46px;
}

/*content*/
.main-container .main-content {
	width: 476px; 
	height: 422px;
	position: relative;
	overflow: hidden;
}

.main-content .color-black {
	color: #333;
	margin-top: -80px;
	margin-left: 46px;
}

.main-content .time {
	font-size: 15px;
	line-height: 15px;
	color: #747474;
	position: absolute;
	top: 14px;
	right: 32px;
}

.main-content .string-sentence {
	font-size: 24px;
	line-height: 38px;
	padding: 30px;
	margin-top: 146px;
}

.main-content .string-name {
	font-size: 24px;
	float: right;
	margin-right: 30px;
}
</style>
</head>

<body>
	<div class="bg">
		<div class="main-container">
			<div class="main-wrap">
				<div class="main-header clearfix">
					<p class="string-logo">句读</p>
					<ul class="list-button clearfix">
						<li class="list-button-item"><a class="item-link item-comments" href="#"></a><span class="item-num">300</span></li>
						<li class="list-button-item"><a class="item-link item-like" href="#"></a><span class="item-num">3.9K</span></li>
						<li class="list-button-item"><a class="item-link item-share" href="#"></a></li>
					</ul>
				</div>
				<div class="main-banner">
					<div class="photo-txt">
						<p>庚子鼠年</p>
						<p>戊寅月戊寅日</p>
						<p>正月十二</p>
					</div>
					<div class="photo">
						<img src="https://img-blog.csdnimg.cn/20200207204809238.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTM4OTYzMw==,size_16,color_FFFFFF,t_70" />
					</div>
					<div class="date color-white">05</div>
				</div>
				<div class="main-content clearfix">
					<div class="date color-black">05</div>
					<div class="time">2020.02 星期三</div>
					<p class="string-sentence">人间的事,只要生机不灭,即使遭到天灾人祸,暂被阻抑,终有抬头的日子。</p>
					<p class="string-name">丰子恺</p>
				</div>
			</div>
		</div>
	</div>
</body>
</html>

第二次改进:(下划线用border,效果好多了)

在这里插入图片描述
源代码复制粘贴即可查看效果:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!--
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/index.css" />
-->
<style type="text/css">
/*reset.css*/
body, h1, h2, h3, h4, h5, h6, dl, dt, dd, ol, ul, li, p, a, input, form, textarea {
	padding: 0;
	margin: 0;
	font-family: "Microsoft YaHei";
}

ol, ul, li {
	list-style: none;
}

img {
	display: block;
	border: none;
}

a {
	display: block;
	text-decoration: none;
}

.clearfix {
	zoom: 1;
}

.clearfix:after {
	display: block;
	content: '';
	clear: both;
	visibility: hidden;
	height: 0;
}

/*--------------------------------------------------------------------*/

/*index.css*/
.bg {
	min-width: 100%;
	min-height: 100%;
	position: absolute;
	overflow: hidden;
	background: pink;
}

.main-container {}

.main-wrap {
	width: 476px;
	height: 802px;
	background: #fff;
	margin: 100px auto 0;
}

.main-container .date {
	width: 120px;
	font-size: 120px;
	line-height: 120px;
}

/*header*/
.main-container .main-header {
	height: 62px;
}

.main-header .string-logo {
	display: inline-block;		/*block会占行,把它行内化或者行内块级化*/
	font-size: 34px;
	line-height: 34px;
	padding-top: 12px;
	padding-left: 32px;
}

.main-header .list-button {
	float: right;
}

.main-header .list-button .list-button-item {
	float: left;
	width: 73px;
}

.main-header .list-button-item .item-link {
	display: inline-block;
	width: 30px;
	height: 30px;
	float: left;
	margin-top: 18px;
}

.main-header .list-button-item .item-num {
	font-size: 16px;
	color: #747474;
	float: left;
	margin-top: 12px;
	margin-left: 5px;
}

.main-header .list-button-item .item-comments {
	background: url("https://img-blog.csdnimg.cn/20200207204823196.png") no-repeat;
	background-size: 100%;
}

.main-header .list-button-item .item-like {
	background: url("https://img-blog.csdnimg.cn/20200207204830919.png") no-repeat;
	background-size: 100%;
}

.main-header .list-button-item .item-share {
	background: url("https://img-blog.csdnimg.cn/20200207204840140.png") no-repeat;
	background-size: 90%;
	margin-left: 13px;
}

/*banner*/
.main-container .main-banner {
	width: 476px;
	height: 318px;
	overflow: hidden;
	position: relative;
}

.main-banner .photo-txt {
	font-size: 16px;
	line-height: 16px;
	color: #fff;
	writing-mode: vertical-rl;	/*非IE浏览器可识别*/
	writing-mode: tb-rl;		/*IE浏览器只识别这条*/
	letter-spacing: 5px;
	position: absolute;
	top: 30px;
	right: 30px;	/*因为没有了行高line-height的影响,所以直接是30px*/
}

.main-banner .photo-txt .txt {
	display: inline-block;		/*行内块级化令它的padding填充有效,且边的长度跟随文字内容部分,没内容就没边*/
	border-left: 1px solid #fff;
	padding-left: 4px;
	margin-left: 10px;
}

.main-banner .photo img{
	width: 476px;
	height: 318px;
}

.main-banner .color-white {
	color: #fff;
	margin-top: -80px;
	margin-left: 46px;
}

/*content*/
.main-container .main-content {
	width: 476px; 
	height: 422px;
	position: relative;
	overflow: hidden;
}

.main-content .color-black {
	color: #333;
	margin-top: -80px;
	margin-left: 46px;
}

.main-content .time {
	font-size: 15px;
	line-height: 15px;
	color: #747474;
	position: absolute;
	top: 14px;
	right: 32px;
}

.main-content .string-sentence {
	font-size: 24px;
	line-height: 38px;
	padding: 30px;
	margin-top: 146px;
}

.main-content .string-name {
	font-size: 24px;
	float: right;
	margin-right: 30px;
}
</style>
</head>

<body>
	<div class="bg">
		<div class="main-container">
			<div class="main-wrap">
				<div class="main-header clearfix">
					<p class="string-logo">句读</p>
					<ul class="list-button clearfix">
						<li class="list-button-item"><a class="item-link item-comments" href="#"></a><span class="item-num">300</span></li>
						<li class="list-button-item"><a class="item-link item-like" href="#"></a><span class="item-num">3.9K</span></li>
						<li class="list-button-item"><a class="item-link item-share" href="#"></a></li>
					</ul>
				</div>
				<div class="main-banner">
					<div class="photo-txt">
						<span class="txt">庚子鼠年</span>
						<br />
						<span class="txt">戊寅月戊寅日</span>
						<br />
						<span class="txt">正月十二</span>
					</div>
					<div class="photo">
						<img src="https://img-blog.csdnimg.cn/20200207204809238.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTM4OTYzMw==,size_16,color_FFFFFF,t_70" />
					</div>
					<div class="date color-white">05</div>
				</div>
				<div class="main-content clearfix">
					<div class="date color-black">05</div>
					<div class="time">2020.02 星期三</div>
					<p class="string-sentence">人间的事,只要生机不灭,即使遭到天灾人祸,暂被阻抑,终有抬头的日子。</p>
					<p class="string-name">丰子恺</p>
				</div>
			</div>
		</div>
	</div>
</body>
</html>

【素材图片我放上来吧】
在这里插入图片描述https://img-blog.csdnimg.cn/20200207204809238.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTM4OTYzMw==,size_16,color_FFFFFF,t_70
在这里插入图片描述https://img-blog.csdnimg.cn/20200207204823196.png
在这里插入图片描述https://img-blog.csdnimg.cn/20200207204830919.png
在这里插入图片描述https://img-blog.csdnimg.cn/20200207204840140.png

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值