分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

CSS居中

发布时间:2023-09-06 01:47责任编辑:熊小新关键词:CSS

1. margin:0 auto;
此方法只能进行水平居中且对浮动元素和绝对定位元素无效;
对子元素进行设置。

2.text-align:center;
只能对图片,按钮,文字等行内元素(display为inline或inline-block等)进行水平居中。IE6、7能对任何元素进行设置。对父元素进行设置。

3.line-heigt对单行文字垂直居中
把文字的line-height设为文字父容器高度时内部文字处置居中。

4.display:table-cell;
对于不是表格的元素可以通过display:table-cell来模拟表格,这样可以很方便的利用表格的属性来居中。

5.使用绝对定位来居中
此方法我必须知道元素的宽度和高度。例如下面的案例:

 ??<div id="background"> ???????<div class="left-top"></div> ???????<div class="right-bottom"></div> ???</div>
#background{ ???background-color: #ccc; ???width: 400px; ???height: 200px; ???position: absolute; ???left:50%; ???top:50%;margin-left: -200px; ???????//是以左上角对齐的,所以减去div宽高的一半实现中心对齐 ???margin-top: -100px; ?????overflow: hidden;}.left-top{ ???width:50px; ???height: 50px; ???background-color: #fc0; ???position:absolute; ???border-radius: 0 0 50px 0;}.right-bottom{ ???width:100px; ???height: 100px; ???background-color: #fc0; ???position:absolute; ???right:-50px; ???bottom:-50px; ???border-radius: 50px ;}

效果:

 


task_1_4_1

 

CSS居中

原文地址:https://www.cnblogs.com/MandyCheng/p/8656468.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved