Trang chủ Sưu tầm khác

Đăng nhập

Chưa là thành viên? Hãy đăng ký ngay, chỉ 30 giây. Sau khi đăng ký bạn có thể đăng bài viết tại phieubong lab để chia sẻ kiến thức của mình.



Thống kê

Số lần xem bài viết : 15299

Đang online

Hiện có 18 khách Trực tuyến

Quảng cáo


Free web hostingWeb hosting

 

Host PHP miễn phí 1.5GB,
Đăng ký ngay!! Free Website Hosting
Sưu tập khác
HTML Canh giữa (ngang và dọc) một bức hình trong một box PDF. In Email
Web sưu tập - Sưu tập khác
Viết bởi Administrator   
Chủ nhật, 04 Tháng 12 2011 02:15

Centering (horizontally and vertically) an image in a box

The problem

We want to center an image inside a container, bigger than the image and with assigned dimensions. The image is not a background one, it’s a true <img> element.

Any image has well defined dimensions, so the easiest and most reliable way to solve the problem is:

  • make the image display:block
  • assign to the image the needed left and top margins to get it centered in the container. Each of this margin value is obviously half the difference between a dimension of the container and that of the image.

For example if you have images of two different sizes, like in a photo gallery with “landscape” and “portrait” images, to be centered in fixed size containers, then assigning to each <img> one of two classes (with different margins) will solve the problem.

But there are cases when one does not want to use the previous method. For example when many images, all with different dimensions are involved and it is not practical to assign different margins to each of them, or when the dimensions of the container and those of the image are not expressed in the same units (px, em, %, …) hence the difference cannot be computed. It’s useful to have a different solution, independent on the image’s size.

A solution

The horizontal centering is not difficult. If the image is left with its default inline display, then text-align: center is the obvious solution, working well in all browsers.

For the vertical centering the best solution, working in modern browsers, is to assign display: table-cell; vertical-align: middle to the container. This works in Gecko based browsers, Opera 6+, Safari, IE8. It leaves IE7 and lower (both Windows and Mac) out.

For IE7- the idea is to create a sort of line-box having as height the height of the container, and the using again vertical-align: middle. The line-height property cannot be used to achieve this, since it doesn’t work correctly in IE7-/Win in presence of images. Also the use of a big font-size (without specifying line-height) is problematic, because the height of the generated box is slightly bigger than the font-size. And IE5/Mac (differently from IE/Win) is able to resize (according to user choice) line-height and font-size expressed in pixels, so it would have problems with this approach, unless the height of the container is expressed in em. Note: this same argument precludes the general use of such vertical centering method, based on line-height, in Gecko based and Safari browsers.

Fortunately IE7- has (partial) support for display: inline-block. If an empty inline-block element (for example a span) is added inside the container and it is assigned height: 100%; vertical-align: middle then it allows to precisely get what we want: a line box with the desired height. In other words, the inline-block element respects an assigned height (equal to the container’s one) and props the line open, so that vertical-align: middle (both on the extra element and the image) gives the desired vertical centering.

Some details:

  • The extra, empty inline-block element may have zero width in IE7-/Win, but it must have at least width: 1px in IE/Mac, otherwise it has no effect (this sometimes offsets the horizontal centering by 1px. It could be counteracted with a -1px margin, but the problem is barely visible.)
  • In IE5/Mac the vertical centering is some pixels off. The use of a small font-size or line-height on the container seems beneficial, not clear why (IE5/Mac is a bit incoherent here, especially when playing with its font size settings.)

A combined solution, using display: table-cell and the extra span with display: inline-block, works in Gecko based browsers, Opera 6+. Safari, IE5+/Win, IE5/Mac.

A variation of this technique can be used to vertically center a block element (even with unknown height) inside another one (with known height) which is a more interesting problem.

The code

Putting all together, and naming “wraptocenter” the class of the container, that’s the relevant CSS. Code for IE/Mac is wrapped in a suitable filter. Code for IE7-/Win is put in conditional comments.

<style type="text/css">
.wraptocenter {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    width: ...;
    height: ...;
}
.wraptocenter * {
    vertical-align: middle;
}
/*\*//*/
.wraptocenter {
    display: block;
}
.wraptocenter span {
    display: inline-block;
    height: 100%;
    width: 1px;
}
/**/
</style>
<!--[if lt IE 8]><style>
.wraptocenter span {
    display: inline-block;
    height: 100%;
}
</style><![endif]-->

And that’s the relevant HTML

<div class="wraptocenter"><span></span><img src="/..." mce_src="/..." alt="..."></div>

Some examples

To demonstrate the solution, it is applied to two different sized images, and to two different sizes for the containers. The first containers have dimensions expressed in pixels, the second ones in ems. The containers are given a grid background to better check the centering.

  • miao1
  • miao3
  • miao1
  • miao3

Some other examples where the images contain a (red) grid, which should align with the light grey grid of the container background.

Other, slightly more complex, examples: images in floated containers, an image gallery with captions.

An article on this problem by Steve Clay.

Many thanks to Ingo Chao, for his help and comments.

CSS tests home

Link gốc: http://www.brunildo.org/test/img_center.html

Phieubong sưu tầm.

 
HTML Tìm hiểu vertical-align, hay "Làm thế nào để canh giữa một nội dung" PDF. In Email
Web sưu tập - Sưu tập khác
Viết bởi Administrator   
Chủ nhật, 04 Tháng 12 2011 02:12

Understanding vertical-align, or "How (Not) To Vertically Center Content"

A FAQ on various IRC channels I help out on is How do 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:

  1. HTML layout traditionally was not designed to specify vertical behavior. By its very nature, it scales width-wise, and the content flows to an appropriate height based on the available width. Traditionally, horizontal sizing and layout is easy; vertical sizing and layout was derived from that.
  2. The reason vertical-align:middle isn't doing what is desired want is because the author doesn't understand what it's supposed to do, but …
  3. … this is because the CSS specification really screwed this one up (in my opinion)—vertical-align is used to specify two completely different behaviors depending on where it is used.

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 do the same thing:

<td valign="middle"> <!-- but you shouldn't ever use valign --> </td>
<td style="vertical-align:middle"> ... </td>
<div style="display:table-cell; vertical-align:middle"> ... </div>

Shown in your browser, the above (with appropriate wrappers) display as:

<td> using valign="middle"<td> using valign="bottom"
<td> using vertical-align:middle<td> using vertical-align:bottom
<div> using display:table-cell; vertical-align:middle
<div> using display:table-cell; vertical-align:bottom

vertical-align on inline elements

When vertical-align is applied to inline elements, however, it's a whole new ballgame. In this situation, it behaves like the (old, deprecated) align attribute did on <img> elements. In a modern, standards-compliant browser, the following three code snippets do the same thing:

<img align="middle" ...>
<img style="vertical-align:middle" ...>
<span style="display:inline-block; vertical-align:middle"> foo<br>bar </span>

In your browser, here's how the above code renders:

In this paragraph, I have two images—align="middle" and align="bottom"—as examples.

In this paragraph, I have two images—style="vertical-align:middle" and style="vertical-align:text-bottom"—as examples.

In this paragraph, I have a cute little <span> display:inline-block
vertical-align:middle
and display:inline-block
vertical-align:text-bottom
as an example.

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 block elements (like a standard <div>) most browsers set the value to inherit to all inline children of that element.

So how do I vertically-center something?!

If you are reading this page, you're probably not as interested in why what you were doing is wrong. You probably want to know how to do it properly.

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 content that you want to center inside a block and specify a fixed height for that inner content block.
  • It's alright to absolutely-position this content. (Usually fine, since the parent element inside which the content is centered can still be in flow.

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

  1. Specify the parent container as position:relative or position:absolute.
  2. Specify a fixed height on the child container.
  3. Set position:absolute and top:50% on the child container to move the top down to the middle of the parent.
  4. Set margin-top:-yy where yy is half the height of the child container to offset the item up.

An example of this in code:

<style type="text/css">
	#myoutercontainer { position:relative }
	#myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }
</style>
...
<div id="myoutercontainer">
	<div id="myinnercontainer">
		<p>Hey look! I'm vertically centered!</p>
		<p>How sweet is this?!</p>
	</div>
</div>

In your browser, the above example renders as:

Hey look! I'm vertically centered!

How sweet is this?!

Method 2

This method requires that you be able to satisfy the following conditions:

  • You have only a single line of text that you want to center.
  • You can specify a fixed-height for the parent element.

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

  1. Set the line-height of the parent element to the fixed height you want.

An example of this in code:

<style type="text/css">
	#myoutercontainer2 { line-height:4em }
</style>
...
<p id="myoutercontainer2">
	Hey, this is vertically centered. Yay!
</p>

In your browser, the above example renders as:

Hey, this is vertically centered. Yay!

 
Những thay đổi chủ yếu của XHTML so với HTML 4.0 [Phần 2] PDF. In Email
Web sưu tập - Sưu tập khác
Viết bởi Administrator   
Thứ sáu, 01 Tháng 5 2009 15:31

XHTML Syntax

 

Some More XHTML Syntax Rules

  • Attribute names must be in lower case
  • Attribute values must be quoted
  • Attribute minimization is forbidden
  • The id attribute replaces the name attribute
  • The XHTML DTD defines mandatory elements

Attribute Names Must Be In Lower Case

This is wrong:

<table WIDTH="100%">

This is correct:

<table width="100%">


Attribute Values Must Be Quoted

This is wrong:

<table width=100%>

This is correct:

<table width="100%">


Attribute Minimization Is Forbidden

This is wrong:

<input checked>
<input readonly>
<input disabled>
<option selected>
<frame noresize>

This is correct:

<input checked="checked" />
<input readonly="readonly" />
<input disabled="disabled" />
<option selected="selected" />
<frame noresize="noresize" />

Here is a list of the minimized attributes in HTML and how they should be written in XHTML:

HTML XHTML 
compact compact="compact"
checked checked="checked"
declare declare="declare"
readonly readonly="readonly"
disabled disabled="disabled"
selected selected="selected"
defer defer="defer"
ismap ismap="ismap"
nohref nohref="/nohref"
noshade noshade="noshade"
nowrap nowrap="nowrap"
multiple multiple="multiple"
noresize noresize="noresize"


The id Attribute Replaces The name Attribute

HTML 4.01 defines a name attribute for the elements applet, frame, iframe, and img. In XHTML the name attribute is deprecated. Use id instead.

This is wrong:

<img src="/picture.gif" name="pic1" />

This is correct:

<img src="/picture.gif" id="pic1" />

Note: To interoperate with older browsers for a while, you should use both name and id, with identical attribute values, like this:

<img src="/picture.gif" id="pic1" name="pic1" />

IMPORTANT Compatibility Note:

To make your XHTML compatible with today's browsers, you should add an extra space before the "/" symbol.


The Lang Attribute

The lang attribute applies to almost every XHTML element. It specifies the language of the content within an element.

If you use the lang attribute in an element, you must also add the xml:lang attribute, like this:

<div lang="no" xml:lang="no">Heia Norge!</div>


Mandatory XHTML Elements

All XHTML documents must have a DOCTYPE declaration. The html, head, title, and body elements must be present.

This is an XHTML document with a minimum of required tags:

<!DOCTYPE Doctype goes here>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title goes here</title>
</head>

<body>
</body>

</html>

Note: The DOCTYPE declaration is not a part of the XHTML document itself. It is not an XHTML element. You will learn more about the XHTML DOCTYPE in the next chapter.

Note: The xmlns attribute in <html>, specifies the xml namespace for a document, and is required in XHTML documents. However, the HTML validator at w3.org does not complain when the xmlns attribute is missing. This is because the namespace "xmlns=http://www.w3.org/1999/xhtml" is default, and will be added to the <html> tag even if you do not include it.

 
Những thay đổi chủ yếu của XHTML so với HTML 4.0 [Phần 1] PDF. In Email
Web sưu tập - Sưu tập khác
Viết bởi Administrator   
Thứ sáu, 01 Tháng 5 2009 15:16

Differences Between XHTML And HTML

 

Get Ready For XHTML

XHTML is not very different from the HTML 4.01 standard.

So, bringing your code up to the 4.01 standard is a good start.

Our complete HTML 4.01 reference can help you with that.

In addition, you should start NOW to write your HTML code in lowercase letters, and NEVER skip closing tags (like </p>).


The Most Important Differences:

  • XHTML elements must be properly nested
  • XHTML elements must always be closed
  • XHTML elements must be in lowercase
  • XHTML documents must have one root element

XHTML Elements Must Be Properly Nested

In HTML, some elements can be improperly nested within each other, like this:

<b><i>This text is bold and italic</b></i>

In XHTML, all elements must be properly nested within each other, like this:

<b><i>This text is bold and italic</i></b>

Note: A common mistake with nested lists, is to forget that the inside list must be within <li> and </li> tags.

This is wrong:

<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  <li>Milk</li>
</ul>

This is correct:

<ul>
  <li>Coffee</li>
  <li>Tea
    <ul>
      <li>Black tea</li>
      <li>Green tea</li>
    </ul>
  </li>
  <li>Milk</li>
</ul>

Notice that we have inserted a </li> tag after the </ul> tag in the "correct" code example.


XHTML Elements Must Always Be Closed

Non-empty elements must have a closing tag.

This is wrong:

<p>This is a paragraph
<p>This is another paragraph

This is correct:

<p>This is a paragraph</p>
<p>This is another paragraph</p>


Empty Elements Must Also Be Closed

Empty elements must also be closed.

This is wrong:

A break: <br>
A horizontal rule: <hr>
An image: <img src="/happy.gif" alt="Happy face">

This is correct:

A break: <br />
A horizontal rule: <hr />
An image: <img src="/happy.gif" alt="Happy face" />


XHTML Elements Must Be In Lower Case

Tag names and attributes must be in lower case.

This is wrong:

<BODY>
<P>This is a paragraph</P>
</BODY>

This is correct:

<body>
<p>This is a paragraph</p>
</body>


XHTML Documents Must Have One Root Element

All XHTML elements must be nested within the <html> root element. Child elements must be in pairs and correctly nested within their parent element.

The basic document structure is:

<html>
<head> ... </head>
<body> ... </body>
</html>