div 在java_将<div>元素定位在屏幕中心

回答(12)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

简单的方法,如果你有一个固定的宽度和高度:

#divElement{

position: absolute;

top: 50%;

left: 50%;

margin-top: -50px;

margin-left: -50px;

width: 100px;

height: 100px;

}​

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

随着变形越来越受到支持,你可以在不知道弹出窗口的宽度/高度的情况下做到这一点

.popup {

position: fixed;

top: 50%;

left: 50%;

-webkit-transform: translate(-50%, -50%);

transform: translate(-50%, -50%);

}

Update: 查看https://css-tricks.com/centering-css-complete-guide/以获得有关CSS居中的详尽指南 . 将它添加到这个答案,因为它似乎得到了很多的关注 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

设置宽度和高度,你很好 .

div {

position: absolute;

margin: auto;

top: 0;

right: 0;

bottom: 0;

left: 0;

width: 200px;

height: 200px;

}

如果您希望元素维度具有灵活性(并且不关心旧版浏览器),请使用 XwipeoutX 的答案 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

现在,使用HTML 5和CSS 3更容易:

TODO supply a title

body > div {

position: absolute;

top: 0;

bottom: 0;

left: 0;

right: 0;

display: flex;

justify-content: space-around;

align-items: center;

flex-wrap: wrap;

}

TODO write content

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果你有一个固定的 div 绝对位置,它从顶部开始为50%,左边为50%,负边距分别为高度和宽度的一半左右 . 根据您的需求调整:

div {

position: absolute;

top: 50%;

left: 50%;

width: 500px;

height: 300px;

margin-left: -250px;

margin-top: -150px;

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

它适用于任何对象 . 即使你不知道大小:

.centered {

position: fixed;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

使用flex . 更简单,无论你的 div 尺寸如何都会有效:

.center-screen {

display: flex;

flex-direction: column;

justify-content: center;

align-items: center;

text-align: center;

min-height: 100vh;

}

I'm in the center

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

试试这个:

width:360px;

height:360px;

top: 50%; left: 50%;

margin-top: -160px; /* ( ( width / 2 ) * -1 ) */

margin-left: -160px; /* ( ( height / 2 ) * -1 ) */

position:absolute;

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果有人在寻找解决方案,

我找到了这个,

它是我发现的最佳解决方案!

div {

width: 100px;

height: 100px;

background-color: red;

position: absolute;

top:0;

bottom: 0;

left: 0;

right: 0;

margin: auto;

}

希望你喜欢!

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

另一种在中心显示块的简单灵活方法:使用 line-height 和 text-align 的原生文本对齐 .

解:

.parent {

line-height: 100vh;

text-align: center;

}

.child {

display: inline-block;

vertical-align: middle;

line-height: 100%;

}

和HTML示例:

My center block

我们使 div.parent 全屏,并且他的单个孩子 div.child 与 display: inline-block 对齐为文本 .

好处:

灵活性,没有绝对值

支持调整大小

在手机上运行正常

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我会在CSS中这样做:

div.centered {

position: fixed;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

}

然后在HTML中:

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

试试这个

Your Html code Here

或这个

Your Html code here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { padding: 0; margin: 0; } li { list-style: none; } a { text-decoration: none; } .daohang { background-color: #333333; margin: 0 auto; overflow: hidden; position: relative; } .navigation { width: 1226px; height: 40px; margin-left: 208px; float: left; } .navigation>a{ line-height: 40px; font-size: 12px; color: #b0b0b0; } .navigation>span{ color: #424242; } .navigation>a:hover{ color: #fff; } .parent { position: relative; /* 父元素设置为相对定位 / } .child { position: absolute; / 子元素设置为绝对定位 / top: 100%; / 相对于父元素底部定位 / left: 0; display: none; / 初始状态下隐藏 / z-index: 1; / 提高层级,防止被其他元素遮挡 / } .parent:hover .child { display: block; / 鼠标悬停时显示下拉菜单 */ } </style> </head> <body> <!--大盒子--> <div class="box"> <!--导航栏--> <div class="daohang"> <!--黑盒子--> <div class="navigation"> <a href="">小米官方</a> <span>|</span> <a href="">小米商店</a> <span>|</span> <a href="">MIUI</a> <span>|</span> <a href="">loT</a> <span>|</span> <a href="">云服务</a> <span>|</span> <a href="">天星科技</a> <span>|</span> <a href="">有品</a> <span>|</span> <a href="">小爱开放平台</a> <span>|</span> <a href="">企业团购</a> <span>|</span> <a href="">资质证照</a> <span>|</span> <a href="">协议规则</a> <span>|</span> <a href="" class="parent">下载app <span class="child"> <img src="./img/download.png" width="90" height="90"> 小米商城APP </span> </a> <span>|</span> <a href="">Select Location</a> </div> </div> </div> </body> </html>为什么没有实现鼠标经过图片显现
最新发布
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值