【问题标题】:CSS Styling for a Button: Using <input type="button> instead of <button>按钮的 CSS 样式:使用 <input type="button> 而不是 <button>
【发布时间】:2012-06-15 15:16:28
【问题描述】:

我正在尝试使用&lt;input type="button"&gt; 而不仅仅是&lt;button&gt; 来设置按钮的样式。使用我拥有的代码,按钮一直延伸到屏幕上。我只想让它适合按钮中的文本。有什么想法吗?

查看jsFiddle Example 或继续阅读 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
 <title>WTF</title>
</head>
<style type="text/css">
 .button {
  color:#08233e;
  font:2.4em Futura, ‘Century Gothic’, AppleGothic, sans-serif;
  font-size:70%;
  padding:14px;
  background:url(overlay.png) repeat-x center #ffcc00;
  background-color:rgba(255,204,0,1);
  border:1px solid #ffcc00;
  -moz-border-radius:10px;
  -webkit-border-radius:10px;
  border-radius:10px;
  border-bottom:1px solid #9f9f9f;
  -moz-box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
  -webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
  box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
  cursor:pointer;
 }
 .button:hover {
  background-color:rgba(255,204,0,0.8);
 }
</style>
<body>
 <div class="button">
  <input type="button" value="TELL ME MORE" onClick="document.location.reload(true)">
 </div> 
</body>

【问题讨论】:

  • 你需要设置输入的样式而不是它周围的 div

标签: css button


【解决方案1】:

您真的想为&lt;div&gt; 设置样式吗?或者您想为&lt;input type="button"&gt; 设置样式?如果你想要后者,你应该使用正确的选择器:

input[type=button] {
    color:#08233e;
    font:2.4em Futura, ‘Century Gothic’, AppleGothic, sans-serif;
    font-size:70%;
    /* ... other rules ... */
    cursor:pointer;
}

input[type=button]:hover {
    background-color:rgba(255,204,0,0.8);
}

另见:

【讨论】:

    【解决方案2】:

    在您的.button CSS 中,尝试display:inline-block。看到这个JSFiddle

    【讨论】:

      【解决方案3】:

      你想要像给定的fiddle!这样的东西


      HTML

      <div class="button">
          <input type="button" value="TELL ME MORE" onClick="document.location.reload(true)">
      </div>
      

      CSS

      .button input[type="button"] {
          color:#08233e;
          font:2.4em Futura, ‘Century Gothic’, AppleGothic, sans-serif;
          font-size:70%;
          padding:14px;
          background:url(overlay.png) repeat-x center #ffcc00;
          background-color:rgba(255,204,0,1);
          border:1px solid #ffcc00;
          -moz-border-radius:10px;
          -webkit-border-radius:10px;
          border-radius:10px;
          border-bottom:1px solid #9f9f9f;
          -moz-box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
          -webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
          box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
          cursor:pointer;
          display:block;
          width:100%;
      }
      .button input[type="button"]:hover {
          background-color:rgba(255,204,0,0.8);
      }
      

      【讨论】:

        【解决方案4】:

        Float:left... 虽然我认为您希望输入的样式不是 div?

        .button input{
            color:#08233e;float:left;
            font:2.4em Futura, ‘Century Gothic’, AppleGothic, sans-serif;
            font-size:70%;
            padding:14px;
            background:url(overlay.png) repeat-x center #ffcc00;
            background-color:rgba(255,204,0,1);
            border:1px solid #ffcc00;
            -moz-border-radius:10px;
            -webkit-border-radius:10px;
            border-radius:10px;
            border-bottom:1px solid #9f9f9f;
            -moz-box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
            -webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
            box-shadow:inset 0 1px 0 rgba(255,255,255,0.5);
            cursor:pointer;
        }
        .button input:hover{
            background-color:rgba(255,204,0,0.8);
        }
        

        【讨论】:

          【解决方案5】:

          问题不在于按钮,问题在于div。由于divs 是块元素,它们默认占据其父元素的整个宽度(作为一般规则;如果您在一个文档中使用不同的定位方案,我很确定会有一些例外情况使其占据层次结构中更高元素的整个宽度)。

          无论如何,尝试将float: left; 添加到.button 选择器的规则中。这将导致带有类buttondiv 适合按钮周围,并且如果您想要更多div.buttons,则允许您在同一行上有多个浮动divs。

          【讨论】:

            【解决方案6】:

            试试看

            Display:inline;
            

            在 CSS 中。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-01-22
              • 1970-01-01
              • 2011-05-14
              • 2013-01-03
              • 1970-01-01
              • 2010-10-02
              • 2014-10-15
              相关资源
              最近更新 更多