一个空的div标签如何实现一半白一半黑

新手上路,请多包涵

html+css如何实现一个空的div标签如何实现一半白一半黑

阅读 9.1k
5 个回答
background-image: linear-gradient(to right, #fff 50%, #000 0);
.test {
      width: 100px;
      height: 100px;
      border: 1px solid #000;
    }
    .test:after {
      content: "";
      display: inline-block;
      width: 50px;
      height: 100%;
      background-color: #000;
    }
<style>
    .box{
      height:0;
      width:0;
      border-width:50px;
      border-style:solid;
      border-color:#000 #000 #ccc #ccc;
    }  
</style>
<body>
    <div class="box"></div>
</body>

直接用了border,所以白色不明显,换了灰色。。。

两种方案:
一种是利用伪类::before,::after和绝对定位;
第二种是用背景渐变;

.box{
    width: 100px;
    height: 100px;
    border-left:50px solid #000;
    border-right:50px solid #fff;
    box-sizing: border-box;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题