Hongmu Notes
Home Language Notes css absolute positioning center
Language Notes CSS

css absolute positioning center

css absolute positioning center

There are many ways to implement CSS absolute positioning and centering. Below we will introduce to you four methods of CSS absolute positioning and centering.

1. Usage of mainstream CSS absolute positioning and centering with good compatibility:


.conter{
  width:600px;height:400px;
  position:absolute;left:50%;top:50%;
  margin-top:-200px;/*高度的一半*/
  margin-left:-300px;/*宽度的一半*/
}

Note: This method has an obvious shortcoming, which is that the size of the element needs to be known in advance. Otherwise, the adjustment of negative margin values ​​cannot be accurate. At this time, it is often necessary to obtain it with the help of JS.

2. The emergence of CSS3 has led to a better solution, which is to use transform instead of margin. The percentage value of the translate offset in transform is relative to its own size. You can achieve CSS absolute positioning and centering like this:


.conter{
  width:600px;height:400px;
  position:absolute;left:50%;top:50%;
  transform:translate(-50%,-50%);/*50%为自身尺寸的一半*/
}

3. margin:auto realizes the centering of absolutely positioned elements (0 position positioning for top, bottom, left, and right; margin:auto)


.conter{
  width:600px;height:400px;
  position:absolute;left:0;top:0;right:0;bottom:0;
  margin:auto;/*有了这个就自动居中了*/
}

4. Use css3 box model: flex layout to achieve css absolute positioning and centering. This situation can be used regardless of lower version browsers.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

css positioning

css positioning Language Notes CSS

Syntax: position: static | relative | absolute | fixed | center | page | sticky Default value: static Applies to: All elements except the display attribute defined as table-column-group | table-column Inheritance: No Animation: No...
👁 119
css vh and vw adaptive

css vh and vw adaptive Language Notes CSS

In the mobile terminal, REM is used to change the root HTML, and the mobile terminal adaptation is realized through a piece of JS. This article uses pure CSS viewport units to self-adapt. Although the current compatibility is not fully acceptable, it does not prevent you from recognizing the power of vw and vh. The implementation of responsive layout relies on media queries (Media Queries). Select the width size of mainstream devices as breakpoints to target...
👁 356

Recommended reading

Responsive Fruit and Vegetable Processing Biotechnology Website Template 1204

Responsive Fruit and Vegetable Processing Biotechnology Website Template 1204 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the fruit and vegetable processing and biotechnology industries. Its modern, health-focused design effectively showcases fruit and vegetable processing products, biotechnology innovations, R&D capabilities, and corporate image. This template helps biotechnology companies present their brands online and attract potential partners. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About Installing EasyCMS...
👁 57
(Adaptive Mobile Version) Blue Hardware Products Website Template – Download Corporate General Website Source Code – 0954

(Adaptive Mobile Version) Blue Hardware Products Website Template – Download Corporate General Website Source Code – 0954 Practical Collection pbootcms Template

A blue hardware product-themed website template for the PbootCMS platform, compatible with both PC and WAP devices. Featuring a professional and practical design style, this template is ideal for hardware companies to showcase their product ranges and brand identity. It is an efficient choice for hardware businesses looking to establish an official brand website. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pboo...
👁 63
Linux: Query large files on a server

Linux: Query large files on a server Summary of pitfalls

You can use the `find` command to search for large files in a Linux file system. Enter the following command in your terminal to locate all files larger than 100 MB: `find / -type f-size +100M-exec ls-lh {} \;` Here, `/` represents the root directory to search within; `-type f` specifies that only files (not directories) should be searched;...
👁 194
Responsive specialty food and coarse grain snack website template – 1205

Responsive specialty food and coarse grain snack website template – 1205 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the specialty food, whole-grain, and snack industries. Its appealing design effectively showcases specialty food products, whole-grain items, snack brands, and sales information. It helps food brands present their products online and attract consumers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 45
(Adaptive mobile version) Packaging Materials Website Template – Download Source Code for Printing Packaging Websites – 0955

(Adaptive mobile version) Packaging Materials Website Template – Download Source Code for Printing Packaging Websites – 0955 Practical Collection pbootcms Template

A PbootCMS website template for packaging materials and printed packaging, compatible with both PC and WAP devices. The professional and practical design is ideal for showcasing packaging material products, printing processes, and customized services. It helps packaging and printing enterprises demonstrate their product capabilities online and attract brand clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 58