html - 如何在 html 中设置与百分比 % 相等的宽度

标签 html css css-float

注意: 在我发布我的问题之前,我搜索了这个网站来获得答案,但我找不到任何允许我在百分比值中设置等边距的解决方案。

以下代码未缩放到完整的 25%:

请参阅下面的示例 HTML 代码:

<style>

#Wrapper {
    margin:0 auto;
    width:70%;
}

#MainLogo{
    width:100%;
    height:100px;
    border:1px solid #000;
}

#LeftSide{
    width:25%;
    height: 600px;
    background-color:#90F;
    margin-top:5px;
    float:left;
}

#FloatLeft {
    float:left;
}

#One{
    width:25%;
    height:300px;
    background-color:#000;
    margin-top:5px;
    float:left;
}

#Two{
    width:25%;
    height:300px;
    background-color:#F00;
    margin-top:5px;
    float:left;

}

#Three{
    width:25%;
    height:295px;
    background-color:#F00;
    margin-top:5px;
    float:left;
    clear:both;
    }

#Four{
    width:25%;
    height:295px;
    background-color:#000;
    margin-top:5px;
    float:left;
}


#RightSide{
    width:25%;
    height: 600px;
    background-color:#00F;
    margin-top:5px;
    float:right;
    margin-bottom:5px;
}

#MainFooter{
width:100%;
    height:100px;
    border:1px solid #000;
    clear:both;
}

</style>


<body>

<div id="Wrapper">

<header id="MainLogo">
Logo Goes here!!
</header>

<aside id="LeftSide">
Left side goes here
</aside>

<div id="FloatLeft">

<aside id="One">
One Goes Here!!
</aside>

<aside id="Two">
Two aside goes here!!
</aside>

<aside id="Three">
Three aside goes here!!
</aside>

<aside id="Four">
Four aside goes here!!
</aside>
</div> 

<aside id="RightSide">
Right side goes here
</aside>

<footer id="MainFooter">
Main Footer Goes here!!
</footer>
</div>

最佳答案

它不工作的原因是因为诸如 padding 之类的事情和 margin .您将需要使用 box-sizing属性来获得你想要的效果。

首先,您应该删除所有内容的填充和边距,然后设置 box-sizing属性(property)border-box .这将确保无论多少paddingmargin你有,它将永远停留在height , 和 width您指定。

这是一个示例用法

* {
  padding:0;
  margin:0;
  box-sizing-border-box;
}

box-sizing属性尚未得到完全支持,因此您将需要使用供应商特定的属性

关于html - 如何在 html 中设置与百分比 % 相等的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17410504/

相关文章:

css - 向右浮动 Bootstrap 模态

html - css float 不取消

jquery - 如何仅使用 Sails.js 的 REST API 作为混合 HTML5 应用程序的后端

javascript - 按钮在 iPad 中不起作用

css - Div 未正确 float

javascript - 提高第一个 div 的不透明度,同时减少第二个。并循环

javascript - 如何正确定位国际象棋游戏的排名和文件标签?

html - 文件打开时 HTA 出错

HTML:宽度为 100% 的子输入大于固定宽度的父表

html - 自动缩放背景图像