I created a diagonal line on a container div: when I zoom a whitespace (or maybe a margin) of 1px appears on the bottom of my div showing a piece of the background color of the parent div.
I have been facing this problem for days without a solution.
You can view it here: https://codepen.io/Sirvasile/pen/GyPapb
div {
position: relative;
width: 100%;
height: 212px;
background-color: orange;
}
div::after {
content: "";
position: absolute;
bottom: 0;
width: 0;
height: 0;
border-style: solid;
border-width: 0 0 40px 100vw;
border-color: transparent transparent white transparent;
}
<div></div>
This is a screenshot of the div without zoom.
This is a screenshot of the div when the web page is zoomed (> 100%) (experienced using Google Chrome and Safari):
P.S. I already tried using transform: translateY(1px) but I can't accept it as a solution because using it the div has no more a perfect diagonal line.
I've dealt with similar problems, though not this exact one, and I believe the cause, ultimately, is a rounding error that ends up leaving a spare pixel. It's a browser bug that you won't fix with CSS alone, and transform: translateY(1px) might be the only guaranteed fix to the above code.
But, we can probably avoid that bug just by creating this effect by different means.
div {
position: relative;
width: 100%;
height: 212px;
background-color: orange;
-webkit-clip-path: polygon(100% 0, 100% calc(100% - 40px), 0 100%, 0 100%, 0 0);
clip-path: polygon(100% 0, 100% calc(100% - 40px), 0 100%, 0 100%, 0 0);
}
<div></div>
Have you tried replacing 100% with100vw ?
Sometime using these two different values for the same element can resolve to a weird scroll appearing.
Related
So I have a clip path to create a downward arrow between the sections. In other words, I have gradient on section 1, and then a gradient on section 2. In section 2, I've clip-pathed it so that it looks like a downward arrow that is connected to the first section.
What doesn't make sense, is that this code is the same for all the other sections (with differnet id's obviously), but it's showing properly. At the top of section 2, it has a 1px line that's visible (and showcases the different sections) as not being connected. But the problem isn't anywhere else. I've cleared cache, attempted to change the code, and even used the dev tools. Nothing is working. Any help is appreciated.
Here is the image so you can see what it looks like:
Here is the code:
#divider-red-section-3 {
position: relative;
background-size: cover;
background-color: $white-bg;
height: 15rem;
width: auto;
.primary-overlay-4 {
background: $primary-what-amidoing-overlay;
position: absolute;
-webkit-clip-path: polygon(100% 30%, 50% 100%, 0 30%, 0 0, 100% 0);
clip-path: polygon(100% 30%, 50% 100%, 0 30%, 0 0, 100% 0);
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
I want to create a triangle which looks like red triangle in below image:
So, I tried my luck with css as follows:
HTML:
<div class="div-1"></div>
<div class="div-2"></div>
<div class="div-3"></div>
CSS:
.div-1 {
position: absolute;
bottom: 0; left: 0;
border-bottom: 385px solid #222;
border-right: 175px solid transparent;
width: 0;
z-index: 5;
pointer-events:none;
}
.div-2 {
position: absolute;
bottom: 0; left: 45px;
border-bottom: 285px solid #ED3237;
border-right: 130px solid transparent;
width: 0;
z-index: 5;
}
.div-3 {
position: absolute;
bottom: 0; left: 45px;
border-bottom: 286px solid #222;
border-right: 105px solid transparent;
width: 0;
z-index: 5;
pointer-events:none;
}
And I got it. Here is the JSFiddle
Now, I want to change the triangle's color to blue when I hover over it.
So, I tried this CSS:
.div-2:hover{
border-bottom: 285px solid blue;
}
At first sight it looks like it is working fine. But we can notice the problem if we take a look at it closely.
Whenever mouse pointer is on transparent area of .div-2, then also color of triangle is changed. I don't want that. I only want to change the color of triangle to blue when cursor is hovered over visible(red) part of .div-2.
So, I again searched on google. Which explained me that I should use rotate transform instead of borders of div.
But I can't find a good tutorial on creating right angled triangles of different width and height as used above. So, I asked this question. Here is the question: How to create a triangle as shown in above image using rotate transform, css3 property.
Update:
SVG is really easy. But I want it done with css because I also intend to hide the bottom part of carousel as shown here:
With all due respect, your request doesn't make much sense. From where I see it, you should not be asking for do this using that (or the other) technique.
Code is convention.
It all boils down to using some conventions over others, in order to display the result we want in particular browsers. It really does not matter what technique or language one uses, as long as they achieve the result without side-effects on other functionality/behavior.
Getting back to your question, you'll always have trouble trying to control the :hover state of an element while hovering a border but not the other.
You could, of course, add a mask to your element thus limiting pointer-events but, than again, why not use the mask in the first place, for display?
Here's how I'd tackle this layout:
.image-container {
background-color: #222;
position: relative;
}
.image-container img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
-webkit-clip-path: polygon(0 0, 73% 0, 100% 50%, 100% 100%, 27% 100%);
clip-path: polygon(0 0, 73% 0, 100% 50%, 100% 100%, 27% 100%);
}
l-border,
r-border {
position: absolute;
height: 100%;
width: 100%;
background-color: #600;
top: 0;
transition: background-color .5s cubic-bezier(.5, 0, .3, 1);
}
l-border:hover,
r-border:hover {
background-color: red;
}
l-border {
-webkit-clip-path: polygon(0 0, 24% 100%, 27% 100%);
clip-path: polygon(0 0, 24% 100%, 27% 100%);
}
r-border {
-webkit-clip-path: polygon(73% 0, 100% 50%, 76% 0);
clip-path: polygon(73% 0, 100% 50%, 76% 0);
}
body {
margin: 0;
}
/* changing both borders when hovering image
just showing you it's possible */
.image-container img:hover ~ * {
background-color: #f50;
}
<div class="image-container">
<img src="https://unsplash.it/1200/450" />
<l-border></l-border>
<r-border></r-border>
</div>
Depending on image ratio or even responsiveness, you might prefer px instead of %:
.image-container img {
-webkit-clip-path: polygon(0 0, calc(100% - 150px) 0, 100% 50%, 100% 100%, 150px 100%);
clip-path: polygon(0 0, calc(100% - 150px) 0, 100% 50%, 100% 100%, 150px 100%);
}
l-border {
-webkit-clip-path: polygon(0 0, 130px 100%, 150px 100%);
clip-path: polygon(0 0, 130px 100%, 150px 100%);
}
r-border {
-webkit-clip-path: polygon(calc(100% - 130px) 0, 100% 50%, calc(100% - 150px) 0);
clip-path: polygon(calc(100% - 130px) 0, 100% 50%, calc(100% - 150px) 0);
}
What I personally do is go to Clippy to get a basic shape polygon going fast (close to what I want) and fine-tune it to my needs in browser, applied to the live example until it fits the current bill.
You should note clip-path is not fully supported, currently at 88.42% support. One could say there is nothing "micro" or "soft" about the clip-path property at the moment.
I tried my best https://codepen.io/CrUsH20/pen/owWeNo
You can change stylus preprocessor in settings it'll be shown as pure CSS. You'll need to press down-arrow at middle block and select View compiled CSS.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to create a diagonal background image as seen in the attached image. I can create a diagonal line using linear-gradient however as I have two different angles this didn't work.
Using Linear Gradients:
This can be done using multiple background images and positioning them accordingly. In the snippet below I've used 3 different layers - one for the top angle (a triangle which is transparent for 50% and is colored for the rest), one for the middle which is essentially nothing but a solid colored rectangle, this is created using linear gradients as it is easier to control the dimensions of an image and finally one for the bottom angle (same approach as the top one but this has a different height and so different angle.)
The output is also responsive as you can see by hovering the element in the below snippet. In the 2nd div, I've set different colors for each image so that you can see how it is formed.
div {
height: 300px;
width: 100%;
background: linear-gradient(to bottom right, transparent 50%, lightblue 51%), linear-gradient(lightblue, lightblue), linear-gradient(to top right, transparent 50%, lightblue 51%);
background-size: 100% 30px, 100% calc(100% - 130px), 100% 100px;
background-position: top left, left 30px, bottom left;
background-repeat: no-repeat;
transition: all 1s ease; /* just for demo */
}
/* just for demo */
div {
margin-bottom: 10px;
}
div:hover {
height: 400px;
}
div:nth-of-type(2) {
background: linear-gradient(to bottom right, transparent 50%, lightblue 51%), linear-gradient(lightpink, lightpink), linear-gradient(to top right, transparent 50%, lightgreen 51%);
background-size: 100% 30px, 100% calc(100% - 130px), 100% 100px;
background-position: top left, left 30px, bottom left;
background-repeat: no-repeat;
}
<div></div>
<div></div>
Using SVG: recommended
This is the approach that I generally recommend and is the best. It involves creating the shape using SVG and then placing it absolutely behind the div element.
div {
position: relative;
height: 300px;
width: 100%;
}
svg {
position: absolute;
height: 100%;
width: 100%;
}
polygon {
fill: lightblue;
}
<div>
<svg viewBox='0 0 300 100' preserveAspectRatio='none'>
<polygon points='0,10 300,0 300,100 0,75z' />
</svg>
</div>
Using Clip-path:
Another approach that can be used is to position a pseudo-element behind the main div and then set a clip-path in the required shape to this pseudo-element.
Note: This snippet will currently work only in WebKit powered browsers. Firefox would need the clip-path to be created via SVG element whereas IE doesn't support it all.
div {
position: relative;
height: 300px;
width: 100%;
}
div:before {
position: absolute;
content: '';
height: 100%;
width: 100%;
background: lightblue;
-webkit-clip-path: polygon(0% 5%, 100% 0%, 100% 100%, 0% 75%);
clip-path: polygon(0% 5%, 100% 0%, 100% 100%, 0% 75%);
}
<div></div>
CSS Perspective
You can use a CSS Perspective Transform to create the shape you want.
div {
margin-top: 25px;
width: 500px;
height: 150px;
transform: perspective( 800px ) rotateY( -25deg );
background: blue;
}
<div></div>
CSS Tricks Docs
Perspective - CSS | MDN
You can apply perspective to the parent container of the rotated div to give it 3-dimensional depth from the front of the viewport.
N.B. For the difference between transform: perspective(value) and perspective: value, see the CSS Tricks Almanac entry on perspective:
Important: Please note the perspective property doesn't affect how the element is rendered; it simply enables a 3D-space for children
elements. This is the main difference between the transform: perspective() function and the perspective property. The first
gives element depth while the latter creates a 3D-space shared by all
its transformed children.
After applying a 3-dimensional depth to the parent container using perspective, you can then apply rotateY to the div you want to rotate.
Working Example:
section {
position: relative;
width: 600px;
perspective: 800px;
transform: translateX(-60px);
}
div:nth-of-type(1) {
position: absolute;
top:30px;
left: 0;
width: 100%;
height: 100px;
background-color: rgb(235,250,255);
transform: rotateY(320deg);
}
div:nth-of-type(2) {
position: absolute;
top: 0;
left: 220px;
width: 120px;
height: 140px;
background-color: rgb(103,201,236);
box-shadow: 6px 6px 6px rgba(127,127,127,0.5);
}
div:nth-of-type(3) {
position: absolute;
top: 24px;
left: 340px;
width: 120px;
height: 140px;
background-color: rgb(255,255,255);
box-shadow: 6px 6px 6px rgba(127,127,127,0.5);
}
<section>
<div></div>
<div></div>
<div></div>
</section>
I am trying to convert PSD into HTML using CSS.
I have a plain rectangle like this :
Now a oval shape glow element : ( As in PSD )
Because of this if you look at only rectangle , With a glow at top it looks like below :
How to achieve the same ? Any lead is appreciated :)
Using Radial Gradients:
You can sort of achieve that by placing a radial-gradient image on top of your rectangle with the solid color. The positioning and size of the gradient may need to be modified to suit your needs.
The radial-gradient that I had used is very similar to the one in your PSD image. That is is starts from a bluish color and then gradually moves to transparent. This gradient is then positioned such that its center point is at 75% width of the parent and a distance that is 25% of the parent's height above it.
div {
height: 200px;
width: 400px;
background-color: rgb(17, 45, 67);
background-image: radial-gradient(ellipse at 75% -25%, rgb(14, 102, 150) 0%, transparent 50%);
background-size: 100% 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div></div>
The main thing to worry with using radial-gradient is the relatively poor browser support.
Using Box Shadow:
Below is a slightly different approach using a pseudo-element and box-shadow. The box-shadow has a very high spread radius which produces a glow like effect.
This has better browser support than radial-gradient (even as low as IE8) but box-shadow cannot take values in percentage and hence this solution wouldn't be very useful for dynamic sized containers.
div {
position: relative;
width: 1280px;
height: 480px;
background-color: rgb(17, 45, 67);
overflow: hidden;
}
div:after {
position: absolute;
content: '';
right: 150px;
top: -250px;
height: 250px;
width: 300px;
border-radius: 50%;
background: rgb(14, 102, 150);
box-shadow: 25px 25px 150px 250px rgba(14, 102, 150, 0.5);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div></div>
The only way I can see is to use a pseudo element and put a gradient background on it. I've made this quickly to show you but it does not reproduce exactly your image :
.rectangle {
position: relative;
width: 300px;
height: 100px;
background-color: #112D43;
}
.rectangle:after {
content: "";
display: block;
position: absolute;
top: 0;
right: 0;
width: 100px;
height: 40px;
background: radial-gradient(ellipse at center, #094567 0%, rgba(0, 0, 0, 0) 50%);
}
<div class="rectangle"></div>
I am very proficient in CSS but I can't for the life of me figure this one out.
I need to recreate a number of shapes in pure CSS (if possible) for a project. What makes it even more harder is that I need the shapes to use a background images. I have tried numerous CSS3 properties such as skew, transform, rotate etc... however none has worked so far. Skew got me closest but the background and it's contents where skewed. I tried setting an image inside the div and giving it opposite properties to the div skew which straightened the image but then I couldn't position the image correctly.
Is it possible in anyway to recreate this using CSS?
Even if someone can help me find the right property to use so I can research how it'll be done that would be helpful.
Thanks in advance.
Difficult shape to make this one. You can create this shape using clip-path but I'm afraid the support is not amazing for this property.
See the support here.
Now here is an example of the shape with a background.
.shape {
width: 400px;
height: 400px;
background: #000;
-webkit-clip-path: polygon(24% 0, 100% 20%, 100% 100%, 0 40%);
clip-path: polygon(24% 0, 95% 20%, 100% 100%, 0 40%);
background-image: url(http://p1.pichost.me/640/39/1629941.jpg);
background-size: cover;
}
<div class="shape"></div>
This will give a diamon so you can tweak it to get your shape :
#diamond-narrow {
width: 0;
height: 0;
border: 50px solid transparent;
border-bottom: 70px solid red;
position: relative;
top: -50px;
}
#diamond-narrow:after {
content: '';
position: absolute;
left: -50px; top: 70px;
width: 0;
height: 0;
border: 50px solid transparent;
border-top: 70px solid red;
}
http://css-tricks.com/examples/ShapesOfCSS/