


How to center the content vertically and horizontally when writing a modal box?
Hello everyone, today we will talk about how to center the content vertically and horizontally when writing a modal box. Let’s keep our focus simple first,
This problem is actually equivalent to how to center a div vertically and horizontally. So how do we achieve this problem? There are 5 solutions.
First of all, in the first case, when we know the width and height of div1:
<div></div> #div1{
<span style="font-size: 18px"> width: 200px;</span><br><span style="font-size: 18px"> height: 100px;</span><br><span style="font-size: 18px"> background: #54fa45;</span><br><span style="font-size: 18px"> position: absolute;</span><br><span style="font-size: 18px"> left:50%;</span><br><span style="font-size: 18px"> top:50%;</span><br><span style="font-size: 18px"> margin-left:-100px; //设置div1的左边距为宽度的一半</span><br><span style="font-size: 18px"> margin-top:-50px; //设置div1的上边距为高度的一半</span><br><span style="font-size: 18px"> }<br> 第二种情况下:不知道div1的宽高,就好比设置图片的垂直水平居中:<br></span>
<span style="font-size: 18px"> img{</span><br><span style="font-size: 18px"> position: absolute;</span><br><span style="font-size: 18px"> left: 0;</span><br><span style="font-size: 18px"> top: 0;</span><br><span style="font-size: 18px"> right: 0;</span><br><span style="font-size: 18px"> bottom: 0;</span><br><span style="font-size: 18px"> margin: auto;</span><br><span style="font-size: 18px"> }</span>
<span style="font-size: 18px"> 这种情况其实很简单,给图片一个绝对定位,然后上下左右都为0,最后margin:auto;<br> 第三种情况:利用display:table<br></span>
<span style="font-size: 18px"> html,body{</span><br><span style="font-size: 18px"> height: 100%;</span><br><span style="font-size: 18px"> overflow:hidden;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> #box{</span><br><span style="font-size: 18px"> width:100%;</span><br><span style="font-size: 18px"> height:100%;</span><br><span style="font-size: 18px"> background: #dbffd8;</span><br><span style="font-size: 18px"> display:table;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> #div1{</span><br><span style="font-size: 18px"> height:100%;</span><br><span style="font-size: 18px"> width:100%;</span><br><span style="font-size: 18px"> display:table-cell;</span><br><span style="font-size: 18px"> text-align:center;</span><br><span style="font-size: 18px"> vertical-align:middle;</span><br><span style="font-size: 18px"> }<br> 第四种情况:利用display:</span><span style="font-size: 18px"><em style="line-height: 1.5">table-cell;<br></em></span>
<span style="font-size: 18px"> html,body{</span><br><span style="font-size: 18px"> height: 100%;</span><br><span style="font-size: 18px"> overflow:hidden;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> #box{</span><br><span style="font-size: 18px"> width:100%;</span><br><span style="font-size: 18px"> height:100%;</span><br><span style="font-size: 18px"> background: #dbffd8;</span><br><span style="font-size: 18px"> display:table;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> #div1{</span><br><span style="font-size: 18px"> height:100%;</span><br><span style="font-size: 18px"> width:100%;</span><br><span style="font-size: 18px"> <span style="color: #ccffff"> <span style="color: #000000">display:table-cell;</span></span></span><br><span style="font-size: 18px; color: #000000"> text-align:center;</span><br><span style="font-size: 18px; color: #000000"> vertical-align:middle;</span><br><span style="font-size: 18px"> }<br> 最后一种就是利用盒子的怪异盒模型了<br></span>
<span style="font-size: 18px"> <div id="box"> <br><span style="font-size: 18px"> <img src="/static/imghwm/default1.png" data-src="img/map-pin.png" class="lazy" alt=""></span><br><span style="max-width:90%"> </span> </div> <br></span>
<span style="font-size: 18px"> html,body{</span><br><span style="font-size: 18px"> height: 100%;</span><br><span style="font-size: 18px"> overflow:hidden;</span><br><span style="font-size: 18px"> }</span><br><span style="font-size: 18px"> #box{</span><br><span style="font-size: 18px"> width:100%;</span><br><span style="font-size: 18px"> height:100%;</span><br><span style="font-size: 18px"> display:-webkit-box;</span><br><span style="font-size: 18px"> -webkit-box-align:center;</span><br><span style="font-size: 18px"> -webkit-box-pack:center;</span><br><span style="font-size: 18px"> }<br>这就是我对元素水平居中的理解,你们学会了吗?</span>
<br>
The above is the detailed content of How to center the content vertically and horizontally when writing a modal box?. For more information, please follow other related articles on the PHP Chinese website!

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools
