Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box-shadow trimmed in CSS columns in Chrome

I want the block elements inside CSS columns to have box shadow. The following, simplified code renders as expected in IE10 and Firefox 21, but in current Chrome version (28.0.1500.72) shadows near the column sides are trimmed.

The images present results in IE/FF (on the left), and Chrome on the right:

Code rendered in IE/FirefoxCode rendered in Chrome

(there's also some vertical shift, but it's not an issue)

Here's the jsfiddle: http://jsfiddle.net/buli_pl/KxYRc/1/

div#column-container {    /* Set 2 columns*/    -moz-column-count: 2;    -webkit-column-count: 2;    column-count: 2;  }  div#column-container div {    background-color: yellow;    /* set shadow for yellow elements */    box-shadow: 0px 1px 3px #000;    -webkit-box-shadow: 0px 0px 15px #000;    -moz-box-shadow: 0px 0px 15px #000;    box-shadow: 0px 0px 15px #000;    /* Make sure that yellow div is not split between columns */    display: inline-block;    width: 100%;    /* the rest - just to better present the problem */    height: 70px;    margin-top: 0;    margin-bottom: 20px;  }
<div id="column-container">    <div>box 1</div>    <div>box 2</div>    <div>box 3</div>    <div>box 4</div>    <div>box 5</div>    <div>box 6</div>  </div>

Am I misusing some of those properties, or this is a Chrome issue? How can it be fixed at the moment?

like image 653
Paweł Bulwan Avatar asked Jul 26 '13 12:07

Paweł Bulwan


People also ask

How do you clip a box shadow in CSS?

Solution 1: clip-path (experimental) In your case you would use clip-path: inset(px px px px); where the pixel values are calculated from the edge in question (see below). This will clip the div in question at: 0 pixels from the top. 5 pixels outside of the right edge (to include the shadow)

How do you add a shadow to all sides of a box in CSS?

box-shadow: h-shadow v-shadow blur spread color inset; In your example you're offsetting shadow by 10px vertically and horizontally. Like in other comments set first two values to 0px in order to have even shadow on all sides.

Is Box shadow supported by CSS?

CSS box-shadow browser support It's important to note that the box-shadow is not fully supported on all browsers, especially earlier versions, so it's necessary to use the webkit extension when styling shadows. -webkit-box-shadow: 1px 1px 0px rgba(0,0,0,0.

How do I reduce the size of a shadow in CSS?

you can increase offset and reduce size of box shadow and draw 2 of them. This is a fourth value. Positive values will cause the shadow to expand and grow bigger, negative values will cause the shadow to shrink. If not specified, it will be 0 (the shadow will be the same size as the element).


2 Answers

Just happened upon a potentially more straightforward solution that seems to work. Applying transform: translateZ(0); to the elements with box-shadows seems to be resolving this issue. In the supplied code, you would add this to your div#column-container div rule.

.container{   break-inside: avoid;   column-count: 2;   column-gap: 2rem; } .box{   border-radius: 4px;   box-shadow: 2px 2px 20px rgba(0, 0, 0, 0.2);   margin-bottom: 1rem;   padding: 1rem;   break-inside: avoid;   transform: translateZ(0); } 

https://codepen.io/MarkitDigital/pen/RdLoRG

like image 145
Colin Stevenson Avatar answered Oct 13 '22 20:10

Colin Stevenson


You could use flexbox for this instead of css columns.

FIDDLE

NB: This currently doesn't work in Firefox because it still doesn't support the flex-wrap property, however according to caniuse - this will be supported in version 28

CSS

div#column-container {        height: 270px; /* NB: IE requires the height property. max-height won't work on IE)*/     display: flex;     flex-direction: column;     flex-wrap: wrap;     align-content: flex-start; } 

EDIT: (Updated FIDDLE which includes support for Firefox)

As per @buli's suggestion to temporarily use the -moz-colums-count for Firefox as long as flex-wrap is not supported:

Well, you could do this with the @supports which allows us to perform feature queries - sort of like Modernizr, but with CSS.

The good thing here, is that Firefox supports them.

So if I add the following code: (updated as per Pavlo's suggestion)

@supports (not (flex-wrap: wrap)) and (-moz-columns: 2) {     div#column-container {          -moz-column-count: 2;         column-count: 2;         display: block;         width: 50%;     } } 

Now, Firefox will use CSS columns, whereas other browsers will use flexbox.

like image 34
Danield Avatar answered Oct 13 '22 21:10

Danield