glitchy behaviour div with background image, border radius in Chrome - html

I'm encountering some glitchy behaviour in chrome.
I'm trying to have a div with a border-radius, a background-image, a border and overflow:hidden.
Within that div is some kind of overlay that has to be 'masked' by the overflow:hidden and border-radius properties of the parent div.
This works but I'm seeing a fine border of the background-image between the overlay div and the border of the parent div.
Is there a workaround for this?
body{
background-color:black;
}
.circle{
background-image: url('http://www.augustaga.gov/images/pages/N1617/Black%20tupelo%20-%20photo%201.jpg');
width: 250px;
height: 250px;
position: absolute;
background-size: cover;
background-position: center;
border-radius: 50%;
border: 2px solid white;
overflow:hidden;
}
.overlay{
top: 150px;
position: absolute;
height : 100px;
width: 100%;
background-color:white;
text-align: center;
}
<div class="circle">
<div class="overlay"> text</div>
</div>

Try adding background-clip: padding-box; to your .circle class
-webkit-background-clip: padding-box;
-moz-background-clip: padding;
background-clip: padding-box;
Source

Please try this:
You need to remove the border :2px solid white;
form .circle class.
so it should be:
.circle{
background-image: url('http://www.augustaga.gov/images/pages/N1617/Black%20tupelo%20-%20photo%201.jpg');
width: 250px;
height: 250px;
position: absolute;
background-size: cover;
background-position: center;
border-radius: 50%;
/* border: 2px solid white; */
overflow: hidden;
}

Related

Space image inside the circle

I have this fiddle https://jsfiddle.net/obzpLy1g/
Basically its an image inside a red circle. Would it be possible to space the image inside the circle
My html looks like this
<div style="
width: 50px;
height: 50px;
border-radius: 50%;
background-image: url('https://m.media-amazon.com/images/I/41erGZf8kNL._AC_.jpg');
background-size: cover;
background-position: center;
background-origin: padding-box;
background-clip: padding-box;
padding: 20px;
border: 1px solid red;
"></div>
My code do not give the image inside the circle a padding of 20px like i would have wanted.
You need to clip the image to the content-box not the padding-box
<div style="
width: 50px;
height: 50px;
border-radius: 50%;
background-image: url('https://m.media-amazon.com/images/I/41erGZf8kNL._AC_.jpg');
background-size: contain;
background-position: center;
background-origin: content-box;
background-clip: content-box;
background-repeat: no-repeat;
padding: 20px;
border: 1px solid red;
"></div>
To ensure you get the whole of the image inside the circle, but still have padding of 20px all round, this snippet puts the image as background to the before pseudo element. That way it doesn't get clipped by the radius setting of the div.
It sets the size to contain to ensure all the image is visible. The given image is rectangular rather than square so it doesn't seem to make sense to clip it if you don't want a headless chicken.
div::before {
content: '';
width: calc(100% - (2 * var(--padding)));
height: calc(100% - (2 * var(--padding)));
position: absolute;
background-image: url('https://m.media-amazon.com/images/I/41erGZf8kNL._AC_.jpg');
background-size: contain;
background-position: center;
background-repeat: no-repeat;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div style="
width: 50px;
height: 50px;
border-radius: 50%;
--padding: 20px;
padding: var(--padding);
border: 1px solid red;
position: relative;
background-color: #eeeeee;
"></div>
Note: the snippet gives the div a gray background for demo purposes, just to make clear the exact positioning and size of the image.
Is this what you are looking for? Run the snippet !
<div style="
width: 50px;
height: 50px;
border-radius: 50%;
background-image: url('https://m.media-amazon.com/images/I/41erGZf8kNL._AC_.jpg');
background-size: cover;
background-position: center;
background-origin: padding-box;
background-clip: content-box;
padding: 20px;
border: 1px solid red;
"></div>
background-clip:content-box;
Clip the image inside content-box
let me know if it is not what you asked I will try again.

How do I make my background-image take full height of div?

I have a div with a background image that I am trying to give a transparent type border to.
Currently, this works for the side borders but the top and bottom borders do not fill with the image. How would I achieve this?
.picture-div {
background: url(https://www.princeton.edu/sites/default/files/styles/half_2x/public/images/2022/02/KOA_Nassau_2697x1517.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
border: 25px solid rgba(100, 100, 100, .50);
width: 200px;
height: 200px;
border-radius: 60px;
}
<div class="picture-div" />
Add background-origin to border-box, so the image will fill the border.
You can read the detail in : https://www.w3schools.com/cssref/css3_pr_background-origin.asp
.picture-div {
background: url(https://www.princeton.edu/sites/default/files/styles/half_2x/public/images/2022/02/KOA_Nassau_2697x1517.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-origin: border-box;
border: 25px solid rgba(100, 100, 100, .50);
width: 200px;
height: 200px;
border-radius: 60px;
}
<div class="picture-div" />
You can use border on the pseudo :before of the picture-div class as follows:
.picture-div {
background: url(https://www.princeton.edu/sites/default/files/styles/half_2x/public/images/2022/02/KOA_Nassau_2697x1517.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
position: relative;
width: 200px;
height: 200px;
border-radius: 60px;
}
.picture-div:before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border: 25px solid rgba(100, 100, 100, .50);
border-radius: 60px;
}
<div class="picture-div" />

Is it possible to set a gradient box-shadow to div that has a background-image?

I have a div that has an image as a background-image, I need to set a linear gradient box-shadow but it can't get two background-image.
I need something like the below image
my Html code is:
<div class="top-container"></div>
css:
.top-container {
position: relative;
min-height: 632px;
background-position: bottom;
background-size: cover;
background-repeat: no-repeat;
width: 100%;
height: auto;
padding-top: 25px;
background-image: url("/assets/images/homepage-images/header-desk-bg-img.jpg");
}
I need just gradient shadow from top to bottom of the below image
You can do this by adding box-shadow property.
I do not know what values you should give to it, but this is an excellent tool where you can play with box-shadow values to set it up the way you like the most.
https://www.cssmatic.com/box-shadow
So, by the end of your trying you will end up with something like this:
.top-container {
position: relative;
min-height: 632px;
background-position: bottom;
background-size: cover;
background-repeat: no-repeat;
width: 100%;
height: auto;
padding-top: 25px;
background-image: url("/assets/images/homepage-images/header-desk-bg-img.jpg");
box-shadow: 10px 10px 5px 0px rgba(0,0,0,0.75);
}
You can use ::after if you want to apply a linear-gradient.
.top-container{
width: 50px;
height: 50px;
margin: 20px;
background: cyan;
}
.top-container::after{
position: absolute;
content: '';
display: block;
width: 50px;
margin-top: 50px;
height: 10px;
background: linear-gradient(to bottom, #ccc, transparent);;
}
<div class='top-container'></div>
The CSS property box-shadow can also do the work.
.top-container{
width: 50px;
height: 50px;
margin: 20px;
background: cyan;
box-shadow: 0 0 10px grey;
}
<div class='top-container'></div>

Make a curve in a div with background image

I'm stuck with one task and I can't find any solution over the internet.
I have this situation:
Images 1 and 2 has background images.
I need one 1st or 2nd image have that bump.
If it would be 1st image, that bump should extend div bottom and overlay the 2nd background.
If it would be 2nd div then I need like a crater/hole at the top and be under 1st div.
I can't cut my images to .png/.gif and cut with that bump in photoshop. These images are changed by client, so he can't prepare exact images all the time, so I need to extend them by code.
I tried to radial-gradient() background and cut with svg, but those aren't supported by Firefox.
Is it possible to make this with code who adapts to all background images?
Here is a solution that uses background-size: cover, so it is easier to adapt. (It would be easier with known dimension images).
The drawback is a little complex markup, 3 auxiliar divs are needed.
The curves are standard border-radius, so that can be adjusted as needed
.container {
width: 600px;
height: 400px;
border: solid 1px blue;
position: relative;
}
.up {
width: 100%;
height: 50%;
position: relative;
border-bottom: 40px solid transparent;
background-image: url(http://lorempixel.com/600/400);
background-size: cover;
background-position: center bottom;
background-origin: border-box;
background-clip: padding-box;
margin-bottom: -40px;
}
.addon {
width: 25%;
height: calc(100% + 40px);
position: absolute;
left: 37.5%;
border-radius: 0px 0px 50px 50px;
overflow: hidden;
background-image: inherit;
z-index: 2;
}
.addon:before {
content: "";
position: absolute;
width: 400%;
height: 100%;
left: -150%;
background-image: inherit;
background-size: cover;
background-position: center bottom;
background-origin: padding-box;
background-clip: padding-box;
}
.down {
width: 100%;
height: 50%;
position: relative;
bottom: 40px;
border-top: 40px solid transparent;
background-image: url(http://lorempixel.com/400/200);
background-size: cover;
background-position: center top;
background-origin: border-box;
background-clip: padding-box;
margin-top: -40px;
}
.addleft {
width: 37.5%;
height: calc(100% + 40px);
position: absolute;
left: 0px;
bottom: 0px;
border-radius: 0px 50px 0px 0px;
overflow: hidden;
background-color: tomato;
background-image: inherit;
background-size: 0px 0px;
}
.addleft:before {
content: "";
position: absolute;
width: 266.667%;
height: 100%;
left: 0px;
background-image: inherit;
background-size: cover;
background-position: center top;
background-origin: padding-box;
background-clip: padding-box;
}
.addright {
width: 37.5%;
height: calc(100% + 40px);
position: absolute;
right: 0px;
bottom: 0px;
border-radius: 50px 0px 0px 0px;
overflow: hidden;
background-image: inherit;
background-size: 0px 0px;
}
.addright:before {
content: "";
position: absolute;
width: 266.667%;
height: 100%;
right: 0px;
background-image: inherit;
background-size: cover;
background-position: center top;
background-origin: padding-box;
background-clip: padding-box;
}
<div class="container">
<div class="up">
<div class="addon"></div>
</div>
<div class="down">
<div class="addleft"></div>
<div class="addright"></div>
</div>
</div>
You need to use border-color
border-color: transparent transparent #555 transparent;
Basically you need to mark some percentage of left and right of the image border-color as transparent.
And then set border-radius to give the curve
Thanks.

Parallel diagonal lines on background

I want to draw 2 parallel diagonal lines on the background of my div.
Please see my table here:
body {
background-image: url("http://i.imgur.com/TnPgXl4.jpg");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat: no-repeat;
padding: 40px;
}
#table {
width: 800px;
height: 300px;
background-color: transparent;
border: solid 1px white;
}
<div id="table"></div>
I want to achieve something like this:
You can achieve the 2 diagonal lines with a rotated pseudo element. The 2 lines are the top and bottom borders of the absolutely positioned pseudo element:
body {
background-image: url("http://i.imgur.com/TnPgXl4.jpg");
background-size: cover;
background-repeat: no-repeat;
padding: 40px;
}
#table {
position: relative;
width: 800px; height: 300px;
background-color: transparent;
border: solid 1px white;
overflow: hidden;
}
#table:before {
content: '';
position: absolute;
right: 30%; bottom: 100%;
height: 20px; width: 100%;
border-top: 1px solid #fff;
border-bottom: 1px solid #fff;
transform-origin: 100% 100%;
transform: rotate(-70deg);
}
<div id="table"></div>
This is how this works :
the width between the 2 lines is controled by the height of the pseudo element
the thickness of the lines is controled by the border-width
the slant of the lines is controled by the rotation angle
the overflowing parts of the lines are hidden with the overflow:hidden; property on the div
Note that you need to add vendor prefixes to the transform and transform origin properties for browser support and you probably don't need the vendor prefixes on the background-size property:
canIuse for background-size
canIuse for 2D transforms
You can do this with :after and :before pseudo elemnts and trasform: rotate()
body {
background-image: url("http://www.planwallpaper.com/static/images/cool-background.jpg");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat: no-repeat;
padding:40px;
}
#table {
width: 70%;
height: 300px;
background-color: transparent;
border: solid 1px white;
margin: 0 auto;
position: relative;
box-sizing: border-box;
}
#table:before, #table:after {
content: "";
position: absolute;
left: 60%;
height: 102%;
border-left: 1px solid white;
transform: rotate(10deg);
transform-origin: top;
}
#table:after {
left: 65%;
}
<div id="table"></div>
An alternative to wek-tiki and Nenad Vracar's answers would be to use the skewX() CSS transform.
This solution won't require you to hide anything that overflows the edge and therefore adds a little more flexibility.
body {
background-image: url("http://i.imgur.com/TnPgXl4.jpg");
background-size: cover;
background-repeat: no-repeat;
padding: 40px;
}
#table {
position: relative;
width: 800px;
height: 300px;
background-color: transparent;
border: solid 1px white;
}
#table:before {
content: '';
position: absolute;
left: 50%;
top: 0;
height: 100%;
width: 20px;
border-right: 1px solid #fff;
border-left: 1px solid #fff;
transform-origin: 100% 100%;
transform: skewX(-20deg);
}
<div id="table"></div>
Svg
You could use an svg element and span the svg to your div.
body {
background-color: #222;
margin: 20px;
}
.container {
width: 100%;
height: 150px;
border: 2px solid white;
}
.container svg {
width: 100%;
height: 100%;
}
<div class="container">
<svg viewBox="0 0 100 100">
<line stroke="white" x1="47" x2="57" y1="100" y2="0" />
<line stroke="white" x1="57" x2="67" y1="100" y2="0" />
</svg>
</div>