Moving Picture Text Vertical Center implementation method

Source: Internet
Author: User

Recently in the development of the public number in the picture and text center of the problem. For example, we would like to achieve the following effect:

Or.

In the development process, it may be my own code reasons or compatibility reasons, in iOS and Android it is difficult to fully achieve such a center effect.

In order to achieve this, and compatible with iOS and Android, I used the following three ways to implement (the following code I only write the main attributes, the specific style we each add according to their own needs):

First, display:table and Display:table-cell use;

For example:

1 <div style= "display:table;vertical-align:middle;" > 2 <span class= "Display:table-cell;vertical-align:middle" ></span>4 <span class= "Display:table-cell;vertical-align:middle" > Text </span > 5 </div>

Implemented by this combination of CSS styles.

Second, the use of background to achieve;

For example:

<style>

div{background: URL (/images/shop/cpbh.png) no-repeat left;

  1. Background-size: 14px;
  2. Display: inline-block;
  3. Padding-left: 20px;
  4. Color: #222;
  5. }

</style>

<div> text </div>

Use the background to refer to the picture, and then take advantage of the Padding-left property to keep the text from obscuring the picture, and we can use the Background-position property to adjust the picture position if the picture deviates.

Third, the use of positioning to solve.

<style>

div{

position:relative;

Padding-left: 20px;

}

Div img{

Position:absolute;

top:0;

Left:4px

}

</style>


<div style= "position:relative" > "
Text </div>
Use the positioning to hold the photo, Padding-left let the text do not cover the photo implementation.

PS: The above CSS style only write the key attributes, the specific style according to the different needs of the self-adjustment.
I only list three ways if there is any doubt or I write the wrong kind of, you are welcome to guide the discussion!






Moving Picture Text Vertical Center implementation method

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.