Rounded cornes (border radius) Safari issue - html

.activity_rounded {
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
-khtml-border-radius: 50%;
border: 3px solid #fff;
behavior: url(css/PIE.htc);
}
<img src="img/demo/avatar_3.jpg" class="activity_rounded" alt="" />
This is my CSS & HTML. I want to make an image look like a circle. Everything works fine in IE8+, Google Chrome, and Mozilla Firefox. But Safari is acting kinda strange. Here is a demo picture:

To illustrate the problem in Safari, let's begin with a plain image.
Here we have an image of 100px x 100px. Adding a border of 3px increases the element dimensions to 106px x 106px:
Now we give it a border radius of 20%:
You can see it starts cropping from the outer boundary of the element, not from the image itself.
Further increasing the magnitude to 50%:
And changing the border color to white:
You can now see how the issue arises.
Because of such behavior of the browser, when creating an image in a circle with a border, we have to make sure both the image and the border are given a border radius. One way to ensure this is to separate the border from the image by placing the image inside a container, and apply border radius to both of them.
<div class="activity_rounded"><img src="http://placehold.it/100" /></div>
.activity_rounded {
display: inline-block;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
-khtml-border-radius: 50%;
border: 3px solid #fff;
}
.activity_rounded img {
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
-khtml-border-radius: 50%;
vertical-align: middle;
}
And now we have a nice circle border around the image on Safari.
See DEMO.

Seems this one works:
.wrap{
-webkit-transform: translateZ(0);
-webkit-mask-image: -webkit-radial-gradient(circle, white 100%, black 100%);
}
http://jsfiddle.net/qWdf6/82/

Try this by adding overflow: hidden; to the set of rules. This is an issue with all the webkit browsers:
.activity_rounded {
-webkit-border-radius: 50%;
-khtml-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
border: 3px solid #fff;
behavior: url(css/PIE.htc);
overflow: hidden;
}

Just simply use box-shadow if you don't care the old browsers.
.rounded {
box-shadow: 0 0 0 10px pink;
}

Add the following CSS Code to the root html element:
-webkit-backface-visibility: hidden;
-webkit-transform: translate3d(0, 0, 0);

Have you tried the longhand markup?
-webkit-border-top-left-radius
-webkit-border-top-right-radius
-webkit-border-bottom-left-radius
-webkit-border-bottom-right-radius
It seems like there are some bugs on using the short-hand notation with some versions of Safari.

Simple way i did was use rounded PNG images and apply a border and radius of 50%
example :
http://www.laugfs.lk/#ourbusiness

Instead of putting the border on the image itself, put it on the container. Make sure the border-radius is on both the image and the container
.img-container {
border-radius 100%;
border: solid 1px #000;
overflow: hidden;
}
.img {
border-radius: 100%;
}

If the image's border radius is set the same as its parent div, the accepted solution works fine for circular images but not rounded rectangles because the width of the image is less than that of its parent div and the border radius needs to be scaled in proportion to the image, otherwise the image will appear more rounded than the parent div and there will be a gap between the inside edges of the parent div and the outside edges of the image.
However, if you can specify your div/image widths in absolute dimensions it's possible to set a border radius for the image so that it will fit exactly inside its parent div, by taking into account the border width.
HTML:
<div class="image-container-1"><img src="my_image.jpg" /></div>
<div class="image-container-2"><img src="my_image.jpg" /></div>
CSS:
.image-container-1 {
border: 6px solid #FF0000;
border-radius: 20px;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
height: 250px;
overflow: hidden;
width: 250px;
}
.image-container-2 {
border: 6px solid #FF0000;
border-radius: 20px;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
height: 250px;
overflow: hidden;
width: 250px;
}
.image-container-2 img {
border-radius: 14px; /* 20px border radius - 6px border */
-moz-border-radius: 14px;
-webkit-border-radius: 14px;
height: 250px;
width: 250px;
}
RESULT:
This solution was also tested in Internet Explorer 9 and Chrome 43 and the results were the same.

But if you have a border with radius on a div and inside it you have dynamic content (like if you click on that div, it slides down and show some other content), and you want to redesign your border with the same radius, you can use an aux class that redraw the radius (but the hack is that if you don't change the colour of the border the webkit will not redraw it).
Eg:
$(document).on('click', '.parent', function(e){ $('.content').toggleClass('opened').slideToggle(300);
$(this).toggleClass('refreshBorders');
});
.parent{
cursor:pointer;
text-align:center;
-webkit-border:2px solid black;
-moz-border:2px solid black;
border:2px solid black;
-webkit-border-radius:50px;
-moz-border-radius:50px;
border-radius:50px;
-webkit-background-clip:padding-box;
transition: all 0.15s ease-in-out;
}
.content{
text-align:center;
display:none;
}
.opened{
display:inline-block;
}
.refreshBorders{
-webkit-border:2px solid red;
-moz-border:2px solid red;
border:2px solid red;
-webkit-border-radius:50px;
-moz-border-radius:50px;
border-radius:50px;
-webkit-background-clip:padding-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<div class="first">
<h1> title </h1>
</div>
<div class="content">
<p> content content content</p>
</div>
</div>

do not use the position:relative|absolute style attribute for your overflow:hidden
rounded corner item
for example
<style>
.rounded_corner_style
{
background-color: #00FF00;
width: 100px;
height: 100px;
overflow: hidden;
border-radius:100px; /* you can also use border-radius:100% | border-radius:2px*/
}
</style>
<div class="rounded_corner_style">
<img src="https://i.stack.imgur.com/Kgblc.png" style="height:100%"/>
</div>

Related

CSS, height makes the border color change

Sometimes CSS acts weird in chrome, the difference between the two divs are only the height parameter, but as a result: the border color is different.
body {
background: black;
}
#div1 {
border: 1px solid white;
height: 41px;
width: 100px;
transform: rotateZ(270deg);
transform-origin: right;
}
#div2 {
border: 1px solid white;
height: 40px;
width: 100px;
transform: rotateZ(270deg);
transform-origin: right;
}
<div id="div1">
</div>
<br><br><br><br><br><br>
<div id="div2">
</div>
PS : I am working under chrome with 100% zoom
result :
This is in fact an anti aliasing issue.
The 1px border is in between two pixels, resulting in this grey looking color.
To visualize this I have taken a screenshot for you, zoomed in and put a grid over it:
Keep that in mind when transforming objects.

Border radius and overflow hidden fail when CSS filter is applied in Chrome

I have a div with rounded corners in Chrome with overflow set as hidden.
It works as expected: the child content is cut off at the corners.
However, when a filter is applied (in my case, drop shadow), the child content is no longer cut off and has square corners. This happens too with other filters, like blur.
Sample Code:
HTML:
<div class="card">
<div class="full">
This div should have rounded corners too.
</div>
</div>
CSS:
.card{
overflow: hidden;
border-radius: 10px;
/* Removes hidden corners in Chrome */
filter: drop-shadow(4px 4px 8px rgba(0,0,0,0.3));
background: gray;
width: 200px;
height: 200px;
}
.full{
background: black;
color: white;
width: 100%;
height: 100%;
}
JS Fiddle: https://jsfiddle.net/uc1v5nzk/
Firefox renders the element properly when filters are applied.
Is there any elegant fix to this on Chrome, especially when there might be many child elements that may or may not be in the corner?
Chrome Version: Version 66.0.3359.117 (Official Build) (64-bit)
OS: Ubuntu 16.04 64 bit
Adding an absolutely-positioned ::before element on wrapper seems to fix the bug.
.card::before{
content: '';
position: absolute;
}
Fiddle: https://jsfiddle.net/swordys/uc1v5nzk/2/
This is strange behavior and seems a bug in chrome.
You can use a pseudo element to create a layer and apply filter on it:
.card{
/* Critical CSS */
overflow: hidden;
border-radius: 10px;
background: gray;
position: relative;
width: 200px;
height: 200px;
}
.card::before {
filter: drop-shadow(4px 4px 8px rgba(0,0,0,0.3));
position: absoulte;
content: '';
bottom: 0;
right: 0;
left: 0;
top: 0;
}
.full{
background: black;
color: white;
width: 100%;
height: 100%;
}
<div class="card">
<div class="full">
This div should have rounded corners too.
</div>
</div>
If all you want is to add box shadow, you can replace the filter: drop-shadow attribute with box-shadow like so:
/* filter: drop-shadow(4px 4px 8px rgba(0,0,0,0.3)); */
box-shadow: 4px 4px 8px rgba(0,0,0,0.3);
In general, for shadow, I rather use box-shadow attribute due to a wide browser support.

Remove border around circular image

I have a round image (a .png file) which is transparent in the middle. I need to make the background inside the image a solid color. To do this, I made the background solid, and then put border-radius:50%, but this creates an ugly small white line. Is there anyway to get rid of this, or would I have to manually color the image in an image editor?
div {
width: 500px;
height: 500px;
background: black;
}
div img {
margin: 100px;
max-width: 50%;
background: white;
border-radius: 50%;
}
<div>
<img src="http://i.imgur.com/sDU7Lhz.png">
</div>
Fiddle here: https://jsfiddle.net/h3nwkoe1/
The problem is not with the image. The image is a transparent one and has no background to it at all. The problem is caused by the background: white and the border-radius: 50% added to the image element. It is due to the anti-aliasing pixel in browsers and is the same issue described in this thread.
The solution would be to use some method to fill the background partially to the element and not fully (that is, just enough to cover till the black circle that is already present on the image). Since the img tag cannot have pseudo-elements (atleast it won't work cross-browser), the best option is to use a radial-gradient for the background like in the below snippet.
Note: The thick green border is only for demo and can be removed without any side effect.
div {
width: 500px;
height: 500px;
background: black;
}
div img {
margin: 100px;
max-width: 50%;
background: radial-gradient(circle at center, white 60%, transparent 61%);
border-radius: 50%;
overflow: hidden;
border: 4px solid green;
}
<div>
<img src="http://i.imgur.com/sDU7Lhz.png">
</div>
I totally agree with Harry's explanation.
Another workaround could be to enclose the image in a div slightly smaller than the image (like 1px on each side), so that the circle formed using border-radius is smaller than the external black circle on the image.
It is a bit messier than the solution proposed by Harry. But it could be an alternative to gradient.
div#black {
width:500px;
height:500px;
background:black;
border: solid black 1px;
box-sizing: border-box;
}
div#circle {
margin: 100px;
width: 250px;
height: 250px;
background: white;
border-radius: 50%;
text-align: center;
}
div#circle img {
width: 252px;
height: 252px;
margin-left: -1px;
margin-top: -1px;
}
<div id="black">
<div id="circle">
<img src="http://i.imgur.com/sDU7Lhz.png">
</div>
</div>

Making harvey ball with border style css

I am trying to make a css styling for a harvey ball with an image inside, but so far I haven't figure out a way to do it right. This is what I have now:
.three {width: 43px;
border-radius: 100%;
border-style: solid;
border-width: 4px;
border-left-color: #dadad9;
border-top-color: #009ee3;
border-right-color: #009ee3;
border-bottom-color: #009ee3;
width:40px;
height:40px;
}
.lead-name {
font-size: 16px;
font-family:Symantec Sans;
color:#424242;
font-weight: 600;
margin-bottom:0px;
}
.lead-title {
font-size: 14px;
font-family:Symantec Sans;
color:#424242;
margin-top: -3px;
margin-bottom: 10px;
}
<div class="lead-designer">
<img class="three" src="http://orig09.deviantart.net/09e7/f/2008/159/0/1/side_profile_vector_by_sruphil.png"/>
<div style="display:inline-block; margin-bottom:0px; margin-top:5px;">
<p class="lead-name">Designer Name</p>
<p class="lead-title">Messaging PO</p>
</div>
</div>
https://jsfiddle.net/yiluka/dtauydrz/
What I want is something like
As you can see, I want the circle to be divided straight and have part of the image grey scaled.
I have a lot of them and I really want to do it in code instead of photoshop to save some labor.
You can also do it using the pseudo element ::after - https://jsfiddle.net/dtauydrz/3/
The HTML:
<div class="image-container">
<img class="three" src="http://orig09.deviantart.net/09e7/f/2008/159/0/1/side_profile_vector_by_sruphil.png"/>
</div>
<div style="display:inline-block; margin-bottom:0px; margin-top:5px;">
<p class="lead-name">Designer Name</p>
<p class="lead-title">Messaging PO</p>
</div>
The CSS:
.three {
border-radius: 100%;
border-left-color: #dadad9;
border-top-color: #009ee3;
border-right-color: #009ee3;
border-bottom-color: #009ee3;
width:40px;
height:40px;
border-style: solid;
border-width: 4px;
border-color: #dadad9;
}
.image-container::after{
content: "";
display:block;
position: absolute;
margin-top: -52px;
background-color: #009ee3;
-moz-border-radius: 25px 0 0 0;
border-radius: 25px 0 0 0;
width: 25px;
height: 25px;
opacity: 0.5;
}
After an hour of messing with it, I finally finished my solution.
TL;DR
JSFiddle Demo
JSFiddle Demo with a kitten(pick this one)
JSFiddle Demo with the unhappy king of all kittens(Actually this one is amazing)
This solution, after being implemented, renders this(minus, of course, the amazing hand-drawn circle):
This solution doesn't require square images, playing with the background-image placement, and is quite easy to implement.
Let's get started!
First of all, we take your nice <img> HTML element, and replace it with this monstrosity of HTML(It really isn't that bad):
<div class="image-wrapper">
<img class="main" src="http://orig09.deviantart.net/09e7/f/2008/159/0/1/side_profile_vector_by_sruphil.png">
<div class="grayscale">
<img class="gray" src="http://orig09.deviantart.net/09e7/f/2008/159/0/1/side_profile_vector_by_sruphil.png">
</div>
</div>
Now for a little explanation. We use two different image elements so we can gray-scale one of them. We do not use a background image, since this requires a massive amount of changes if you want to make the icon bigger, or the images are different sizes.
.image-wrapper is the container div, the elements inside are positioned relative to it. It's CSS is stupid simple:
.image-wrapper {
position: relative;
}
(If you can't understand that CSS, go read HTML5 and CSS3 for dummies. That's how I started with css... #destroying_my_reputation)
.main is, of course, the main image in color. It's CSS is a little mor complicated, but still very basic:
.main {
width: 100px;
border-radius: 100%;
border: 5px solid #dadad9;
}
The width can be changed to whatever you want, if you do change the width, make sure you also change the width of the .gray image. border-radius:100% makes a circle, and border obviously adds a border.
Now on to the more complicated CSS(It's all pretty simple)!
.grayscale is the div used to hold the gray-scale image. If you know CSS, you can probably tell what is happening.
.grayscale {
position: absolute;
top: 0;
left: 0;
overflow: hidden;
height: 50px;
width: 50px;
border-radius: 100% 0 0 0;
background: #009ee3;
padding-top: 5px;
padding-left: 5px;
}
The div is positioned absolute at the top-left corner of .image-wrapper. Anything overflowing it is hidden. It's top-left corner is given a border-radius of 100%, making it into the quarter-circle shape. Instead of a border, we change it's background color, and add a padding. This is because if we use a border, it is added to all sides, messing up the desired shape.
And then the .gray img:
.gray {
filter: grayscale(100%);
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
width: 100px;
border-radius: 50% 0 0 0;
}
Simple, the image is changed to gray-scale using the grayscale() CSS filter. Make sure the width is the same as .main. And a border radius to add the round effect.
That's a wrap!
And here is the long awaited demo, with all the code
I just created a div that has the shape of a quarter circle
.quarter-circle-top-left {
position: absolute;
top: 0;
left: 0;
width: 50%;
height: 50%;
background: rgba(0, 0, 0, 0.4);
border-radius: 100px 0 0 0;
-moz-border-radius: 100px 0 0 0;
-webkit-border-radius: 100px 0 0 0;
border-left: 4px solid #009ee3;
border-top: 4px solid #009ee3;
}
And absolutely positioned that div on top of your image. It's got a transparent gray background and a top and left border with your blue. Both are now contained within an wrapper div so that the quarter circle would have something to be relative to.
Here's where the quarter circle css came from: http://1stwebmagazine.com/css-quarter-circle (I changed the class names because they seemed backwards to me).
And here's the updated fiddle: https://jsfiddle.net/ingridly/dtauydrz/1/
UPDATE:
I incorporated the idea from the other answers of filling another element with the image and grayscale-ing that, and now I think this answer does everything:
https://jsfiddle.net/ingridly/dtauydrz/6/

How do I create a circle or square with just CSS - with a hollow center?

It should just basically be an outline of the square or circle - that I can style accordingly (i.e. change the color to whatever I want, change the thickness of the border, etc.)
I would like to apply that circle or square over something else (like an image or something) and the middle part should be hollowed out, so you can see the image beneath the square or circle.
I would prefer for it to be mainly CSS + HTML.
Try This
div.circle {
-moz-border-radius: 50px/50px;
-webkit-border-radius: 50px 50px;
border-radius: 50px/50px;
border: solid 21px #f00;
width: 50px;
height: 50px;
}
div.square {
border: solid 21px #f0f;
width: 50px;
height: 50px;
}
<div class="circle">
<img/>
</div>
<hr/>
<div class="square">
<img/>
</div>
More here
You can use special characters to make lots of shapes. Examples:
http://jsfiddle.net/martlark/jWh2N/2/
<table>
<tr>
<td>hollow square</td>
<td>□</td>
</tr>
<tr>
<td>solid circle</td>
<td>•</td>
</tr>
<tr>
<td>open circle</td>
<td>๐</td>
</tr>
</table>
Many more can be found here: HTML Special Characters
i don't know of a simple css(2.1 standard)-only solution for circles, but for squares you can do easily:
.squared {
border: 2px solid black;
}
then, use the following html code:
<img src="…" alt="an image " class="squared" />
If you want your div to keep it's circular shape even if you change its width/height (using js for instance) set the radius to 50%. Example:
css:
.circle {
border-radius: 50%/50%;
width: 50px;
height: 50px;
background: black;
}
html:
<div class="circle"></div>
Circle Time! :) Easy way of making a circle with a hollow center : use border-radius, give the element a border and no background so you can see through it :
div {
display: inline-block;
margin-left: 5px;
height: 100px;
border-radius: 100%;
width:100px;
border:solid black 2px;
}
body{
background:url('http://lorempixel.com/output/people-q-c-640-480-1.jpg');
background-size:cover;
}
<div></div>
To my knowledge there is no cross-browser compatible way to make a circle with CSS & HTML only.
For the square I guess you could make a div with a border and a z-index higher than what you are putting it over. I don't understand why you would need to do this, when you could just put a border on the image or "something" itself.
If anyone else knows how to make a circle that is cross browser compatible with CSS & HTML only, I would love to hear about it!
#Caspar Kleijne border-radius does not work in IE8 or below, not sure about 9.
Shortly after finding this questions I found these examples on CSS Tricks: http://css-tricks.com/examples/ShapesOfCSS/
Copied so you don't have to click
.square {
width: 100px;
height: 100px;
background: red;
}
.circle {
width: 100px;
height: 100px;
background: red;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
}
/* Cleaner, but slightly less support: use "50%" as value */
<div class="square"></div>
<div class="circle"></div>
There are many other shape examples in the above link, but you will have to test for browser compatibility.
In case of circle all you need is one div, but in case of hollow square you need to have 2 divs.
The divs are having a display of inline-block which you can change accordingly. Live Codepen link: Click Me
In case of circle all you need to change is the border properties and the dimensions(width and height) of circle. If you want to change color just change the border color of hollow-circle.
In case of the square background-color property needs to be changed depending upon the background of page or the element upon which you want to place the hollow-square. Always keep the inner-circle dimension small as compared to the hollow-square. If you want to change color just change the background-color of hollow-square. The inner-circle is centered upon the hollow-square using the position, top, left, transform properties just don't mess with them.
Code is as follows:
/* CSS Code */
.hollow-circle {
width: 4rem;
height: 4rem;
background-color: transparent;
border-radius: 50%;
display: inline-block;
/* Use this */
border-color: black;
border-width: 5px;
border-style: solid;
/* or */
/* Shorthand Property */
/* border: 5px solid #000; */
}
.hollow-square {
position: relative;
width: 4rem;
height: 4rem;
display: inline-block;
background-color: black;
}
.inner-circle {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 3rem;
height: 3rem;
border-radius: 50%;
background-color: white;
}
<!-- HTML Code -->
<div class="hollow-circle">
</div>
<br/><br/><br/>
<div class="hollow-square">
<div class="inner-circle"></div>
</div>