不规则div之<div两垂直边向里凹陷>

       今天公司网站上要做一个页面,设计图就有设计到垂直边向里凹陷的很多块,而且这些块高度是不一致,如果直接切图来做背景很不现实,还是得用样式来实现这个效果。下面就来记录一下实现的方法,以便自己以后重用。如果你看到了这篇文章,也希望你能给我一些好的建议。(对于IE只适用于IE9)

源代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<title>div两垂直边向里凹陷</title>

<style type="text/css">
	.testdiv,.testdiv2{
		position: relative;
		margin: 0 auto;
		margin-top: 30px;
		width: 200px;
		height: 200px;
		background: yellow;
		text-align: center;
	}
	.content{
		margin: 0 auto;
		background: yellow;
		width: 150px;
		height: 190px;
		padding: 5px;
	}
	.testdiv::before,.testdiv2::before{
		position: absolute;
		left: -15px;
		top: 0;
		display: block;
		content: '我是前面';
		background: #fff; 
		width: 30px;
		height: 100%;  /*高度为100%,与父元素相等*/
		border-radius: 50%;
	}
	.testdiv::after,.testdiv2::after{
		position: absolute;
		left: 185px;
		top: 0;
		display: block;
		content: '我是后面';
		background: #fff; 
		width: 30px;
		height: 100%;  <span style="font-family: Arial, Helvetica, sans-serif;">/*高度为100%,与父元素相等*/</span>
		border-radius: 50%;
	}
	.testdiv2::before,.testdiv2::after{
		background-color: red;
	}
</style>
</head>

<body>

<div class="testdiv">
	<div class="content">
		我是实验框实验框<br />
		我是实验框实验框<br />
		我是实验框实验框<br />
		我是实验框实验框<br />
		我是实验框实验框<br />
	</div>
</div>
<div class="testdiv2">我是实验框</div>

</body>
</html>

效果:

效果图

实现原理:

  • css3中的 ::before 和 ::after 选择器在被选元素的内容前面插入内容。

::before 选择器在被选元素的内容前面插入内容

::after 选择器在被选元素的内容后面插入内容
.testdiv::before,.testdiv2::before{
		position: absolute;
		left: -15px;
		top: 0;
		display: block;
		content: '我是前面';
		background: #fff; 
		width: 30px;
		height: 100%;
		border-radius: 50%;
	}
利用这两个选择器分别在被选中元素的前后插入一个块,利用css调整这两个块的样式,将被选中元素的垂直边遮挡,就形成了如效果图所示的“垂直边向里凹陷的效果”。

注意:
1、被选中的元素的position设置为relative相对定位;
2、插入的元素的position设置为absolute绝对定位。并将其垂直边利用border-radius设置为50%,形成弧形。再利用left调整其位置对被选中元素形成遮挡;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是美化这些`<div>`标签的样式: ```css /* 父容器样式 */ .popup-content { display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100%; padding: 10px; } /* 子容器样式 */ .popup-row { display: flex; justify-content: center; align-items: center; margin-bottom: 5px; } /* 子元素样式 */ .popup-item { display: flex; justify-content: center; align-items: center; width: 60px; height: 60px; border-radius: 50%; font-size: 20px; color: #333; background-color: #fff; /* 背景色 */ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* 阴影 */ transition: all 0.3s ease; /* 过渡效果 */ } /* 鼠标悬停样式 */ .popup-item:hover { transform: scale(1.1); /* 放大动画 */ box-shadow: 0 0 20px rgba(0, 0, 0, 0.2); /* 放大后的阴影 */ cursor: pointer; } /* 选中样式 */ .popup-item.active { color: #fff; background-color: #f00; } ``` 您可以将这些样式添加到您的样式表中,或将其添加到`<style>`标签中。然后,您需要在弹窗中添加以下代码: ```html <van-popup v-model="show" position="bottom" :style="{ height: '50%' }"> <div class="popup-content"> <div class="popup-row"> <div class="popup-item">5</div> <div class="popup-item">10</div> <div class="popup-item">20</div> <div class="popup-item">30</div> </div> <div class="popup-row"> <div class="popup-item">50</div> <div class="popup-item">100</div> <div class="popup-item">200</div> <div class="popup-item">500</div> </div> </div> </van-popup> ``` 这样就可以应用这些样式,并且实现了鼠标悬停和选中效果。如果您需要根据具体需求进行修改,可以对样式进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值