让一个div里面的其它div或者标签内容居中

一、行高(line-height)法

如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如:

p { height:30px; line-height:30px; width:100px; overflow:hidden; }

这段代码可以达到让文字在段落中垂直居中的效果。

二、内边距(padding)法

另一种方法和行高法很相似,它同样适合一行或几行文字垂直居中,原理就是利用padding将内容垂直居中,比如:

p { padding:20px 0; }

这段代码的效果和line-height法差不多。

三、模拟表格法

将容器设置为display:table,然后将子元素也就是要垂直居中显示的元素设置为display:table-cell,然后加上vertical-align:middle来实现。

html结构如下:

<div id="wrapper">
    <div id="cell">
        <p>测试垂直居中效果测试垂直居中效果p>
        <p>测试垂直居中效果测试垂直居中效果p>
    div>
div>

css代码:

#wrapper {display:table;width:300px;height:300px;background:#000;margin:0 auto;color:red;}
#cell{display:table-cell; vertical-align:middle;}

实现如图所示:

遗憾的是IE7及以下不支持。

四、CSS3的transform来实现

css代码如下:

.center-vertical{
  position: relative;
  top:50%;
  transform:translateY(-50%);
}.center-horizontal{
  position: relative;
  left:50%;
  transform:translateX(-50%); 
}

五:css3的box方法实现水平垂直居中

html代码:

<div class="center">
  <div class="text">
    <p>我是多行文字p>
    <p>我是多行文字p>
    <p>我是多行文字p>
  div>
</div>

css代码:

.center {
  width: 300px;
  height: 200px;
  padding: 10px;
  border: 1px solid #ccc;
  background:#000;
  color:#fff;
  margin: 20px auto;

display: -webkit-box; -webkit-box-orient: horizontal; -webkit-box-pack: center; -webkit-box-align: center; display: -moz-box; -moz-box-orient: horizontal; -moz-box-pack: center; -moz-box-align: center; display: -o-box; -o-box-orient: horizontal; -o-box-pack: center; -o-box-align: center; display: -ms-box; -ms-box-orient: horizontal; -ms-box-pack: center; -ms-box-align: center; display: box; box-orient: horizontal; box-pack: center; box-align: center; }

结果如图:

html文件要放在哪里,script 标签应该放在html文件的什么位置

问题: 当在html文件中嵌入script标签时,正确的位置应该放在哪里?

分析

当浏览器加载一个含有

1 获取HTML文件,拉取HTML页面(比如:index.html)

2 开始解析html文件

3 当解析器遇到一个

4 当解析器获取js文件时,同时中断了页面上其他html的解析

5 一段时间后,js文件解析完毕,页面上其他的html标签继续解析

在第四步的时候,页面上的html解析阻断,给用户带来了不好的体验。

为什么会出现中断html解析?

任何script代码都能改变HTML的结构,通过document.write() 这种方式或者其他方式。 这就导致了HTML解析必须等待

然而,大部分的Javascript开发者在加载文档过程中,不会通过script操作HTML的DOM结构。然而,他们必须等到

废弃的解决方式

之前解决这个问题的方式是将

但这种加载方式存在的问题就是只有当所有的html元素加载完成后才能开始加载

现在的解决方案

现在浏览器

async

async标记的

上述代码 script2 可能比 script1 先下载完并执行完。

defer

defer标签的script顺序执行,这种方式也不会阻断浏览器解析HTML,同时下载结束时

上述代码中 script2 肯定比 script1 后执行完

结论

使用 async 和 defer属性来让你的网页不被block,但是前提要确保你的浏览器支持 这两个属性。

本站内容来自用户投稿,如果侵犯了您的权利,请与我们联系删除。联系邮箱:835971066@qq.com

本文链接:http://news.xiuzhanwang.com/post/2364.html

友情链接: