X close button only using css
Asked Answered
Q

13

98

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

I've been searching for a long time, and cannot found how.... When I look at source code on a website using it, there's always something weird which makes the code I take unusable.

The X button I want: http://tympanus.net/Tutorials/ThumbnailGridExpandingPreview/

When you click an image, this is the cross on the right:

enter image description here

I think this would be great if somebody can post a simple universal CSS code to make a simple X cross in CSS3.

Qua answered 4/9, 2013 at 10:18 Comment(5)
A duplicate of https://mcmap.net/q/218611/-pure-css-close-buttonTweedsmuir
you can use fontawesome..Dysphemia
CSS is really really the wrong tool for this job. Just about anything else would be better. A font with the appropriate glyph, an image, SVG, Canvas, anything. But not CSS.Boardwalk
thx for your answers, but my question wasn't 'is it a good practice', or 'what can replace css to do it (fontawesome)', i wanted to know if someone have the css-only code to put a X close button (as we can see on codrop's link). Adding content: 'x' on stylesheet is not what i want do beacause the X cross will depend too much on the font-family used. I want to make it with pure css shapping.Qua
i've posted an answer on this question https://mcmap.net/q/218611/-pure-css-close-button/…, my code doesn't use any text only cssJudaize
R
54

Main point you are looking for is:

.tag-remove::before {
  content: 'x'; // here is your X(cross) sign.
  color: #fff;
  font-weight: 300;
  font-family: Arial, sans-serif;
}

FYI, you can make a close button by yourself very easily:

#mdiv {
  width: 25px;
  height: 25px;
  background-color: red;
  border: 1px solid black;
}

.mdiv {
  height: 25px;
  width: 2px;
  margin-left: 12px;
  background-color: black;
  transform: rotate(45deg);
  Z-index: 1;
}

.md {
  height: 25px;
  width: 2px;
  background-color: black;
  transform: rotate(90deg);
  Z-index: 2;
}
<div id="mdiv">
  <div class="mdiv">
    <div class="md"></div>
  </div>
</div>
Recipient answered 4/9, 2013 at 10:28 Comment(6)
Adding content: 'x' on stylesheet is not what i want do beacause the X cross will depend too much on the font-family used. I want to make it with pure css shapping.Qua
@user2746369: please check the edit. is it you are looking for.Recipient
Thank you very much! It's exactly what i was searching for! thanks lotQua
You may to use × (times symbol) instead "X".Dearborn
Warning: do not use first example, as using letter X for close symbol is wrong. There is better alternative, that looks much better, see https://mcmap.net/q/216496/-x-close-button-only-using-cssWorkhorse
This is still problematic. The times symbol renders differently in terms of vertical alignment across different browsers.Synthiasyntonic
A
101

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 :).

Alcoholicity answered 3/12, 2014 at 1:30 Comment(3)
The "plus" symbol is code \002b if anyone is wants that.Diacid
Do you know how to call a function on click of content in angular? Say I want to close the div.. is that possible?Ephemera
Another way to do this is to use the &times; html symbol in your html, and then style it accordingly... Like so: <a href="#" class="close-button">&times;</a> and CSS .close-button { color: red; }Alcoholicity
S
74

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">
Smriti answered 9/11, 2017 at 7:52 Comment(4)
could you please explain how your code works? I am struggling to understand form where the X is coming from? It seems that the content=''plus height and width are doing the trick but I am unable to understand the logicHippodrome
I suppose I got it. I didn't know that a pseudo element is actually an element in itself and like all elements, has a box around it. So you are setting width, height of the box. As width is 2px, it effectively creates a bar like 'I'. You could leave the content property empty and just treat the pseudo-element like a content-less boxHippodrome
Yes this X come from this line if you know css well plz inspect it and check it out these how work.if you have still confusion let me know .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); }Smriti
Plz inspect this code and check it out thoroughly you get to know how this code made with thatSmriti
R
54

Main point you are looking for is:

.tag-remove::before {
  content: 'x'; // here is your X(cross) sign.
  color: #fff;
  font-weight: 300;
  font-family: Arial, sans-serif;
}

FYI, you can make a close button by yourself very easily:

#mdiv {
  width: 25px;
  height: 25px;
  background-color: red;
  border: 1px solid black;
}

.mdiv {
  height: 25px;
  width: 2px;
  margin-left: 12px;
  background-color: black;
  transform: rotate(45deg);
  Z-index: 1;
}

.md {
  height: 25px;
  width: 2px;
  background-color: black;
  transform: rotate(90deg);
  Z-index: 2;
}
<div id="mdiv">
  <div class="mdiv">
    <div class="md"></div>
  </div>
</div>
Recipient answered 4/9, 2013 at 10:28 Comment(6)
Adding content: 'x' on stylesheet is not what i want do beacause the X cross will depend too much on the font-family used. I want to make it with pure css shapping.Qua
@user2746369: please check the edit. is it you are looking for.Recipient
Thank you very much! It's exactly what i was searching for! thanks lotQua
You may to use × (times symbol) instead "X".Dearborn
Warning: do not use first example, as using letter X for close symbol is wrong. There is better alternative, that looks much better, see https://mcmap.net/q/216496/-x-close-button-only-using-cssWorkhorse
This is still problematic. The times symbol renders differently in terms of vertical alignment across different browsers.Synthiasyntonic
A
22

.close-btn {
  font-size: 60px;
  font-weight: bold;
  color: #000;
}
<span class="close-btn">&times;</span>
Antler answered 13/4, 2021 at 20:48 Comment(1)
So much shorter and easier than the answers above!Susiesuslik
L
21

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;
}
Lobar answered 12/9, 2014 at 18:45 Comment(0)
A
13

-- Simple HTML Solution --

Final result of easy to resize icon:

two close X buttons with different thickness.

JSfiddle demo: https://jsfiddle.net/allenski/yr5gk3cm/

The simple HTML:

<a href="#" class="close" tabindex="0" role="button">close</a>

Note:

  • tabindex attribute is there to help accessibility focus of iOS touch devices.
  • role attribute is to let screen readers users know this is a button.
  • The word close is also intended for screen readers to mention.

The CSS code:

.close {
    position: absolute;
    top: 0;
    right: 0;
    display: block;
    width: 50px;
    height: 50px;
    font-size: 0;
}
.close:before, 
.close:after {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 5px;
    height: 20px;
    background-color: #F0F0F0;
    transform: rotate(45deg) translate(-50%, -50%);
    transform-origin: top left;
    content: '';
}
.close:after {
    transform: rotate(-45deg) translate(-50%, -50%);
}

To adjust thickness of close X icon, change the width property. Example for thinner icon:

.close:before, 
.close:after {
    width: 2px;
}

To adjust length of close X icon, change the height property. Example:

.close:before, 
.close:after {
    height: 33px;
}
Anatase answered 23/2, 2021 at 7:35 Comment(0)
O
9

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>
Osyth answered 5/7, 2017 at 2:36 Comment(1)
Make sure to use viewBox, not viewPort. This is what I'm currently using: <svg class="close" viewBox="0 0 1 1" height="1" width="1" version="1.1" xmlns="http://www.w3.org/2000/svg"><line x1="0" y1="1" x2="1" y2="0" stroke="black" stroke-width="0.1" /><line x1="0" y1="0" x2="1" y2="1" stroke="black" stroke-width="0.1" /></svg>.Visby
P
4

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>
Papke answered 25/10, 2020 at 19:12 Comment(0)
G
2

Here's a good drop-in solution for perfectly centered circular X icon buttons

  • Using only CSS
  • Not relying on a font
  • The thickness and length of the tines of the X can be configured without affecting centering, using width and height in the pseudo element rule .close::before, .close::after
  • Screen reader support using aria-label
  • Works on a light or dark background by using transparent grays and currentColor to adapt to the current text color specified on the button or an ancestor.

.close {
    vertical-align: middle;
    border: none;
    color: inherit;
    border-radius: 50%;
    background: transparent;
    position: relative;
    width: 32px;
    height: 32px;
    opacity: 0.6;
}
.close:focus,
.close:hover {
    opacity: 1;
    background: rgba(128, 128, 128, 0.5);
}
.close:active {
    background: rgba(128, 128, 128, 0.9);
}
/* tines of the X */
.close::before,
.close::after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    height: 20px;
    width: 4px;
    background-color: currentColor;
}
.close::before {
    transform: translate(-50%, -50%) rotate(45deg);
}
.close::after {
    transform: translate(-50%, -50%) rotate(-45deg);
}
<div style="padding: 15px">
    <button class="close" aria-label="Close"></button>
</div>
<div style="background: black; color: white; padding: 15px">
    <button class="close" aria-label="Close"></button>
</div>
<div style="background: orange; color: yellow; padding: 15px">
    <button class="close" aria-label="Close"></button>
</div>
Ginder answered 12/11, 2020 at 18:18 Comment(0)
Y
0

<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>
Yarndyed answered 19/9, 2020 at 2:43 Comment(0)
T
0

A simple solution would be to use two divs that you style as strokes and then rotate with +/- 45%.

body {
  background: gray;
  padding: 20px;
}
.close {
    transform:translateY(-50%);
    z-index:1;
    width: 30px;
    height:30px;
    cursor:pointer;
  background: green;
  position:relative;  
}
.close span{
    position:absolute;
    width:80%;
    height:3px;
    left:10%;
    top:45%;
    background-color: black;
}

.first {
    transform:rotate(45deg);
}
.last {
    transform:rotate(-45deg);
}
<div class="close">
  <span class="first"></span>
  <span class="last"></span>
</div>
Teacher answered 15/9, 2022 at 15:14 Comment(0)
S
0
.class a::after {
    content: " x";
    font-size: 8px;
    vertical-align: super;
}
Susiesuslik answered 4/7, 2023 at 16:38 Comment(0)
R
-1

Following Zubair Saif's answer, I created it in css-in-js for my use case. I'm adding it here if it helps someone :

closeButton: {
      position: "absolute",
      right: "32px",
      width: "32px",
      height: "32px",
      opacity: "0.3",
      "&:hover": {
        opacity: 1,
      },
      "&:before": {
        position: "absolute",
        left: "15px",
        content: '""',
        height: "33px",
        width: "2px",
        backgroundColor: "black",
        transform: "rotate(45deg)",
      },
      "&:after": {
        position: "absolute",
        left: "15px",
        content: '""',
        height: "33px",
        width: "2px",
        backgroundColor: "black",
        transform: "rotate(-45deg)",
      }

You'll note the difference in the content prop (it doesn't work with the regular " ")

Responsible answered 10/7, 2022 at 21:24 Comment(2)
This is not CSS. Is it SCSS, or LESS or something? Should make it clear.Gonad
It's css-in-js, as stated in my answer.Responsible

© 2022 - 2024 — McMap. All rights reserved.