How to give a div oval shape?

梦想的初衷 提交于 2019-11-26 17:01:10

问题


I tried a lot on oval shape which have cut in both sides but not able to do it please

I need code for oval shape with cut in both side..

Here's my code below:-

.demo{
    width: 100%;
    height: 600px;
    background: white;
    -moz-border-radius: 100px / 50px;
    -webkit-border-radius: 100px / 178px;
    border-radius: 694px / 208px;

    z-index: 100;
    position: relative;
    }

回答1:


Is this OK ?

HTML

<div id="oval_parent">
    <div id="oval"></div>
</div>

CSS

#oval_parent{
    background:black;
    width:200px;
    height:120px;
    overflow:hidden;
}

#oval{
    width: 220px;
    height: 100px;
    margin:10px 0 0 -10px;  
    background: white;
    -moz-border-radius: 100px / 50px;
    -webkit-border-radius: 100px / 50px;
    border-radius: 100px / 50px;
}

DEMO.




回答2:


Try this:

#oval-shape {
    width: 200px;
    height: 100px;
    background: blue;
    -moz-border-radius: 100px / 50px;
    -webkit-border-radius: 100px / 50px;
    border-radius: 100px / 50px;
}

Notice the ratios in the corner values in relation to the height.

Demo - http://jsfiddle.net/XDLVx/




回答3:


Change the values on css:

#demo {
    width: 100%;
    height: 600px;
    background: white;
    -moz-border-radius: 50% / 250px;
    -webkit-border-radius: 40% / 250px;
    border-radius: 50% / 250px;

    z-index: 100;
    position: relative;
}



回答4:


Put it inside another div which is high enough to show all the oval, not quite wide enough, and set overflow: hidden. If it's positioned at the centre the edges will be cut off, but you won't be able to side-scroll.




回答5:


Here are two possible variants:

Method #01:

Use radial-gradient():

background: radial-gradient(ellipse 65% 40%, transparent 0, transparent 90%, black 90%);

body {
  background: linear-gradient(orange, red);
  padding: 0 20px;
  margin: 0;
}
.oval {
  background: radial-gradient(ellipse 65% 40%, transparent 0, transparent 90%, black 90%);
  height: 100vh;
}
<div class="oval">

</div>

Method #02:

  1. Create an overlay with :before or :after pseudo element.
  2. Add border-radius.
  3. Apply a large box-shadow with overflow: hidden on parent to hide undesired area.

body {
  background: linear-gradient(orange, red);
  padding: 0 20px;
  margin: 0;
}
.oval {
  position: relative;
  overflow: hidden;
  height: 100vh;
}

.oval:before {
  box-shadow: 0 0 0 500px #000;
  border-radius: 100%;
  position: absolute;
  content: '';
  right: -10%;
  left: -10%;
  top: 10%;
  bottom: 10%;
}
<div class="oval">

</div>


来源:https://stackoverflow.com/questions/12972145/how-to-give-a-div-oval-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!