来源:http://www.ido321.com/824.html
一、水平居中
1、内联元素居中:相对父级块级元素居中对齐
1: .center-children {
2: text-align: center;
3: }
2、块级元素居中:设置margin-left和margin-right为auto让它居中(同时还要设置width,否则它就会承满整个容器,无法看出居中效果)
1: .center-me {
2: margin: 0 auto;
3: }
如果有很多块级元素需要水平居中成一行,最好使用一个不同的display类型。这是一个使用inline-block和flex的例子。
演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/embedded/result/
1: <main class="inline-block-center"></main>
2: <div> <pre style="代码" class="precsshei"> 3: I'm an element that is block-like with my siblings and we're centered in a row.
4:
5: <div> <pre style="代码" class="precsshei"> 6: I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
7:
8: <div> <pre style="代码" class="precsshei"> 9: I'm an element that is block-like with my siblings and we're centered in a row.
10:
11:
12: <main class="flex-center"></main>
13: <div> <pre style="代码" class="precsshei"> 14: I'm an element that is block-like with my siblings and we're centered in a row.
15:
16: <div> <pre style="代码" class="precsshei"> 17: I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
18:
19: <div> <pre style="代码" class="precsshei"> 20: I'm an element that is block-like with my siblings and we're centered in a row.
21:
22:
css:
1: body {
2: background: #f06d06;
3: font-size: 80%;
4: }
5: main {
6: background: white;
7: margin: 20px 0;
8: padding: 10px;
9: }
10: main div {
11: background: black;
12: color: white;
13: padding: 15px;
14: max-width: 125px;
15: margin: 5px;
16: }
17: .inline-block-center {
18: text-align: center;
19: }
20: .inline-block-center div {
21: display: inline-block;
22: text-align: left;
23: }
24: .flex-center {
25: display: flex;
26: justify-content: center;
27: }
二、垂直居中
1、内联元素:设置相等的上下padding,或者利用line-height
1: .link {
2: padding-top: 30px;
3: padding-bottom: 30px;
4: }
文本不会换行的情况下,可以使用line-height,让其与height相等去对齐文本。
1: .center-text-trick {
2: height: 100px;
3: line-height: 100px;
4: white-space: nowrap;
5: }
多行的文本也可以利用上下等padding的方式也可以让多行居中,但是如果这方法没用,你可以让这些文字的容器按table cell模式显示,然后设置文字的vertical-align属性对齐,
演示地址: http://jsfiddle.net/Web_Code/5fvrwwk1/1/embedded/result/
html:
1:
8: <div class="center-table"> <pre style="代码" class="precsshei"> 9: <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
10:
css
1: body {
2: background: #f06d06;
3: font-size: 80%;
4: }
5: table {
6: background: white;
7: width: 240px;
8: border-collapse: separate;
9: margin: 20px;
10: height: 250px;
11: }
12: table td {
13: background: black;
14: color: white;
15: padding: 20px;
16: border: 10px solid white;
17: /* default is vertical-align: middle; */
18: }
19: .center-table {
20: display: table;
21: height: 250px;
22: background: white;
23: width: 240px;
24: margin: 20px;
25: }
26: .center-table p {
27: display: table-cell;
28: margin: 0;
29: background: black;
30: color: white;
31: padding: 20px;
32: border: 10px solid white;
33: vertical-align: middle;
34: }
2、块级元素
若元素有固定高度,可以这样垂直居中
1: .parent {
2: position: relative;
3: }
4: .child {
5: position: absolute;
6: top: 50%;
7: height: 100px;
8: margin-top: -50px; /* 如果没有使用: border-box; 的盒子模型则需要设置这个 */
9: }
如果不知道元素高度,则这样
1: .parent {
2: position: relative;
3: }
4: .child {
5: position: absolute;
6: top: 50%;
7: transform: translateY(-50%);
8: }
也可以使用flexbox
1: <main> </main>
2: <div> <pre style="代码" class="precsshei"> 3: I'm a block-level element with an unknown height, centered vertically within my parent.
4:
5:
1: body {
2: background: #f06d06;
3: font-size: 80%;
4: }
5: main {
6: background: white;
7: height: 300px;
8: width: 200px;
9: padding: 20px;
10: margin: 20px;
11: display: flex;
12: flex-direction: column;
13: justify-content: center;
14: resize: vertical;
15: overflow: auto;
16: }
17: main div {
18: background: black;
19: color: white;
20: padding: 20px;
21: resize: vertical;
22: overflow: auto;
23: }
三、同时水平和垂直居中
1、元素有固定高度和宽度:先绝对居中,然后上移和左移50%的宽度即可
1: //这种方案有极好的跨浏览器支持。
2: .parent {
3: position: relative;
4: }
5: .child {
6: width: 300px;
7: height: 100px;
8: padding: 20px;
9: position: absolute;
10: top: 50%;
11: left: 50%;
12: margin: -70px 0 0 -170px;
13: }
2、元素的高度和宽度未知或可变的:使用transofrm属性在两个方向都平移负50%
1: .parent {
2: position: relative;
3: }
4: .child {
5: position: absolute;
6: top: 50%;
7: left: 50%;
8: transform: translate(-50%, -50%);
9: }

HTML、CSS和JavaScript在Web开发中的作用分别是:1.HTML定义网页结构,2.CSS控制网页样式,3.JavaScript添加动态行为。它们共同构建了现代网站的框架、美观和交互性。

HTML的未来充满了无限可能。1)新功能和标准将包括更多的语义化标签和WebComponents的普及。2)网页设计趋势将继续向响应式和无障碍设计发展。3)性能优化将通过响应式图片加载和延迟加载技术提升用户体验。

HTML、CSS和JavaScript在网页开发中的角色分别是:HTML负责内容结构,CSS负责样式,JavaScript负责动态行为。1.HTML通过标签定义网页结构和内容,确保语义化。2.CSS通过选择器和属性控制网页样式,使其美观易读。3.JavaScript通过脚本控制网页行为,实现动态和交互功能。

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增强WebevebDevelopment。

HTML是构建网页结构的基石。1.HTML定义内容结构和语义,使用、、等标签。2.提供语义化标记,如、、等,提升SEO效果。3.通过标签实现用户交互,需注意表单验证。4.使用、等高级元素结合JavaScript实现动态效果。5.常见错误包括标签未闭合和属性值未加引号,需使用验证工具。6.优化策略包括减少HTTP请求、压缩HTML、使用语义化标签等。

HTML是一种用于构建网页的语言,通过标签和属性定义网页结构和内容。1)HTML通过标签组织文档结构,如、。2)浏览器解析HTML构建DOM并渲染网页。3)HTML5的新特性如、、增强了多媒体功能。4)常见错误包括标签未闭合和属性值未加引号。5)优化建议包括使用语义化标签和减少文件大小。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

HTML的作用是通过标签和属性定义网页的结构和内容。1.HTML通过到、等标签组织内容,使其易于阅读和理解。2.使用语义化标签如、等增强可访问性和SEO。3.优化HTML代码可以提高网页加载速度和用户体验。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版
好用的JavaScript开发工具