美文网首页程序员
CSS实现水平垂直的几种方法

CSS实现水平垂直的几种方法

作者: Game0ver | 来源:发表于2020-03-15 11:50 被阅读0次

目录

  • 水平居中
  • 垂直居中
  • 水平垂直居

一、水平居中

内联元素和块级元素实现水平居中的方法不一样,其中块级元素又分定宽和不定宽两种情况

  • 内联元素水平居中
    父元素为块级元素,在父元素设置 text-align: center; 即可

代码:

<!-- HTML--->
<div class="container">
    <p>我是内联元素</p>
</div>

<!-- CSS -->
<style>
.container{
            width: 100%;
            height: 100px;
            background-color: darkseagreen;
            /* 父元素是块级元素,直接设置text-align属性 */
            text-align: center;
        }
</style>

效果:


image.png
  • 块级元素水平居中(定宽)
    块级定宽,谁居中谁 margin: 0 auto;

代码:

<!-- HTML -->
<div class="container">
   <div class="first_box"></div>
</div>

<!-- CSS -->
<style>
.container{
            width: 100%;
            height: 100px;
            background-color: darkseagreen;
        }

        .first_box{
            width: 50px;
            height: 50px;
            background-color: rosybrown;
            /* 块级定宽,谁居中谁margin: 0 auto; */
            margin: 0 auto;
        }
</style>

效果:


image.png

代码:

<!-- HTML -->
<div class="container">
    <div class="second_box">我是不定宽的块级元素</div>
</div>

<!-- CSS -->
<style>
.container{
            width: 500px;
            height: 100px;
            background-color: darkseagreen;
 
            text-align: center;
        }

.second_box{
            background-color: rosybrown;
            display: inline;

        }
</style>

效果:


image.png

代码:

<!-- HTML -->
<div class="container1">
    <div class="third_box"></div>
</div>

<!-- CSS -->
<style>
.container1{
            width: 500px;
            height: 100px;
            background-color: darkseagreen;

            position: relative;

        }

        .third_box{
            width: 50px;
            height: 50px;
            background-color: rosybrown;
            position: absolute;
            left: 50%;
            margin-left: -25px;
        }
</style>

效果


image.png

代码:

<!-- HTML -->
<div class="container2" >
    <div></div>
</div>

<!-- CSS -->
<style>
.container2{
            width: 500px;
            height: 100px;
            background-color: darkseagreen;

            display: flex;
            justify-content: center;
        }

        .container2 div{
            width: 50px;
            height: 50px;
            background-color: rosybrown;
        }
</style>

效果:


image.png

flex布局详见阮一峰的flex布局教程 语法篇

二、垂直居中

实现垂直居中,亦分内联元素和块级元素,其中块级元素同分定高和不定高两种情况

单行代码:

<!-- HTML -->
<div class="container">
    <p>单行行内元素</p>
</div>

<!--CSS-->
<style>
.container{
            width: 500px;
            height: 200px;
            background-color: darkseagreen;
        }

        .container p{
            line-height: 200px;
        }
</style>

效果:


image.png

多行代码:

<!-- HTML -->
<div class="container">
    <p>我是多行行内元素我是多行行内元素我是多行行内元素我是多行行内
        元素我是多行行内元素我是多行行内元素我是多行行内元素我是多行行内元素我是多行行内元素</p>
</div>

<!--CSS-->
<style>
.container{
            width: 500px;
            height: 200px;
            background-color: darkseagreen;

            display: table-cell;
            vertical-align: middle;
        }
</style>

效果:


image.png

代码:

<!-- HTML -->
<div class="container">
    <div></div>
</div>

<!-- CSS -->
<style>
.container{
            width: 500px;
            height: 200px;
            background-color: darkseagreen;

            position: relative;

        }

        .container div{
            width: 50px;
            height: 50px;
            background-color: rosybrown;

            position: absolute;
            top: 50%;
            margin-top: -25px;
        }
</style>

效果:


image.png
<!-- HTML -->
<div class="container">
    <div>不定高度</div>
</div>

<!-- CSS -->
<style>
 .container{
            width: 500px;
            height: 200px;
            background-color: darkseagreen;

            position: relative;

        }

        .container div{
            width: 50px;
            background-color: rosybrown;

            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }
</style>

效果:


image.png
  • 使用flex实现垂直居中
    代码:
<!-- HTML -->
<div class="container">
    <div>不定高度</div>
</div>

<!-- CSS -->
<style>
.container{
            width: 500px;
            height: 200px;
            background-color: darkseagreen;

            display: flex;
            align-items: center;

        }

        .container div{
            width: 50px;
            background-color: rosybrown;
        }
</style>

效果:


image.png

flex布局详见阮一峰的flex布局教程 语法篇

三、水平垂直居中

代码:

<!-- HTML -->
<div class="container">
    <div></div>
</div>
<!-- CSS -->
<style>
.container{
            width: 500px;
            height: 200px;
            background-color: darkseagreen;

            position: relative;

        }

        .container div{
            width: 50px;
            height: 50px;
            background-color: rosybrown;

            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto;
        }
</style>

效果:

代码:

<!-- HTML -->
<div class="container">
    <div></div>
</div>

<!-- CSS -->
<style>
.container{
            width: 500px;
            height: 200px;
            background-color: darkseagreen;

            display: flex;
            justify-content: center;
            align-items: center;

        }

        .container div{
            width: 50px;
            height: 50px;
            background-color: rosybrown;

        }
</style>

效果:

相关文章

  • CSS解决盒模型居中的问题

    CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法 CSS实现盒子模型水平居中的方法 全局样式 第一种:...

  • css实现盒子内部 div水平垂直居中

    总结一下利用css实现盒子内部 div居中的几种方法 1.水平居中 1)margin: 0 auto 2.水平垂直...

  • 面试题整理

    1.CSS中实现水平垂直居中几种方法(这里举出两种方法) 解一(利用flex布局).box{display: fl...

  • 不定宽高的div盒子和不定宽高的盒子水平垂直居中

    让一个不定宽高的div,垂直水平居中的几种实现方式 不定宽高的div垂直居中的方式: 1、使用CSS方法: 父盒子...

  • CSS实现水平垂直的几种方法

    目录 水平居中 垂直居中 水平垂直居 一、水平居中 内联元素和块级元素实现水平居中的方法不一样,其中块级元素又分定...

  • css实现水平垂直居中的方法总结

    css实现水平垂直居中的方法总结 实现水平垂直居中你有多少种方法?这是前端面试必考题啊!-=- 这段时间公司招前端...

  • 2019-01-26

    一、总结一下CSS的几种布局以及实现方法 左右布局 左中右 水平居中 垂直居中 1. 左右布局 左右布局很多种实现...

  • CSS布局(不完全)总结

    CSS布局(不完全)总结 实现水平居中布局的几种方式 方法一: 通过以下CSS代码实现水平居中布局 方法二: 通过...

  • 面试之道 - 元素实现水平垂直居中的CSS解决方案

    声明:文章转载自《面试之道 - 元素实现水平垂直居中的CSS解决方案》 写出几种常见的水平垂直居中方式。 这是一个...

  • 面试题收集

    收集面试题用于公司面试: 一、html、css 部分 1.清除浮动的几种方法 2.如何实现不定宽高的垂直水平居中 ...

网友评论

    本文标题:CSS实现水平垂直的几种方法

    本文链接:https://www.haomeiwen.com/subject/lfcfehtx.html