CSS3 FLEXBOX轻松实现元素的水平居中和垂直居中

前端开发   发布日期:2023年05月29日   浏览次数:51
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <!--1.清除输入框内的空格-->
  9. <!--<input type="text" onBlur="replaceSpace(this)" />
  10. <script type="text/javascript">
  11. function replaceSpace(obj) {
  12. obj.value = obj.value.replace(/\s/gi, '')
  13. }
  14. </script>-->
  15. <!--2.CSS3 FLEXBOX轻松实现元素的水平居中和垂直居中-->
  16. <!--①单个元素水平居中-->
  17. <!--<style>
  18. .box{
  19. display: -webkit-flex;
  20. display: -ms-flexbox;
  21. display: flex;
  22. justify-content: center;
  23. background: #0099cc;
  24. }
  25. h1{
  26. color: #FFF
  27. }
  28. </style>
  29. <div class="box">
  30. <h1>flex弹性布局justify-content属性实现元素水平居中</h1>
  31. </div>-->
  32. <!--②多个h1元素水平居中-->
  33. <!--<style>
  34. .box{
  35. display: flex;
  36. justify-content: center;
  37. width: 100%;
  38. background: #0099cc
  39. }
  40. h1{
  41. font-size: 1rem;
  42. padding: 1rem;
  43. border: 1px dashed #FFF;
  44. color: #FFF;
  45. font-weight: normal;
  46. }
  47. </style>
  48. <div class="box">
  49. <h1>flex弹性布局justify-content属性实现元素水平居中</h1>
  50. <h1>flex弹性布局justify-content属性实现元素水平居中</h1>
  51. <h1>flex弹性布局justify-content属性实现元素水平居中</h1>
  52. </div>-->
  53. <!--③多个h1标签并排垂直居中-->
  54. <!--<style>
  55. .box{
  56. display: flex;
  57. width: 980px;
  58. height: 30rem;
  59. align-items:center;
  60. background: #0099cc
  61. }
  62. h1{
  63. font-size: 1rem;
  64. padding: 1rem;
  65. border: 1px dashed #FFF;
  66. color: #FFF
  67. }
  68. </style>
  69. <div class="box">
  70. <h1>flex弹性布局justify-content属性实现元素水平居中</h1>
  71. <h1>flex弹性布局justify-content属性实现元素水平居中</h1>
  72. <h1>flex弹性布局justify-content属性实现元素水平居中</h1>
  73. </div>-->
  74. <!--④同时包含水平,垂直居中-->
  75. <!--<style>
  76. body{display: flex;justify-content:center}
  77. .box{
  78. display: flex;
  79. width: 980px;
  80. height: 30rem;
  81. justify-content:center;
  82. background: #0099cc;
  83. flex-direction:column;
  84. align-items:center;
  85. }
  86. h1{
  87. display: flex;
  88. justify-content:center;
  89. font-size: 1rem;
  90. padding: 1rem;
  91. border: 1px dashed #FFF;
  92. color: #FFF;
  93. width: 28rem
  94. }
  95. </style>
  96. <div class="box">
  97. <h1>flex弹性布局justify-content属性实现元素垂直居中</h1>
  98. <h1>flex弹性布局justify-content属性实现元素垂直居中</h1>
  99. <h1>flex弹性布局justify-content属性实现元素垂直居中</h1>
  100. <h1>flex弹性布局justify-content属性实现元素垂直居中</h1>
  101. </div>-->
  102. <!--3.CSS3中的px,em,rem,vh,vw辨析-->
  103. <!--1、px:像素,精确显示
  104. 2、em:继承父类字体的大小,相当于“倍”,如:浏览器默认字体大小为16px=1em,始终按照div继承来的字体大小显示,进场用于移动端
  105. em换算工具:http://www.runoob.com/tags/ref-pxtoemconversion.html
  106. 3、rem:与em类似,rem是继承根节点的属性(即<html>标签),其他的与em相同
  107. 4、vh:当前可见高度的1%=1vh
  108. 区别是:当div中没有内容时,height=100%,则高度不显示出来
  109. 当div中没有内容时,height=100vh,则高度依然能显示出来
  110. 5、vw:当前可见宽度的1%=1vw
  111. 区别是:当div中没有内容时,width=100%,则宽度不显示出来
  112. 当div中没有内容时,width=100vh,则宽度依然能显示出来-->
  113. <!--4.CSS全局控制-->
  114. <!--<style>
  115. body {
  116. margin: 0;
  117. padding: 0;
  118. font-size: 14px;
  119. line-height: 22px;
  120. height: auto;
  121. font-family: "微软雅黑";
  122. color: #3e3938;
  123. -webkit-text-size-adjust: none;
  124. }
  125. body,div,p,h1,h2,h3,h4,h5,h6,ul,ol,li,dl,dd,dt,img,form {
  126. padding: 0px;
  127. margin: 0px;
  128. border: 0;
  129. font-size: 14px;
  130. }
  131. .clear {
  132. clear: both;
  133. }
  134. a:link,
  135. a:visited {
  136. text-decoration: none;
  137. }
  138. a:hover {
  139. text-decoration: none;
  140. }
  141. a:focus {
  142. outline: none;
  143. }
  144. a {
  145. text-decoration: none;
  146. blr: expression(this.onFocus=this.blur());
  147. }
  148. a {
  149. outline: none;
  150. }
  151. ul,
  152. ol li {
  153. list-style: none;
  154. }
  155. </style>-->
  156. <!--5.css水平垂直居中(绝对定位居中)-->
  157. <!--<style>
  158. #box {
  159. position: relative;
  160. height: 500px;
  161. background: #ddd;
  162. }
  163. .child {
  164. width: 100px;
  165. height: 100px;
  166. background: #269ABC;
  167. margin: auto;
  168. position: absolute;
  169. top: 0;
  170. right: 0;
  171. bottom: 0;
  172. left: 0;
  173. }
  174. </style>
  175. <div >
  176. <div class="child"></div>
  177. </div>-->
  178. <!--6.css水平垂直居中(绝对定位居中)-->
  179. <!--<style type="text/css">
  180. ul li
  181. {
  182. list-style-type:georgian;
  183. text-align:left;
  184. }
  185. .mark
  186. {
  187. width:140px;
  188. height:40px;
  189. color:Olive;
  190. text-align:center;
  191. line-height:40px;
  192. margin:5px;
  193. float:left;
  194. }
  195. .redball
  196. {
  197. width:40px;
  198. height:40px;
  199. border-radius:20px;
  200. background-color:Red;
  201. text-align:center;
  202. line-height:40px;
  203. margin:5px;
  204. float:left;
  205. }
  206. .ball
  207. {
  208. width:40px;
  209. height:40px;
  210. border-radius:20px;
  211. background-color:Aqua;
  212. text-align:center;
  213. line-height:40px;
  214. margin:5px;
  215. float:left;
  216. }
  217. .line
  218. {
  219. clear:left;
  220. }
  221. header
  222. {
  223. height:80px;
  224. border:1px solid gray;
  225. }
  226. .left
  227. {
  228. border:1px solid gray;
  229. float:left;
  230. width:30%;
  231. height:480px;
  232. margin-left:0px;
  233. margin-right:0px;
  234. }
  235. aside
  236. {
  237. text-align:center;
  238. }
  239. section
  240. {
  241. width:69.5%;
  242. float:left;
  243. height:480px;
  244. border:1px solid gray;
  245. margin-left:0px;
  246. margin-right:0px;
  247. }
  248. footer
  249. {
  250. clear:left;
  251. height:60px;
  252. border:1px solid gray;
  253. }
  254. input[type="button"]
  255. {
  256. width:80px;
  257. text-align:center;
  258. margin-top:10px;
  259. }
  260. </style>
  261. <header>
  262. <h1>直接插入排序(Straight Insertion Sort)Demo</h1>
  263. </header>
  264. <aside class="left">
  265. <input type="button" />
  266. <br />
  267. <input type="button" />
  268. <br />
  269. <input type="button" />
  270. <br />
  271. <h3>直接插入排序(Straight Insertion Sort)</h3>
  272. <ul>
  273. <li>将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表。</li>
  274. <li>即:先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插入,直至整个序列有序为止。</li>
  275. <li>如果碰见一个和插入元素相等的,那么插入元素把想插入的元素放在相等元素的后面。所以,相等元素的前后顺序没有改变。</li>
  276. <li>从原无序序列出去的顺序就是排好序后的顺序,所以插入排序是稳定的。</li>
  277. <li>时间复杂度O(n<sup>2</sup>)</li>
  278. </ul>
  279. </aside>
  280. <section >
  281. </section>
  282. <footer>
  283. 这是底部信息
  284. </footer>
  285. <script type="text/javascript">
  286. function initDiv() {
  287. var mainArea = document.getElementById("mainArea");
  288. for (var i = 0; i < 8; i++) {
  289. var newDivLine = document.createElement("div");
  290. newDivLine.setAttribute("class", "line");
  291. mainArea.appendChild(newDivLine);
  292. for (var j = 0; j < 9; j++) {
  293. var newDiv = document.createElement("div");
  294. var id = i.toString() + j.toString();
  295. newDiv.setAttribute("id", id);
  296. if (j < 8) {
  297. newDiv.setAttribute("class", "ball");
  298. } else {
  299. newDiv.setAttribute("class", "mark");
  300. }
  301. newDivLine.appendChild(newDiv);
  302. }
  303. }
  304. }
  305. //初始元素赋值
  306. var arrTmp = [4, 6, 8, 7, 9, 2, 10, 1];
  307. function setElementsValue() {
  308. for (var i = 0; i < arrTmp.length; i++) {
  309. document.getElementById("0" + i.toString()).innerText = arrTmp[i];
  310. }
  311. document.getElementById("08").innerText = "原始数据";
  312. }
  313. //排序
  314. function setSISortValue() {
  315. for (var i = 1; i < arrTmp.length; i++) {
  316. var m = 0;//为了记录插入的位置,方便标记
  317. //若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入
  318. if (arrTmp[i] < arrTmp[i - 1]) {
  319. var x = arrTmp[i]; //复制为哨兵(临时变量),即存储待排序元素
  320. var j = i - 1;
  321. arrTmp[i] = arrTmp[i - 1]; //先后移一个元素
  322. //循环查找在有序表的插入位置,如果要插入的值小于,则继续查找,并将元素后移。
  323. while (x < arrTmp[j]) {
  324. arrTmp[j + 1] = arrTmp[j];
  325. j--;
  326. }
  327. //查找完毕后,插入到正确位置(即要插入的值大于前面的元素)
  328. arrTmp[j + 1] = x;
  329. m = j + 1;
  330. } else {
  331. m = i;
  332. }
  333. //显示出来
  334. for (var k = 0; k < arrTmp.length; k++) {
  335. document.getElementById((i).toString() + k.toString()).innerText = arrTmp[k];
  336. if (m == k) {
  337. document.getElementById((i).toString() + (k).toString()).setAttribute("class", "redball");
  338. }
  339. }
  340. document.getElementById((i).toString() + "8").innerText = "第 " + (i).toString() + " 趟排序";
  341. }
  342. }
  343. </script>-->
  344. <!--7.css水平垂直居中(绝对定位居中)-->
  345. <!--<style>
  346. .container{
  347. width: 500px;
  348. height: 400px;
  349. border: 2px solid #379;
  350. position: relative;
  351. }
  352. .inner{
  353. width: 480px;
  354. height: 380px;
  355. background-color: #746;
  356. position: absolute;
  357. top: 50%;
  358. left: 50%;
  359. margin-top: -190px;
  360. margin-left: -240px;
  361. }
  362. </style>
  363. <div class="container">
  364. <div class="inner"></div>
  365. </div>-->
  366. <!--8.css水平垂直居中(绝对定位居中)-->
  367. <!--<style>
  368. div{
  369. width: 300px;
  370. height: 300px;
  371. border: 3px solid #555;
  372. display: table-cell;
  373. vertical-align: middle;
  374. text-align: center;
  375. }
  376. img{
  377. vertical-align: middle;
  378. }
  379. </style>
  380. <div>
  381. <img src="img/HBuilder.png" />
  382. </div>-->
  383. <!--9.css水平垂直居中(绝对定位居中)-->
  384. <!--<style>
  385. .container{
  386. width: 300px;
  387. height: 200px;
  388. border: 3px solid #546461;
  389. display: -webkit-flex;
  390. display: flex;
  391. -webkit-align-items: center;
  392. align-items: center;
  393. -webkit-justify-content: center;
  394. justify-content: center;
  395. }
  396. .inner{
  397. border: 3px solid #458761;
  398. padding: 20px;
  399. }
  400. </style>
  401. <div class="container">
  402. <div class="inner">
  403. 我在容器中水平垂直居中
  404. </div>
  405. </div>-->
  406. </html>

 

以上就是CSS3 FLEXBOX轻松实现元素的水平居中和垂直居中的详细内容,更多关于CSS3 FLEXBOX轻松实现元素的水平居中和垂直居中的资料请关注九品源码其它相关文章!