How to style a div like the button-element?

后端 未结 5 1506
南笙
南笙 2021-01-04 13:12

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

相关标签:
5条回答
  • 2021-01-04 13:45

    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/

    0 讨论(0)
  • 2021-01-04 13:46

    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.

    0 讨论(0)
  • 2021-01-04 14:02

    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.

    0 讨论(0)
  • 2021-01-04 14:05

    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);
    }
    
    0 讨论(0)
  • 2021-01-04 14:06

    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... :)

    0 讨论(0)
提交回复
热议问题