X close button only using css

后端 未结 8 1499
感情败类
感情败类 2020-12-25 10:21

How to make a cross (X) only in css3, to use as a close button ?

I\'ve been searching since long time, i cannot found how.... When i look at source code on website u

相关标签:
8条回答
  • 2020-12-25 10:47

    <div style="width: 10px; height: 10px; position: relative; display: flex; justify-content: center;">
       <div style="width: 1.5px; height: 100%; background-color: #9c9f9c; position: absolute; transform: rotate(45deg); border-radius: 2px;"></div>
       <div style="width: 1.5px; height: 100%; background-color: #9c9f9c; position: absolute; transform: rotate(-45deg); border-radius: 2px;"></div>
    </div>

    0 讨论(0)
  • 2020-12-25 10:55

    You can use svg.

    <svg viewPort="0 0 12 12" version="1.1"
         xmlns="http://www.w3.org/2000/svg">
        <line x1="1" y1="11" 
              x2="11" y2="1" 
              stroke="black" 
              stroke-width="2"/>
        <line x1="1" y1="1" 
              x2="11" y2="11" 
              stroke="black" 
              stroke-width="2"/>
    </svg>

    0 讨论(0)
  • 2020-12-25 10:55

    True CSS with proper semantic and accessibility settings.

    It is a <button>, It has text for screen readers.

    https://codepen.io/specialweb/pen/ExyWPYv?editors=1100

    button {
      width: 2rem;
      height: 2rem;
      padding: 0;
      position: absolute;
      top: 1rem;
      right: 1rem;
      cursor: pointer;
    }
    
    .sr-only {
      position: absolute;
      width: 1px;
      height: 1px;
      padding: 0;
      margin: -1px;
      overflow: hidden;
      clip: rect(0,0,0,0);
      border: 0;
    }
    
    button::before,
    button::after {
        content: '';
        width: 1px;
        height: 100%;
        background: #333;
        display: block;
        transform: rotate(45deg) translateX(0px);
        position: absolute;
        left: 50%;
        top: 0;
    }
    
    button::after {
        transform: rotate(-45deg) translateX(0px);
    }
    
    
    /* demo */
    body {
      background: black;
    }
    .pane {
      margin: 0 auto;
      width: 50vw;
      min-height: 50vh;
      background: #FFF;
      position: relative;
      border-radius: 5px;
    }
    <div class="pane">
      <button type="button"><span class="sr-only">Close</span></button>
    </div>

    0 讨论(0)
  • 2020-12-25 10:57

    As a pure CSS solution for the close or 'times' symbol you can use the ISO code with the content property. I often use this for :after or :before pseudo selectors.

    The content code is \00d7.

    Example

    div:after{
      display: inline-block;
      content: "\00d7"; /* This will render the 'X' */
    }
    

    You can then style and position the pseudo selector in any way you want. Hope this helps someone :).

    0 讨论(0)
  • 2020-12-25 11:01

    Here's some variety for you with several sizes and hover animations.. demo(link)

    enter image description here

    <ul>
      <li>Large</li>
      <li>Medium</li>
      <li>Small</li>
      <li>Switch</li>
    </ul>
    
    <ul>
      <li class="ele">
        <div class="x large"><b></b><b></b><b></b><b></b></div>
        <div class="x spin large"><b></b><b></b><b></b><b></b></div>
        <div class="x spin large slow"><b></b><b></b><b></b><b></b></div>
        <div class="x flop large"><b></b><b></b><b></b><b></b></div>
        <div class="x t large"><b></b><b></b><b></b><b></b></div>
        <div class="x shift large"><b></b><b></b><b></b><b></b></div>
      </li>
      <li class="ele">
        <div class="x medium"><b></b><b></b><b></b><b></b></div>
        <div class="x spin medium"><b></b><b></b><b></b><b></b></div>
        <div class="x spin medium slow"><b></b><b></b><b></b><b></b></div>
        <div class="x flop medium"><b></b><b></b><b></b><b></b></div>
        <div class="x t medium"><b></b><b></b><b></b><b></b></div>
        <div class="x shift medium"><b></b><b></b><b></b><b></b></div>
    
      </li>
      <li class="ele">
        <div class="x small"><b></b><b></b><b></b><b></b></div>
        <div class="x spin small"><b></b><b></b><b></b><b></b></div>
        <div class="x spin small slow"><b></b><b></b><b></b><b></b></div>
        <div class="x flop small"><b></b><b></b><b></b><b></b></div>
        <div class="x t small"><b></b><b></b><b></b><b></b></div>
        <div class="x shift small"><b></b><b></b><b></b><b></b></div>
        <div class="x small grow"><b></b><b></b><b></b><b></b></div>
    
      </li>
      <li class="ele">
        <div class="x switch"><b></b><b></b><b></b><b></b></div>
      </li>
    </ul>
    

    css

    .ele div.x {
    -webkit-transition-duration:0.5s;
      transition-duration:0.5s;
    }
    
    .ele div.x.slow {
    -webkit-transition-duration:1s;
      transition-duration:1s;
    }
    
    ul { list-style:none;float:left;display:block;width:100%; }
    li { display:inline;width:25%;float:left; }
    .ele { width:25%;display:inline; }
    .x {
      float:left;
      position:relative;
      margin:0;
      padding:0;
      overflow:hidden;
      background:#CCC;
      border-radius:2px;
      border:solid 2px #FFF;
      transition: all .3s ease-out;
      cursor:pointer;
    }
    .x.large { 
      width:30px;
      height:30px;
    }
    
    .x.medium {
      width:20px;
      height:20px;
    }
    
    .x.small {
      width:10px;
      height:10px;
    }
    
    .x.switch {
      width:15px;
      height:15px;
    }
    .x.grow {
    
    }
    
    .x.spin:hover{
      background:#BB3333;
      transform: rotate(180deg);
    }
    .x.flop:hover{
      background:#BB3333;
      transform: rotate(90deg);
    }
    .x.t:hover{
      background:#BB3333;
      transform: rotate(45deg);
    }
    .x.shift:hover{
      background:#BB3333;
    }
    
    .x b{
      display:block;
      position:absolute;
      height:0;
      width:0;
      padding:0;
      margin:0;
    }
    .x.small b {
      border:solid 5px rgba(255,255,255,0);
    }
    .x.medium b {
      border:solid 10px rgba(255,255,255,0);
    }
    .x.large b {
      border:solid 15px rgba(255,255,255,0);
    }
    .x.switch b {
      border:solid 10px rgba(255,255,255,0);
    }
    
    .x b:nth-child(1){
      border-top-color:#FFF;
      top:-2px;
    }
    .x b:nth-child(2){
      border-left-color:#FFF;
      left:-2px;
    }
    .x b:nth-child(3){
      border-bottom-color:#FFF;
      bottom:-2px;
    }
    .x b:nth-child(4){
      border-right-color:#FFF;
      right:-2px;
    }
    
    0 讨论(0)
  • 2020-12-25 11:06

    Try This Cross In CSS

    .close {
      position: absolute;
      right: 32px;
      top: 32px;
      width: 32px;
      height: 32px;
      opacity: 0.3;
    }
    .close:hover {
      opacity: 1;
    }
    .close:before, .close:after {
      position: absolute;
      left: 15px;
      content: ' ';
      height: 33px;
      width: 2px;
      background-color: #333;
    }
    .close:before {
      transform: rotate(45deg);
    }
    .close:after {
      transform: rotate(-45deg);
    }
    <a href="#" class="close">

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