Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capsule shape using border-radius without a set width or height?

Tags:

css

css-shapes

Is it possible to make a capsule shape using border-radius without a set width or height?

I want the left and right sides to be completely rounded while the capsule would remain straight along it's horizontal length. Setting the radius to 50% doesn't seem to give the desired affect.

Capsule Shape

like image 703
christian Avatar asked Sep 13 '13 20:09

christian


People also ask

When would you use a border radius?

The border-radius CSS property rounds the corners of an element's outer border edge. You can set a single radius to make circular corners, or two radii to make elliptical corners.

What is the difference between border and border radius?

The only difference between them is the right button has a radius of 5px applied. As with borders, radius allows you to set different properties for each side of the element using the toggle controls.

Can I use border top right radius?

The border-top-right-radius property is used to round the top right corner of an element. The property takes in one or two values that define the radii of a quarter ellipse that defines the shape of the corner of the outer border edge (see the diagram below).


Video Answer


2 Answers

Applying a very large border radius seems to work on many browsers (IE9+, FF, Chrome) like this mod of David's fiddle http://jsfiddle.net/cthQW/1/

border-radius: 500px; 
like image 129
Jeremy Cook Avatar answered Oct 13 '22 06:10

Jeremy Cook


Yes, this is possible (albeit I've only tested in Chromium 28/Ubuntu 12.10):

div {     /* this is the only relevant part: */     border-radius: 20%/50%;      /* this is irrelevant, and just so the element can be visualised/displayed: */     width: 50%;     height: 5em;     margin: 2em auto;     background-color: #000; } 

JS Fiddle demo.

The important information is, obviously, the 20%/50% property-value; the 20% is the 'horizontal length' of the radius, whereas the 50% is the 'vertical length'; using two different measurements gives an elliptical curve to the border, instead of a single measurement, which yields the more circular radius. Obviously this requires a certain amount of adjustment to your own requirements

References:

  • border-radius (at Mozilla Developer Network).
  • CSS Backgrounds and Borders Module Level 3: 5.1. Curve Radii: the ‘border-radius’ properties.
like image 42
David Thomas Avatar answered Oct 13 '22 05:10

David Thomas