首页 > 其他分享 >css:元素居中整理水平居中、垂直居中、水平垂直居中

css:元素居中整理水平居中、垂直居中、水平垂直居中

时间:2023-11-05 19:36:50浏览次数:35  
标签:居中 box center color 50% 垂直 background css



目录

  • 1、水平居中
  • 1.1、行内元素
  • 1.2、块级元素
  • 2、垂直居中
  • 2.1、单行文字
  • 2.2、多行文字
  • 2.3、图片垂直居中
  • 3、水平垂直居中
  • 参考文章


1、水平居中

1.1、行内元素

行内元素(比如文字,span,图片等)的水平居中,其父元素中设置

text-align: center;

示例

<style>
   body {
     background-color: #eeeeee;
   }
  
  .box {
    background-color: green;
    color: #fff;
    margin-top: 20px;
  }

  .box--center {
    text-align: center;
  }
</style>

<div class="box">两个黄鹂鸣翠柳,一行白鹭上青天</div>

<div class="box box--center">两个黄鹂鸣翠柳,一行白鹭上青天</div>

效果

css:元素居中整理水平居中、垂直居中、水平垂直居中_前端

1.2、块级元素

块级元素,比如 div,其默认宽度是100%。给定一个宽度的时候,可以居中对齐

width: 50%;
margin: 0 auto;

示例

<style>
    body {
    background-color: #eeeeee;
    }

    .box {
    background-color: green;
    height: 50px;
    }

    .box--center {
    width: 50%;
    margin: 0 auto;
    margin-top: 20px;
    }
    
</style>

<div class="box"></div>

<div class="box box--center"></div>

实现效果

css:元素居中整理水平居中、垂直居中、水平垂直居中_垂直居中_02

2、垂直居中

2.1、单行文字

对于单行文字居中,可以设置父元素的行高来实现,将其行高与元素高度设置为相同的值即可:

height: 50px;
line-height: 50px;

示例

<style>
    body {
        background-color: #eeeeee;
    }

    .box {
        background-color: green;
        color: #fff;
        margin-top: 20px;
        height: 50px;
    }

    .box--center {
        line-height: 50px;
    }
</style>

<div class="box">两个黄鹂鸣翠柳,一行白鹭上青天</div>

<div class="box box--center">两个黄鹂鸣翠柳,一行白鹭上青天</div>

css:元素居中整理水平居中、垂直居中、水平垂直居中_垂直居中_03

2.2、多行文字

也适用于单行文字、行内元素

height: 200px;
display:table-cell;
vertical-align:middle;

示例

<style>
 body {
    background-color: #eeeeee;
    display: flex;
  }

  .box {
    background-color: green;
    color: #fff;
    height: 200px;
    width: 130px;
  }

  .box--center {
    display: table-cell;
    vertical-align: middle;
  }

  .box-wrap {
    margin-left: 20px;
  }
</style>

<div class="box">两个黄鹂鸣翠柳,一行白鹭上青天。</div>

<div class="box-wrap">
  <div class="box box--center">两个黄鹂鸣翠柳,一行白鹭上青天。</div>
</div>

css:元素居中整理水平居中、垂直居中、水平垂直居中_css_04

2.3、图片垂直居中

上面的方法也可以让图片垂直居中

display: table-cell;
vertical-align: middle;

示例

<style>
  body {
    background-color: #eeeeee;
    display: flex;
  }

  .box {
    background-color: green;
    color: #fff;
    height: 200px;
    width: 200px;
  }

  .box--center {
    display: table-cell;
    vertical-align: middle;
  }

  .box-wrap {
    margin-left: 20px;
    color: #fff;
    position: relative;
  }

  .image {
    width: 192px;
    height: 108px;
    vertical-align: middle;
  }

  .label {
    position: absolute;
    right: 0;
    top: 0;
    background-color: pink;
    padding: 0 4px;
  }
</style>

<div class="box-wrap">
  <div class="box">
    <img
      class="image"
      src="https://cn.bing.com/th?id=OHR.ViennaAutumn_ZH-CN7011999199_1920x1080.webp"
      alt=""
    />
  </div>
  <div class="label">box</div>
</div>

<div class="box-wrap">
  <div class="box box--center">
    <img
      class="image"
      src="https://cn.bing.com/th?id=OHR.ViennaAutumn_ZH-CN7011999199_1920x1080.webp"
      alt=""
    />
    <div class="label">box--center</div>
  </div>
</div>

css:元素居中整理水平居中、垂直居中、水平垂直居中_css_05

3、水平垂直居中

使用绝对定位实现:子绝父相(子元素绝对定位,父元素相对定位)

.box-wrap--center {
    position: relative;
  }

  .box-wrap--center .box {
    position: absolute;
    left: 50%; /* x轴方向相对父元素的宽移动 50% */
    top: 50%; /* y轴方向相对父元素的高移动 50% */
    transform: translate(-50%, -50%); /* x轴、y轴方向相对自身元素的宽、高移动 -50% */
  }

示例

<style>
  body {
    background-color: #eeeeee;
    display: flex;
  }

  .box-wrap {
    height: 300px;
    width: 300px;
    background-color: green;

    margin-right: 20px;
  }

  .box {
    background-color: pink;
    height: 100px;
    width: 100px;
  }

  .box-wrap--center {
    position: relative;
  }

  .box-wrap--center .box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
</style>

<div class="box-wrap">
  <div class="box"></div>
</div>

<div class="box-wrap box-wrap--center">
  <div class="box "></div>
</div>

css:元素居中整理水平居中、垂直居中、水平垂直居中_javascript_06

参考文章

  1. 前端开发中的各种居中问题,小小总结一下


标签:居中,box,center,color,50%,垂直,background,css
From: https://blog.51cto.com/mouday/8194759

相关文章

  • css:transform实现平移、旋转、缩放、倾斜元素
    目录文档语法示例旋转元素transform-rotate旋转过渡旋转动画参考文章文档https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform语法/*Keywordvalues*/transform:none;/*Functionvalues*/transform:matrix(1,2,3,4,5,6);transform:translate(12px,50......
  • css常见的居中操作(一)
    原创声明:本文所有图片和代码皆由本人制作和编写。目录目标当前html毛坯房方法一纯CSS2第0步排版之前的一些css基本设置第1步图标与文字对齐第2步把li居于ul的垂直中部且均匀分布第3步li的子内容水平居中于li方法二使用CSS3的flex第0步排版之前的一些css基本设置第1步图......
  • [CSS]关于<img>标签距离底部盒子5px的问题
     问题描述:在某个盒子内部放入一个<img>标签,不写样式的情况下,<img>总是和父盒子有5px空隙。<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>清除图片多5px问题</title><linkrel="stylesheet"......
  • CSS必学:元素之间的空白与行内块的幽灵空白问题
    作者:WangMin格言:努力做好自己喜欢的每一件事CSDN原创文章博客地址......
  • CSS学习
    知识体系                           案例整理案例:1)需求:商品页面2)代码:<!DOCTYPEhtml><html><head><title>商品页面</title><style>body{height:100%;margin......
  • css的浮动和定位
    CSS是前端开发中不可或缺的一部分,其中浮动和定位是常用的布局方式。本篇博客将详细介绍浮动和定位的概念、用法和注意事项,适合新手学习。一、浮动1.概念浮动是指将元素从正常的文档流中移除,使其脱离文档流并向左或向右移动,直到其外边缘碰到包含块或另一个浮动元素的边缘为止。......
  • 一些有用的css函数
    var使用自定义的属性值。:root{--main-bg-color:pink;}body{background-color:var(--main-bg-color);}attr使用html上data-*属性引用的文本。<pdata-foo="hello">world</p>p:before{content:attr(data-foo)"";}属性也可以被解析为colo......
  • 使用CSS写一个带追踪特效的渐变按钮
    写一个带追踪特效的渐变按钮开头先观看这张GIF图:是否被它的出色动画效果所吸引?这是从一个完美竞技平台中录制出来的我脑海中萌生了用CSS来模仿这一效果的念头绘画元素我们先记录下这个按钮浮动的颜色(#868BFF),还有按钮的背景的渐变色(#39D5FF->#868bff)外部使用一个div元素表......
  • postcss-pxtorem 使用和问题
    postcss-pxtorem是存放在postcss.config.js文件里的。结构如下:module.exports={ plugins:{  autoprefixer:{},  'postcss-pxtorem':{   rootValue({file}){    return10//尺寸   },   propList:['*'],   //替......
  • PyQt5-如何设置主窗口居中?退出应用程序如何操作?
    (15如何设置主窗口居中?退出应用程序如何操作?)1如何实现主窗口居中显示?让主窗口居中,其实就是让窗口的左右边缘到左右屏幕距离相等,让窗口的上下边缘到上下屏幕的距离相等;主要是需要进行计算和移动工作;可以使用QDesktopWidget类来获取屏幕的大小和位置信息,然后根据这些信息计......