【问题标题】:CSS button size inside divdiv内的CSS按钮大小
【发布时间】:2014-08-21 16:42:37
【问题描述】:

我的 html 页面有问题。我创建了 CSS 按钮并将它们放置在 div(行)中,其行为应该像表格一样。我希望每个按钮的大小是 div“单元格”的高度和宽度的 60%。但是,它们总是将空间填充到最大大小。

这是我的fiddle

这是我的 CSS 代码:

html,body
{
    height: 100%;
    width: 100%;
    margin: 0px;
    padding: 0px;
    background-color: blue;
    text-align: center;
}

.container
{
    display: table;
    width: 80%;
    height: 70%;
    position: relative;
    background: white;
    -moz-border-radius: 25px;
    -webkit-border-radius: 25px;
    border-radius: 25px;
    display: inline-block;
    overflow: hidden;
}

.row
{
    width: 99%;
    height: 33%;
    //display: table;
    display: table-row;
    table-layout: fixed;
    position: relative;
    float: left;
}

.row span
{
    width: 100%;
    height: 100%;
    display: table;
}

.row span a
{
    display: table-cell;
    width: 33%;
    vertical-align: middle;
}

.button
{
    width: 60%;
    height: 60%;
    -moz-box-shadow: inset 0px 1px 0px 0px orange;
    -webkit-box-shadow: inset 0px 1px 0px 0px orange;
    box-shadow: inset 0px 1px 0px 0px orange;
    filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='orange', endColorstr='orange',GradientType=0);
    background-color: orange;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    display: inline-block;
    cursor: pointer;
    color: black;
    font-size: 100%;
    font-weight: bold;
    text-decoration: none;
    text-shadow: 0px 1px 0px orange;
}

.button:hover
{
    filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#dfbf91', endColorstr='#dfd691',GradientType=0);
    background-color: yellow;
}

.button:active
{
    position: relative;
    top: 1px;
}

我在寻找问题的解决方案时遇到了困难,因此我们将不胜感激!

【问题讨论】:

  • 您对预期行为的解释不清楚。你能详细说明一下吗?
  • div 组成了一个 3 行 3 个单元格的表格,对吧?在这些“单元格”中,我只有链接按钮。我希望每个按钮具有相应单元格高度和宽度的 60%。否则,链接周围没有空白区域。既不在顶部/底部,也不在左侧/右侧。这就是为什么我认为取值 60% 会调整每个按钮的大小。我希望现在更清楚了:)

标签: html css


【解决方案1】:

这就是你要找的:

只需在 css 中添加一个媒体查询……我在下面添加了一个示例……

html,body
{
    height: 100%;
    width: 100%;
    margin: 0px;
    padding: 0px;
    background-color: blue;
    text-align: center;
}

.container
{
    display: table;
    width: 80%;
    height: 70%;
    position: relative;
    background: white;
    -moz-border-radius: 25px;
    -webkit-border-radius: 25px;
    border-radius: 25px;
    display: inline-block;
    overflow: hidden;
}

.row
{
    width: 99%;
    height: 30%;
    //display: table;
    display: table-row;
    table-layout: fixed;
    position: relative;
    float: left;
    margin-top: 5px;
}

.row span
{
    width: 100%;
    height: 100%;
    padding-top:5%;
}

.row span a {
    display: inline-block;
    vertical-align: middle;
    width: 25%;
    margin: 3%;
}

.button
{
    width: 40%;
    height: 40%;
    -moz-box-shadow: inset 0px 1px 0px 0px orange;
    -webkit-box-shadow: inset 0px 1px 0px 0px orange;
    box-shadow: inset 0px 1px 0px 0px orange;
    filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='orange', endColorstr='orange',GradientType=0);
    background-color: orange;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    display: inline-block;
    cursor: pointer;
    color: black;
    font-size: 100%;
    font-weight: bold;
    text-decoration: none;
    text-shadow: 0px 1px 0px orange;
}
@media all and (max-width: 480px) 
        {
            .button
{
    width: 40%;
    height: 40%;
    -moz-box-shadow: inset 0px 1px 0px 0px orange;
    -webkit-box-shadow: inset 0px 1px 0px 0px orange;
    box-shadow: inset 0px 1px 0px 0px orange;
    filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='orange', endColorstr='orange',GradientType=0);
    background-color: orange;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    display: inline-block;
    cursor: pointer;
    color: black;
    font-size: 60%;
    font-weight: bold;
    text-decoration: none;
    text-shadow: 0px 1px 0px orange;
    overflow: hidden;
    text-overflow: ellipsis;
}            
        }

.button:hover
{
    filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#dfbf91', endColorstr='#dfd691',GradientType=0);
    background-color: yellow;
}

.button:active
{
    position: relative;
    top: 1px;
}

【讨论】:

  • 感谢您的回复!这只会给我 60% 的高度。我也希望每个按钮的宽度为 60%。
  • 这个 CSS 就是你要找的。​​span>
  • 这正是我想要的!我只有一个要求。当按钮中的文本很短时,这就像一个魅力。但是,如果我有更长的文本,则在调整窗口大小时它将显示在按钮之外。你也能给我一个解决办法吗?我会将您的答案标记为正确。
【解决方案2】:

更改 CSS 中的 row.span 属性,将其设置为 100%

【讨论】:

    【解决方案3】:

    HTML

    <body>
        <div id="buttonPanel" class="container">
            <div id="row1" class="row">
    
                    <a href="#" class="button">Link 1</a>
                    <a href="#" class="button">Link 2</a>
                    <a href="#" class="button">Link 3</a>
    
            </div>
            <div id="row2" class="row">
    
                    <a href="#" class="button">Link 4</a>
                    <a href="#" class="button">Link 5</a>
                    <a href="#" class="button">Link 6</a>
    
            </div>
            <div id="row3" class="row">
    
                    <a href="#" class="button">Link 7</a>
                    <a href="#" class="button">Link 8</a>
                    <a href="#" class="button">Link 9</a>
    
            </div>
        </div>
    </body>
    

    CSS

     html,body
        {
            height: 100%;
            width: 100%;
            margin: 0px;
            padding: 0px;
            background-color: blue;
            text-align: center;
        }
    
        .container
        {
            display: table;
            width: 80%;
            height: 70%;
            position: relative;
            background: white;
            -moz-border-radius: 25px;
            -webkit-border-radius: 25px;
            border-radius: 25px;
            display: inline-block;
            overflow: hidden;
        }
    
        .row
        {
            width: 100%;
            height: 36%;
        }
    
        .row span
        {
            width: 100%;
            height: 100%;
            display: table;
        }
    
        .row  a
        {
            display: inline-block;
        /* vertical-align: middle; */
        width: 32.7%;
        height: 70%;
        vertical-align: sub;
        margin: 0px;
        }
    
        .button
        {
            width: 60%;
            height: 60%;
            -moz-box-shadow: inset 0px 1px 0px 0px orange;
            -webkit-box-shadow: inset 0px 1px 0px 0px orange;
            box-shadow: inset 0px 1px 0px 0px orange;
    
            background-color: orange;
            -moz-border-radius: 3px;
            -webkit-border-radius: 3px;
            border-radius: 3px;
            display: inline-block;
            cursor: pointer;
            color: black;
            font-size: 100%;
            font-weight: bold;
            text-decoration: none;
            text-shadow: 0px 1px 0px orange;
        }
    
        .button:hover
        {
    
            background-color: yellow;
        }
    
     .button:active
        {
            position: relative;
            top: 1px;
        }
    

    http://jsfiddle.net/yugi47/YV9cX/

    【讨论】:

      猜你喜欢
      • 2014-09-10
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      相关资源
      最近更新 更多