博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将div垂直居中放置在另一个div中[重复]
阅读量:2289 次
发布时间:2019-05-09

本文共 2400 字,大约阅读时间需要 8 分钟。

本文翻译自:

This question already has answers here :
这个问题已经在这里有了答案
(17 answers)
(17个答案)
Closed 7 months ago .
7个月前关闭。

I want to center a div which is added inside another div. 我想将添加到另一个div内的div居中。

This is the CSS I am currently using. 这是我当前正在使用的CSS。

#outerDiv{        width: 500px;        height: 500px;        position:relative;    }    #innerDiv{        width: 284px;        height: 290px;        position:absolute;        top: 50%;        left:50%;        margin-top: -147px;        margin-left: -144px;    }

As you can see,the approach I use now depends on values for width and height of innerDiv .If the width/height changes, I will have to modify the margin-top and margin-left values.Is there any generic solution that I can use to center the innerDiv always irrespective of its size? 如您所见,我现在使用的方法取决于innerDiv width和height的innerDiv 。如果width / height发生变化,我将不得不修改margin-topmargin-left值。始终使用innerDiv而不考虑其大小?

I figured out that using margin:auto can horizontally allign the innerDiv to the middle.But what about vertical allign middle? 我发现使用margin:auto可以水平将innerDiv对齐到中间,但是垂直对齐中间呢?


#1楼

参考:


#2楼

I know that question was created year ago... Anyway thanks CSS3 you can easily vertically aligns div in div (example there ) 我知道这个问题是在一年前创建的...无论如何,感谢CSS3,您可以轻松地在div中垂直对齐div(例如 )

Go to Hell!
div{display:-moz-box;-moz-box-align:center;}

#3楼

Another way of achieving this horizontal and vertical centering is: 实现这种水平和垂直居中的另一种方法是:

.Absolute-Center {  margin: auto;  position: absolute;  top: 0; left: 0; bottom: 0; right: 0;}

( ) ( )


#4楼

Instead of tying myself in a knot with hard-to-write and hard-to-maintain CSS (that also needs careful cross-browser validation!) I find it far better to give up on CSS and use instead wonderfully simple HTML 1.0: 与其将自己束缚在难以编写和难以维护的CSS上(这还需要仔细的跨浏览器验证!),不如将它束之高阁,我发现放弃CSS并使用非常简单的HTML 1.0更好:

This accomplishes everything the original poster wanted, and is robust and maintainable. 这样可以完成原始海报所需的一切,并且功能强大且可维护。


#5楼

I have been using the following solution since over a year, it works with IE 7 and 8 as well. 自一年多以来,我一直在使用以下解决方案,它也可以与IE 7和8一起使用。

Line 1

Line 2


#6楼

You can do this with a simple javascript (jQuery) block. 您可以使用一个简单的javascript(jQuery)块来执行此操作。

CSS : CSS

#outerDiv{    height:100%;}

Javascript : Javascript

转载地址:http://zscnb.baihongyu.com/

你可能感兴趣的文章
IO流列出目录下所有内容,带层次
查看>>
IO流删除带内容的目录
查看>>
IO流创建java文件列表
查看>>
Properties存取配置文件
查看>>
记录应用程序运行次数
查看>>
打印流PrintStream和PrintWriter
查看>>
IO流 合并流 SequenceInputStream
查看>>
IO流切割文件
查看>>
IO流-对象序列化操作流
查看>>
io流-多线程连接管道流
查看>>
RandomAccessFile可实现数据的分段写入也就是多线程下载
查看>>
DataInputStream与DataOutputStream用于操作基本数据类型的数据的流对象
查看>>
ByteArrayStream用于操作字节数组的流对象
查看>>
IO流-字符编码表转换示例
查看>>
IO流-转换流的字符编码转换-ISO-8859-1和utf-8和GBK互转
查看>>
网络游戏线上活动的类型及特点(二)
查看>>
sleeping-to-save-cpu
查看>>
键盘符号英语读法
查看>>
[转]char_traits
查看>>
[转载] 人生三重境界
查看>>