css垂直居中的几种方法

2025-10-16 13:26:08

1、鼠标点击打开HBuilder软件,如图所示:

css垂直居中的几种方法

2、新建一个Web项目,如图所示:

css垂直居中的几种方法

3、输入web项目名CSS,然后点击完成按钮,如图所示:

css垂直居中的几种方法

4、然后在html文件中键入如下代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>CSS垂直居中的几种方法?</title>

</head>

<style type="text/css">

#one1 {

display: table;

background-color: red;

}

#frist1 {

display: table-cell;

vertical-align: middle;

}

.second{

position: absolute;

top: 50%;

height: 240px;

margin-top: -120px; /* negative half of the height */

}

#three {

float: left;

height: 50%;

margin-bottom: -120px;

}

#thrid {

clear: both;

height: 240px;

position: relative;

}

#four {

height: 100px;

line-height: 100px;

}

</style>

<body>

<h4>方法一、</h4>

<div id="one1">

<div id="frist1">

<div>方法一内容</div>

</div>

</div>

<br/>

<h4>方法二、</h4>

<div class="second"> 方法二内容</div>

<br/>

<h4>方法三、</h4>

<div id="three">

<div id="thrid">方法三内容</div>

</div>

<br/>

<h4>方法四、</h4>

<div id="four">方法四内容</div>

</body>

</html>

css垂直居中的几种方法

5、然后点击运行按钮,点击运行-->浏览器运行,如图所示:

css垂直居中的几种方法

6、在浏览器中查看居中结果,有些属性是文字居中,如图所示:

css垂直居中的几种方法

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢