【问题标题】:How to add box shadow around the below shape [duplicate]如何在以下形状周围添加框阴影[重复]
【发布时间】:2023-03-14 09:04:01
【问题描述】:

大家好,我正在尝试在使用 css 创建的自定义形状周围添加一个盒子阴影 如下图所示

body{
   padding:50px
}
div{
    height: 45px;
    width: 209px;
    float: left;
    color: #fff;
    line-height: 45px;
    text-align: center;
    position: relative;
    font-family: Arial;
    font-weight: bold;
    font-size: 16px;
    background-color: #50b3cf;
}
div::after{
    position: absolute;
    z-index: 2;
    content: "";
    width: 0;
    height: 0;
    border-top: 22.5px solid transparent;
    border-bottom: 22.5px solid transparent;
    right: 1px;
    transform: translateX(100%);
    border-left: 22.5px solid #50b3cf;
}
<div></div>

【问题讨论】:

    标签: html css


    【解决方案1】:
    1. 声明伪元素的值 width & height 属性;
    2. 清除边界;
    3. 然后旋转有问题的伪元素
    4. 现在根据需要申请box-shadow
    5. 拿一个冷的;

    body{
       padding:50px
    }
    div{
        height: 45px;
        width: 209px;
        float: left;
        color: #fff;
        line-height: 45px;
        text-align: center;
        position: relative;
        font-family: Arial;
        font-weight: bold;
        font-size: 16px;
        background-color: #50b3cf;
        box-shadow: 0px 0px 5px 3px #000000;
    }
    div::after {
        position: absolute;
        z-index: 2;
        content: "";
        left: 100%;
        width: 32px;
        height: 32px;
        background: #50b3cf;
        transform: rotate(46deg);
        transform-origin: 0 0;
        box-shadow: 3px -3px 5px 0px #000000;
    }
    div::before { /* ver 2.0 Patch */
        content: "";
        position: absolute;
        background: #50b3cf;
        top: 0;
        bottom: 0;
        width: 25px;
        right: 0;
        z-index: 9;
    }
    <div></div>

    【讨论】:

    • 这是个好技巧 :)
    • @sjahan 一个和其他人一起放进包里
    • 嗨@UncaughtTypeError 当我运行上面的代码时,我看不到盒子阴影
    • @codegeek 您是说您在我的答案中嵌入的代码 sn-p 中看不到它,或者当您尝试将我的答案中的样式应用于您的生产代码时?仔细查看,比较版本,并确保您没有遗漏任何内容。 pseudo-element 应用了重大更改,div 选择器的唯一添加是为“完整效果”添加的box-shadow 规则。除此之外,我不能说 - 我只能解释我得到的工作。
    • 嗨 @UncaughtTypeError 我在代码 sn-p 中看不到它
    【解决方案2】:

    .box{
    	border:1px solid white;
    	width:400px;
    	height:150px;	
    	margin-left:40px;
    	box-shadow: 0 0 9px 3px rgba(0,0,0,0.5);
    }
    .arrow {
       width: 100px;
       height: 100px;
       position: relative;
       top:20px;
       left:-100px;
       overflow: hidden;
       box-shadow: 0 10px 10px -17px rgba(0,0,0,0.5);
         transform: rotate(270deg);  
    }
    .arrow:after {
       content: "";
       position: absolute;
       width: 50px;
       height: 50px;
       background: white;
       transform: rotate(45deg);
       top: 76px;
       left: 25px;
       box-shadow: -2px -2px 9px 0px rgba(0,0,0,0.5);
    }
    <div class="box">
    <div class="arrow"></div>
     </div>

    试试这个代码

    【讨论】:

      【解决方案3】:

      也许这就是你想要做的。
      第一个有盒子阴影,第二个没有盒子阴影,但您可以使用此代码添加它box-shadow: 0px 0px 6px 0px #000;in class "arrow-r"

      <style type="text/css">
          .main-box{
            position: relative;
            padding: 0 35px 90px;
      }
      .box{
        font-size: 20px;
          position: relative;
          display: inline-block;
          clear: both;
          margin-bottom: 8px;
          padding: 13px 14px;
          vertical-align: top;
          border-radius: 5px;
      }
      .arrow-l {
      
        float: left;
        color: #fff;
        background-color: #08abf4;    box-shadow: 0px 0px 6px 0px #000;
      
      }
      .arrow-r {
      
        float: right;
        color: #1a1a1a;
        background-color: #e2e2e2;
      
      }
      .box:before{
            position: absolute;
          top: 24px;
          width: 8px;
          height: 6px;
          content: '\00a0';
          -webkit-transform: rotate(30deg) skew(-36deg);
          transform: rotate(30deg) skew(-36deg);
      }
      .box.left:before {
        left: -4px;
        background-color: #08abf4;
      
      }
      
      .box:before{
            position: absolute;
          top: 21px;
          width: 8px;
          height: 6px;
          content: '\00a0';
          -webkit-transform: rotate(30deg) skew(-36deg);
          transform: rotate(30deg) skew(-36deg);
      }
      .box.right:before {
        right: -4px;
        background-color: #e2e2e2;
      }
      
      </style>
      
      <div class="main-box">
        <div class="box arrow-l left">
                          I'm Liam Lababidi
                      </div>
          <div class="box arrow-r right">
                          What about u?
                      </div>
      </div>
      

      【讨论】:

        【解决方案4】:

        您想为此使用box-shadow: 10px 10px 5px #888888;。每个px表示哪边,#表示颜色。

        body{
           padding:50px
        }
        div{
            height: 45px;
            width: 209px;
            float: left;
            color: #fff;
            line-height: 45px;
            text-align: center;
            position: relative;
            font-family: Arial;
            font-weight: bold;
            font-size: 16px;
            background-color: #50b3cf;
            box-shadow: 0px 10px 5px #888888;
        }
        div::after{
            position: absolute;
            z-index: 2;
            content: "";
            width: 0;
            height: 0;
            border-top: 22.5px solid transparent;
            border-bottom: 22.5px solid transparent;
            right: 1px;
            transform: translateX(100%);
            border-left: 22.5px solid #50b3cf;
        }
        &lt;div&gt;&lt;/div&gt;

        【讨论】:

        • 可以看到这里的阴影没有按照自定义的形状。我猜 OP 已经知道box-shadow
        猜你喜欢
        • 2012-10-03
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-16
        相关资源
        最近更新 更多