Css clip-path with no gap in between - css

I am trying to apply clip-path on three items which are aligned next to each other. Since paths are calculating the space between each other based on the non-clipped div shape, there is an unwanted gap. In the code below when I apply the commented styles in .class2 I get a close result of what I want, but then it's no longer responsive. Any other way to get the similar result with a more suitable approach ?
https://codepen.io/SpoyrazY/pen/erbKXx
HTML
<div class="class1">
<h1>1</h1>
</div>
<div class="class2">
<h1>2</h1>
</div>
<div class="class3">
<h1>3</h1>
</div>
CSS
.class1{
background-image:url(https://dummyimage.com/600x400/66ccff/fff&text=+);
width: 33.33333333%;
height: 400px;
float: left;
-webkit-clip-path: polygon(0 0, 100% 0, 85% 100%, 0 100%);
clip-path: polygon(0 0, 100% 0, 85% 100%, 0 100%);
text-align: center;
color: white;
}
.class2{
background-image:url(https://dummyimage.com/600x400/66ff66/fff&text=+);
width: 33.33333333%;
height: 400px;
float: left;
-webkit-clip-path: polygon(15% 0, 100% 0%, 85% 100%, 0%, 100%);
clip-path: polygon(15% 0, 100% 0%, 85% 100%, 0% 100%);
/*
margin-left: -90px;
margin-right: -90px;
width: 42.7%;
-webkit-clip-path: polygon(12% 0, 100% 0%, 85% 100%, 0%, 100%);
clip-path: polygon(12% 0, 100% 0%, 88% 100%, 0% 100%);
*/
text-align: center;
color: white;
}
.class3{
background-image:url(https://dummyimage.com/600x400/ff99ff/fff&text=+);
width: 33.33333333%;
height: 400px;
float: left;
-webkit-clip-path: polygon(15% 0, 100% 0, 100% 100%, 0 100%);
clip-path: polygon(15% 0, 100% 0, 100% 100%, 0 100%);
text-align: center;
color: white;
}

This happens due to clip-path. you can achieve your result by setting scale in a custom class. so your html will be like this
<div class="class1 block">
<h1>1</h1>
</div>
<div class="class2 block">
<h1>2</h1>
</div>
<div class="class3 block">
<h1>3</h1>
</div>
Add some lines of css
.block {
transform: scale(1.35);
transform-origin: top;
}
working fiddle here

Related

CSS Responsive Hexagon Image Frame [duplicate]

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 6 months ago.
I'm trying to make a responsive hexagon image frame, but I'm stuck on aligning the inner hexagon. Does anyone know how to center the inner hexagon? Here's my code:
.inner {
background-color: red;
width: 95%;
height: 0%;
padding-bottom: 98%;
margin-top: auto;
margin-bottom: auto;
}
.outer {
background-color: mediumspringgreen;
width: 12%;
height: 0%;
padding-bottom: 13%;
}
.hexagon {
display: flex;
justify-content: center;
align-items: center;
position: relative;
-webkit-clip-path: polygon( 50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}
<div class="hexagon outer">
<div class="hexagon inner">
<img src="https://placekitten.com/400/400" class="absolute inset-0 h-full w-full" alt="" />
</div>
</div>
My result:
Expected result:
It seems as you use the padding only for your inner div, the outer div only gets that height. I would have a container that you put the width on, then you can add padding to your outer hexagon and absolutely position your inner hexagon:
.container {
width: 12%;
}
.inner {
background-color: red;
width: calc(100% - 10px);
/* can change this to be a percentage if you want a variable width border. with calc, the border will always be half of what you subtract */
height: calc(100% - 10px);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.outer {
background-color: blue;
width: 100%;
padding-top: 100%;
position: relative;
}
.hexagon {
clip-path: polygon(0% 25%, 0% 75%, 50% 100%, 100% 75%, 100% 25%, 50% 0%);
}
.img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="container">
<div class="hexagon outer">
<div class="hexagon inner">
<img src="https://placekitten.com/400/400" class="img">
</div>
</div>
</div>
From comments solution using aspect ratio instead of padding and inset instead of calc on the width of inner:
.container {
width: 12%;
}
.inner {
background-color: red;
/* insets the element 5px from the edges of
the element against which its positioned: */
inset: 5px;
position: absolute;
}
.outer {
/* allows one dimension to be set (here 'inline-size'/'width'),
and the other dimension ('block-size'/'height') will be
an equal width: */
aspect-ratio: 1;
background-color: blue;
/* logical property, equivalent to 'width' in the English -
left-to-right - language: */
inline-size: 100%;
position: relative;
}
.hexagon {
clip-path: polygon(0% 25%, 0% 75%, 50% 100%, 100% 75%, 100% 25%, 50% 0%);
}
.img {
width: 100%;
object-fit: cover;
}
<div class="container">
<div class="hexagon outer">
<div class="hexagon inner">
<img src="https://placekitten.com/400/400" class="img">
</div>
</div>
</div>
Try it:
.inner {
background-color: red;
width: 95%;
height: 0%;
padding-bottom: 98%;
margin-top: auto;
margin-bottom: auto;
}
.outer {
background-color: mediumspringgreen;
width: 12%;
height: 0%;
padding-bottom: 13%;
}
.hexagon {
display: flex;
justify-content: center;
align-items: center;
-webkit-clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
position: absolute;
top: 50%;
left: 50%;
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
<div class="hexagon outer">
<div class="hexagon inner">
<img src="https://placekitten.com/400/400" class="absolute inset-0 h-full w-full" alt="" />
</div>
</div>

How to remove whitespace from clipped elements?

How can I remove extra whitespace from the clipped divs? The whole page should look like the first 3 divs.
overflow: hidden doesn't work, transform: translateY didn't cure the problem as well.
Or maybe there's a different way how to code this style?
View it on codepen
div {
width: 100%;
height: 80vh;
}
div:nth-child(even) {
background-color: rgb(182, 128, 128);
clip-path: polygon(0% 15%, 100% 0%, 100% 100%, 0% 85%);
transform: translateY(-15%);
}
div:nth-child(odd) {
background-color: rgb(109, 127, 177);
clip-path: polygon(0% 0%, 100% 15%, 100% 85%, 0% 100%);
}
/* selects all odd divs except the first one */
div:not(:first-child):nth-child(odd) {
transform: translateY(-30%);
}
div:first-child {
clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 0% 100%);
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
So I've added margin-top -15vh accordingly so that it applies evenly to all divs and remove spacing.
div {
width: 100%;
height: 80vh;
margin-top: -15vh;
}
div:nth-child(even) {
background-color: rgb(182, 128, 128);
clip-path: polygon(0% 15%, 100% 0%, 100% 100%, 0% 85%);
/* transform: translateY(-15%); */
}
div:nth-child(odd) {
background-color: rgb(109, 127, 177);
clip-path: polygon(0% 0%, 100% 15%, 100% 85%, 0% 100%);
}
/* selects all odd divs except the first one */
div:not(:first-child):nth-child(odd) {
/* transform: translateY(-30%); */
}
div:first-child {
clip-path: polygon(0% 0%, 100% 0%, 100% 85%, 0% 100%);
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

flipping an image horizontally with CSS

I'm trying to build a frame. I did the top edge properly, but when doing the left edge it doesn't output properly. The left edge is almost correct, I just need to flip it horizontally from its current position.
Could you flip it for me and provide me a working example on your answer (snippet preview) or JSFiddle?
.trapezoid-top {
width: 400px;
height: 100px;
background-image: url('https://image.ibb.co/e5Kaw7/image.png');
background-size: contain;
clip-path: polygon(0 0, 100% 0, calc(100% - 100px) 100%, 100px 100%);
transform-origin: top left;
transform: rotate(0deg);
}
.trapezoid-left {
width: 600px;
height: 100px;
margin-top: -100px;
background-image: url('https://image.ibb.co/e5Kaw7/image.png');
background-size: contain;
clip-path: polygon(0 0, 100% 0, calc(100% - 100px) 100%, 100px 100%);
transform-origin: top left;
transform: rotate(90deg);
}
<div style="margin:0 100px;">
<div class="trapezoid-top"></div>
<div class="trapezoid-left"></div>
</div>
Thanks!
Try this:
CSS:
.container {
position: relative;
margin: 0 100px;
}
.trapezoid-top {
width: 400px;
height: 100px;
background-image: url('https://image.ibb.co/e5Kaw7/image.png');
background-size: contain;
clip-path: polygon(0 0, 100% 0, calc(100% - 100px) 100%, 100px 100%);
transform-origin: top left;
transform: rotate(0deg);
}
.trapezoid-left {
width: 600px;
height: 100px;
margin-top: -100px;
background-image: url(https://image.ibb.co/e5Kaw7/image.png);
background-size: contain;
clip-path: polygon(0 0, 100% 0, calc(100% - 100px) 100%, 100px 100%);
transform-origin: top right;
transform: rotate(-90deg);
position: absolute;
right: calc(100% - 16px);
}
HTML:
<div class="container">
<div class="trapezoid-top"></div>
<div class="trapezoid-left"></div>
</div>
Change transform-origin to top right and position the element to place it on left.
Demo: http://jsfiddle.net/GCu2D/3568/

Is it possible to fill in the background color so there's no white? I am using the clip-path css property in this screenshot

https://imgur.com/a/ZTZp4
Codepen: https://codepen.io/yongelee/pen/eVobRd
I want to make the background have no white, so basically have that white space as the color of the next block's background. But unfortunately when I clip-path, the div doesn't change from a rectangle to a trapezoid!
My code (JSX):
<Wrapper>
<HeroBlock>
<h1>hi</h1>
<h4>hihi</h4>
</HeroBlock>
<IntroBlock>
<h1>heyyy</h1>
<h4>YO??</h4>
</IntroBlock>
<SkillsBlock>
<h1>Heyy</h1>
<h4>okkk</h4>
</SkillsBlock>
</Wrapper>
const Wrapper = styled.div`
display: grid;
grid-template-columns: 1fr;
`
const HeroBlock = styled.div`
background: skyblue;
height: 50vh;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
`
const IntroBlock = styled.div`
background: orange;
padding-bottom: 100px;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 90%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 90%);
`
const SkillsBlock = styled.div`
background: red;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
`
Using styled-components for this.
Simply consider some negative margin like this:
.container {
display: grid;
grid-template-columns: 1fr;
}
.hero {
background: skyblue;
height: 50vh;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
}
.intro {
margin-top: -40px;
background: orange;
padding-bottom: 100px;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 90%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 90%);
}
.skills {
margin-top: -40px;
background: red;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
}
<div class="container">
<div class="hero">
<h1>Hey</h1>
<h4>heyy</h4>
</div>
<div class="intro">
<h1>Hii</h1>
<h4>Yoo</h4>
</div>
<div class="skills">
<h1>One</h1>
<h4>Noooo</h4>
</div>
</div>
Ok Here is what I came up with:
h1 {
margin-top:0;
}
.container {
display: grid;
grid-template-columns: 1fr;
}
.hero_cont {
background-color:orange;
}
.hero {
padding-top:21px;
background: skyblue;
height: 50vh;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
}
.intro_cont {
background-color:red;
}
.intro {
padding-top:21px;
background: orange;
padding-bottom: 100px;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 90%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0% 90%);
}
.skills_cont {
background-color:transparent;
}
.skills {
padding-top:21px;
background: red;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
clip-path: polygon(0 0, 100% 0, 100% 90%, 0% 100%);
}
<div class="container">
<div class='hero_cont'>
<div class="hero">
<h1>Hey</h1>
<h4>heyy</h4>
</div>
</div>
<div class='intro_cont'>
<div class="intro">
<h1>Hii</h1>
<h4>Yoo</h4>
</div>
</div>
<div class='skills_cont'>
<div class="skills">
<h1>One</h1>
<h4>Noooo</h4>
</div>
</div>
</div>
i'd like to add clip path into div not stricte to section. I can clip div, and i can make linear-gradient on section, so section up to clip-path is for example red, so top of my section where is clip is also red, and bottom blue, bc next section gonna be blue. It seperate nice content :D
.section-stats {
background: linear-gradient(to bottom, rgb(247, 247, 247), rgba(0, 0, 0, 0));
position: relative;
width: 100%;
padding: 5rem 0;
// this is element which we want to clip
&--clip {
height: 100%;
width: 100%;
padding: 10rem 0;
background-image: linear-gradient(
to right bottom,
rgba(0, 0, 0, 0.6),
rgba(0, 0, 0, 0.6)
),
url("../../img/renovation-tools.jpeg");
background-size: cover;
background-position: top;
background-attachment: fixed;
-webkit-clip-path: polygon(100% 0, 100% 90%, 50% 100%, 0 90%, 0 0, 50% 10%);
clip-path: polygon(100% 0, 100% 80%, 50% 100%, 0 80%, 0 0, 50% 20%);
}
& .row {
margin: 20rem 0 10rem;
}
&--icon {
font-size: 10rem;
color: $color-secondary-light;
#include respond(tab-port) {
font-size: 6rem;
}
}
&--count {
display: block;
font-size: 7rem;
color: $color-secondary-light;
margin-top: -2rem;
#include respond(tab-port) {
font-size: 5rem;
}
}
&--sub {
display: inline-block;
transform: translateY(0.8rem);
}
}
<section class="section-stats ">
<div class="section-stats--clip u-flex">
<div class="row">
<div class="col-1-of-3 u-center-text">
<i class="lni lni-apartment section-stats--icon"></i>
<span class="section-stats--count"
>2<span class="section-stats--sub">3</span>8</span
>
<h3 class="heading-tertiary heading-white">COMPLETED PROJECTS</h3>
</div>
<div class="col-1-of-3 u-center-text">
<i class="lni lni-apartment section-stats--icon"></i>
<span class="section-stats--count"
>7<span class="section-stats--sub">5</span>1</span
>
<h3 class="heading-tertiary heading-white">COMPLETED PROJECTS</h3>
</div>
<div class="col-1-of-3 u-center-text">
<i class="lni lni-apartment section-stats--icon"></i>
<span class="section-stats--count"
>7<span class="section-stats--sub">5</span>1</span
>
<h3 class="heading-tertiary heading-white">COMPLETED PROJECTS</h3>
</div>
</div>
</div>
</section>

CSS transition background-image jump hover in Safari

I have an issue with a transition effect in Safari and could use some help. My CSS hover swaps background images, as it should, but the hover image 'jumps' from its position, up and to the left (which I don't want to happen). Thanks.
Structure:
.sidebar .widget {
height: 276px;
width: 326px;
background:url('/wp-content/themes/a-theme/svg/polygon-image.svg') no-repeat center center;
background-size: 100%;
}
.sidebar .widget:hover {
background:url('/wp-content/themes/a-theme/svg/polygon-image-hover.svg') no-repeat center center;
background-size: 100%;
transition: 0.5s ease-in-out;
}
<div class="sidebar">
<aside id="black-studio-tinymce-18" class="widget widget_black_studio_tinymce">
<div class="textwidget">
<h3 style="text-align: center;">Sample Text<br></h3>
</div>
</aside>
</div>
avoid browsers bugs and clean:
.sidebar .widget {
height: 276px;
width: 326px;
background-repeat:no-repeat;
background-position: center center;
background-image:url(/wp-content/themes/a-theme/svg/polygon-image.svg);
background-size: 100%;
transition:background-image 0.5s ease-in-out;
}
.sidebar .widget:hover {
background-image:url(/wp-content/themes/a-theme/svg/polygon-image-hover.svg);
}
From your help, I modified my CSS to get exactly what I needed. I had a background image of a polygon shape and a graphic in the middle of it. I kept the graphic and used a CSS polygon for the shape, instead, and now my hover effect works in Firefox, Chrome, and Safari.
See my CSS below and thanks again for the replies.
sidebar .widget {
background: url('/wp-content/uploads/2017/08/image.png') no-repeat center center;
background-color: rgba(255, 165, 0, 0.85);
background-size: 70%;
-webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
height: 276px;
width: 326px;
display: flex;
justify-content: center;
align-items: center;
}
.sidebar .widget:hover {
background: url('/wp-content/uploads/2017/08/image.png') no-repeat center center;
background-color: rgba(108, 218, 244, 0.8);
background-size: 80%;
-webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
height: 276px;
width: 326px;
transition: 0.5s ease-in-out;
}

Resources