How can I add a box-shadow on one side of an element?

后端 未结 13 1115
萌比男神i
萌比男神i 2020-11-28 17:34

I need to create a box-shadow on some block element, but only (for example) on its right side. The way I do it is to wrap the inner element with box-shado

相关标签:
13条回答
  • 2020-11-28 18:03

    This could be a simple way

    border-right : 1px solid #ffffd;
    height:85px;    
    box-shadow : 10px 0px 5px 1px #eaeaea;
    

    Assign this to any div

    0 讨论(0)
  • 2020-11-28 18:06

    To get the clipped effect on up to two sides you can use pseudo elements with background gradients.

    header::before, main::before, footer::before, header::after, main::after, footer::after {
        display:    block;
        content:    '';
        position:   absolute;
        width:      8px;
        height:     100%;
        top:        0px;
    }
    
    header::before, main::before, footer::before {
        left:       -8px;
        background: linear-gradient(to left, rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0));
    }
    
    header::after, main::after, footer::after {
        right:      -8px;
        background: linear-gradient(to right, rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0));
    }
    

    will add a nice shadow-like effect to the left and right of the elements that normally make up a document.

    0 讨论(0)
  • 2020-11-28 18:07

    Just use ::after or ::before pseudo element to add the shadow. Make it 1px and position it on whatever side you want. Below is example of top.

    footer {
       margin-top: 50px;
       color: #fff;
       background-color: #009eff;
       text-align: center;
       line-height: 90px;
       position: relative;
    }
    
    footer::after {
        content: '';
        position: absolute;
        width: 100%;
        height: 1px;
        top: 0;
        left: 0;
        z-index: -1;
        box-shadow: 0px 0px 10px 1px rgba(0, 0, 0, 0.75);
    }
    <footer>top only box shadow</footer>

    0 讨论(0)
  • 2020-11-28 18:07

    Here's a little hack that I did.

    <div id="element"><!--element that I want an one-sided inset shadow from the bottom--></div> 
    <div class="one_side_shadow"></div>
    

    1. Create a <div class="one_side_shadow"></div> right below the element that I want to create the one-side box shadow (in this case I want a one-sided inset shadow for id="element" coming from the bottom)

    2. Then I created a regular box-shadow using a negative vertical offset to push the shadow upwards to one-side.

    `box-shadow: 0 -8px 20px 2px #DEDEE3;`
    
    0 讨论(0)
  • 2020-11-28 18:08

    clip-path is now (2020) one of simplest ways to achieve box-shadows on specific sides of elements, especially when the required effect is a "clean cut" shadow at particular edges (which I believe was what the OP was originally looking for) , like this:

    .shadow-element {
        border: 1px solid #333;
        width: 100px;
        height: 100px;
        box-shadow: 0 0 15px rgba(0,0,0,0.75);
        clip-path: inset(0px -15px 0px 0px);
    }
    <div class="shadow-element"></div>

    ...as opposed to an attenuated/reduced/thinning shadow like this:

    .shadow-element {
        border: 1px solid #333;
        width: 100px;
        height: 100px;
        box-shadow: 15px 0px 15px -10px rgba(0,0,0,0.75);
    }
    <div class="shadow-element"></div>

    Simply apply the following CSS to the element in question:

    box-shadow: 0 0 Xpx [hex/rgba]; /* note 0 offset values */
    clip-path: inset(Apx Bpx Cpx Dpx);
    

    Where:

    • Apx sets the shadow visibility for the top edge
    • Bpx right
    • Cpx bottom
    • Dpx left

    Enter a value of 0 for any edges where the shadow should be hidden and a negative value (the same as the box-shadow blur radius - Xpx) to any edges where the shadow should be displayed.

    0 讨论(0)
  • 2020-11-28 18:09

    Here is a codepen to demonstrate for each side, or a working snippet:

    .boxes {
      display: flex;
      flex-wrap: wrap;
    }
    
    .box {
      margin: 20px;
      border: 1px solid #ccc;
      font-family: Helvetica Neue, Arial, sans-serif;
      font-weight: 100;
      letter-spacing: 2px;
      color: #999;
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center;
      flex: 1;
      padding: 40px;
      line-height: 1.4em;
    }
    
    .top {
      box-shadow: 0 -5px 5px -5px #333;
    }
    
    .right {
      box-shadow: 5px 0 5px -5px #333;
    }
    
    .bottom {
      box-shadow: 0 5px 5px -5px #333;
    }
    
    .left {
      box-shadow: -5px 0 5px -5px #333;
    }
    <div class="boxes">
      <div class="box top">Top Only</div>
      <div class="box right">Right Only</div>
      <div class="box bottom">Bottom Only</div>
      <div class="box left">Left Only</div>
    </div>

    0 讨论(0)
提交回复
热议问题