Ish*_*Ish 1034 css center css-position absolute
我需要在窗口的中心放置一个div
(with position:absolute;
)元素.但我这样做有问题,因为宽度未知.
我试过这个.但需要根据宽度的响应进行调整.
.center {
left: 50%;
bottom:5px;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Mat*_*ler 1756
这对我有用:
#content {
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
width: 100px; /* Need a specific value to work */
}
Run Code Online (Sandbox Code Playgroud)
<body>
<div>
<div id="content">
I'm the content
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
bob*_*nce 1293
<body>
<div style="position: absolute; left: 50%;">
<div style="position: relative; left: -50%; border: dotted red 1px;">
I am some centered shrink-to-fit content! <br />
tum te tum
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
Pro*_*mit 706
这里是一个很好的解决方案为响应式设计或未知的尺寸一般,如果你不需要支持IE8和更低.
.centered-axis-x {
position: absolute;
left: 50%;
transform: translate(-50%, 0);
}
Run Code Online (Sandbox Code Playgroud)
.outer {
position: relative; /* or absolute */
/* unnecessary styling properties */
margin: 5%;
width: 80%;
height: 500px;
border: 1px solid red;
}
.inner {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
/* unnecessary styling properties */
max-width: 50%;
text-align: center;
border: 1px solid blue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="outer">
<div class="inner">I'm always centered<br/>doesn't matter how much text, height or width i have.<br/>The dimensions or my parent are irrelevant as well</div>
</div>
Run Code Online (Sandbox Code Playgroud)
线索是,left: 50%
相对于父级,而translate
变换是相对于元素宽度/高度.
这样,您就拥有了一个完美居中的元素,在子元素和父元素上都具有灵活的宽度.奖励:即使孩子比父母大,这也有效.
你也可以用它垂直居中(同样,父母和孩子的宽度和高度可以完全灵活(和/或未知)):
.centered-axis-xy {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
Run Code Online (Sandbox Code Playgroud)
请记住,您可能还需要transform
供应商前缀.例如-webkit-transform: translate(-50%,-50%);
pra*_*abu 48
非常好的帖子..只是想添加如果有人想用单个div标签然后在这里出路:
以width
作为900px
.
#styleName {
position: absolute;
left: 50%;
width: 900px;
margin-left: -450px;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,人们应该width
事先知道.
Utk*_*agi 38
<div style='position:absolute; left:50%; top:50%; transform: translate(-50%, -50%)'>
This text is centered.
</div>
Run Code Online (Sandbox Code Playgroud)
这将使位置类型为静态或相对的 div 内的所有对象居中。
Mic*_*tto 35
假设div中的元素,是另一个div...
此解决方案工作正常:
<div class="container">
<div class="center"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
该容器可以是任何大小(必须是相对位置):
.container {
position: relative; /* Important */
width: 200px; /* Any width */
height: 200px; /* Any height */
background: red;
}
Run Code Online (Sandbox Code Playgroud)
元素(div)也可以是任意大小(必须小于容器):
.center {
position: absolute; /* Important */
top: 50%; /* Position Y halfway in */
left: 50%; /* Position X halfway in */
transform: translate(-50%,-50%); /* Move it halfway back(x,y) */
width: 100px; /* Any width */
height: 100px; /* Any height */
background: blue;
}
Run Code Online (Sandbox Code Playgroud)
结果看起来像这样。运行代码片段:
<div class="container">
<div class="center"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
.container {
position: relative; /* Important */
width: 200px; /* Any width */
height: 200px; /* Any height */
background: red;
}
Run Code Online (Sandbox Code Playgroud)
我发现它很有帮助。
Ija*_*een 34
绝对中心
HTML:
<div class="parent">
<div class="child">
<!-- content -->
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
Run Code Online (Sandbox Code Playgroud)
演示: http ://jsbin.com/rexuk/2/
在Google Chrome,Firefox和IE8中测试过
希望这可以帮助 :)
Moh*_*ahi 31
这项工作适用于纵向和横向
#myContent{
position: absolute;
left: 0;
right: 0;
top:0;
bottom:0;
margin: auto;
}
Run Code Online (Sandbox Code Playgroud)
如果你想要父元素的元素中心,设置父亲的位置
#parentElement{
position:relative
}
Run Code Online (Sandbox Code Playgroud)
编辑:
对于垂直中心,将高度设置为元素.感谢@Raul
如果您想要父元素的元素中心,请将父元素的位置设置为相对
cav*_*ila 19
寻找解决方案我得到了上面的答案,可以使内容集中在Matthias Weiler的答案,但使用text-align.
#content{
position:absolute;
left:0;
right:0;
text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
使用chrome和Firefox.
mic*_*czy 16
如果您也需要水平和垂直居中:
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
Run Code Online (Sandbox Code Playgroud)
Gas*_*ass 16
可以通过使用将具有绝对位置的元素居中aspect-ratio:1
calc()
在下面的示例中,我使用圆形,因为它更容易解释和理解,但相同的概念可以应用于任何形状,意味着aspect-ratio:1
和width
相等height
。(关于纵横比)
:root{
--diameter: 80px;
}
div{
position:absolute;
top: calc(50% - var(--diameter)/2);
right:calc(50% - var(--diameter)/2);
aspect-ratio:1;
width:var(--diameter);
border-radius:100%;
background:blue;
}
Run Code Online (Sandbox Code Playgroud)
<div/>
Run Code Online (Sandbox Code Playgroud)
Dha*_*osh 15
Flexbox可用于将绝对定位的 div 居中。
display: flex;
align-items: center;
justify-content: center;
Run Code Online (Sandbox Code Playgroud)
display: flex;
align-items: center;
justify-content: center;
Run Code Online (Sandbox Code Playgroud)
.relative {
width: 275px;
height: 200px;
background: royalblue;
color: white;
margin: auto;
position: relative;
}
.absolute-block {
position: absolute;
height: 36px;
background: orange;
padding: 0px 10px;
bottom: -5%;
border: 1px solid black;
}
.center-text {
display: flex;
justify-content: center;
align-items: center;
box-shadow: 1px 2px 10px 2px rgba(0, 0, 0, 0.3);
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*oss 11
我知道这个问题已经有了一些答案,但我从来没有找到一个适用于几乎所有类别的解决方案,这些解决方案也很有意义并且很优雅,所以这是我在调整一堆后的看法:
.container {
position: relative;
}
.container .cat-link {
position: absolute;
left: 50%;
top: 50%;
transform: translate3d(-50%,-50%,0);
z-index: 100;
text-transform: uppercase; /* Forces CSS to treat this as text, not a texture, so no more blurry bugs */
background-color: white;
}
.color-block {
height: 250px;
width: 100%;
background-color: green;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
<a class="cat-link" href="">Category</a>
<div class="color-block"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
这样做是说给我一个top: 50%
和一个left: 50%
,然后在X/Y轴上转换(创建空间)到-50%
值,在某种意义上"创建一个镜像空间".
因此,这在div的所有4个点上创建了相等的空间,它总是一个盒子(有4个边).
这将:
low*_*sun 11
适用于您希望在容器元素中心具有的绝对定位元素的任意随机未知宽度.
<div class="container">
<div class="box">
<img src="https://picsum.photos/200/300/?random" alt="">
</div>
</div>
.container {
position: relative;
width: 100%;
}
.box {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这对于未知宽度是不可能实现的.
您可以 - 如果在您的场景中有效 - 绝对定位具有100%宽度和高度的不可见元素,并使用margin:auto和可能vertical-align使元素居中.否则,你需要Javascript来做到这一点.
小智 7
我想补充一下@ bobince的答案:
<body>
<div style="position: absolute; left: 50%;">
<div style="position: relative; left: -50%; border: dotted red 1px;">
I am some centered shrink-to-fit content! <br />
tum te tum
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
改进:///这使得水平滚动条不会出现在居中div中的大元素.
<body>
<div style="width:100%; position: absolute; overflow:hidden;">
<div style="position:fixed; left: 50%;">
<div style="position: relative; left: -50%; border: dotted red 1px;">
I am some centered shrink-to-fit content! <br />
tum te tum
</div>
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
这是对我们有用的其他答案的组合:
.el {
position: absolute;
top: 50%;
margin: auto;
transform: translate(-50%, -50%);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
只需用一个新的 div 包装您的内容并使用 display flex ,然后使用align-items: center; 并调整内容:中心;看一看...
<div class="firstPageContainer">
<div class="firstPageContainer__center"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
<div class="firstPageContainer">
<div class="firstPageContainer__center"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是一个有用的jQuery插件.在这里找到.我认为纯粹使用CSS是不可能的
/**
* @author: Suissa
* @name: Absolute Center
* @date: 2007-10-09
*/
jQuery.fn.center = function() {
return this.each(function(){
var el = $(this);
var h = el.height();
var w = el.width();
var w_box = $(window).width();
var h_box = $(window).height();
var w_total = (w_box - w)/2; //400
var h_total = (h_box - h)/2;
var css = {"position": 'absolute', "left": w_total+"px", "top":
h_total+"px"};
el.css(css)
});
};
Run Code Online (Sandbox Code Playgroud)
#content {
position: absolute;
left: 50%;
top: 50%;
@include vendor(transform, translate(-50%, -50%));
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这对我有用:
<div class="container><p>My text</p></div>
.container{
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1082649 次 |
最近记录: |