当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。所以需要对父div的 line-height 进行调整。利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置 margin:auto 会使它居中显示。
转载自喜欢JS的无名小站
例如 一个父div(w:100%;h:400px)中有一个子div(w:100px;100px;)。让其上下左右居中。
利用表格单元格的居中属性。
父div外层配置一个div,同时设置为表格元素 (display: table),宽度为100%
父div配置为表格单元格元素 (display: table-cell)
父div配置居中属性(vertical-align: middle),使子div上下居中
子div通过margin配置左右居中(margin-left:auto; margin-right:auto)
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .table {display: table; width: 100%;} .father {display: table-cell; vertical-align: middle;} .son {margin: auto;} </style> <body> <div class="table" > <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </div> </body>
注:
表格单元格比较特殊,如果只有一个单元格时,它的宽度默认会占父级(table|tr)宽度的100%;
table默认宽度不会撑开,需要手动配置width:100%;
当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。通过文本居中属性text-align:center
,可以使内部行内元素或行内块元素左右居中显示。
子div设定为行内块元素(display:inline-block);
父div设置行高(line-height)使子div上下居中
父div设置文本居中(text-align:center)使子div左右居中。
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {line-height: 500px; text-align: center; font-size: 0;} .son { display: inline-block; /* display: inline-flex; display: inline-grid; display: inline-table; */ } </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
注: 行高如果设置为当前父div的高度(400px)的话,有固定高度的子div并不会居中显示的,问题出在浏览器默认将其当做文本居中的,即把它当做了一段文本(chrome默认font-size:16px;hight:21px)进行居中,没把它当做高度100px进行居中。所以需要对父div的line-height
进行调整。以font-size:0
(对应的字体高度为0)为例子,则需要line-height增加一个子div的高度(400px + 100px;)。
利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。
父div标记下定位(position:relative|absolute|fixed);子div绝对定位(position:absolute)
子div上下居中:top:50%;margin-top:-h/2;
或是 bottom:50%;margin-bottom:-h/2;
;
子div左右居中: left:50%;margin-left:-w/2
或是 right:50%;margin-right:-w/2
;
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px; } </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置margin:auto会使它居中显示。
父div标记下定位(position:relative|absolute|fixed|sticky);子div绝对定位(position:absolute)
子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto
子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto} </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
弹性盒子,自带的一个居中功能
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {display: flex; align-items: center} .son {margin: auto} </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
flex兼容性,以及存在的已知问题
方法二和方法三兼容性要比其它好些
Can I use
css-vertical-center-solution
CSS实现垂直居中的5种方法--前端观察
网页设计中经常用到css来设计各种边框样式以及颜色等,有时候需要一个div只显示一个边框,那么你可能会用到下面的一些方法。
一、CSS border-width 属性
border-width是实现显示边框的重要属性。用法如下:
border-width:top right bottom left
参数说明:
top:上边框属性,可以设置像素,也可以设置样式,意思为上边框的宽度。
right:右边框属性,可以设置像素,也可以设置样式,意思为上边框的宽度。
bottom:下边框属性,可以设置像素,也可以设置样式,意思为上边框的宽度。
left:左边框属性,可以设置像素,也可以设置样式,意思为上边框的宽度。
其中像素如:10px 20px等
内置样式有:
thin:定义细的边框;
medium:默认值,定义中等边框;
thick:定义粗的边框;
inherit:继承父元素的边框宽度。
二、CSS border-style 属性
border-style是用来设置边框线样式的,语法如下:
border-style:样式;
其中可设置的样式有:
none 定义无边框。
hidden 与 "none" 相同。不过应用于表时除外,对于表,hidden 用于解决边框冲突。
dotted 定义点状边框。在大多数浏览器中呈现为实线。
dashed 定义虚线。在大多数浏览器中呈现为实线。
solid 定义实线。
double 定义双线。双线的宽度等于 border-width 的值。
groove 定义 3D 凹槽边框。其效果取决于 border-color 的值。
ridge 定义 3D 垄状边框。其效果取决于 border-color 的值。
inset 定义 3D inset 边框。其效果取决于 border-color 的值。
outset 定义 3D outset 边框。其效果取决于 border-color 的值。
inherit 规定应该从父元素继承边框样式。
三、实例应用
只要定义边框不为0,即可显示边框(但是需要定义边框线样式),如果想要只显示下边框就相当于把top、right、left设置为0px;下边框不为0即可。
实例如下:
<html>
<head>
<meta charset='utf-8'>
<title>标题</title>
</head>
<body>
<style>
.show{ border-width: 0 0 1px 0; border-style: solid; border-color: black; }
</style>
<div class='show'>只显示下边框</div>
</body>
</html>
显示如下:
如果想要只显示右边框只需要改border-width属性为 0 1px 0 0即可。
lt;!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function req(){
var div = document.getElementById("div1");
document.getElementById("li1").innerHTML = (div.offsetTop)+"px";//div1距离屏幕顶部的距离
document.getElementById("li2").innerHTML = (div.offsetLeft)+"px";//div1距离屏幕左部的距离
document.getElementById("li3").innerHTML = (div.scrollTop)+"px";//div1纵向滚动条滚动的距离
document.getElementById("li4").innerHTML = (div.scrollLeft)+"px";//div1横向滚动条滚动的距离
}
</script>
</head>
<body style="border:10px solid #ccc;padding:0px 0px;margin:5px 10px">
<div style="width:60%;border-right:1px dashed red;float:left;">
<div style="float:left;">
<div id="div1" style="border:5px red solid;height:300px;width:200px;overflow:auto">
<div style="height:500px;width:400px">请调整横竖滚动条后,点击按钮后查看offsetTop、offsetLeft、scrollTop、scrollLeft值。</div>
</div>
<input type="button" value="点击我!" onclick="req()" style="border: 1px solid purple;height: 25px;"/>
</div>
</div>
<div style="width:30%;float:left;">
<ul style="list-style-type: none; line-height:30px;">结果:
<li>offsetTop : <span id="li1"></span></li>
<li>offsetLeft : <span id="li2"></span></li>
<li> scrollTop : <span id="li3"></span></li>
<li>scrollLeft : <span id="li4"></span></li>
</ul>
</div>
<div style="clear:both;"></div>
</body>
</html>
*请认真填写需求信息,我们会在24小时内与您取得联系。