How to do Tilt button?

Asked

Viewed 791 times

20

I am using img but it is not a good practice, so I would like to use a button with some css attribute. It is possible?

inserir a descrição da imagem aqui

2 answers

13

You can use CSS Transform with skew thus:

#inclinada {
    width: 100px;
    height: 20px;
    -webkit-transform: skew(-20deg);
       -moz-transform: skew(-20deg);
         -o-transform: skew(-20deg);
            transform: skew(-20deg);
    background: red;
    cursor: pointer;
    margin-left: 10px;
    padding: 10px;
}

jsFiddle: https://jsfiddle.net/5b5eboeg/

In my example I used margin and padding, and other properties, but this is optional. What matters is the transform: skew(-20deg); and its variants for different browsers.

Change the inclination degrees to suit your needs.

So the text doesn’t slant, you can do it like this (https://jsfiddle.net/5b5eboeg/1/) as @bfavaretto said or so (https://jsfiddle.net/5b5eboeg/2/) as @mgibsonbr said

  • 2

    I got the impression that the text in the question is not inclined, while that of your example is. A solution (gambiarra?) for this would be to put one more tag inside, and apply a reverse skew to it.

  • 2

    @bfavaretto Or maybe two Ivs with absolute positioning, one (not transformed) on top of the other (transformed).

  • Only good ideas... I’ll put together

  • exactly what I was going to ask rs, tested font-style: normal;

  • As the bfavaretto suggestion worked well, it is preferable compared to mine - more organized hierarchically, simpler to apply styles/actions.

13


An alternative would be to use the pseudo elements :before and :after combined with border and position you can add these two "corners" inclined, ie the effect is "simulated", example:

.exemplo {
  color: #fff;
  position: relative;/*faz os elementos pseudos acompanharem o elemento com a classe*/
  display: inline-block;
  background-color: #CC0000;
  line-height: 20px; /*Centraliza o texto*/
  height: 20px;
  padding:0 10px;
  margin: 0 10px;
}

.exemplo:before, .exemplo:after {
  content: "";
  width: 0px;
  height: 0px;
  top: 0px;
  position: absolute;
}

.exemplo:before {
  border-top: 20px solid transparent;
  border-right: 5px solid #CC0000;
  left: -5px;
}

.exemplo:after {
  border-bottom: 20px solid transparent;
  border-left: 5px solid #CC0000;
  right: -5px;
}
<div class="exemplo">test</div> <div class="exemplo">test</div>

For the corners depending on the height, change the border-width from 20px to the desired height on:

.exemplo:before {
  border-top: 20px solid transparent;

and

.exemplo:after {
  border-bottom: 20px solid transparent;

To adjust the inclination you must change the border-right and the left of .exemplo:before and change border-left and the right of .exemplo:after

Browser other questions tagged

You are not signed in. Login or sign up in order to post.