1

enter image description here

Can I achieve this with css only?

50% does not work on rectangle

1 more thing: no fixed height.

11
  • You can set a min-height to the element if you don't need to fix the height. Commented Mar 27, 2015 at 8:21
  • This is how I would do it: jsfiddle.net/lharby/5sanz08e and don't forget the vendor prefixes. Commented Mar 27, 2015 at 8:55
  • @lharby It fails when the content of the box is large: jsfiddle.net/5sanz08e/2 — Besides, nowadays using vendor prefixes is not necessary for border-radius. Commented Mar 27, 2015 at 9:00
  • This related question might help stackoverflow.com/questions/29048234/… althought it is for a fluid with rather than fluid height. Commented Mar 27, 2015 at 9:03
  • Is it specified in the question that there will be text content inside the div? Commented Mar 27, 2015 at 9:04

5 Answers 5

5

One option is to set a fixed value for horizontal border-radius as follows:

div {
  display: inline-block;
  border-radius: 50px / 50%;
  
  padding: 1em 2em;
  border: 2px solid #000;
  background-color: #009EE0;
  text-align: center; 
}
<div>
  Stuff goes <br>
  here... <br>
  & here... <br>
  Setting a fixed value of horizontal border-radius does the trick!
</div>

You may also want to use a different percentage value instead:

div {
  display: inline-block;
  border-radius: 12% / 50%;

  padding: 1em 2em;
  border: 2px solid #000;
  background-color: #009EE0;
  text-align: center; 
}
<div>
  Stuff goes <br>
  here... <br>
  & here... <br>
  Or set a different percentage value for horizontal border-radius
</div>

Sign up to request clarification or add additional context in comments.

Comments

2

Try this

div{
    width:200px;
    height:100px;
    background:lightblue;
    border-radius:50px;
  border:2px solid black
}
<div></div>

Edit: use 50vw for making it responsive any height will be okay

div{
    width:500px;
    height:300px;
    background:orange;
    border-radius:50vw;
}
<div></div>

8 Comments

Can I achieve this on a rectangle without a fixed height.
@Timeless No fixed height??
@Timeless Do you have text in it?
@Timeless the css he posted is not wrong in the example there is a fixed height, but you could just as good use something as height:auto;. Since the question was about the rounded corners and all that awnsers that would be border-radius:50px;. For more information about it please take a look here.
@maam27 Check this fiddle i used 50px but it doesn't give a nice effect so i used 50vw
|
1

There is a trick for that

Just us a very large border-radius value!

e.g. {border-radius:10000px;}

Here is link to demo (Try changing width/height to see working) : http://jsfiddle.net/890z699p/

.rect {
  width: 300px;
  height: 200px;
  background: #000;
  border-radius: 10000px;
}
<div class="rect"></div>

2 Comments

This will work, the problem is that your fiddle uses percentages for border-radius instead of a hight px value as you show in your answer. Also, please add the code of your example in the question itself.
@web-tiki Thanks for pointing out mistake! I didn't updated the jsFiddle :D, It is fine now!
0

It works on me, below is my code snippet

div {
  display: block;
  border-radius: 50%;
  width: 200px;
  height: 100px;
  border: 2px solid #000;
  background-color: #009EE0;
}
<div>
</div>

1 Comment

You may want to double check your final result, the OP wants a rectangle with rounded corners, this code produces an oval.
0

#rec{
  height: 15vh;
  width: 20vw;
  background-color: #009EE0;
  border: 2px solid #16100E;
  border-radius: 5vw;
}
      <div id='rec'/>

just use the border-radius which suits your design

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.