Circle a div in html and css

1.4k views Asked by At

I want to circle a div that contains a live preview of the webcam using webcam.js. I have tried to make it into a circle, but only the sides became round.

This is the html for the webcam div:

<div id="camera" class="camera" ></div>

This is the css code:

body
{
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
.camera
{
  width: 450px;
  height: 450px;
  border-radius: 50%;
  overflow: hidden;
  transform: rotateY(180deg);
}

This is my js code:

Webcam.set({

            dest_width: 600,

            dest_height: 600,

            image_format: 'jpeg',

            jpeg_quality: 90

    });

Webcam.attach( '#camera' );
4

There are 4 answers

10
Codeneutrino On

If you set border-radius to 100%, you should get a circle (provided height and width are the same values. As for the programming etiquette in a situation like this, I don't know…

2
Ruhul Amin On

You can use border-radius: 100%; to make the div circle.

.camera
{
  width: 450px;
  height: 450px;
  border-radius: 100%;
  overflow: hidden;
  transform: rotateY(180deg);
}

Updated :

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
body
{
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
.camera
{
  width: 450px;
  height: 450px;
  border-radius: 100%;
  overflow: hidden;
  background:red;
}

</style>
</head>
<body>
<div id="camera" class="camera" ></div>

</body>
</html>

Output :

enter image description here

0
svin83 On

I'm guessing your webcam code scales to fit it's parent element...

  1. Put #camera inside another div.
  2. Both should have equal height and width.
  3. Both should hide overflow.
  4. Only the outer div should be circular. Not the one passed Webcam.attach()
3
Joel On

From the screenshot you posted, there seems to be no issue with the CSS but rather with the webcam feed aspect ratio, try adapting the values of webcamJS with relative values:

CSS - Change the border to 100%

body
{
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
.camera
{
  width: 450px;
  height: 450px;
  border-radius: 100%;
  overflow: hidden;
  transform: rotateY(180deg);
}

JS - Set the height to a relative value

Webcam.set({

            dest_width: 600,

            dest_height: 600/1.33500,

            image_format: 'jpeg',

            jpeg_quality: 90

    });

Webcam.attach( '#camera' );

You can check it on codepen https://codepen.io/godpers/pen/ExZQpEQ