如何实现固定宽高的DOM元素的水平垂直居中
效果:
实现代码1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body{margin: 0; padding: 0;}
#div1{
margin-top: 100px;
margin-left: 150px;
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
#div2{
width: 120px;
height: 120px;
background-color: black;
position: absolute;
/*(300-120)/2/300*/
top: 30%;
left: 30%;
}
</style>
</head>
<body>
<div id="div1" class="classname">
<div id="div2" class="classname">
</div>
</div>
</body>
</html>
实现代码2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body{margin: 0; padding: 0;}
#div1{
margin-top: 100px;
margin-left: 150px;
width: 300px;
height: 300px;
background-color: red;
}
#div2{
width: 120px;
height: 120px;
background-color: black;
/*(300-120)/2/300=30%*/
position: relative;
top: 30%;
left: 30%;
}
</style>
</head>
<body>
<div id="div1" class="classname">
<div id="div2" class="classname">
</div>
</div>
</body>
</html>
实现代码3:
如何实现固定宽高的DOM元素的水平垂直居中的更多相关文章
- 已知宽高和未知宽高的div块的水平垂直居中
		
//已知宽高的情况 .div1_container{ border:1px solid #00ee00; height:300px; position:relative; } ...
 - css固定宽高DIV内部元素垂直居中的方法
		
应用案例 案例是这样的,一个外层div,高宽是固定的,但是里面内容不是固定的.很多朋友的做法是头部加一个padding或者margin,这样,里面内容显得貌似是居中了,但是假如内容变化,这样头部的固定 ...
 - div 固定宽高 水平垂直居中方法
		
div固定宽高,水平垂直居中,根据所用单位不同,分成两种情况,分别是"px"和"%"情况. 例:将三层div做出三个边框,要求水平垂直居中.效果如图 情况一(单 ...
 - 固定宽高的DIV绝对居中示例
		
看了一些代码,然后自己试验了一番,分享如下示例: 实现点: 如果元素的宽高固定,那么,css指定样式为top:50%;left:50%; 而margin-top和 margin-left 指定为负数, ...
 - css中固定宽高div与不固定宽高div垂直居中的处理办法
		
固定高宽div垂直居中 如上图,固定高宽的很简单,写法如下: position: absolute; left: 50%; top: 50%; width:200px; height:100px; m ...
 - 偏前端 - 不是固定宽高,页面随设备同比 放大/缩小 展示。不妨看看rem单位,你就有眉目上手做了!!!
		
为什么要使用rem 之前有些适配做法,是通过js动态计算viewport的缩放值(initial-scale). 例如以屏幕320像素为基准,设置1,那屏幕375像素就是375/320=1.18以此类 ...
 - table 与 div 固定宽高问题
		
div {width:100px;height:200px;word-wrap: break-word;overflow:hidden;} 这个可以固定div的宽高,溢出内容会被剪除. table{t ...
 - 不定宽高的文字在div中垂直居中
		
本人在面试的时候被问到:如何使一段不定宽高的文字垂直居中呢? 现在来总结一下: 在body中写入结构 <div id="main"> <div id=&qu ...
 - CSS子元素在父元素中水平垂直居中的几种方法
		
1. 水平居中(margin: auto;)子父元素宽度固定,子元素上设置 margin: auto; 子元素不能设置浮动,否则居中失效. #div1{ width: 300px; height: 3 ...
 
随机推荐
- SpringCloud创建Eureka模块集群
			
1.说明 本文详细介绍Spring Cloud创建Eureka模块集群的方法, 基于已经创建好的Spring Cloud Eureka Server模块, 请参考SpringCloud创建Eureka ...
 - 巅峰对决!Spring Boot VS .NET 6
			
原文作者:Putu Prema 原文链接: https://medium.com/@putuprema/spring-boot-vs-asp-net-core-a-showdown-1d38b89c6 ...
 - 『无为则无心』Python函数 — 30、Python变量的作用域
			
目录 1.作用于的概念 2.局部变量 3.全局变量 4.变量的查找 5.作用域中可变数据类型变量 6.多函数程序执行流程 1.作用于的概念 变量作用域指的是变量生效的范围,在Python中一共有两种作 ...
 - svn创建多个版本库
			
mkdir /pangbing cd /pangbing/ svnadmin create 1 svnadmin create 2 svnadmin create3 启动时候这样启动 svnserve ...
 - tomcat启动卡在了 At least one JAR was scanned for TLDs yet contained no TLDs 的根本原因与解决办法
			
1.前言 有时候服务器开启时启动不了,卡在了 org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned fo ...
 - linux修改配置文件关闭终端失效问题
			
当前shell环境为 交互式login-shell(非图形化界面环境) /etc/profile /etc/bash.bashrc ~/.profile ~/bashrc 当前环境为 交互式非logi ...
 - POJ prime distance
			
https://oj.shiyancang.cn/Problem/781.html 素数距离,数据范围21亿,如果用素数筛存,并且进行做的话,按照x/lnx计算会是一个非常恐怖的复杂度.确定要做什么, ...
 - JS定时器不可靠的原因及解决方案
			
前言 在工作中应用定时器的场景非常多,但你会发现有时候定时器好像并没有按照我们的预期去执行,比如我们常遇到的setTimeout(()=>{},0)它有时候并不是按我们预期的立马就执行.想要知道 ...
 - 一文读懂 HTTP/1HTTP/2HTTP/3
			
转自 https://zhuanlan.zhihu.com/p/102561034
 - gin中的多模板和模板继承的用法
			
1. 简单用法 package main import ( "github.com/gin-contrib/multitemplate" "github.com/gin- ...