I have a card with image, text and overlay gradient.
The image is much bigger than its parent div. The image is made responsive to the size of parent div. I need to position the image inside div a certain way, so I use object-position for it.
However, when I try to position it, I get white space between the image and parent container, even though the image is bigger than the div..
I used position values from Figma which are :
position: absolute;
width: 386px;
height: 458px;
left: -33px;
top: -94px;
On the screenshot you can see how it should look like (on the left) and how it's done with the code below (on the right):
DEMO
https://github.com/meri-maki/stackoverflow-demo-ingrad
https://meri-maki.github.io/stackoverflow-demo-ingrad
.card {
border-radius: 24px;
width: 100%;
height: auto;
min-height: 328px;
max-height: 534px;
position: relative;
display: inline-block;
overflow: hidden;
}
.card img {
display: block;
vertical-align: bottom;
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
/* ----------IMAGE POSITIONING---------- */
object-position: top -94px left -33px;
}
/* ----------gradient---------- */
.card:after {
content: '';
position: absolute;
left: 0;
right: 0;
bottom: 0;
min-width: 100%;
height: 66%;
background: linear-gradient(0deg, #181818 0%, rgba(25, 23, 29, 0.447294) 48.44%, rgba(24, 24, 24, 0) 100%);
opacity: 0.9;
}
/* ----------caption---------- */
.caption {
z-index: 10;
position: absolute;
left: 4.27%;
right: 8.54%;
top: 63.41%;
bottom: 7.32%;
}
<div class="card">
<img src="https://picsum.photos/1200" alt="">
<div class="caption">
<h4>Caption</h4>
</div>
</div>
I tried using transform: translate but got same result. What could be wrong?
You're shifting the image with negative position values. I'm not sure why, but you'd need to compensate by adding the reciprocal value to increase the size of the image accordingly.
Alternatively, set position to zero or center and eliminate those negative values.
.card {
border-radius: 24px;
width: 100%;
height: auto;
min-height: 328px;
max-height: 534px;
position: relative;
display: inline-block;
overflow: hidden;
}
.card img {
display: block;
vertical-align: bottom;
position: absolute;
object-fit: cover;
object-position: top -94px left -33px;
width: calc(100% + 33px); /* <------------- HERE */
height: calc(100% + 94px); /* <-------- AND HERE */
}
.card:after {
content: '';
position: absolute;
left: 0;
right: 0;
bottom: 0;
min-width: 100%;
height: 66%;
background: linear-gradient(0deg, #181818 0%, rgba(25, 23, 29, 0.447294) 48.44%, rgba(24, 24, 24, 0) 100%);
opacity: 0.9;
}
.caption {
z-index: 10;
position: absolute;
left: 4.27%;
right: 8.54%;
top: 63.41%;
bottom: 7.32%;
color: #fff;
}
<div class="card">
<img src="https://picsum.photos/1200" alt="">
<div class="caption">
<h4>Caption</h4>
</div>
</div>
Related
I want to do this:
So far I got this:
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: #red;
z-index: 10000;
height: 10px;
overflow: visible;
}
.header:after {
content: '';
position: relative;
top: 100%;
display: block;
margin: 0 auto;
background: red;
width: 50px;
height: 25px;
border-radius: 10px 10px 50px 50px;
}
<div class="header"></div>
Codepen.
I can't get the top radius to go outwards the half circle like in the image.
How to do this with CSS?
You cannot make a negative radius on a border.
There is the possibility to make an SVG path or radial gradient... I made a new div as circle and radial gradient on pseudo-elements. It's not perfect, but it will possibly show you the direction to solution :)
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: red;
z-index: 10000;
height: 10px;
overflow: visible;
}
.header-circ {
position: relative;
top: 100%;
display: block;
margin: 0 auto;
background: red;
width: 500px;
height: 250px;
border-radius: 10px 10px 250px 250px;
}
.header-circ::before, .header-circ::after {
content: "";
position: absolute;
width: 100px;
height: 100px;
z-index: -1;
}
.header-circ::before {
left:-94px;
background: radial-gradient(circle at bottom left, white 0%,white 75%,red 75%);
}
.header-circ::after {
right:-94px;
background: radial-gradient(circle at bottom right, white 0%,white 75%,red 75%);
}
<div class="header"></div>
<div class="header-circ"></div>
What i need: a box with notched angle, a border around it and png image inside.
What i have: a box with notched angle made with linear-gradients, a border around it and png image over the border.
What i've tried: change z-indexes - zero result.
Any idea?
My code: https://codepen.io/drsg/pen/eYZEXNz
.item {
margin: 0 auto;
position: relative;
width: 340px;
height: 380px;
overflow: hidden;
background-image: linear-gradient(135deg, transparent 30px, orange 30px);
z-index: 999;
}
.item::before {
content: "";
position: absolute;
left: 10px;
top: 10px;
display: block;
width: 320px;
height: 360px;
background-image: linear-gradient(135deg, transparent 30px, black 30px);
overflow: hidden;
}
.item__image {
position: absolute;
bottom: -90px;
left: -105px;
width: 370px;
height: 360px;
z-index: 1;
}
.item__image img {
width: 100%;
height: 100%;
}
body {
background-color: grey;
}
Using clip path you can get the same effect.
.box {
position: relative;
width: 360px;
height: 360px;
padding: 10px;
background-color: orange;
clip-path: polygon(24% 0, 100% 0, 100% 100%, 0 100%, 0 23%);
}
.box::before {
display: block;
background-color: black;
content: 'hello';
width: 100%;
height: 100%;
clip-path: polygon(24% 0, 100% 0, 100% 100%, 0 100%, 0 23%);
}
.box img {
width: 300px;
position: absolute;
z-index: 1;
bottom: -35px;
left: -60px;
}
<div class="box">
<img src="https://pngicon.ru/file/uploads/vinni-pukh-v-png.png" />
</div>
Can you try with this css:
.item {
margin: 0 auto;
position: relative;
width: 340px;
height: 380px;
overflow: hidden;
background-image: linear-gradient(135deg, transparent 30px, orange 30px);
z-index: 999;
}
.item::before {
content: "";
position: absolute;
left: 10px;
top: 10px;
display: block;
width: 320px;
height: 360px;
background-image: linear-gradient(135deg, transparent 30px, black 30px);
z-index: 1;
}
.item__image {
position: relative;
left: 10px;
top: 10px;
width: 320px;
height: 360px;
background: transparent;
overflow: hidden;
}
.item__image img {
width: 100%;
height: 100%;
position: absolute;
z-index: 1;
bottom: -80px;
left: -80px;
}
body {
background-color: grey;
}
How to create a partial width opacity ?
I have a div that has a background image with transparency, I used after to do get the effect like this
.indicators-menu {
width: 100%;
height: 100%;
display: block;
position: absolute;
top: 0;
z-index: 1;
}
.indicators-menu::after {
background-image: url('bg_platform_repeat.jpg');
content: "";
opacity: 0.9;
top: 0;
position: absolute;
z-index: -1;
left: 0;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: cover;
background-position: unset;
}
This works great, but what I need to do is to split the opacity by width
instead of 100% to 80% with opacity 0.9 and 20% with opacity 1
I thought to use the CSS mask property but I see that its not well supported
what i need to do is to split the opacity by width instead of 100% to 80% with opacity 0.9 and 20% with opacity 1
Use two pseudo-elements with the same background image but position them differently.
div {
width: 460px;
height: 300px;
margin: 1em auto;
border: 1px solid red;
position: relative;
}
div:before,
div:after {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
background-image: url(http://www.fillmurray.com/460/300);
background-repeat: no-repeat;
background-size: cover;
}
div:before {
width: 80%;
opacity: 0.5;
/* for example */
}
div:after {
width: 20%;
left: 80%;
background-position: 100% 0;
}
<div>
</div>
One idea is to use an overlay above the image to simulate this effect. The color used need to be the same as the below background:
.box {
background:
linear-gradient(rgba(255,255,255,0.3),rgba(255,255,255,0.3)) left/80% 100%,
url('https://picsum.photos/200/200?image=1069') center/cover;
background-repeat: no-repeat;
width: 200px;
height: 200px;
}
<div class="box">
</div>
Use :before with background: white; and opacity:0.1(I set 0.4 only you to see the difference) and width:80%
.indicators-menu::after,.indicators-menu::before{
background-image: url('https://i.imgur.com/BK7wL0d.jpg');
content: "";
opacity:1;
top: 0;
position: absolute;
z-index: -1;
left: 0;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: cover;
background-position: unset;
}
.indicators-menu::before{
background: white;
opacity: 0.4;
z-index: 2;
width: 80%;
}
<div class="indicators-menu">
</div>
I have a div with a triangle as a pseudo element on top, and an image inside of this div, as you can see in this fiddle. I am trying to make the image contained within the bounds of the parent with the pseudo element, so that the image extends all the way through the triangle.
However, I am not sure how to do this. I have tried a few ways, including skewing the container etc but have not managed to create an elegant, responsive solution.
Please give me your suggestions if possible.
Edit: I am trying to make the image look like the following:
e.g. the ring is quite large and simply gets cut off by the containing element.
.bg {
background: black;
color: white;
position: relative;
filter: drop-shadow(0 0 3vh rgba(30, 14, 43, 1));
height: 20vh;
width: 100vw;
margin: 30vh 0;
}
.bg::before {
content: "";
position: absolute;
bottom: 100%;
left: 0;
right: 0;
display: block;
border-bottom: 18vh solid black;
border-right: 12vw solid transparent;
border-left: 88vw solid transparent;
}
.ring {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.container {
height: 100%;
width: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: hidden;
}
<div class='bg'>
<div class='container'>
<img src='http://pngimg.com/uploads/jewelry/jewelry_PNG6788.png' class='ring'>
</div>
</div>
You may try to have the shape as one element and consider some rotation transform and overflow:hidden :
go full page for better result
body {
margin: 0;
}
.bg {
color: white;
position: relative;
height: 90vh;
overflow: hidden;
text-align: right;
}
.container {
position: absolute;
filter: drop-shadow(0 0 3vh rgba(30, 14, 43, 1));
height: 160%;
transform: rotate(-20deg);
top: 42%;
left: -2%;
right: -4%;
background: #000;
overflow: hidden;
}
img {
position: absolute;
top: 15%;
left: 61%;
transform: translate(-50%, -50%) rotate(20deg);
]
<div class='bg'>
<div class="container">
<img src='http://pngimg.com/uploads/jewelry/jewelry_PNG6788.png' class='ring'>
</div>
</div>
i want help to add two image one over the other and position two images in center
<div class="im1"> </div>
.im1{
position: relative; top: 0; left: 0;
background-image:url("../images/img-shadow.png"),url("../images/img-1.png");
background-size:contain;
height:358px;
background-repeat: no-repeat,no-repeat;
}
background image is with shadow
i want it to be
This actually can be solved only with CSS.
I created an example here that generates the rotated border of the image.
Now you can insert any image inside :)
p.s of course you need to change the width and the height of the image inside to be the same as the frame diagonal. you can use CSS calculate for this.
body {
background-color: #F3F5F6;
}
.shadow:before,
.shadow:after {
content: "";
position: absolute;
z-index: -1;
height: 10%;
max-width: 90%;
width: 90%;
}
.shadow:before {
-webkit-transform: rotate(86deg);
left: -72px;
right: auto;
top: 118px;
box-shadow: 0 15px 5px rgba(0, 0, 0, 0.2);
}
.shadow:after {
-webkit-transform: rotate(84deg);
left: auto;
right: -92px;
bottom: 75px;
box-shadow: 0 -15px 5px rgba(0, 0, 0, 0.2);
}
.rotate {
-webkit-transform: rotate(45deg);
}
.box {
width: 200px;
height: 200px;
position: absolute;
top: 100px;
left: 100px;
}
.pic-wrapper {
width: 100%;
height: 100%;
border: 10px solid #fff;
overflow: hidden;
}
.pic {
background-image: url('http://modernschoolec.com/wp-content/uploads/2015/04/11-980x408.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
/* (side)(sqrt(2)) */
width: 282px;
height: 282px;
-webkit-transform: rotate(-45deg);
position: relative;
top: -40px;
left: -40px;
}
<div class="box shadow rotate">
<div class="pic-wrapper">
<div class="pic"></div>
</div>
</div>
You could either give the shadow image a z-index of 100 and give the school image a z-index of 101 or set one to be position relative and one to be position absolute, but you will need media queries to make it responsive.
.im1{
background-image:url("../images/img-1.png"),url("../images/img-shadow.png");
height:358px;
background-repeat: no-repeat,no-repeat;
background-position: center , center;
}