search
HomeWeb Front-endCSS TutorialHow to center horizontally and vertically absolutely positioned elements

This article mainly introduces the method of horizontal and vertical centering of absolutely positioned elements. There are 3 methods for reference. Friends who need it can take a look.

1.css to achieve centering

Disadvantages: Need to know the width and height of the element in advance.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{
            width: 600px; 
            height: 400px;
            position: absolute; 
            left: 50%; top: 50%;
            border:1px solid #000;
            background:red;
            margin-top: -200px;    /* 高度的一半 */
            margin-left: -300px;    /* 宽度的一半 */
        }
    </style>
</head>
<body>
    <p class="box">
    </p>
</body>
</html>

2. CSS3 achieves horizontal and vertical centering

Note: Regardless of the size of the element Whatever it is, it can be centered. However, this writing method is only compatible with IE8 and above, and can be ignored on mobile terminals.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{
            width: 600px; 
            height: 400px;
            position: absolute; 
            left: 50%;
            top: 50%;
            border:1px solid #000;
            background:red;
            transform: translate(-50%, -50%);    /* 50%为自身尺寸的一半 */
        }
    </style>
</head>
<body>
    <p class="box">
    </p>
</body>
</html>

3. Margin: auto to achieve centering

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{
            width: 600px; 
            height: 400px;
            position: absolute; 
            left: 0;
            top: 0; 
            right: 0; 
            bottom: 0;
            border:1px solid #000;
            background:red;
            margin: auto;    /* 有了这个就自动居中了 */
        }
    </style>
</head>
<body>
    <p class="box">
    </p>
</body>
</html>

More For related articles on horizontal and vertical centering methods of absolutely positioned elements, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
3060显卡的性能如何?3060显卡的性能如何?Jan 27, 2024 am 10:36 AM

有很多游戏玩家对显卡3060表示非常好奇,想入手一张3060的显示,但是不知道RTX3060是在一个什么水平,其实这款显卡也是在一个比较主流的水平。3060显卡什么水平:答:主流水平。这款RTX3060显卡非常受广大玩家的欢迎,并且也在热卖中,其拥有RTX30的系列特有的优点,性价比非常高,对游戏画面要求较高的玩家可以选择这张显卡。3060显卡拓展介绍:帧率:被广大玩家所喜爱,拥有12GB的大显存,非常受欢迎。在12GB的大显存下,能够流畅运行起3A大作无任何卡顿,帧数甚至可达到60。性价比:它

WPS表格居中怎么设置WPS表格居中怎么设置Mar 19, 2024 pm 09:34 PM

随着WPS的功能越来越强大,我们遇到的关于功能使用的问题也越来越多。在WPS中,我们经常会使用到WPS表格,如果我们需要打印WPS表格,为了让表格看起来美观,这时候我们需要让表格居中。那么,问题来了,我们该如何让WPS表格居中呢?今天我在这里分享一下教程,希望能够帮到你们哦!步骤详情:1、我以实战操作来讲解,以下是我用WPS表格制作了一个简单的表格。2、通过打印预览,我们可以发现,WPS表格默认是居左的。如果我们要是想把表格居中的话该怎么办呢?3、这时候,我们需要点击【工具栏】中的【页面布局】的

评测麒麟9000s的性能与水平评测麒麟9000s的性能与水平Mar 18, 2024 pm 03:21 PM

麒麟9000s作为今年手机市场的一颗新星,备受关注。伴随着智能手机市场的竞争日益激烈,作为华为公司的旗舰处理器,麒麟系列一直以来都备受瞩目。而作为最新推出的一款处理器,麒麟9000s究竟表现如何,它的性能和水平又如何呢?下面将对麒麟9000s进行评测,从各个方面来剖析其优劣。首先从性能方面来看,麒麟9000s采用了先进的5nm工艺制造,集成了ARM最新的Co

如何将一个div居中在另一个div中?如何将一个div居中在另一个div中?Sep 08, 2023 am 11:13 AM

简介div的居中对齐是前端开发最重要的方面之一。在本文中,我们将了解使用HTML和CSS将一个div置于另一个div中的技术。在本教程中,我们将有一个父div,它应具有子div。我们的任务是将子div放置在父div的中心。使用Transform翻译和位置语法这不是一种非常流行的将一个div居中对齐到另一个div中的方法语法left:50%;top:50%;Transform:translate(-50%,-50%);上面的语法执行以下操作-CSS规则“left:50%;”将元素的水平位置设置为其

操作方法:如何调整Win11任务栏图标大小且居中显示操作方法:如何调整Win11任务栏图标大小且居中显示Jan 03, 2024 am 08:17 AM

win11带来了全新的界面包括任务栏也进行了改变,但是呢很多的用户使用的时候都感觉任务栏的图标太大了不习惯,为此下面就给大家带来了win11居中任务栏图标变小操作方法,快来一起学习一下吧。win11居中任务栏图标怎么变小:1、说先用户用户要打开进入注册表编辑器。2、然后依次展开:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\。3、然后在右边新建一个“TaskbarSi”的DWORD值,将

如何在HTML中使文本框居中显示如何在HTML中使文本框居中显示Feb 19, 2024 pm 11:01 PM

html文本框怎么居中,需要具体代码示例在网页设计中,居中对齐元素是一种常见的布局需求。对于HTML文本框,想要使其居中显示,可以通过几种方法实现。下面将为大家详细介绍一种常用的居中方式,并附上具体的代码示例。方法一:使用CSS样式表中的居中属性要实现文本框的居中显示,可以利用CSS样式表中的text-align属性。将文本框所在的父容器元素设置为居中对齐即

在CSS中创建水平可滚动的部分在CSS中创建水平可滚动的部分Aug 29, 2023 am 09:29 AM

水平可滚动的部分是一种常见的网页设计模式,用于展示超出视口宽度的内容。这种设计模式允许用户水平滚动,提供了一种独特而吸引人的方式来展示大型图像、画廊、时间轴、地图和其他内容。这是通过使用CSS属性,如overflow−x:auto或overflow−x:scroll来实现的。这使用本机浏览器功能进行水平滚动,并且跨设备响应。允许轻松导航和探索内容。它不需要任何额外的库或插件。算法使用“container”类定义容器元素。将容器的“overflow−x”属性设置为“auto”以启用水平滚动。将容器

连接3个点所需的水平或垂直线段的数量连接3个点所需的水平或垂直线段的数量Aug 25, 2023 pm 04:49 PM

假设给定三个不同的点(或坐标),你想要找出通过连接这三个点可以形成的水平或垂直线段的数量。这样的线段也被称为折线。为了解决这个问题,你需要计算几何的概念。在本文中,我们将讨论在C++中解决这个问题的各种方法。输入输出场景假设c1,c2和c3是笛卡尔平面上3个点的坐标。连接这3个点的水平或垂直线段的数量将如下所示。Input:c1=(-1,-1),c2=(-2,3),c3=(4,3)Output:1Input:c1=(1,-1),c2=(1,3),c3=(4,3)Output:2Input:c1=

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.