I need to draw the following pattern with CSS as a separator between sections of my page:
Using the skewX()
technique from this answer, I was able to mimic the triangular cutout accurately (two pseudo-elements are appended to the top of the lower section, one skewed left and one skewed right, so that the background of the upper section shows through):
But I can't figure out how to then add the border, as shown in the first image.
The problem is that the gap between the border and the lower section has to be transparent, because the background of the upper section can be a gradient, an image, etc. Therefore, I can't simply use an image for the triangular cutout, because I can't know what content will be behind it.
Is there any possible way to do this with CSS?
Couldn't resist to post another version ...
Too many hand calculated values to make it a perfect solution, but can suit you if the dimension are fixed.
here anoter approach with gradient and (@Harry )borders & shadow : A known width helps much, else, you need to adjust some shadows offset values with calc().
pen to play with
It is possible to do this with CSS but the only approach that I could find is a very complex one using a fair amount of gradients to mimic the borders. This couldn't be done with normal borders because that resulted in either the borders extending beyond their meeting point or the inner area never meeting. It couldn't be done with box shadow's also because the gap between the border and the filled area need to be transparent. (Not saying it cannot be done with those but just that I couldn't get it to work, maybe there is a way with those too).
The below is how I've managed to achieve the border:
The output - as you can see by hovering the element - is responsive too.
The following is how the background properties need to be set based on the required border color and width (all calculations for 45 degree skew angle, different angle will need different calculations):
#DDD
then all color values should be changed to red.The thickness of the border will be determined using
background-size
for the first two gradients and using the gradient's color-stop points for the next two gradients.background-size
in Y-axis is nothing but the border thickness. Change it to suit the needed thickness. Thebackground-size
in X-axis is nothing but 50% minus the height of the pseudo minus half of border thicknessbackground-position
should be set such that the required gap is there between the colored area and the border.background-position
in X-axis should be left edge (0%) and right edge (100%) respectively. Their position in Y-axis should be above the pseudo-element and so it should be 100% minus pseudo-element's height minus the spacing.background-position
involves more complex calculation involving the border spacing, thickness and pseudo-element's height. The logic is present below.If you need the transparent cut with border on the top part of the lower
div
then you could achieve it like in the below snippet.