CSS居中小谈-上下左右居中

http://web.jobbole.com/88804/

 

内联元素居中方案

 

水平居中设置:

  1. 行内元素 设置 text-align:center;
  2. Flex布局 设置display:flex;justify-content:center;(灵活运用,注意兼容性和前缀)

垂直居中设置:

  1. 父元素高度确定的单行文本(内联元素) 设置 line-height = height;
  2. 父元素高度确定的多行文本(内联元素)
    • a:插入 table (插入方法和水平居中一样),然后设置 vertical-align:middle;
    • b:先设置 display:table-cell 再设置 vertical-align:middle;

块级元素居中方案

水平居中设置:

  1. 定宽块状元素设置 左右 margin 值为 auto;
  2. 不定宽块状元素
  • a:在元素外加入 table 标签(完整的,包括 table、tbody、tr、td),该元素写在 td 内,然后 设置 margin 的值为 auto;
  • b:给该元素设置 displa:inine 方法;
  • c:父元素设置 position:relative 和 left:50%,子元素设置 position:relative 和 left:50%;

垂直居中设置:

1.使用position:absolute(fixed),设置left、top、margin-left、margin-top的属性;

CSS

1

2

3

4

5

6

7

.box{

position:absolute;/*或fixed*/

top:50%;

left:50%;

margin-top:-100px;

margin-left:-200px;

}

2.利用position:fixed(absolute)属性,margin:auto这个必须不要忘记了;

CSS

1

2

3

4

5

6

7

8

.box{

    position: absolute;或fixed

    top:0;

    right:0;

    bottom:0;

    left:0;

    margin: auto;

}

3.利用display:table-cell属性使内容垂直居中;

CSS

1

2

3

4

5

6

7

8

.box{

    display:table-cell;

    vertical-align:middle;

    text-align:center;

    width:120px;

    height:120px;

    background:purple;

}

4.使用css3的新属性transform:translate(x,y)属性;

CSS

1

2

3

4

5

6

7

.box{

    position: absolute;

    transform: translate(50%,50%);

    -webkit-transform:translate(50%,50%);

    -moz-transform:translate(50%,50%);

    -ms-transform:translate(50%,50%);

}

5.最高大上的一种,使用:before元素;

CSS

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

.box{

    position:fixed;

    display:block;

    background:rgba(0,0,0,.5);

}

.box:before{

    content:'';

    display:inline-block;

    vertical-align:middle;

    height:100%;

}

.box.content{

    width:60px;

    height:60px;

    line-height:60px;

    color:red;

6.Flex布局;

CSS

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

.box{

    display: -webkit-box;

    display: -webkit-flex;

    display: -moz-box;

    display: -moz-flex;

    display: -ms-flexbox;

    display: flex;

 

    水平居中

    -webkit-box-align: center;

    -moz-box-align: center;

    -ms-flex-pack:center;

    -webkit-justify-content: center;

    -moz-justify-content: center;

    justify-content: center;

 

     垂直居中

    -webkit-box-pack: center;

    -moz-box-pack: center;

    -ms-flex-align:center;

    -webkit-align-items: center;

    -moz-align-items: center;

    align-items: center;

}

 

结语

博主暂时掌握了这些居中方法,读者如果还有好方法或是觉得那个地方不对,欢迎评论,不吝感谢。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要让文字上下居中,可以使用CSS的`line-height`属性来实现。将`line-height`属性的值设置为元素的高度即可实现垂直居中。 例如,如果你有一个高度为50px的元素,可以使用以下CSS代码来实现文字上下居中: ``` .element { height: 50px; line-height: 50px; } ``` 如果你想将文字垂直居中到元素的底部,可以将`vertical-align`属性设置为`bottom`: ``` .element { height: 50px; line-height: 50px; vertical-align: bottom; } ``` 这样,文字就会垂直居中到元素的底部了。 ### 回答2: 在CSS中,我们可以使用`text-bottom`来控制文本在垂直方向上的对齐方式。具体而言,`text-bottom`属性将文本的基线与其父元素的底部对齐。 假设我们有一个包含文本的`<div>`元素,并且希望将文本在垂直方向上置于父元素的中间,我们可以按照以下步骤进行操作。 首先,我们需要设置父元素的`display`属性为`flex`,以便方便地进行垂直居中对齐。具体代码如下: ``` .parent { display: flex; align-items: center; } ``` 接下来,在父元素中嵌套一个`<span>`元素,作为包含文本的容器。然后,将这个`<span>`元素的样式设置为`text-bottom`,以达到上述对齐效果。具体代码如下: ``` .parent span { vertical-align: text-bottom; } ``` 最后,将文本添加到`<span>`元素中,即可实现上下居中对齐。 ``` <div class="parent"> <span>这里是文本</span> </div> ``` 总之,通过使用`text-bottom`属性以及结合`flex`布局,我们可以实现在CSS中将文本在垂直方向上置于父元素中间的效果。 ### 回答3: 在CSS中,可以使用text-bottom属性来实现文字对齐的上下居中效果。 text-bottom是一个相对于基线的度量值,它的作用是将元素中的文本内容垂直对齐到周围元素的底部。通过将text-bottom属性设置为0,可以达到文字在元素中垂直居中的效果。 下面是一个示例的CSS代码: ```css .container { display: flex; align-items: center; justify-content: center; height: 200px; /* 设置容器高度 */ border: 1px solid black; } .text { text-bottom: 0; /* 设置文本内容垂直居中 */ } ``` 通过以上的代码,可以实现一个容器中的文本内容在垂直方向上居中对齐。我们使用了flex布局来实现垂直和水平居中,align-items:center属性用于垂直居中,而justify-content:center属性用于水平居中。 以上是关于CSS的text-bottom属性的简单介绍和示例。通过对CSS样式的设置,我们可以轻松达到上下居中的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值