Zhengchun's CSS Notes (5)

Source: Internet
Author: User

One day everyone in the team to discuss the "unknown picture vertically centered" problem, the whim of vertical-align:middle this attribute to achieve, so took the time to do the following immature example:
Css:

div{
  width:140px;
  height:140px;
  text-indent:-8px;
  Text-align:center;
  line-height:138px;
  background:red;
  font-size:12px;
  *font-size:120px;
  *text-indent:-60px;
}
img{
  width:100px;
  height:100px;
  Vertical-align:middle;
}

Html:

< div>& nbsp;

The original intention is not to use position to do, after all, a large number of pictures display browser in the rendering time will consume more resources.
The disadvantage is the output of meaningless nbsp, and the Font-size and FONT-INDENTD formulas are not very simple (so the above numbers are pooled ^_^).

Later pony saw this idea, with a little time to upgrade the code, with: After output instead of boring nbsp, code see below:
Css:

. tb-p-c{
  Display:block;
  width:140px;
  height:140px;
  line-height:140px;
  Text-align:center;
  *font-size:123px
}
. Tb-p-c img{
  vertical-align:middle;
Tb-p-c:after {
  content: ".";
  Visibility:hidden;
  font-size:12px;
  Margin-left: -5px;
}

Html:

  

In this way, the annoying nbsp disappears. We have a further understanding of the way vertical-align:middle is rendered. Also let a lot of comrades found after this pseudo class can also be used to hack.

The result center also gives CSS a secondary:
Css:

. tb-p-c{
  Display:table-cell;
  Vertical-align:middle;
  width:140px;
  height:140px;
  Text-align:center;
  *display:block;
  *font-size:122px;
  Background:red
}
. tb-p-c img {
  vertical-align:middle;
}

This is the third upgrade, due to the time relationship, did not test the above code, the reason is to use display to render, the feeling will still allow the browser to render many times, so I did not test-_-.
This discussion has given us a lot of harvest, I believe that if there is any participation, there will be more ways and we do not find the point of view. Everybody don't be stingy, more to post a reply, together raise.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.