简体   繁体   English

为什么我不能用 border-radius 做一个圆圈?

[英]Why can't I make a circle with border-radius?

 * { margin: 0; } div { background-color: green; height: 900px; width: 50%; margin: auto; border-radius: 50px; overflow: hidden; padding: 20px; border: 4px solid red; box-sizing: border-box; } div:hover { box-shadow: 5px 5px 5px 1px blue; } @media screen and (max-width:600px) { div { background-color: aqua; color: red; width: 100%; height: 10%; } body { background-color: chocolate } }.divas { background-color: yellow; position: relative; margin-top: 20%; height: 300px; border-radius: 50%; }
 <div>This is text. This is text. This is text. This is text. This is text. This is text. This is text. This is text. This is text. This is text. This is text. This is text. </div> <div class="divas"></div>

Hello I would like to make a perfectly rounded circle.你好,我想做一个完美的圆形。 I tried creating it with border-radius, but it doesn't allow me to create a perfect circle.我尝试用 border-radius 创建它,但它不允许我创建一个完美的圆。 Can someone explain me why?有人可以向我解释为什么吗? I tried changing padding and such but it doesn't work anyways.?我尝试更改填充等,但它仍然不起作用。? Thanks in advance.提前致谢。

If you want to create circle with border-radius then height and width should be same for the div which you are applying border-radius如果要创建带有边框半径的圆,则应用边框半径的 div 的高度和宽度应相同

Then only border-radius will look like circle那么只有边界半径看起来像圆

Example例子

 #circle{ width: 100px; height: 100px; border-radius: 50%; background: #000; }
 <div id="circle"></div>

For a perfect circle you need an element with the same height and width.对于完美的圆,您需要一个具有相同高度和宽度的元素。 You also only have a border-radius:50px instead of border-radius:50% defined.您也只定义了border-radius:50px而不是border-radius:50%

 * { margin: 0; } div { background-color: green; height: 500px; width: 500px; margin: auto; border-radius: 50%; overflow: hidden; padding: 25px 28px 28px; text-align: center; border: 4px solid red; box-sizing: border-box; display: flex; align-items: center; font-size: 60px; } div:hover { box-shadow: 5px 5px 5px 1px blue; } @media screen and (max-width:600px) { div { background-color: aqua; color: red; width: 100%; height: 10%; } body { background-color: chocolate } } .divas { background-color: yellow; position: relative; margin-top: 20%; height: 300px; width: 300px; border-radius: 50%; }
 <div> This is text.<br> This is text. This is text. This is text. This is text. This is text. </div> <div class="divas"></div>

Div with same height and width具有相同高度和宽度的 Div

border-radius is half the width gives a circle.边界半径是宽度的一半给出一个圆。

 .mycircle { background-color: green; height: 200px; width: 200px; border-radius: 100px; }
 <div class="mycircle"></div>

For Creating a circle in css object should have same width and height add width:300px;对于在 css 对象中创建一个圆应该具有相同的宽度和高度添加 width:300px; to .divas class.到 .divas 类。

您需要使用border-radius: 100%并使您的高度和宽度相等。

In order to make a perfect circle, you need to set width and height equal.为了制作一个完美的圆形,您需要将宽度和高度设置为相等。 If you do so by CSS you need to define them both in 'px', rather than '%' since the window width & height may vary from device to device.如果您通过 CSS 这样做,您需要在 'px' 而不是 '%' 中定义它们,因为窗口宽度和高度可能因设备而异。 Now set border-radius as 50%.现在将边界半径设置为 50%。 Below is the CSS:下面是CSS:

 div { background-color: green; height: 900px; width: 900px; margin: auto; border-radius: 50%; overflow: hidden; padding: 20px; border: 4px solid red; box-sizing: border-box; }

In case your with is dependent on the window's width (as you have used %), you can set height equal to width using javascript.如果您的 with 依赖于窗口的宽度(如您使用的 %),您可以使用 javascript 将高度设置为等于宽度。 Here is how to do it.这是如何做到的。

HTML HTML

 <div></div>

JavaScript JavaScript

 var requiredWidth = window.innerWidth * (0.5); //window width excluding scrollbar. var div = document.querySelector('div'); div.style.width = requiredWidth + 'px'; div.style.height = requiredWidth + 'px';

Now that you have defined height and width using javascript, set border-radius to 50% in your CSS.现在您已经使用 javascript 定义了高度和宽度,在您的 CSS 中将 border-radius 设置为 50%。

CSS CSS

 div { border-radius: 50%; background-color: red; }

You want to create a circle with border-radius then height and width should be the same for the element which you are applying border-radius:100%您想创建一个带边框半径的圆,那么您应用border-radius:100%的元素的高度和宽度应该相同border-radius:100%

 * { margin: 0; } div { background-color: green; height: 600px; width: 600px; margin: auto; border-radius: 100%; overflow: hidden; padding: 90px; border: 4px solid red; box-sizing: border-box; } div:hover { box-shadow: 5px 5px 5px 1px blue; } @media screen and (max-width:600px) { div { background-color: aqua; color: red; width: 100%; height: 10%; } body { background-color: chocolate } } .divas { background-color: yellow; position: relative; margin-top: 20%; height: 300px; width: 300px; border-radius: 100%; }
 <div>This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . This is text . </div> <div class="divas"></div>

Use the aspect-ratio CSS' property.使用aspect-ratio CSS 的属性。 Compatible with all major browsers...兼容所有主流浏览器...

 .fixed-units { width: 150px; aspect-ratio: 1 / 1; border: 1px solid #000; background-color: #bada55; border-radius: 50%; }.dynamic-units { width: 50%; aspect-ratio: 1 / 1; border: 1px solid #000; background-color: #daba55; border-radius: 50%; }
 <label>For Fixed Units</label> <div class="fixed-units"></div> <br> <label>For Dynamic Units</label> <div class="dynamic-units"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM