Furnez
Furnez

Reputation: 9

How to do this kind of Button in CSS?

I've been searching the Internet for a while and found nothing, so I'm turning to you guys.

I was wondering how you can do a button like this with CSS (On/Off button example):

Button On/Off

I already tried something like this :

HTML :

                <a class="button_tooltip" href="#">On</a>
                <a class="button_tooltip" href="#">Off</a>

CSS:

a {
    color: #76daff;
    display: inline-block;
    padding: 8px 16px;
    position: relative;
    text-decoration: none;
}
a {
    color: #76daff;
}
a.button_tooltip {
    background: #ccc none repeat scroll 0 0;
    color: black;
}
a.button_tooltip::after {
    border-top-color: #cccccc;
}
a.button_tooltip::after {
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 8px solid #ccc;
    content: "";
    margin-left: -8px;
}
a.button_tooltip {
    background: #cccccc none repeat scroll 0 0;
    color: #000000;
}
a.button_tooltip::after {
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 8px solid #ccc;
    content: "";
}
a.button_tooltip::after {
    border-top-color: #cccccc;
}

But only gave me the square without the little triangle underneath.

Upvotes: 0

Views: 75

Answers (5)

Nick De Jaeger
Nick De Jaeger

Reputation: 1249

$(document).ready(function() {
  $('.toggle-btn button').click(function() {
    $(this).parent('.toggle-btn').children('button').removeClass('active');
    $(this).addClass('active');
  });
});
.toggle-btn {
  border: 1px solid #4c8cca;
  display: inline-block;
  line-height: 1;
  width: 100px;
}

button {
  line-height: 1;
  float: left;
  height: 30px;
  background: none;
  background-color: transparent;
  border: none;
  padding: 0;
  position: relative;
  outline: 0;
  width: 50%;
  cursor: pointer;
}

button:hover {
  background-color: #EEE;
}

button.active {
  background: #4c8cca;
  background-color: #4c8cca; 
  color: #FFF;
}

button.active:after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  top: 25px;
  transform: rotate(45deg);
  background-color: #4c8cca;
  left: 50%;
  margin-left: -4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-btn">
  <button>Yes</button>
  <button class="active">No</button>
</div>

Upvotes: 1

Stewartside
Stewartside

Reputation: 20905

This is easily possible with 2 elements and using a pseudo element to get the arrow bit underneath to show.

It doesn't take a great deal of HTML/CSS to complete and can easily be changed to work as an input or as an <a> tag. Whatever your requirements are.

$(document).ready(function() {
  $('.btn').click(function() {
    $('.btn').toggleClass('active');
  });
});
.container {
  width: 200px;
  height: 100px;
}
.btn {
  width: 100px;
  height: 100px;
  box-sizing: border-box;
  border: 1px solid blue;
  float: left;
  cursor: pointer;
}
.active {
  background: blue;
  position: relative;
}
.active:before {
  content: '';
  position: absolute;
  width: 50px;
  height: 50px;
  transform: rotate(45deg);
  bottom: -10px;
  left: 25px;
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="btn"></div>
  <div class="btn active"></div>
</div>

Upvotes: 2

Viktor Maksimov
Viktor Maksimov

Reputation: 1465

  <a href="#">Button</a>

  a { font-size: 0; line-height: 0; text-indent: -4000px; background: #fff; border: 1px solid #007bff; width: 30px; height: 30px; display: block; position: relative; }
  a:after { position: absolute; top: -1px; left: 30px; width: 30px; height: 30px; content: ''; display: block; background: #007bff; border: 1px solid #007bff; }
  a:before { content: ''; display: block; width: 0; height: 0; border-style: solid; border-width: 10px 10px 0 10px; border-color: #007bff transparent transparent transparent; position: absolute; bottom: -10px; right: -25px; }

Upvotes: 0

Ranjana
Ranjana

Reputation: 815

You can do that using css itself. Here is the example - https://jsfiddle.net/p653bpbe/

html

<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
    <label class="onoffswitch-label" for="myonoffswitch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

css

.onoffswitch {
    position: relative; width: 90px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 1px solid #2E8DEF; border-radius: 0px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 26px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
    border: 2px solid transparent;
    background-clip: padding-box;
}
.onoffswitch-inner:before {
    content: "";
    padding-left: 10px;
    background-color: #FFFFFF; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "";
    padding-right: 10px;
    background-color: #FFFFFF; color: #333333;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 45px; margin: 0px;
    background: #2E8DEF;
    position: absolute; top: 0; bottom: 0;

    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}

Upvotes: 0

Kristine
Kristine

Reputation: 747

Here you go: http://jsfiddle.net/es_kaija/negzqemp/

<div class="switch off">
    <div class="toggle"></div>
<span class="on">ON</span>
<span class="off">OFF</span>
</div>


<style>
.switch {
position: relative;
display: inline-block;
font-size: 1em;
font-weight: bold;
color: #ccc;
text-shadow: 0px 1px 1px rgba(255,255,255,0.8);
height: 18px;
padding: 6px 6px 5px 6px;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,0.2);
border-radius: 4px;
background: #ececec;
box-shadow: 0px 0px 4px rgba(0,0,0,0.1), inset 0px 1px 3px 0px rgba(0,0,0,0.1);
cursor: pointer;
}

.switch span { display: inline-block; width: 35px; }
.switch span.On { color: #33d2da; }

.switch .toggle {
position: absolute;
top: 1px;
width: 37px;
height: 25px;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,0.3);
border-radius: 4px;
background: #fff;
background: -moz-linear-gradient(top,  #ececec 0%, #ffffff 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ececec), color-stop(100%,#ffffff));
background: -webkit-linear-gradient(top,  #ececec 0%,#ffffff 100%);
background: -o-linear-gradient(top,  #ececec 0%,#ffffff 100%);
background: -ms-linear-gradient(top,  #ececec 0%,#ffffff 100%);
background: linear-gradient(top,  #ececec 0%,#ffffff 100%);

box-shadow: inset 0px 1px 0px 0px rgba(255,255,255,0.5);
z-index: 999;

-webkit-transition: all 0.15s ease-in-out;
-moz-transition: all 0.15s ease-in-out;
-o-transition: all 0.15s ease-in-out;
-ms-transition: all 0.15s ease-in-out;
}

.switch.on .toggle { left: 2%; }
.switch.off .toggle { left: 54%; }


</style>

<script>
$(document).ready(function() {

    // Switch toggle
    $('.switch').click(function() {
        $(this).toggleClass('on').toggleClass('off');
    });

});
</script>

Upvotes: 0

Related Questions