起因:经常在一些APP、网站、公众号、海报等等途径看到两色组成(左右或上下)的一个字,希望自己去通过所学知识用CSS实现
(当然,一些CSS3特效网一堆源码,但是,为何不自己亲手尝试呢?这样不更容易加深对知识点的理解?是否和专业源码一样先不考虑,只要视觉上达到了,就成功了一步。)例:句读app(还原仿写放在最下面,含源码)
一、刚开始我能想到的方法思路是: 分别做两个图层(两个同级元素),通过overflow(溢出隐藏)
- html部分,创建两个同级元素,内容要一样,不然怎么重叠,颜色不同就行了:
- 虽然定义 line-height 等于元素高来垂直居中,但是因为“字”有点长,下面一点就超出了元素底边,后面需要调整 line-height :
- 设置文字颜色:
- 移位重叠(这里我用 margin-top,当然,absolute+top也是可以的,第二种方法我才用absolute):
(额之前加边是为了说明“字”即使设置了行高,也会超出底边,忘了去掉)
- 给处于最上层的元素 overflow,配合 width 来隐藏掉一部分:
- 由于文字小部分超出了元素的高(底边),所以line-height做了微调来移动文字在元素盒子内的垂直位置:
- 移动整个部分到粉色盒子(box)的中间(把做好的一字两色用wrap包裹成一个整体,只需移动wrap):
- 父元素忘了给 overflow: hidden; 导致margin-top/bottom无效(带着整个父元素一起跑了):
- 最后去背景色完成(当然,我举例步骤说明才加的背景,实际不需要的):
<!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
发现,可行且更灵活,你可以任意裁剪显示想要的部分
- 前面的步骤与上面是一样的:
- 将下方元素移动到上方,我这用
position: absolute;
,如果跟上面一样用margin-top
也可以,看你喜欢: - 然后给最上层的元素,设置
clip
:(关于clip的用法,我会另外写一篇博客详细介绍) - 灵活的
clip
裁剪属性:
- 最后让该部分居中于粉色盒子(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