html - CSS 宽度 100% - X px

标签 html css css-float fluid-layout fixed-width

我需要一个具有固定右列的流畅布局,我尝试使用 this作为指南,但我无法按照自己的意愿使用它。

问题是主图像没有 100% 填充它的容器(100%,减去边栏的 300 像素)。我还需要它来保持两者之间的固定间距 (30px)

HTML:

<div class="imagecol portrait">
    <div class="mainimg">
        <img class="product_image" id="product_image_15" alt="Test Product" title="Test Product" src="http://example.com/main.jpg">
    </div>

    <div class="products_list clearfix related-products">

        <div class="product">
            <img src="http://example.com/200x200.jpg" title="Product 01" alt="Product 01">
            <p>Product 01</p>
        </div>
                
        <div class="product">
            <img src="http://example.com/200x200.jpg" title="Product 03" alt="Product 03">
        </div>

        <div class="product">
            <img src="http://example.com/200x200.jpg" title="Test Product" alt="Test Product">
        </div>
    </div>
</div>

一些CSS:

.single_product_display .imagecol{
    width:77%;
    float:right;
    margin:0;
}
.imagecol .mainimg{
    width:100%;
    float:left;
}
.imagecol .product_image{
    margin-right: 330px;
    cursor:default !important;
}
.imagecol.portrait .products_list{
    margin: 0px 0 0 -330px;
    float: left;
    width: 300px;   
}
.imagecol .products_list .product{
    margin:0 5px 5px 0;
    width: 145px;
    height: auto;
}

最佳答案

我的建议是采用完全响应式布局。找到父元素的 % 宽度,它最接近代表右列的宽度。仅使用 % 作为宽度和边距,并确保所有这些的总和等于 100%。例如:

在下图中,每个 * 代表布局总宽度的 5%。

|****|*|**********|*|****|
|    | |          | |    |
|    | |          | |    |
|20% |5|    50%   |5|20% |
|    |%|          |%|    |
|    | |          | |    |
|****|*|**********|*|****|

20 + 5 =25 + 50 = 75 + 20 = 95 + 5 = 100%

关于html - CSS 宽度 100% - X px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15547947/

相关文章:

javascript - 使用 HTML5 Canvas 将图像解析为 tileset

html - float div 中的内容导致下面的 div 下降

html - 当屏幕宽度减半时,Bootstrap 将我的内容向下推送

javascript - jQuery 在 div 和切换类之外找到下一个类

css - 从按钮 CSS 中删除边框?

html - 设置溢出 :auto, 但不显示水平滚动条

html - 使用 z-index 在 Div 之上的 Div

css-float - 列表中的并排 div

css - 制作绝对文本列表

html - 固定位置的 child ,溢出隐藏的 parent 。只隐藏底部