A FAQ on various IRC channels I help out on is How vì I vertically center my stuff inside this area? This question is often followed by I"m using vertical-align:middle but it"s not working!

The problem here is three-fold:

HTML layout traditionally was not designed lớn specify vertical behavior. By its very nature, it scales width-wise, & the nội dung flows khổng lồ an appropriate height based on the available width. Traditionally, horizontal sizing & layout is easy; vertical sizing và layout was derived from that.The reason vertical-align:middle isn"t doing what is desired want is because the author doesn"t understand what it"s supposed khổng lồ vày, but …… this is because the CSS specification really screwed this one up (in my opinion)—vertical-align is used to lớn specify two completely different behaviors depending on where it is used.

Bạn đang xem: Vertical-align là gì

quý khách hàng đang xem: Vertical-align là gì

vertical-align in table cells

When used in table cells, vertical-align does what most people expect it to, which is mimic the (old, deprecated) valign attribute. In a modern, standards-compliant browser, the following three code snippets vì chưng the same thing:

...

vertical-align on inline elements

When vertical-align is applied khổng lồ inline elements, however, it"s a whole new ballgame. In this situation, it behaves lượt thích the (old, deprecated) align attribute did on
*

and
*

—as examples.

In this paragraph, I have two images—

*

&
*

—as examples.

In this paragraph, I have a xinh đẹp little display:inline-blockvertical-align:middle và display:inline-blockvertical-align:text-bottom as an example.

Xem thêm: “Thánh Nữ Cover” J Fla Là Ai, Khám Phá Tiểu Sử Và Sự Nghiệp Của Thánh Nữ Cover

vertical-align on other elements

Technically, this CSS attribute doesn"t go on any other kinds of elements. When the novice developer applies vertical-align to normal bloông chồng elements (lượt thích a standard ) most browsers set the value to lớn inherit to lớn all inline children of that element.

So how do I vertically-center something?!

Method 1

The following example makes two (non-trivial) assumptions. If you can meet these assumptions, then this method is for you:

You can put the nội dung that you want lớn center inside a bloông xã and specify a fixed height for that inner content bloông chồng.It"s alright lớn absolutely-position this content. (Usually fine, since the parent element inside which the nội dung is centered can still be in flow.

If you can accept the above necessities, the solution is:

Specify the parent container as position:relative sầu or position:absolute.Specify a fixed height on the child container.Set position:absolute and top:1/2 on the child container khổng lồ move the top down to the middle of the parent.Set margin-top:-yy where yy is half the height of the child container to offset the thắng lợi up.

Xem thêm: Tiểu Sử Vũ Khắc Tiệp Là Ai, Tiểu Sử, Sự Nghiệp Và Đời Tư Ông Trùm Chân Dài

An example of this in code:

...Hey look! I"m vertically centered!

How sweet is this?!

In your browser, the above sầu example renders as:

Hey look! I"m vertically centered!

How sweet is this?!

Method 2

This method requires that you be able lớn satisfy the following conditions:

If you can accept the above necessities, the solution is:

Set the line-height of the parent element lớn the fixed height you want.

An example of this in code:


Chuyên mục: ĐỊNH NGHĨA
Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *