html - 将div平分并居中

标签 html css responsive-design

html代码:

<body>    
    <div id="grid-main_content">
        <div class="block ">
            <div class="column column-1">
                <p>Esse ingeniis instituendarum...</p>
            </div>
        </div>
        
        <div class="block">
            <div class="column column-2">
                <p>Quis voluptate o comprehenderit non fugiat ullamco..</p>
            </div>
            <div class="column column-2">
                <p>Irure an arbitror de appellat fugiat offendit,..</p>
            </div>
            <span class="clear-both"></span>
        </div>
        
        <div class="block">
            <div class="column column-3">
                <p>Cernantur est possumus,..</p>
            </div>
            <div class="column column-3">
                <p>Multos quamquam deserunt ea minim sed consequat,..</p>
            </div>
            <div class="column column-3">
                <p>Eiusmod illum mandaremus quo appellat...</p>
            </div>
            <span class="clear-both"></span>
        </div>
        
        <div class="block">
            <div class="column column-4">
                <p>Duis arbitror sed dolor sint...</p>
            </div>
            <div class="column column-4">
                <p>Eram expetendis doctrina ut offendit ipsum et deserunt familiaritatem,..</p>
            </div>
            <div class="column column-4">
                <p>Cupidatat aut elit appellat...</p>
            </div>
            <div class="column column-4">
                <p>Eu irure summis...</p>
            </div>
            <span class="clear-both"></span>
        </div>
    </div>
</body>

CSS:

.block {
    border: 1px solid black;
    margin: 2px;
}

.column {
    padding: 5px;
    margin: 5px;
}

.column-1 {
    background: lightblue;
    width: 100%;
}

.column-2 {
    background: lightgreen;
    width: 48%;
    float: left;
}

.column-3 {
    background: yellow;
    width: 32%;
    float: left;
}

.column-4 {
    background: red;
    width: 23%;
    float: left;
}

它看起来像这样:

enter image description here

正如您所看到的,每个 block 的所有内容长度不相等并且不居中,因为我希望 block (列)响应宽度。我如何将它们平均分为具有全宽度的column-1、具有1/2宽度的column-2、具有1/3宽度的column-3和具有1/4宽度的column-4?并水平居中。

最佳答案

CSS:

body, html{
    margin:0;
    padding:2px;
}
*{
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    box-sizing:border-box;
}
.block {
    border: 1px solid black;
    padding: 2px;
    float:left;
    width:100%;
}

.column {
    padding: 5px;
}

.column-1 {
    background: lightblue;
    width: 100%;
}

.column-2 {
    background: lightgreen;
    width: 50%;
    float: left;
}

.column-3 {
    background: yellow;
    width: 33.333%;
    float: left;
}

.column-4 {
    background: red;
    width: 25%;
    float: left;
}

js:

$(function(){
            equalHeight();
        });
        $(window).resize(function(){
            equalHeight();
        });
        function equalHeight(){
            var maxHeight = 0;
            $(".column-4").each(function(){
               if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
            });
            $(".column-4").height(maxHeight);

            $(".column-3").each(function(){
               if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
            });
            $(".column-3").height(maxHeight);

            $(".column-2").each(function(){
               if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
            });
            $(".column-2").height(maxHeight);

        }

查找 fiddle dmeo

关于html - 将div平分并居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157089/

相关文章:

html - 在小型设备上保持内容增长的部分

html - 使用 CSS 选择性显示内容,避免冲突

html - 处理草图不适用于网络

javascript - 在一定比例的窗口大小上不断更改 div 大小

html - overflow hidden 的响应式垂直居中

JavaScript:调用函数时表单 onSubmit 不起作用 'submit'

python - BS4 某些行不返回任何内容

html - 可以使用 css 将图像反射(reflect)为阴影吗?

javascript - 如何使用淡入效果查看archive.org上的网站?

css:根据父级调整一个元素的高度,保持其他元素的高度