广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    如何使DIV中的内容居中

    来源:网络收集  点击:  时间:2024-03-06
    【导读】:

    只需要加上padding元素,内容四周便会留下空白,实现水平垂直居中的效果

    css代码如下:

    .demo{

    width: 200px;

    border: 1px solid red;

    padding: 20px;

    }

    HTML代码如下:

    div class=demo

    this is a test of margin

    this is a test of margin

    this is a test of margin

    this is a test of margin

    this is a test of margin

    /div

    效果是这样的:

    扩展资料:

    关于上述中div设置具体高度

    1、内容只有一行

    设置div的line-height和div的高度一样即可。

    2、内容不确定有几行

    这时候需要在div中再加一层结构,用p标签或者div都可以。

    css代码如下:

    .demo{

    position: absolute;

    width: 200px;

    height: 200px;

    border: 1px solid red;

    }

    p{

    position: absolute;

    width: 150px;

    top: 50%;

    left:50%;

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

    border: 1px solid black;

    }

    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_256683.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-07 02:48:32  耗时:0.026
    0.0257s