How do I create a circle or square with just CSS - with a hollow center?

后端 未结 8 2502
南旧
南旧 2020-12-13 08:47

It should just basically be an outline of the square or circle - that I can style accordingly (i.e. change the color to whatever I want, change the thickness of the border,

相关标签:
8条回答
  • 2020-12-13 09:04

    In case of circle all you need is one div, but in case of hollow square you need to have 2 divs. The divs are having a display of inline-block which you can change accordingly. Live Codepen link: Click Me

    In case of circle all you need to change is the border properties and the dimensions(width and height) of circle. If you want to change color just change the border color of hollow-circle.

    In case of the square background-color property needs to be changed depending upon the background of page or the element upon which you want to place the hollow-square. Always keep the inner-circle dimension small as compared to the hollow-square. If you want to change color just change the background-color of hollow-square. The inner-circle is centered upon the hollow-square using the position, top, left, transform properties just don't mess with them.

    Code is as follows:

    /* CSS Code */
    
    .hollow-circle {
      width: 4rem;
      height: 4rem;
      background-color: transparent;
      border-radius: 50%;
      display: inline-block;
      
      /* Use this */
      border-color: black;
      border-width: 5px;
      border-style: solid;
      /* or */
      /* Shorthand Property */
      /* border: 5px solid #000; */
    }
    
    .hollow-square {
      position: relative;
      width: 4rem;
      height: 4rem;
      display: inline-block;
      background-color: black;
    }
    
    .inner-circle {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 3rem;
      height: 3rem;
      border-radius: 50%;
      background-color: white;
    }
    <!-- HTML Code -->
    
    <div class="hollow-circle">
    </div>
    
    <br/><br/><br/>
    
    <div class="hollow-square">
      <div class="inner-circle"></div>
    </div>

    0 讨论(0)
  • 2020-12-13 09:09

    Circle Time! :) Easy way of making a circle with a hollow center : use border-radius, give the element a border and no background so you can see through it :

    div {
        display: inline-block;
        margin-left: 5px;
        height: 100px;
        border-radius: 100%;
        width:100px;
        border:solid black 2px;
    }
    
    body{
        background:url('http://lorempixel.com/output/people-q-c-640-480-1.jpg');
        background-size:cover;
    }
    <div></div>

    0 讨论(0)
  • 2020-12-13 09:12

    Try This

    div.circle {
      -moz-border-radius: 50px/50px;
      -webkit-border-radius: 50px 50px;
      border-radius: 50px/50px;
      border: solid 21px #f00;
      width: 50px;
      height: 50px;
    }
    
    div.square {
      border: solid 21px #f0f;
      width: 50px;
      height: 50px;
    }
    <div class="circle">
      <img/>
    </div>
     <hr/>
    <div class="square">
      <img/>
    </div>

    More here

    0 讨论(0)
  • 2020-12-13 09:15

    You can use special characters to make lots of shapes. Examples: http://jsfiddle.net/martlark/jWh2N/2/

    <table>
      <tr>
        <td>hollow square</td>
        <td>&#9633;</td>
      </tr>
      <tr>
        <td>solid circle</td>
        <td>&bull;</td>
      </tr>
      <tr>
        <td>open circle</td>
        <td>&#3664;</td>
      </tr>
    
    </table>

    enter image description here

    Many more can be found here: HTML Special Characters

    0 讨论(0)
  • 2020-12-13 09:21

    To my knowledge there is no cross-browser compatible way to make a circle with CSS & HTML only.

    For the square I guess you could make a div with a border and a z-index higher than what you are putting it over. I don't understand why you would need to do this, when you could just put a border on the image or "something" itself.

    If anyone else knows how to make a circle that is cross browser compatible with CSS & HTML only, I would love to hear about it!

    @Caspar Kleijne border-radius does not work in IE8 or below, not sure about 9.

    0 讨论(0)
  • 2020-12-13 09:22

    Shortly after finding this questions I found these examples on CSS Tricks: http://css-tricks.com/examples/ShapesOfCSS/

    Copied so you don't have to click

    .square {
      width: 100px;
      height: 100px;
      background: red;
    }
    .circle {
      width: 100px;
      height: 100px;
      background: red;
      -moz-border-radius: 50px;
      -webkit-border-radius: 50px;
      border-radius: 50px;
    }
    /* Cleaner, but slightly less support: use "50%" as value */
    <div class="square"></div>
    <div class="circle"></div>

    There are many other shape examples in the above link, but you will have to test for browser compatibility.

    0 讨论(0)
提交回复
热议问题