css代码如何实现多列布局

发布时间:2023-01-14 09:04:26 作者:iii
来源:亿速云 阅读:130

这篇文章主要介绍“css代码如何实现多列布局”,在日常操作中,相信很多人在css代码如何实现多列布局问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”css代码如何实现多列布局”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一. 定宽 + 自适应

期望效果: 左侧宽度固定, 右侧宽度自适应
公共代码:
html:

<div class="parent">
    <div class="left">
        <p>left menu</p>
    </div>
    <div class="right">
        <li>right item1</li>
        <li>right item2</li>
        <li>right item3</li>
    </div>
</div>

css:

html, body, p, ul, li {
    margin: 0;
    padding: 0;
}
div.left {
    background: #d2e3e3;
}
div.right {
    background: #77DBDB;
}

方案一: float

.left {
    float: left;
    width: 100px;
}
.right {
    margin-left: 100px; // 或 overflow: hidden
}

方案二: table

.parent {
    display: table;
    width: 100%;
    table-layout: fixed; // https://blog.csdn.net/qq_36699230/article/details/80658742
    .left, .right {
        display: table-cell;
    }
    .left {
        width: 100px;
    }
}

方案三: flex

.parent {
    display: flex;
    .left {
        width: 100px; // 或 flex: 0 0 100px;
    }
    .right {
        flex: 1;
    }
}
.parent {
    display: table;
    width: 100%;
    // 设置table-layout: fixed; 会使单元格等宽, 因此此处不设置
    .left, .right {
        display: table-cell;
    }
    .left {
        width: 0.1%; // 宽度设置一个极小值, 由于没有设置table-layout: fixed; 所以宽度由内容决定
        white-space:nowrap;
    }
}

二. 等宽(两/多列)布局

公共代码:
html

<div class="parent">
    <div class="column">
        <p>1</p>
    </div>
    <div class="column">
        <p>2</p>
    </div>
    <div class="column">
        <p>3</p>
    </div>
    <div class="column">
        <p>4</p>
    </div>
</div>

css

html, body, div, p {
    margin: 0;
    padding: 0;
}
.parent {
    width: 800px;
    border: 1px solid coral;
    .column {
        height: 30px;
        background: bisque;
        p {
            background: #f0b979;
            height: 30px;
        }
    }
}

方案一: float (个人并不喜欢, 写法很死, 需要知道有多少列, 而且有边框的情况下会超出容器)

.parent {
    margin-left: -20px;
    overflow: hidden;
    .column {
        float: left;
        width: 25%;
        padding-left: 20px;
        box-sizing: border-box;
    }
}


 

方案二: flex (推荐)

.parent {
    display: flex;
    .column {
        flex: 1;
        &+.column {
            margin-left: 10px;
        }
    }
}

三. 等高布局

推荐方案:

.parent {
    display: flex;
}
.left, .right {
    flex: 1;
}

css的选择器有哪些

css的选择器可以分为三大类,即id选择器、class选择器、标签选择器。它们之间可以有多种组合,有后代选择器、子选择器、伪类选择器、通用选择器、群组选择器等等

到此,关于“css代码如何实现多列布局”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. CSS混合模式
  2. CSS 浅析position:relative/absolute定位方式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css

上一篇:Go语言中chan通道有什么作用

下一篇:CSS中flex-basis文本溢出问题如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》