HTML--元素居中各种处理方法
1、水平居中
对于行内元素可以使用:
.center-children {
text-align: center;
}
对于块元素,你可以设置其左右外边距为:auto;同时你还应该设置该元素的宽度,不然的话,元素的宽度会撑满整个浏览器或者一种是没反应(不过你设置背景就会看到了)。
.center-me {
margin: 0 auto;
}
如果你想让多个块元素在一行当中显示,首先你得设置display的属性,inline-block;在使用上面说的方法。还有一种方式是设置display:flex;justify-content: center;
CSS代码:
body {
background: #f06d06;
font-size: 80%;
}
main {
background: white;
margin: 20px 0;
padding: 10px;
}
main div {
background: black;
color: white;
padding: 15px;
max-width: 125px;
margin: 5px;
}
.inline-block-center {
text-align: center;
}
.inline-block-center div {
display: inline-block;
text-align: left;
}
.flex-center {
display: flex;
justify-content: center;
}
HTML代码:
<main class="inline-block-center">
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
<div>
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.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
</main> <main class="flex-center">
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
<div>
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.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
</main>
2、垂直居中
1)如果只是单行的情况:让行高等于元素的高度来欺骗别人达到居中的目的。
<main>
<div>
I'm a centered line.
</div>
</main>
body {
background: #f06d06;
font-size: 80%;
}
main {
background: white;
margin: 20px 0;
padding: 40px;
}
main div {
background: black;
color: white;
height: 100px;
line-height: 100px;
padding: 20px;
width: 50%;
white-space: nowrap;
}
white-space: nowrap;表示段落中的文本不换行;超出宽度的将不会在显示。
2)如果要多行居中,一般设置上下的内边距来实现,不行的话还有两种方法:一种是将文本放置在表格单中。另一种则是模仿表格的形式。首先为其设置一个容器,再将装有文本的容器放在里面。设置边框,对齐方式,显示方式等就可以了。
<table>
<tr>
<td>
I'm vertically centered multiple lines of text in a real table cell.
</td>
</tr>
</table> <div class="center-table">
<p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
</div> body {
background: #f06d06;
font-size: 80%;
} table {
background: white;
width: 240px;
border-collapse: separate;
margin: 20px;
height: 250px;
} table td {
background: black;
color: white;
padding: 20px;
border: 10px solid white;
/* default is vertical-align: middle; */
} .center-table {
display: table;
height: 250px;
background: white;
width: 240px;
margin: 20px;
}
.center-table p {
display: table-cell;
margin:;
background: black;
color: white;
padding: 20px;
border: 10px solid white;
vertical-align: middle;
}
border-collapse 属性设置表格的边框是否被合并为一个单一的边框,还是象在标准的 HTML 中那样分开显示;
display:table- cell属性指让标签元素以表格单元格的形式呈现,类似于td标签。目前IE8+以及其他现代浏览器都是支持此属性的,但是IE6/7只能对你说 sorry了。
如果上述方法都不行,恐怕就得使用flex了
<div class="flex-center">
<p>I'm vertically centered multiple lines of text in a flexbox container.</p>
</div> body {
background: #f06d06;
font-size: 80%;
} div {
background: white;
width: 240px;
margin: 20px;
} .flex-center {
background: black;
color: white;
border: 10px solid white;
display: flex;
flex-direction: column;
justify-content: center;
height: 200px;
resize: vertical;
overflow: auto;
}
.flex-center p {
margin:;
padding: 20px;
}
如果这个也行不通的话,使用下面的ghost-center.
<div class="ghost-center">
<p>I'm vertically centered multiple lines of text in a container. Centered with a ghost pseudo element</p>
</div> body {
background: #f06d06;
font-size: 80%;
} div {
background: white;
width: 240px;
height: 200px;
margin: 20px;
color: white;
resize: vertical;
overflow: auto;
padding: 20px;
} .ghost-center {
position: relative;
}
.ghost-center::before {
content: " ";
display: inline-block;
height: 100%;
width: 1%;
vertical-align: middle;
}
.ghost-center p {
display: inline-block;
vertical-align: middle;
width: 190px;
margin:;
padding: 20px;
background: black;
}
HTML--元素居中各种处理方法的更多相关文章
- css使子元素在父元素居中的各种方法
html结构: <div class="parent"> <div class="child"></div> </di ...
- 使用CSS完成元素居中的七种方法
在网页布局中元素水平居中比元素垂直居中要简单不少,同时实现水平居中和垂直居中往往是最难的.现在是响应式设计的时代,我们很难确切的知道元素的准确高度和宽度,所以一些方案不大适用.据我所知, 在CSS中至 ...
- <转载>使CSS文字图片div元素居中方法之水平居中的几个方法
文字居中,文字垂直居中水平居中,图片居中,图片水平居中垂直居中,块元素垂直居中?当我们在做前端开发是时候关于css居中的问题是很常见的.情 况有很多种,不同的情况又有不同的解决方式.水平居中的方式解决 ...
- cssy元素居中的方法有哪些?
css的元素居中 各位小伙伴们在努力写网页的时候有没有遇到过这样的一个问题呢? 在写的时候发现他不居中,可是要分分钟逼死强迫症的啊! 别急,我来啦 哈哈哈 今天就带来三种css的元素居中的方法 第一种 ...
- 网页元素居中的n种方法
导语:元素居中对齐在很多场景看上去很和谐很漂亮.除此之外,对于前端开发面试者的基础也是很好的一个考察点.下面跟着作者的思路,一起来看下吧. 场景分析 一个元素,它有可能有背景,那我要它的背景居中对齐 ...
- HTML 元素居中的方法
网址:http://www.cnblogs.com/asqq/archive/2012/04/09/2438745.html 1. 元素的定位的方法选择 :absolute . 2. 给定元素的宽和高 ...
- 大前端学习笔记整理【一】CSS盒模型与基于盒模型的6种元素居中方案
概览 CSS盒模型,规定了元素框来处理元素的 内容.内边距.边框和外边距的方式 元素部分是指内容部分,也是最实际的内容,包围内容的称之为内边距,内边距外围是边框,边框外围就是外边距:且外边距是透明的, ...
- css中元素居中总结
很多时候,我们需要让元素居中显示:1. 一段文本的水平居中,2. 一张图片的水平居中,3. 一个块级元素的水平居中:4. 单行文本的竖直居中,5. 不确定高度的一段文本竖直居中,6. 确定高度的块级元 ...
- div(固定宽度和不固定宽度)居中显示的方法总结
今天我总结一下css实现div居中的方法,有的是固定宽度的,还有的是不固定宽度的. 1.使用自动外边距实现DIV CSS居中 CSS中首选的让元素水平居中的方法就是使用margin属性—将元素的mar ...
- Css元素居中设置
你对DIV CSS居中的方法是否了解,这里和大家分享一下,用CSS让元素居中显示并不是件很简单的事情,让我们先来看一下CSS中常见的几种让元素水平居中显示的方法. DIV CSS居中 用CSS让元素居 ...
随机推荐
- SimpleTagSupport 获取request、session
开发jsp系统时,我们经常会用到tag来写java的逻辑代码,一般会继承两个类,一个是SimpleTagSupport,另一个是TagSupport,由于TagSupport书写配置比较复杂(我个人才 ...
- 通过jQuery给<li>绑定点击事件
背景:有个需求需要js中实现li标签的事件绑定,li通过在ajax请求中动态添加,按照常理,使用jQuery,可以使用以下方法绑定点击事件: $(function(){ commonAjaxCall ...
- win7 安装vb6
1. 用setup.exe有问题,用acmsetup.exe 2.打开setupwiz.ini,把"acme=acmboot.exe"改为"=setup\acmsetup ...
- The listener supports no services oracle注册监听
问题登场: [oracle@my-e450 ~]$ lsnrctl status …… The listener supports no servicesThe command completed s ...
- [PHP] 三种运行模式 ISAPI模式 APACHE2HANDLER模式 CGI模式 FastCGI模式
ISAPI模式:ISAPI是微软提供的一套标准,PHP的ISAPI模式意思是PHP在windows系统上的IIS进行配合的运行模式,在PHP5.3之后不再支持,php5isapi.dll文件,PHP进 ...
- Rust中的错误处理
Result & Panic 这次讲得详细,从错误的来历及简写过程, 都写明白了, 先浅,再深,先深,再浅, 反复之, 学习王道~ use std::fs::File; //use std:: ...
- 【洛谷P3973】[TJOI2015]线性代数(最小割)
洛谷 题意: 给出一个\(n*n\)的矩阵\(B\),再给出一个\(1*n\)的矩阵\(C\). 求一个\(1*n\)的\(01\)矩阵\(A\),使得\(D=(A\cdot B-C)\cdot A^ ...
- c# 第15节 StringBuilder
本节内容: 1:StringBuilder 2:内容总结 1:StringBuilder 实例: 2:内容总结 项目:
- 201871010105-曹玉中《面向对象程序设计(java)》第十三周学习总结
201871010105-曹玉中<面向对象程序设计(java)>第十三周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- datagrid editor动态的改变不同行修改列的editor属性
onBeforeEdit: function (row) { let options = $(this).treegrid('options'); options.tempeditor = optio ...