【发布时间】:2021-02-10 12:55:30
【问题描述】:
如何使用 HTML5 和 CSS3 绘制圆?
里面也可以放文字吗?
【问题讨论】:
标签: html css geometry css-shapes
如何使用 HTML5 和 CSS3 绘制圆?
里面也可以放文字吗?
【问题讨论】:
标签: html css geometry css-shapes
你不能画一个圆本身。但你可以制作与圆形相同的东西。
您必须创建一个圆角矩形(通过border-radius),其宽度/高度是您要制作的圆的一半。
#circle {
width: 50px;
height: 50px;
-webkit-border-radius: 25px;
-moz-border-radius: 25px;
border-radius: 25px;
background: red;
}
<div id="circle"></div>
【讨论】:
border-radius: 50%; 效果很好,可以根据需要更改大小。对于颜色,您可以使用background-color 或border。
这在 HTML 5 中是完全可能的。您的选择是:Embedded SVG 和 <canvas> tag。
在嵌入式 SVG 中画圆:
<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="50" fill="red" />
</svg>
圈在<canvas>:
var canvas = document.getElementById("circlecanvas");
var context = canvas.getContext("2d");
context.arc(50, 50, 50, 0, Math.PI * 2, false);
context.fillStyle = "red";
context.fill()
<canvas id="circlecanvas" width="100" height="100"></canvas>
【讨论】:
您可以使用一些 unicode 圆圈:
* { font-size: 50px; }
○
◌
◍
◎
●
更多形状here.
如果您愿意,可以在圆圈上叠加文字:
#container {
position: relative;
}
#circle {
font-size: 50px;
color: #58f;
}
#text {
z-index: 1;
position: absolute;
top: 21px;
left: 11px;
}
<div id="container">
<div id="circle">●</div>
<div id="text">a</div>
</div>
如果您希望它在不同系统上看起来相同的可能性更高,您也可以使用自定义字体(如this),因为并非所有计算机/浏览器都安装了相同的字体。
【讨论】:
border-radius:50% 如果您希望圆圈调整到容器的任何尺寸(例如,如果文本是可变长度的)
不要忘记 (前缀不再需要)-moz- 和 -webkit- 前缀!
div{
border-radius: 50%;
display: inline-block;
background: lightgreen;
}
.a{
padding: 50px;
}
.b{
width: 100px;
height: 100px;
}
<div class='a'></div>
<div class='b'></div>
【讨论】:
截至 2015 年,您可以使用少至 15 行 CSS (Fiddle) 将文本居中:
body {
background-color: #fff;
}
#circle {
position: relative;
background-color: #09f;
margin: 20px auto;
width: 400px;
height: 400px;
border-radius: 200px;
}
#text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>circle with text</title>
</head>
<body>
<div id="circle">
<div id="text">Text in the circle</div>
</div>
</body>
</html>
没有任何-webkit-s,它适用于 IE11、Firefox、Chrome 和 Opera,并且是有效的 HTML5(实验性)和 CSS3。
在 MS Edge (2020) 上也是如此。
【讨论】:
.circle{
height: 65px;
width: 65px;
border-radius: 50%;
border:1px solid red;
line-height: 65px;
text-align: center;
}
<div class="circle"><span>text</span></div>
【讨论】:
border-radius: 50%; 会将所有元素变成一个圆圈,无论大小。至少,只要目标的height和width相同,否则会变成椭圆形。
#target{
width: 100px;
height: 100px;
background-color: #aaa;
border-radius: 50%;
}
<div id="target"></div>
注意:browser prefixes are not needed anymore for border-radius
或者,您也可以使用clip-path: circle(); 将元素变成圆形。即使元素的width 比height 大(或相反),它仍然会变成圆形,而不是椭圆形。
#target{
width: 200px;
height: 100px;
background-color: #aaa;
clip-path: circle();
}
<div id="target"></div>
注意:clip-path is not (yet) supported by all browsers
您可以在圆圈内放置文字,只需将文字写在目标标签内即可,
如下:
<div>text</div>
如果您想在圆圈中居中文本,您可以执行以下操作:
#target{
width: 100px;
height: 100px;
background-color: #aaa;
border-radius: 50%;
display: flex;
align-items: center;
}
#text{
width: 100%;
text-align: center;
}
<div id="target">
<div id="text">text</div>
</div>
【讨论】:
您可以使用border-radius 属性为其赋予一个与元素的border-radius 等效的border-radius。例如:
<div style="border-radius 10px; -moz-border-radius 10px; -webkit-border-radius 10px; width: 20px; height: 20px; background: red; border: solid black 1px;"> </div>
(使用 -moz 和 -webkit 扩展的原因是为了支持 Gecko 和 Webkit 的 CSS3 最终版本。)
this page 上有更多示例。至于插入文本,你可以这样做,但你必须注意定位,因为大多数浏览器的框填充模型仍然使用外部正方形。
【讨论】:
技术上没有办法用 HTML 画圆(没有<circle> HTML 标签),但可以画圆。
绘制一个的最佳方法是将border-radius: 50% 添加到诸如div 之类的标签中。这是一个例子:
<div style="width: 50px; height: 50px; border-radius: 50%;">You can put text in here.....</div>
【讨论】:
你可以使用border-radius属性,或者制作一个高度和宽度固定的div,背景为png圆形。
【讨论】:
以下是我的 9 个解决方案。随意在 div 或 svg 元素中插入文本。
var c = document.getElementById('myCanvas');
var ctx = c.getContext('2d');
ctx.beginPath();
ctx.arc(50, 50, 50, 0, 2 * Math.PI);
ctx.fillStyle = '#B90136';
ctx.fill();
#circle1 {
background-color: #B90136;
width: 100px;
height: 100px;
border-radius: 50px;
}
#circle2 {
background-color: #B90136;
width: 100px;
height: 100px;
clip-path: circle();
}
#circle3 {
color: #B90136;
font-size: 100px;
line-height: 100px;
}
#circle4::before {
content: "";
display: block;
width: 100px;
height: 100px;
border-radius: 50px;
background-color: #B90136;
}
#circle5 {
background-image: radial-gradient(#B90136 70%, transparent 30%);
height: 100px;
width: 100px;
}
<h3>1 border-radius</h3>
<div id="circle1"></div>
<hr/>
<h3>2 clip-path</h3>
<div id="circle2"></div>
<hr/>
<h3>3 html entity</h3>
<div id="circle3">⬤</div>
<hr/>
<h3>4 pseudo element</h3>
<div id="circle4"></div>
<hr/>
<h3>5 radial-gradient</h3>
<div id="circle5"></div>
<hr/>
<h3>6 svg circle & path</h3>
<svg width="100" height="100">
<circle cx="50" cy="50" r="50" fill="#B90136" />
</svg>
<hr/>
<h3>7 canvas arc()</h3>
<canvas id="myCanvas" width="100" height="100"></canvas>
<hr/>
<h3>8 img tag</h3>
<img src="circle.png" width="100" height="100" />
<hr/>
<h3>9 pre tag</h3>
<pre style="line-height:8px;">
+++
+++++
+++++++
+++++++++
+++++++++++
+++++++++++
+++++++++++
+++++++++
+++++++
+++++
+++
</pre>
【讨论】:
只需在脚本标签中执行以下操作:
<!Doctype html>
<html>
<head>
<title>Circle Canvas</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid
#d3d3d3;">
<body>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.stroke();
</script>
</body>
</body>
</html>
然后你就得到了你的圈子。
【讨论】:
h1 {
border: dashed 2px blue;
width: 200px;
height: 200px;
border-radius: 100px;
text-align: center;
line-height: 60px;
}
<h1> <br>hello world</h1>
【讨论】:
.at-counter-box {
border: 2px solid #1ac6ff;
width: 150px;
height: 150px;
border-radius: 100px;
font-family: 'Oswald Sans', sans-serif;
color:#000;
}
.at-counter-box-content {
position: relative;
}
.at-counter-content span {
font-size: 40px;
font-weight: bold ;
text-align: center;
position: relative;
top: 55px;
}
【讨论】:
<head>
<style>
#circle{
width:200px;
height:200px;
border-radius:100px;
background-color:red;
}
</style>
</head>
<body>
<div id="circle"></div>
</body>
简单和新手:)
【讨论】:
这是我用于 CS 1.6 统计网站的圆圈。 一个漂亮的四色圆圈。
#circle {
border-top: 8px ridge #d11967;
border-right: 8px ridge #d32997;
border-bottom: 8px ridge #5246eb;
border-left: 8px ridge #fc2938;
border-radius: 50%; width: 440px; height: 440px;
}
<div id="circle"></div>
您还可以使用 skewY()、skewX() 和 rotate() 进行旋转和倾斜:
transform: rotate(60deg);
transform: skewY(-5deg);
transform: skewX(-15deg);
【讨论】:
<div class="at-counter-box-content">
<div class="at-counter-content">
<span>40%</span>
</div><!--at-counter-content-->
</div><!--at-counter-box-content-->
【讨论】:
如果您使用 sass 编写 CSS,您可以这样做:
@mixin draw_circle($radius){
width: $radius*2;
height: $radius*2;
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}
.my-circle {
@include draw_circle(25px);
background-color: red;
}
哪些输出:
.my-circle {
width: 50px;
height: 50px;
-webkit-border-radius: 25px;
-moz-border-radius: 25px;
border-radius: 25px;
background-color: red;
}
【讨论】:
.circle {
background: green;
border-radius: 50%;
width: 1rem;
aspect-ratio: 1/1;
}
<div class="circle"></div>
浏览器支持:
【讨论】: