热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

无法水平居中div,涉及的位置固定-Unabletocenterdivhorizontally,positionfixedinvolved

IhaveadivthatIwanttocenterhorizontallyinsidemybrowserwindowwithoutchangingthevertic

I have a div that I want to center horizontally inside my browser window without changing the vertical position. Here's my div:

我有一个div,我想在浏览器窗口中水平居中,而不改变垂直位置。这是我的div:

{ERROR MESSAGE HERE}

And here's the class:

这是班级:

.ErrorClientSide {
  display: none;
  position: fixed;
  top: 4px;
  left: 370px;
  padding: 10px 25px 10px 25px;
  border-radius: 7px;
}

Note: I removed the color styles and such from the class above for brevity.

注意:为了简洁起见,我从上面的类中删除了颜色样式等。

Try it out here. Instructions: Leave all boxes blank and just click the Next >> button. You should see a pink error message div appear at the top that says "Error: All fields are required.", but the horizontal positioning is way off.

在这里尝试一下。说明:将所有框留空,只需单击下一步>>按钮。您应该会在顶部看到一个粉红色的错误消息div,显示“错误:所有字段都是必需的。”,但水平定位是关闭的。

Problem: I cannot center divErrorMessage horizontally in my browser window. I'm pretty sure that position:fixed is the problem, but if I remove that, my div disappears and I can't find it. left:370px is clearly a problem as well. If I remove that, the div aligns at the left of the browser and I can't find any way to center it.

问题:我无法在浏览器窗口中水平居中divErrorMessage。我很确定这个位置:修复是问题,但是如果我删除它,我的div消失了,我找不到它。左:370px显然也是一个问题。如果我删除它,div在浏览器的左边对齐,我找不到任何方法来居中它。

Here are my only positioning requirements for this div:

以下是我对此div的唯一定位要求:

  1. It needs to be positioned vertically a little below the brown header at the top.
  2. 它需要垂直放置在顶部的棕色标题下方。

  3. It needs to be horizontally centered in the browser window.
  4. 它需要在浏览器窗口中水平居中。

If CSS can't do this easily I would be happy for a jquery solution as well, but I'm guessing this should be easy with CSS.

如果CSS不能轻易做到这一点,我也很乐意使用jquery解决方案,但我猜这应该很容易用CSS。

Important note: The error message div and much of the content is inside an iFrame.

重要说明:错误消息div和大部分内容都在iFrame中。

6 个解决方案

#1


4  

If the absolutely positioned error box doesn't have a fixed width, you could achieve the horizontal alignment by a combination of left: 50%; and transform: translateX(-50%);.

如果绝对定位的错误框没有固定宽度,则可以通过左边的组合实现水平对齐:50%;和转换:translateX(-50%);.

For instance:

.ErrorClientSide {
  position: fixed;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
}

Vendor prefixes omitted due to brevity.

由于简洁,省略了供应商前缀。


It's worth noting that translate transform is supported in IE9+. For further reading you could refer to the topic below:

值得注意的是IE9 +支持translate转换。如需进一步阅读,请参阅以下主题:

  • How to center a "position: absolute" element
  • 如何将“position:absolute”元素居中

#2


0  

As your error div is using a fixed position, you should change its left and margin-left position accordingly.

由于您的错误div使用固定位置,您应该相应地更改其左侧和左侧位置。

Let's suppose this error div will have a width of 200px, you could adjust your CSS with the following change:

假设这个错误div的宽度为200px,你可以通过以下更改来调整CSS:

 .ErrorClientSide {
    left: 50%;
    margin-left: -100px;
 }

Your margin-left attribute should be the negation of half your div's width (for instance, if your div's width is 378px instead of 200px, you should use margin-left: -189px;).

您的margin-left属性应该是div宽度的一半(例如,如果你的div的宽度是378px而不是200px,你应该使用margin-left:-189px;)。

#3


0  

it actually is located outside the form - which means it's falling in the DOM below the form if position is not fixed. If you want to not move it - you'll need a negative positioning top and a width sort of like this:

它实际上位于表单之外 - 这意味着如果位置不固定,它将落在表单下方的DOM中。如果你不想移动它 - 你需要一个负面定位顶部和宽度类似如下:

.ErrorClientSide {
display: block;
position: relative;
top: -319px;
width: 300px;
margin: 0 auto;
}

#4


0  

Remove position, top and left and use margin: 0 auto and width. Modify the markup to put divErrorMessage before the form. Or wrap it with an absolute position div then center it inside of that.

移除位置,顶部和左侧并使用边距:0自动和宽度。修改标记以将divErrorMessage放在表单之前。或者用绝对位置div包装然后将其置于其中。

But first, please don't use

as it's been deprecated.

但首先,请不要使用

,因为它已被弃用。

#5


0  

Lets say your div has width: 100px, then you can do left: 50%; margin-left: -50px;

让我们说你的div有宽度:100px,然后你可以做左:50%; margin-left:-50px;

.ErrorClientSide {
  display: none;
  position: fixed;
  top: 4px;
  width: 100px;
  left: 50%;
  margin-left: -50px;
}

Alternatively you can do:

或者你可以这样做:

.ErrorClientSide {
  display: none;
  position: fixed;
  top: 4px;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
}

This centre you div inside window

这个中心你在窗口内

#6


-2  

If you want to position a DIV vertically and horizontally you have to wrap it in 3 divs.

如果要垂直和水平放置DIV,则必须将其包裹在3个div中。

The Content

Once upon a midnight dreary...

CSS

.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}

.middle {
display: table-cell;
vertical-align: middle;
}

.inner {
margin-left: auto;
margin-right: auto; 
width: /*whatever width you want*/;
}

I pulled this answer from How to vertically center a div for all browsers?

我从如何垂直居中所有浏览器的div中提取了这个答案?


推荐阅读
  • 一篇文章搞定css3 3d效果
    css33d学习心得卡片反转魔方banner图首先我们要学习好css33d一定要有一定的立体感通过这个图片应该清楚的了解到了x轴y轴z轴是什么概念了。首先先给大家看一个小 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • position属性absolute与relative的区别和用法详解
    本文详细解读了CSS中的position属性absolute和relative的区别和用法。通过解释绝对定位和相对定位的含义,以及配合TOP、RIGHT、BOTTOM、LEFT进行定位的方式,说明了它们的特性和能够实现的效果。同时指出了在网页居中时使用Absolute可能会出错的原因,即以浏览器左上角为原始点进行定位,不会随着分辨率的变化而变化位置。最后总结了一些使用这两个属性的技巧。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • Netty源代码分析服务器端启动ServerBootstrap初始化
    本文主要分析了Netty源代码中服务器端启动的过程,包括ServerBootstrap的初始化和相关参数的设置。通过分析NioEventLoopGroup、NioServerSocketChannel、ChannelOption.SO_BACKLOG等关键组件和选项的作用,深入理解Netty服务器端的启动过程。同时,还介绍了LoggingHandler的作用和使用方法,帮助读者更好地理解Netty源代码。 ... [详细]
  • 前端每日实战 2018年10月至2019年6月项目汇总(共 20 个项目)
    过往项目2018年9月份项目汇总(共26个项目)2018年8月份项目汇总(共29个项目)2018年7月份项目汇总(共29个项目)2018年6月份项目汇总(共27个项目)2018年5 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 1.CSS3透明度渐变(从左到右)#grad{background:-webkit-linear-gradient(left,rgba(198,226,255,1),rgba(19 ... [详细]
  • 1、给边框加上圆角及阴影,如下代码:<!DOCTYPEhtmlPUBLIC"-W3CDTDHTML4.01TransitionalEN"" ... [详细]
  • http:js.alixixi.coma2014021292298.shtmlhttp:w3cshare.comexample?pid134http:w3cshare.comc ... [详细]
author-avatar
独孤依人x_762
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有