添加一个按钮元素会改变它所在div的位置吗?

时间:2014-06-09 15:39:23

标签: javascript html css

我有一个基本布局,包含6个动态大小的内容面板,分为两行和三列。

面板为<div>,宽度为26%,边距为2%,display:inline-block;

它们均匀分布并且看起来正确,但是只要我添加<button>元素,第1行和第2列中的框就会向上移动大约20个像素。

有什么想法吗?

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Test Client</title>
    <link rel='stylesheet' type='text/css' href='CSS/Main.css' />
    <script src="JavaScript/Main.js">
    </script>
</head>
<body onload="Main.onLoad();">
    <div class="panel">
        <h3>content</h3>
        <div class="innerContainer" id="">
        </div>
    </div>
    <div class="panel">
        <h3>content</h3>
        <div class="innerContainer" id="">
        </div>
    </div>
    <div class="panel">
        <h3>content</h3>
        <div class="innerContainer" id="">
        </div>
    </div>
    <div class="panel">
        <h3>content</h3>
        <div class="innerContainer" id="">
        </div>
    </div>
    <div class="panel">
        <h3>content</h3>
        <div class="innerContainer" id="">
        </div>
    </div>
    <div class="panel">
        <h3>concent</h3>
        <div class="innerContainer" id="">
        </div>
    </div>
</body>
</html>

CSS:

*, *:before, *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

html {
    margin: 0;
    padding: 0;
    height: 100%;
}

body {
    margin: 0;
    padding: 0;
    background-color: rgb(227, 111, 30);
    height: 100%;
}

.innerContainer {
    margin:0;
    padding:0;
    width:100%;
}

.panel {
    display: inline-block;
    clear:none;
    width: 26%;
    height: 41%;
    margin: 2%;
    padding: 30px;
    border: 2px solid black;
    box-shadow: 10px 10px 5px #888888;
    -moz-border-radius: 40px;
    -webkit-border-radius: 40px;
    border-radius: 40px; /* future proofing */
    -khtml-border-radius: 40px; /* for old Konqueror browsers */
    background-color: white;
}

.panel h3 {
    text-align: center;
    text-transform: uppercase;
    padding:0;
    margin:0;
    clear:both;
}

.panel button {
}

1 个答案:

答案 0 :(得分:1)

尝试使用vertical-align:top。通过一个工作示例来看这个小提琴:http://jsfiddle.net/dxGsN/1/

所以你的css会是:

.panel {
   display: inline-block;
   clear:none;
   width: 26%;
   height: 41%;
   margin: 2%;
   padding: 30px;
   border: 2px solid black;
   box-shadow: 10px 10px 5px #888888;
   -moz-border-radius: 40px;
   -webkit-border-radius: 40px;
   border-radius: 40px; /* future proofing */
   -khtml-border-radius: 40px; /* for old Konqueror browsers */
   background-color: white;
   /*ADD THIS LINE*/
   vertical-align:top;
}

或者,您可以使用float:left代替display:inline-block。看到这个小提琴:http://jsfiddle.net/dxGsN/2/

display: block;
float: left;