How to style a div like the button-element?

情到浓时终转凉″ 提交于 2020-01-13 08:40:20

问题


I've noticed that when using the <button></button> element, the browsers will naturally assume that you want to center the inline content, and that the element is clickable. Is it possible to have a div behave in the same way through css? I went through the UA-stylesheet to see if I could figure it out, but nothing made it center it vertically.

The only other way i know to get the same result is with 2 divs. One wrapper with display: table, and a div with display: table-cell and vertical-align: middle.

But that creates an extra div. Is it possible to achieve the same behaviour with just one element?

Thanks.


回答1:


http://jsfiddle.net/8Sj4A/3/ - this does center vertically and horizontally (just added text-align: center; to the originally answer)

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);
}



回答2:


You can center the content of a divby adding the same amount amount of padding on each side.

padding:2px 10px;

This adds 2px to the top and bottom and 10px to the left and right, thus centering the content in the div.

I also styled the rest of the div to look and behave like a button. Looks are based on Firefox's default <button>.

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




回答3:


Maybe you are asking the wrong question.

It may be simpler to give your button a class and then ensure the inline content is styled as you want.

Anything that you can put in a DIV, you should be able to put in a button.




回答4:


You don't need multiple divs, check this JSFiddle, it's a single DIV that looks and behaves like a button.

the HTML:

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

the 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 actually link to something (behave like a link), simply change your HTML to this:

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

If you use bootstrap you don't have to define the .btn class, it's included in that.




回答5:


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>

This gets you as close as I could get to a real button.

For anything other than Chrome: look up appearance. Actually, just use Chrome... :)



来源:https://stackoverflow.com/questions/23608346/how-to-style-a-div-like-the-button-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!