css - 试图并排获得三个部门

标签 css html

这是我当前的代码,但我看不出问题出在哪里。我是 html 的新手,所以我不太确定。我想在左侧有一个列,占空间的 20%,中间的列占 60% 的空间,右侧的列占 20%。

#wrapper {
    background-color: #788D9A;
    padding-bottom: 1000px;
    margin-bottom: -1000px;
}

#mainleft {
    width: 20%;
    float: left;
    padding: 10px;
    text-align: center;
    background-color: #ABB8C0;
    padding-bottom: 1000px;
    margin-bottom: -1000px;
    border-right: solid black;
    display:inline-block;
}

#maincenter {
    width: 60%;
    float: left;
    overflow: hidden;
    text-align: center;
    padding: 10px;
    padding-bottom: 1000px;
    margin-bottom: -1000px;
    display:inline-block;


}

#mainright {
    width: 20%;
    float: left;
    padding: 10px;
    text-align: center;
    background-color: #ABB8C0;
    padding-bottom: 1000px;
    margin-bottom: -1000px;
    border-right: solid black;

}

最佳答案

使用padding-left padding-right margin-left margin-right 时需要注意border-leftborder-right 当你想要那种类型的布局时。

这些样式中的每一个都会影响该元素的整体宽度,因此添加 padding: 10px 实际上会使您的 div width = 20% + 20px


如果你想要内部填充和边框样式一个内部 div

示例:http://jsfiddle.net/b62Ju/2/


HTML

<div id="wrapper">    
    <div id="mainleft">
        <div>L</div>
    </div>
    <div id="maincenter">
        <div>C</div>
    </div>
    <div id="mainright">
        <div>R</div>
    </div>
</div>​

CSS

#wrapper {
    background-color: #788D9A;
}

#wrapper > div
{
    height: 1000px;
    float: left;
    text-align: center;
}

#mainleft {
    width: 20%;    
    background-color: #ABB8C0;
}

#maincenter {
    width: 60%;
}

#mainright {
    width: 20%;
    background-color: #ABB8C0;
}

#maincenter > div
{    
    height: 1000px;
    border-left: solid black;
    border-right: solid black;
}

#mainleft > div, 
#maincenter > div,
#mainright > div
{
    padding: 10px;
}

或者,您可以使用 box-model 样式:

.box
{
    box-sizing: border-box;
    ms-box-sizing: border-box;
    webkit-box-sizing: border-box;
    moz-box-sizing: border-box;
}

更多信息:http://www.quirksmode.org/css/box.html

关于css - 试图并排获得三个部门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13865914/

相关文章:

html - CSS:用省略号截断标签,但始终显示 'required indicator'

javascript - 如何在下拉列表中将事件类添加到列表和父列表

Javascript 函数在 IE<10 中无法正常工作

javascript - 通过 Ajax 下载 PDF 并在新浏览器选项卡中打开

javascript - 一种无需为不同 ID 创建不同功能即可访问所有表单元素的方法

HTML 不加载 CSS

javascript - 当文本输入有焦点时淡化正文背景

html - 居中 float 元素

javascript - angularjs ng-pattern根据正则表达式条件更改元素的CSS样式

用户输入时 JavaScript 自动完成下拉菜单