CSS学习笔记(九) 居中方案

简介: CSS学习笔记(九) 居中方案

在 CSS 中,居中对齐是我们常常需要用到的布局方式,下面介绍一些常用的居中方法


1、文字居中


(1)文字水平居中


<!DOCTYPE html>
<html>
<head>
    <style>
    .box {
        width: 500px;
        height: 300px;
        border: 1px solid black;
        text-align: center; /* 设置文字居中对齐 */
    }
    </style>
</head>
<body>
    <div class="box">
        <p class="item">我居中啦<br/>我居中啦</p>
        <p class="item">我也居中啦</p>
    </div>
</body>
</html>


(2)文字垂直居中


<!DOCTYPE html>
<html>
<head>
    <style>
    .box {
        width: 500px;
        height: 300px;
        border: 1px solid black;
        display: table-cell;    /* 设置元素生成框的类型为 表格单元 */
        vertical-align: middle; /* 设置元素垂直对齐方式为 中线对齐  */
    }
    </style>
</head>
<body>
    <div class="box">
        <p class="item">我居中啦<br/>我居中啦</p>
        <p class="item">我也居中啦</p>
    </div>
</body>
</html>


(3)文字垂直居中


<!DOCTYPE html>
<html>
<head>
    <style>
    .box {
        width: 500px;
        height: 300px;
        border: 1px solid black;
        line-height: 300px; /* 设置 lin-height 属性,值与 height 属性相等 */
    }
    .item {
        margin: 0;  /* 设置 margin  值为 0,防止偏移 */
        padding: 0; /* 设置 padding 值为 0,防止偏移 */
        display: inline-block;  /* 设置元素生成框的类型为 行内块框 */
        vertical-align: middle; /* 设置元素垂直对齐方式为 中线对齐 */
        line-height: 24px; /* 设置行高,覆盖父元素设置 */
    }
    </style>
</head>
<body>
    <div class="box">
        <p class="item">我居中啦<br/>我居中啦</p>
        <p class="item">我也居中啦</p>
    </div>
</body>
</html>


2、块框居中


(1)块框水平居中


<!DOCTYPE html>
<html>
<head>
    <style>
    .box {
        width: 500px;
        height: 300px;
        background: black;
        border: 1px solid black;
    }
    .item {
        width: 100px;
        height: 100px;
        background: gray;
        border: 1px solid white;
        margin: 0 auto; /* 设置外边距,上下外边距为 0,左右外边距为 auto(自动居中处理) */
    }
    </style>
</head>
<body>
    <div class="box">
        <div class="item"></div>
    </div>
</body>
</html>


(2)块框垂直居中


<!DOCTYPE html>
<html>
<head>
    <style>
    .box {
        width: 500px;
        height: 300px;
        background: black;
        border: 1px solid black;
        position: relative; /* 设置 position 为 relative */
    }
    .item {
        width: 100px;
        height: 100px;
        background: gray;
        border: 1px solid white;
        position: absolute;  /* 设置 position 为 absolute */
        top: 50%; /* 距离定位元素顶部 50% */
        transform: translateY(-50%); /* 沿着 Y 轴反向偏移 50% */
    }
    </style>
</head>
<body>
    <div class="box">
        <div class="item"></div>
    </div>
</body>
</html>


(3)块框水平、垂直居中


<!DOCTYPE html>
<html>
<head>
    <style>
    .box {
        width: 500px;
        height: 300px;
        background: black;
        border: 1px solid black;
        display: flex;           /* 使用 Flex 布局 */
        flex-direction: row;     /* 设置主轴沿着水平方向 */
        justify-content: center; /* 设置沿着主轴对齐方式 居中对齐 */
        align-items: center;     /* 设置沿交叉轴对齐方式 居中对齐 */
    }
    .item {
        width: 100px;
        height: 100px;
        background: gray;
        border: 1px solid white;
    }
    </style>
</head>
<body>
    <div class="box">
        <div class="item"></div>
    </div>
</body>
</html>




目录
相关文章
|
10月前
|
前端开发 开发者
HTML+CSS+JS 学习笔记(二)———CSS
HTML+CSS+JS 学习笔记(二)———CSS
149 0
|
3月前
|
前端开发 JavaScript 容器
CSS学习笔记(一) 盒子模型
CSS学习笔记(一) 盒子模型
|
9月前
|
前端开发
前端学习笔记202305学习笔记第二十二天-vue3.0-css预处理器和样式重置
前端学习笔记202305学习笔记第二十二天-vue3.0-css预处理器和样式重置
41 0
|
5月前
|
缓存 前端开发 JavaScript
《Webpack5 核心原理与应用实践》学习笔记->处理CSS
《Webpack5 核心原理与应用实践》学习笔记->处理CSS
36 0
|
10月前
|
前端开发 JavaScript Serverless
前端工程化的前端性能的性能优化方案的渲染层面优化之CSS/JS优化
渲染是一种非常重要的前端性能优化方案,因为它可以在不同的环境中提高网页的响应速度和可接受性。
64 2
|
8月前
|
前端开发 开发者
|
8月前
|
前端开发 开发者
CSS 媒体查询 | 学习笔记
简介:快速学习 css 媒体查询,介绍了 media 语法规则和代码示例。
58 0
|
8月前
|
前端开发 开发者
CSS 媒体查询
快速学习 CSS 媒体查询
139 0
|
8月前
|
前端开发
CSS学习笔记
CSS学习笔记
32 0
CSS学习笔记
|
9月前
|
移动开发 前端开发 JavaScript
css模块化的方案
css模块化的方案
94 0