如何用 css 画出一个椭圆?

.div {
  width: 100px;
  height: 50px;
  border-radius: 25px;
}

不好看。。。

阅读 30.6k
6 个回答

我之前给公司做的一个网站有类似的代码,截图是这样子的:
图片描述

我把它整理了一下,代码如下:
css画椭圆

<div class="container introduce">
    <div class="introduce_zi">
      <div class="introduce_sun"></div> 
    </div>
</div><!--introduce-->

  /*introduce*/
.introduce{max-width:700px; height:260px; background-color:#62a97b; border-radius:50%; overflow:hidden; position:relative; z-index:2;}
.introduce .introduce_zi{max-width:640px; height:200px; background-color:#26699e; border-radius:50%; margin:0 auto; position:relative; top:-25px;}
.introduce .introduce_zi .introduce_sun{max-width:460px; height:140px; background-color:#593568; border-radius:50%; margin:0 auto; position:relative; top:-25px;}

比较全的,用html5的canvas画的 stackoverflow

<script>
var canvas = document.getElementById('thecanvas');

if(canvas.getContext) 
{
  var ctx = canvas.getContext('2d');
  drawEllipse(ctx, 10, 10, 100, 60);
  drawEllipseByCenter(ctx, 60,40,20,10);
}

function drawEllipseByCenter(ctx, cx, cy, w, h) {
  drawEllipse(ctx, cx - w/2.0, cy - h/2.0, w, h);
}

function drawEllipse(ctx, x, y, w, h) {
  var kappa = .5522848,
      ox = (w / 2) * kappa, // control point offset horizontal
      oy = (h / 2) * kappa, // control point offset vertical
      xe = x + w,           // x-end
      ye = y + h,           // y-end
      xm = x + w / 2,       // x-middle
      ym = y + h / 2;       // y-middle

  ctx.beginPath();
  ctx.moveTo(x, ym);
  ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
  ctx.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
  ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
  ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
  //ctx.closePath(); // not used correctly, see comments (use to close off open path)
  ctx.stroke();
}

</script>

1楼的答案.......

svg也是一个比较好的方案

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题