700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > css没设宽度如何居中 – CSS – 前端 css优先bs用什么

css没设宽度如何居中 – CSS – 前端 css优先bs用什么

时间:2023-03-04 19:57:52

相关推荐

css没设宽度如何居中 – CSS – 前端 css优先bs用什么

很多新手在写css的时候经常遇到的一个问题,当div没有固定的宽度或者高度的时候,如何才能让div水平或者垂直居中显示。如果div有固定宽度的话,用padding,margin都很容易实现。方法有很多种。不过经常遇到这种div没有固定的宽度高度的情况,大家就不能用margin,padding设置固定的距离了。这个问题让很多人头疼。而怎么样才能让这个div居中显示呢?其实这种情况解决的办法也是有很多种,js,css都可以实现。这里主要介绍一下采用css的方法,有什么问题还请各位看官指出。

方法一:用一个“ghost”伪元素(看不见的伪元素)和 inline-block / vertical-align 可以搞定居中,非常巧妙。但是这个方法要求待居中的元素是 inline-block,不是一个真正通用的方案。HTML如下:

XML/HTML Code复制内容到剪贴板

<div>

<div>

<h1>haorooms案例题目</h1>

<p>haorooms案例内容,haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容</p>

</div>

</div>

css如下:

CSS Code复制内容到剪贴板

/* This parent can be any width and height */

.block {

text-align: center;

}

/* The ghost, nudged to maintain perfect centering */

.block:before {

content: "";

display: inline-block;

height: 100%;

vertical-align: middle;

margin-right: -0.25em; /* Adjusts for spacing */

}

/* The element to be centered, can

also be of any width and height */

.centered {

display: inline-block;

vertical-align: middle;

width: 50%;

}

方法二:可以用table布局方法,但是这种方法也有局限性!写法如下:

XML/HTML Code复制内容到剪贴板

<table>

<tr>

<td>

Unknown stuff to be centered.

</td>

</tr>

</table>

由于table写法比较费时,你也可以用div代替table,写法如下:html:

XML/HTML Code复制内容到剪贴板

<div>

<div>

Unknown stuff to be centered.

</div>

</div>

css:

CSS Code复制内容到剪贴板

.something-semantic {

display: table;

width: 100%;

}

.something-else-semantic {

display: table-cell;

text-align: center;

vertical-align: middle;

}

方法三,终极解决方法:以上2中方法可能都有其局限性,偶介绍的第三中方法是比较成熟的不是固定高宽div的垂直居中的方法!但是方法是css3的写法,想兼容IE8的童鞋们,建议用上面的方法!方法和大家固定高宽的差不多,但是不用margin大家用的是 translate()demo如下:

CSS Code复制内容到剪贴板

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="/1999/xhtml" xml:lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<title>haorooms不固定高度div写法</title>

<style>

.center {

position: fixed;

top: 50%;

left: 50%;

background-color: #000;

width:50%;

height: 50%;

-webkit-transform: translateX(-50%) translateY(-50%);

}

</style>

</head>

<body>

<div></div>

</body>

</html>

偶上面的css只是针对webkit内核的浏览器,其他内核浏览器写法如下:

CSS Code复制内容到剪贴板

-webkit-transform: translateX(-50%) translateY(-50%);

-moz-transform: translateX(-50%) translateY(-50%);

-ms-transform: translateX(-50%) translateY(-50%);

transform: translateX(-50%) translateY(-50%);

有些弹出层的样式,也可以用这个方法居中

CSS Code复制内容到剪贴板

position: fixed;

top: 50%;

left: 50%;

width: 50%;

max-width: 630px;

min-width: 320px;

height: auto;

z-index: 2000;

visibility: hidden;

-webkit-backface-visibility: hidden;

-moz-backface-visibility: hidden;

backface-visibility: hidden;

-webkit-transform: translateX(-50%) translateY(-50%);

-moz-transform: translateX(-50%) translateY(-50%);

-ms-transform: translateX(-50%) translateY(-50%);

transform: translateX(-50%) translateY(-50%);

无固定高度的div垂直居中代码要点:父容器vc的css属性 display:table;overflow:hidden;子容器vci的css属性 vertical-align:middle;display:table-cell;针对ie6的hack,vci容器的 _position:absolute;_top:50%; 和content容器的 _position:relative; _top:-50%;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。