Skip to content Skip to sidebar Skip to footer

How To Style A Div Like The Button-element?

I've noticed that when using the element, the browsers will naturally assume that you want to center the inline content, and that the element is click

Solution 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: 005px -1pxrgba(0,0,0,0.2);
    cursor:pointer;
    vertical-align:middle;
    max-width: 100px;
    padding: 5px;
    text-align: center;
}
div:active {
    color:red;
    box-shadow: 005px -1pxrgba(0,0,0,0.6);
}

Solution 2:

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: 6px12px;
    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.

Solution 3:

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

padding:2px10px;

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/

Solution 4:

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.

Solution 5:

css:

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

html:

<divrole="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... :)

Post a Comment for "How To Style A Div Like The Button-element?"