Using top position affects whole page [duplicate] - html

This question already has answers here:
position:relative leaves an empty space
(13 answers)
Closed 2 years ago.
I have a page with an angled DIV at the top and a DIV immediately below that which is angled. I've had to offset the position of both top DIVs so that they join together. The only issue is that the next DIV below these has a space the size of the 120px offset.
I could apply this to every DIV but doing this to the footer means that there's empty white space at the bottom of the page.
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
position: relative;
top: -120px;
}
.next {
min-width: 100%;
min-height: 600px;
background: #000000;
}
<div class="top"></div>
<div class="main"></div>
<div class="next"></div>
Any ideas to make the black DIV begin at the bottom of the blue one and the rest of the page not be affected would be appreciated, you can't simply use top: -120px; on every DIV because then there's 120px of empty space at the bottom of the page.

You can change top by margin-top, it will have the same effect for the element, and the normal flow after it will be the same
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
position: relative;
margin-top: -120px;
}
.next {
min-width: 100%;
min-height: 600px;
background: #000000;
}
<div class="top"></div>
<div class="main"></div>
<div class="next"></div>

Related

Is it possible to highlight areas of a background image using multiple DIVs and CSS?

I have two DIVs over the top of a single background image. I want to darken all areas around the two DIVs and keep the image unaffected within the two DIVS, creating a kind of spotlight effect.
I tried using shadows around the DIVs but both cast a shadow over each other.
Is there any way to darken all areas outside of the 2 DIVS?
E.g.
<div class="background-image">
<div class="light-area-1"></div>
<div class="light-area-2"></div>
</div>
If there are just two 'spotlights' and the effect is to be purely visual you could use pseudo elements and clip-paths to overlay the spotlights:
.spotlit {
--bg: url(https://picsum.photos/id/1015/200/300);
background-image: linear-gradient(rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), var(--bg);
background-size: cover;
width: 80vmin;
height: 70vmin;
position: relative;
}
.spotlit::before,
.spotlit::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
display: inline-block;
top: 0;
left: 0;
background-image: var(--bg);
background-size: cover;
}
.spotlit::before {
clip-path: polygon(20% 20%, 50% 20%, 50% 40%, 20% 40%);
}
.spotlit::after {
clip-path: polygon(70% 60%, 90% 60%, 90% 95%, 70% 95%);
}
<div class="spotlit">
</div>
However, if the spotlights are needed as actual elements a similar approach lays them above the main, shadowed element using clip-path as above. This way you can have as many spotlights as required.
.background-image {
--bg: url(https://picsum.photos/id/1015/200/300);
background-image: linear-gradient(rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), var(--bg);
background-size: cover;
width: 80vmin;
height: 70vmin;
position: relative;
}
.light-area-1,
.light-area-2 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: var(--bg);
background-size: cover;
}
.light-area-1 {
clip-path: polygon(20% 20%, 50% 20%, 50% 40%, 20% 40%);
}
.light-area-2 {
clip-path: polygon(70% 60%, 90% 60%, 90% 95%, 70% 95%);
}
<div class="background-image">
<div class="light-area-1"></div>
<div class="light-area-2"></div>
</div>
The shapes can be changed given clip-path's various options such as circle which may be useful to make them look more like an actual spotlight.

How to make a curved shape with gradient?

I have to create image like this using CSS:-
If not possible then how can I use this with minimum image size? Like in below code I have used two images but this is also not working...
<div style="background:url('https://i.stack.imgur.com/veeS8.png') no-repeat top center, url('https://i.stack.imgur.com/2i7ed.png') repeat-y top 50px center; widhth:100%; height:800px; background-size:100%;">
</div>
Also possible with masking without using image. This is more flexible. You can control easily the slope by changing the variable.
.container {
--slope: 100px;
width: 100%;
height: 500px;
--mask: radial-gradient(farthest-side, #000 99%, transparent 100%) 50% 0 / 150% calc(var(--slope) * 2) no-repeat,
linear-gradient(#000, #000) 0 100% / 100% calc(100% - var(--slope)) no-repeat;
-webkit-mask: var(--mask);
mask: var(--mask);
background: linear-gradient(90deg, rgba(133, 132, 242, 1) 0%, rgba(35, 136, 253, 1) 50%, rgba(127, 237, 226, 1) 100%);
}
<div class='container'></div>
something like this would work:
div.cont:before {
content: '';
background-image: url(https://i.stack.imgur.com/2i7ed.png);
background-size: 100% auto;
position: absolute;
top: 44px;
left: 0;
right: 0;
bottom: 0;
}
<div class="cont" style="width: 100%;height:800px;background-image: url(https://i.stack.imgur.com/veeS8.png);background-size: 100% auto;background-repeat: no-repeat;position: relative;">
</div>
a trivial task using clip-path
.container {
height: 400px;
background: linear-gradient(90deg, rgba(133,132,242,1), rgba(35,136,253,1), rgba(127,237,226,1));
clip-path:ellipse(90% 100% at bottom); /* simply adjust the 90% */
}
<div class='container'></div>
To have the same curvature on resize use pixel value
.container {
height: 400px;
max-width:800px;
background: linear-gradient(90deg, rgba(133,132,242,1), rgba(35,136,253,1), rgba(127,237,226,1));
clip-path:ellipse(600px 100% at bottom);
}
<div class='container'></div>
.element {
width: 200px;
height: 200px;
border-radius: 100% 100% 0 0 / 20% 20% 0 0;
background-image: linear-gradient(to right, #8684F2 0%, #1BEEE3 100%);
}
You can use CSS Gradient to get this kind of result. You can also use some tools online to generate some gradients like this one:
https://cssgradient.io/
.container {
width: 500px;
height: 500px;
background: rgb(133,132,242);
background: linear-gradient(90deg, rgba(133,132,242,1) 0%, rgba(35,136,253,1) 50%, rgba(127,237,226,1) 100%);
}
<div class='container'>
</div>

Creating angled DIV for hero

There's a lot of questions similar to this but they're for edges or have answers incompatible with some browsers. I'd thought of using a gradient image for the background but can achieve the same effect using a background gradient and I'd guess this may be easier to implement with minimal code.
I currently have this, which has flat edges;
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
}
<div class="top"></div>
<div class="main"></div>
It uses minimal code but I'd be aiming for an angled edge either on the bottom of one and on the top of the other or just the bottom of the top one so that the DIVs match up.
I'd be aiming for something like this…
Of course I could rotate the DIV but then there's overflow. I want something clean so that both DIVs match up. Something using clip-path: polygon could work but I can't figure out the angles or implementation. Any ideas or resources for where to start would be apprecited.
UPDATE
I've figured out how to angle both so that they match up but the DIVs need to be touching for it to look proper.
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
}
<div class="top"></div>
<div class="main"></div>
UPDATE 1
Would this work? I added position: relative; and top: -150px; to move it up.
.top {
min-width: 100%;
min-height: 400px;
background: linear-gradient(45deg, #FF0000, #00FF00);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
}
.main {
min-width: 100%;
min-height: 600px;
background: #0000FF;
clip-path: polygon(0 0, 100% 20%, 100% 100%, 0 100%);
position: relative;
top: -120px;
}
<div class="top"></div>
<div class="main"></div>

Why is my z-index not working with clip-path? [duplicate]

This question already has answers here:
Why does z-index not work?
(10 answers)
Closed 4 years ago.
I am trying to make my middle div under my background_top div. This is my first time using clip-path, and it seems like it ignores the z-index(?). Down below I have added a codepen.
The background_top div does have a clip-path, giving it a slanted bottom, and I am trying to get it ontop of the middle div.
I have given my background_top a z-index of -100, and my middle a z-index of -250
Here is my code:
<div class="background_top">
<h1>we build<span>futures.<span></h1>
<div class="top_quote">
<h2>“</h2>
</div>
<div class="top_p">
<p></p>
</div>
</div>
<div class="middle">
</div>
</body>
<footer>
<div class="footer">
<div class="">
<p></p>
</div>
</div>
</footer>
.middle {
margin-top: -45vh;
height: 150vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
background-color: #B2DFEE;
z-index: -250;
}
.background_top {
height: 95vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
z-index: -100;
background: #232323;
background-image: url("images/placeholder.jpg");
background-size: cover;
}
.footer {
width: 100vw;
background-color: #232323;
height: 100vh;
position: fixed;
bottom: 0;
z-index: -230;
}
you need to set the position for z-index to work
Just add position:relative;
.middle {
margin-top: -45vh;
height: 150vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 80%);
background-color: #B2DFEE;
z-index: 1;
position: relative;
}
.background_top {
height: 95vh;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
clip-path: polygon(0 0, 100% 0, 100% 60%, 0 100%);
z-index: 2;
background: #232323;
background-image: url("images/placeholder.jpg");
background-size: cover;
position: relative;
}
.footer {
width: 100vw;
background-color: #232323;
height: 100vh;
position: fixed;
bottom: 0;
z-index: -230;
}
<div class="background_top">
<h1>we build<span>futures.</span></h1>
<div class="top_quote">
<h2>“</h2>
</div>
<div class="top_p">
<p></p>
</div>
</div>
<div class="middle">
</div>
<footer>
<div class="footer">
<div class="">
<p></p>
</div>
</div>
</footer>

CSS: Colored div with transparent box [duplicate]

This question already has answers here:
turning a div into transparent to see through two containers
(4 answers)
Closed 6 years ago.
Is there any way to have a div with a background-color that takes up 100% width and a transparent box inside it that shows the original background?
Solution 1: Clip-path
Clip path can be quite useful, as it keeps the code clean and simple. However, it does not have great support (yet) in browsers, and should hence only be used in test environments.
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
height: 300px;
width: 100%;
background: tomato;
position: relative;
-webkit-clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0, 50% 0, 50% 20%, 80% 20%, 80% 80%, 20% 80%, 20% 20%, 50% 20%, 50% 0);
}
<div>
</div>
Solution 2: Box shadow Trick
The box shadow trick uses a pseudo element and overflow:hidden; to create the box shadow/colouring of the element.
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
height: 300px;
width: 100%;
overflow:hidden;
position: relative;
}
div:before{
content:"";
position:absolute;
top:20%;width:60%;height:60%;left:20%;
box-shadow:0 0 0 999px tomato;
}
<div></div>
Solution 3: Gradients
You could use multiple gradient background, however this may or may not be suitable as gradients don't always turn out rendered very nicely:
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
position: relative;
height: 300px;
width: 100%;
background: linear-gradient(tomato, tomato), linear-gradient(tomato, tomato), linear-gradient(tomato, tomato), linear-gradient(tomato, tomato);
background-size: 100% 20%, 20% 100%, 100% 20%, 20% 100%;
background-position: left bottom, right bottom, left top, left top;
background-repeat: no-repeat;
}
<div></div>
Solution 4: Borders
Whilst this may or may not be suitable for you, there is still a chance that it may help, so will post here anyway:
html {
background: url("http://butlers-web.co.uk/Content/Images/BWLOGO.png") 100% 100%;
}
div {
position: relative;
height: 300px;
width: 100%;
box-sizing: border-box;
border-left: 20vw solid tomato;
border-right: 20vw solid tomato;
border-top: 50px solid tomato;
border-bottom: 50px solid tomato;
}
<div></div>
Solution 5: Background attachment
I have recently come across the background-attachment property, so am still coming to grips with it. However, if you wished the background to appear behind you may be able to alter the below snippet to your needs:
body {
background: url('http://butlers-web.co.uk/Content/Images/BWLOGO.png');
background-attachment: fixed;
}
.wrapper {
width: 100%;
height: 300px;
background: tomato;
position: relative;
}
.inner {
width: 80%;
height: 80%;
background: url('http://butlers-web.co.uk/Content/Images/BWLOGO.png');
background-attachment: fixed;
position: absolute;
top: 10%;
left: 10%;
box-sizing:border-box;
border:2px solid black;
}
<div class="wrapper">
<div class="inner"></div>
</div>
You're going to need two div for that. A parent, with the red background, then the inner div.
give the inner div margin: 10px auto; as a start.