TextAlign 与 margin:0 auto

本文详细解释了CSS中text-align:center与margin:0 auto两种不同的居中方式,并探讨了它们在不同浏览器中的表现差异,以及如何正确应用于块元素与内联元素。

margin:0 auto 与 text-align:center 的区别

基本概念:

1.text-align: 属性规定元素中的文本的水平对齐方式; 

       该属性通过指定行框与哪个点对齐,从而设置块级元素内文本的水平对齐方式;

       一般情况下设置文本对齐方式的时使用此属性。支持值 justify。

  Example: div { text-align: left; }  //文本居左对齐

  注释:所有浏览器都支持 text-align 属性;任何的版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit"。

2.margin 是设置对象四边的外延边距,被称为外补丁或外边距。

  Example: div { margin: 20px 10px 30px 40px; }  // 表示对象外边距,顶20px、右10px、下30px、左40px

区别如下:

1.text-align:center 设置文本或img标签等一些内联对象(或与之类似的元素)的居中。

2.margin:0 auto 设置块元素(或与之类似的元素)的居中。

这两个属性IE与FF的理解也有所不同。我们设置一个段落P,在段落内存在一个图片img标签。  

注意:

1.当设置body{text-align:center;}。 在IE中,段落P,图片img同时实现了居中对齐,也就是说text-align:center;同时作用于元素p与元素img。 在FF中,段落P,没有能实现居中对齐,而图片img实现了居中对齐,也就是说text-align:center;作用于img标签,而段落p标签没有起到居中的作用。

2.当设置段落 p {margin:0 auto;}。 在IE与FF中,段落P均实现了居中对齐。图片img由于不是作用对象,所以不会居中对齐。

有三种情况需要说明: 1.margin:0 auto;的选择器是作用对象,如div,p,而不是body。如果设置:body { margin:0 auto; }将不会达到任何效果,除非你定义body的宽度,那将会让body内的元素产生位置变化。如我们设置body宽度为500px。对p段落不作任何设置, 我们最大化窗口将会看到段落并非处于窗口的最左上角。 2.设置段落 p {text-align:center;} 将要实现的并不是段落本身的对齐方式,而是段落内元素居中对齐。 3. 设置图片标签img {margin:0 auto;} ,就犯了一个小错误,img类于内联对象,不可以设置图片img标签的margin属性,如果一定要设置,那么先将它的属性转变为块元素,如下面的代 码:img {display:block; margin:0 auto;}

知识点:块元素,内联元素的区别。

import {Button, Card, Col, Input, Row} from "antd"; import React from "react"; const Dashboard = () => { const mockData = [ { name: '业务系统资产数', value: 1234, week: '1296%', day: '119%' }, { name: '已加工模型数', value: 1234, week: '1296%', day: '119%' }, // ...其他数据 ]; const businessAreas = [ { name: '研发', value: 1234 }, { name: '生产', value: 1234 } ]; //点击查询时 调用的方法 const handleFormSubmit = () => {} return ( <div> <div style={{ textAlign: 'center' ,marginTop:'10%' }}> <div><img src = '/icons/meihaoshenghuo.png'/></div> <Input placeholder="查报表-查数据" size="large" style={{ maxWidth: 522, margin: '0 20px',marginTop:'1px',borderRadius: '8px' }} /> <Button type="primary" style={{ margin: '0 ',backgroundColor:'red',border:"none" }} onClick={handleFormSubmit}>查询</Button> </div> <div style={{ textAlign: 'center' ,marginTop:'10%' }}> <Row gutter={5} justify="center" style={{ width: '100%' }}> <Card title="‌**功能变动**‌" extra={<Button type="link">‌**更多 >**‌</Button>} style={{ width: 150, margin: '20px auto' }} > <p>‌**功能上下线**‌<br />‌**重要通知**‌</p> </Card> <Card title="‌**功能变动**‌" extra={<Button type="link">‌**更多 >**‌</Button>} style={{ width: 150, margin: '20px auto' }} > <p>‌**功能上下线**‌<br />‌**重要通知**‌</p> </Card> <Card title="‌**功能变动**‌" extra={<Button type="link">‌**更多 >**‌</Button>} style={{ width: 150, margin: '20px auto' }} > <p>‌**功能上下线**‌<br />‌**重要通知**‌</p> </Card> <Card title="‌**功能变动**‌" extra={<Button type="link">‌**更多 >**‌</Button>} style={{ width: 150, margin: '20px auto' }} > <p>‌**功能上下线**‌<br />‌**重要通知**‌</p> </Card> </Row> <Row gutter={16}> <Col span={24}> <Card title="按业务域"> <Row gutter={16}> {businessAreas.map((item, index) => ( <Col key={index} span={6}> <Card title={item.name}> {item.value} </Card> </Col> ))} </Row> </Card> </Col> </Row> </div> </div> ); }; export default Dashboard; 修改以上代码,使得功能变动 这几个card 居中显示,card之间间隔20px,且card整体上方搜索框对齐
07-06
<div id="tabbar" v-if="true" class="tabbar" :style='{"padding":"30px 0","boxShadow":"0 0 6px ","margin":"30px 0 0 0","borderColor":"rgba(255, 255, 255, 0.3)","backgroundColor":"rgba(6, 82, 121, 1)","borderRadius":"0","borderWidth":"0","borderStyle":"solid"}' style="position: relative;z-index: 999;"> <img v-if='false' :style='{"boxShadow":"0 0 6px rgba(255,0,0,.8)","margin":"0 auto","borderColor":"rgba(0,0,0,.3)","borderRadius":"100%","borderWidth":"1px","width":"44px","borderStyle":"solid","height":"44px"}' style='display: block;' src='http://codegen.caihongy.cn/20201024/ed5e326ca66f403aa3197b5fbb4ec733.jpg'/> <div :style='{"padding":"0 10px","boxShadow":"0 0 6px rgba(255,0,0,.8)","margin":"10px auto","borderColor":"rgba(0,0,0,1)","backgroundColor":"#000","color":"#fff","textAlign":"center","borderRadius":"0","borderWidth":"0","width":"100%","lineHeight":"32px","fontSize":"14px","borderStyle":"solid"}' class="company"></div> <div :style='{"padding":"0 10px","boxShadow":"0 0 6px rgba(255,0,0,0)","margin":"10px auto","borderColor":"rgba(0,0,0,1)","backgroundColor":"#000","color":"#fff","textAlign":"center","borderRadius":"0","borderWidth":"0","width":"100%","lineHeight":"32px","fontSize":"14px","borderStyle":"solid"}' class="record"></div> <div :style='{"padding":"0 10px","boxShadow":"0 0 0px ","margin":"10px auto 30px auto","borderColor":"rgba(6, 82, 121, 1)","backgroundColor":"rgba(6, 82, 121, 1)","color":"#fff","textAlign":"center","borderRadius":"0","borderWidth":"1px 0","width":"40%","lineHeight":"32px","fontSize":"14px","borderStyle":"solid"}' class="desc">电话:4008000000 | 邮箱:support@21epub.com </div> </div>
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值