CSS element Centering method

Source: Internet
Author: User

Now there are two ways to know

Method One: Mainly applicable to the case where the element is not set height.

Directly on the code

Html:

1 <Divclass= "Nav-content">2                 <ulNg-click= "Navitem ($event)"Ng-model= "Navtype">3                     <LiID= "Nav_msglist">4                         <ahref="#/">Message list</a>5                     </Li>6                     <LiID= "Mislist">7                         <ahref="#/">Notification list</a>8                     </Li>9                     <Li>Ten                         <ahref="#/">Message list</a> One                     </Li> A                     <Li> -                         <ahref="#/">Message list</a> -                     </Li> the                     <LiID= "Userset"> -                         <ahref= "#/userset">Account Settings</a> -                     </Li> -                 </ul> +             </Div>

Css:

1 . nav-content Li a {color: #6f6f6f; position: absolute; -webkit-transform: translatey ( -50%);  Top: 50%; Display: block;}

Method Two: Applies to the case where the height of the element is known.

Html:

1 <Divclass= "Msg-operation">2       <Divclass= "Optico"><imgsrc= "Images/collect-ico.png" /></Div>3        <Divclass= "Optico"><imgsrc= "Images/collect-ico.png" /></Div>4        <DivClas= "Optico"><imgsrc= "Images/collect-ico.png" /></Div>5  </Div>

Css:

1 . optico{width:30%;d isplay:inline-block;position:relative;text-align:right;} 2 . Optico img{height:20px;width:20px;position:absolute;top:50%;margin-top: -10px;left:50%;margin-left: 10px;}

Of course, in the case of the left and right margin without setting margin, Div Center also has a method, that is, margin:0 auto;

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.