如何用css将底部的内容定位到顶部

html-css010

如何用css将底部的内容定位到顶部,第1张

将底部的内容定位到顶部,只要在css层上面加上一个浮动,将定位至top设置成0即可。说明如下:

position:absolute(将对象浮动)

top:0(将对象定位对顶部)

整体css示范如下:

<div style="position:absolute top:0 width:100% height:50px background:#000 color:#FFF text-align:center">内容顶部</div>

如要将内容放至底部,只需将top:0修改成bottom:0即可,整体css未范如下:

<div style="position:absolute bottom:0 width:100% height:50px background:#000 color:#FFF text-align:center">内容底部</div>

希望我的回答能令你满意!

1、首先先在页面里加载一张图片,代码和效果如下图所示:

2、然后设置给图片起一个class名,方便一会儿的操作。

3、然后给图片设置css样式,因为方便的原因就直接在html页面写css样式了。

4、经常使用“margin: 0 auto”来实现水平居中,而一直认为“margin: auto”是不能实现垂直居中,但是实际上,仅需要添加一些限制便能实现效果,就是通过定位:

position: absolute

top: 0

left: 0

bottom: 0

right: 0

设置定位让上下左右都为0,然后通过margin:0 auto,来让元素实现上下左右都居中。

5、设置完CSS样式之后,通过浏览查看代码的效果就可以,可以看到图片已经实现了。

6、最后给大家附上全部的代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>使用CSS让图片水平垂直居中</title>

</head>

<body>

<img class="pic" src="img/timg.jpg" alt="" />

</body>

<style type="text/css">

.pic{

margin: auto

position: absolute

top: 0

left: 0

bottom: 0

right: 0

}

</style>

</html>