居中且长度为50%的div底部边框实现方法
你希望实现的效果是:在html中指定div的id为“d1”,其下方的线条占据50%的宽度,并水平居中。为此,可以使用css代码来实现。
修改后的css代码为:
h1{
background-repeat: no-repeat;
background-image: linear-gradient(90deg, #000 0%, #f00 100%);
background-size: 50% 3px;
background-position: 50% 100%;
display: inline-block;
}具体含义如下:
- background-repeat: no-repeat;:禁止背景图像重复。
- background-image: linear-gradient(90deg, #000 0%, #f00 100%);:设置背景图像为水平线性渐变,从黑色到红色的过渡。
- background-size: 50% 3px;:设置背景图像的大小为50%(宽度)和3px(高度)。
- background-position: 50% 100%;:将背景图像定位在水平居中且距离底部100%(即div底部)的位置。
- display: inline-block;:将“h1”元素设置为内联块元素,使其在保持块状元素特性的同时,也可以像内联元素一样水平排列。










