html主题居中用什么命令,css中常用的几种居中方法(推荐)

在前端面试中,大都会问你div居中的方法:

文笔不好,就随便寥寥几句话概括了,

不过以后文笔肯定会变得更好一些的。

今天我们就细数一下几种方法:

1,使用position:absolute,设置left、top、margin-left、margin-top的属性

CSS Code复制内容到剪贴板

.one{

position:absolute;

width:200px;

height:200px;

top:50%;

left:50%;

margin-top:-100px;

margin-left:-100px;

background:red;

}

这种方法基本浏览器都能够兼容,不足之处就是需要固定宽高。

2,使用position:fixed,同样设置left、top、margin-left、margin-top的属性

CSS Code复制内容到剪贴板

.two{

position:fixed;

width:180px;

height:180px;

top:50%;

left:50%;

margin-top:-90px;

margin-left:-90px;

background:orange;

}

大家都知道的position:fixed,IE是不支持这个属性的

3,利用position:fixed属性,margin:auto这个必须不要忘记了。

CSS Code复制内容到剪贴板

.three{

position:fixed;

width:160px;

height:160px;

top:0;

rightright:0;

bottombottom:0;

left:0;

margin:auto;

background:pink;

}

4,利用position:absolute属性,设置top/bottom/right/left

CSS Code复制内容到剪贴板

.four{

position:absolute;

width:140px;

height:140px;

top:0;

rightright:0;

bottombottom:0;

left:0;

margin:auto;

background:black;

}

5,利用display:table-cell属性使内容垂直居中

CSS Code复制内容到剪贴板

.five{

display:table-cell;

vertical-align:middle;

text-align:center;

width:120px;

height:120px;

background:purple;

}

6,最简单的一种使行内元素居中的方法,使用line-height属性

CSS Code复制内容到剪贴板

.six{

width:100px;

height:100px;

line-height:100px;

text-align:center;

background:gray;

}

这种方法也很实用,比如使文字垂直居中对齐

7,使用css3的display:-webkit-box属性,再设置-webkit-box-pack:center/-webkit-box-align:center

CSS Code复制内容到剪贴板

.seven{

width:90px;

height:90px;

display:-webkit-box;

-webkit-box-pack:center;

-webkit-box-align:center;

background:yellow;

color:black;

}

8,使用css3的新属性transform:translate(x,y)属性

CSS Code复制内容到剪贴板

.eight{

position:absolute;

width:80px;

height:80px;

top:50%;

left:50%;

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

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

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

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

background:green;

}

这个方法可以不需要设定固定的宽高,在移动端用的会比较多,在移动端css3兼容的比较好

9、最高大上的一种,使用:before元素

CSS Code复制内容到剪贴板

.nine{

position:fixed;

display:block;

top:0;

rightright:0;

bottombottom:0;

left:0;

text-align:center;

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

}

.nine:before{

content:'';

display:inline-block;

vertical-align:middle;

height:100%;

}

.nine .content{

display:inline-block;

vertical-align:middle;

width:60px;

height:60px;

line-height:60px;

color:red;

background:yellow;

}

这种方法在我的前面一片文章有详细的介绍:

以上这篇css中常用的几种居中方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 实现垂直居中一直是Web前端开发比较棘手的问题。下面介绍几种常用CSS垂直居中方法。 一、利用flex布局实现垂直居中 父元素设置display:flex; align-items:center; justify-content:center; 实现子元素的垂直居中。 二、利用绝对定位实现垂直居中 将子元素设置为绝对定位,通过设置top:50%; transform:translateY(-50%);实现子元素的垂直居中。 三、利用table-cell属性实现垂直居中 将父元素的display属性设置为table-cell,vertical-align属性设置为middle,实现子元素的垂直居中。 四、利用line-height属性实现垂直居中 将父元素和子元素的line-height属性设置为相同的值,并将子元素设置为display:inline-block,实现子元素的垂直居中。 综上所述,以上列举了常用的四种方法来实现CSS垂直居中,根据需要选择合适的方法来解决垂直居中问题。 ### 回答2: 在网页设计,垂直居中是一个比较常见的需求。但是,实现垂直居中并不是一件容易的事情。在CSS,有几种方法可以实现垂直居中,下面介绍一些常用方法: 1.使用flexbox Flexbox是一种强大的CSS布局模式,可以很容易地实现垂直居中。通过将容器的display属性设置为flex,然后将align-items属性设置为center,就可以将内容垂直居中。 ``` .container { display: flex; align-items: center; justify-content: center; } ``` 2.使用position 在容器使用绝对定位可以实现垂直居中。将子元素的position属性设置为absolute,并将top和bottom都设置为0,然后将margin属性设置为auto,这样就可以将内容垂直居中。 ``` .container { position: relative; } .child { position: absolute; top: 0; bottom: 0; margin: auto; } ``` 3.使用table-cell 将容器的display属性设置为table,子元素的display属性设置为table-cell,并将vertical-align属性设置为middle,就可以将内容垂直居中。 ``` .container { display: table; } .child { display: table-cell; vertical-align: middle; } ``` 4.使用transform 使用transform属性可以让内容在垂直方向上居中。将子元素的position属性设置为absolute,然后将top和left都设置为50%,再使用transform: translate(-50%, -50%),就可以将内容垂直居中。 ``` .container { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` 以上是一些常用CSS垂直居中方法,可以根据具体需求选择合适的方法。 ### 回答3: 在网页设计,垂直居中元素是一个非常常见的需求,而CSS也提供了多种方法实现垂直居中。下面我们来介绍一些常用的垂直居中方法。 1. 使用Flexbox布局 Flexbox是CSS3引入的一种灵活的基于容器和项目的布局方式。我们可以通过设置容器的display属性为flex,再设置justify-content和align-items属性来实现元素的水平和垂直居中。 例如,将含有一个子元素的容器水平和垂直居中,可以将CSS代码写成这样: ``` .container { display: flex; justify-content: center; align-items: center; } .container > div { width: 200px; height: 100px; background-color: #f00; } ``` 2. 使用绝对定位和负边距 我们可以将要居中的元素使用绝对定位,然后设置top、bottom、left、right属性为0,同时使用margin:auto来实现水平居中。 例如: ``` .parent { position: relative; } .child { position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; width: 200px; height: 100px; background-color: #f00; } ``` 但这种方法需要在父元素声明定位属性为relative或absolute。 3. 使用transform 我们可以使用CSS3的transform属性,将要垂直居中的元素的translateY属性设置为-50%。 例如: ``` .parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translateY(-50%); width: 200px; height: 100px; background-color: #f00; } ``` 这种方法同样需要在父元素声明定位属性为relative或absolute。 4. 使用flex布局和auto margin Flexbox布局使用auto margin来实现水平和垂直居中。 例如: ``` .parent { display: flex; } .child { margin: auto; width: 200px; height: 100px; background-color: #f00; } ``` 这种方法非常简单,但需要注意父元素需要设置为flex布局。 总的来说,以上这些方法都可以实现垂直居中,可以根据具体情况选择适合自己的方式。同时,我们也可以根据需求结合使用不同的方法实现垂直居中

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值