How to style a div as a button element?

I noticed that when using an element, <button></button>browsers naturally assume that you want to center the inline content and that this element is clickable. Is it possible that a div behaves the same as css? I went through the UA stylesheet to find out if I can figure it out, but nothing made it vertical.

The only other way I know to get the same result is with two divs. One wrapper with display: tableand div with display: table-celland vertical-align: middle.

But this creates an additional div. Is it possible to achieve the same behavior with only one element?

Thank.

+4
source share
5 answers

http://jsfiddle.net/8Sj4A/3/ - ( text-align: center; )

div {
    display:inline-block;
    color:#444;
    border:1px solid #CCC;
    background:#DDD;
    box-shadow: 0 0 5px -1px rgba(0,0,0,0.2);
    cursor:pointer;
    vertical-align:middle;
    max-width: 100px;
    padding: 5px;
    text-align: center;
}
div:active {
    color:red;
    box-shadow: 0 0 5px -1px rgba(0,0,0,0.6);
}
+7

divby, .

padding:2px 10px;

2px 10px , div.

div, button. Firefox <button>.

http://jsfiddle.net/evSb5/2/

+1

, .

, , , .

, DIV, .

0

CSS

div.button {
  display:inline-block;
  -webkit-appearance:button;
  padding:3px 8px 3px 8px;
  font-size:13px;
  position:relative;
  cursor:context-menu;
}

HTML:

<div role="button" class="button">Press Me!</div>

.

For anything but Chrome: find appearance. Actually, just use Chrome ... :)

0
source

You don't need multiple divs , check out JSFiddle , this is the only DIV that looks and behaves like a button.

HTML:

<div class='btn'>
  this looks like a button
</div>

CSS:

.btn{
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
    text-decoration: none;

    color: #333;
    background-color: #fff;
    border-color: #ccc;
}

If you want the button to really refer to something (behave like a link), just change your HTML to this:

<a href='test.html' class='btn'>
  this looks like a button
</a>

If you use bootstrap , you do not need to define the .btn class included in it.

0
source

Source: https://habr.com/ru/post/1540217/


All Articles