js设置居中

article/2025/9/20 6:08:47

我们在编写html页面的时候,不可避免的会遇到元素居中的问题,水平居中还好说 我们可以通过设置margin: auto;text-align: center;来实现水平居中。垂直居中的话,单个标签我们可以通过设置line-height来实现垂直居中,但是多个标签的时候就没法这么做了。 有一种方法就是通过js算出元素的left和top值来实现居中。一般都是获取父元素的宽高,再获取子元素的宽高,父元素的宽高减去子元素的宽高除以二就是元素本身的left和top的值。 为了方便开发,我将居中写成了几个方法,开发的时候直接调用即可,下面是代码的实现。

js

/*create by yzq*/
/*以浏览器为基准水平居中*/
function centerHorizontal(ele) {var eleW = ele.offsetWidth; //获取元素的宽度var width = client().width; //获取窗口的宽度var left = (width - eleW) / 2; //窗口宽度减去元素宽度,然后除以二ele.style.left=left+"px";}
/*以浏览器为基准垂直居中*/
function centervertical(ele) {var eleH = ele.offsetHeight;var height = client().height;var top = (height - eleH) / 2;ele.style.top = top + 'px';}
/*以浏览器为基准垂直水平居中*/
function centerParent(ele) {var eleW = ele.offsetWidth;var eleH = ele.offsetHeight;var height = client().height;var width = client().width;var left = (width - eleW) / 2;var top = (height - eleH) / 2;ele.style.left=left+"px";ele.style.top=top+"px";
}/*以某个父元素为基准水平居中*/
function centerHorizontalWithEle(child, parent) {var Cw = child.offsetWidth; //获取父元素的宽度var Pw = parent.offsetWidth; //获取子元素的宽度var left = (Pw - Cw) / 2; //父元素宽度减去子元素宽度,然后除以二child.style.left=left+"px";}
/*以某个父元素为基准垂直居中*/
function centerverticalWithEle(child, parent) {var Ch = child.offsetHeight;var Ph = parent.offsetHeight;var top = (Ph - Ch) / 2;child.style.top=top+"px";}
/*以某个父元素为基准居中*/
function centerParentWithEle(child, parent) {var Cw = child.offsetWidth;var Ch = child.offsetHeight;var Pw = parent.offsetWidth;var Ph = parent.offsetHeight;var left = (Pw - Cw) / 2;var top = (Ph - Ch) / 2;child.style.left=left+"px";child.style.top=top+"px";}/*浏览器可视区域的宽高*/
function client(){if(window.innerWidth){return {"width":window.innerWidth,"height":window.innerHeight};}else if(document.compatMode === "CSS1Compat"){return {"width":document.documentElement.clientWidth,"height":document.documentElement.clientHeight};}else{return {"width":document.body.clientWidth,"height":document.body.clientHeight};}
}

html中使用:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><title></title><script type="text/javascript" src="js/centerJs.js" ></script><style>.parent {position: absolute;width: 500px;height: 300px;border: 3px solid red;}.child {width: 100x;height: 100px;position: absolute;border: 2px solid black;}#p {position: absolute;width: 300px;height: 300px;border: 1px solid blue;}#c {position: absolute;border: 1px solid green;}</style><script>window.function  () {center();}window.onresize=function  () {center();}function center () {var p=document.getElementsByClassName("parent")[0];var c=document.getElementsByClassName("child")[0];var p1=document.getElementById("p");var c1=document.getElementById("c");centerParent(p)centerverticalWithEle(c,p);centerHorizontal(p1);centerParentWithEle(c1,p1);}</script></head><body style="width: 100%;height: 100%;"><div class="parent">父元素<div class="child">子元素</div></div><div id="p">父元素<div id="c">子元素</div></div></body></html>

这里写图片描述

demo


http://chatgpt.dhexx.cn/article/clgRfH8X.shtml

相关文章

CSS常见图片居中,文字居中,版心居中集合

1.margin:0 auto&#xff1b;&#xff08;水平居中&#xff09; 适用于&#xff08;块级元素&#xff09; wrapper&#xff08;wrapper只负责版心的效果&#xff09;定义一个固定的宽度&#xff1b;margin&#xff08;外边距&#xff09;左右的值设置为auto。 让带有wrapper…

垂直居中的方法

总结垂直居中的方法 <div class"layout-wrapper"><div class"box1"><h4>垂直居中方法</h4></div></div>.layout-wrapper{width:300px;height:300px;border: 1px solid red; } .box1{height:150px;width:150px;border…

win10任务栏怎样居中win10任务栏居中设定教程

win11系统内置任务栏居中的设置项&#xff0c;但是win10系统没有&#xff0c;倘若win10顾客也想让自己的任务栏居中的话&#xff0c;应当怎样设置呢&#xff1f;你先撤销任务栏锁住&#xff0c;随后新建菜单栏。之后选定一个空白文件夹&#xff0c;之后任务栏就会发生两条竖杠&…

HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结

最近写网页经常需要将div在屏幕中居中显示&#xff0c;遂记录下几个常用的方法&#xff0c;都比较简单。 水平居中直接加上<center>标签即可&#xff0c;或者设置margin:auto;当然也可以用下面的方法 下面说两种在屏幕正中&#xff08;水平居中垂直居中&#xff09;的方…

css字体居中(css字体居中对齐)

css如何让表格居中 层叠样式表(英文全称&#xff1a;Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。 关于网页设计CSS文本垂直居中的问题 text-align:center;文本居中显示 vertical-align…

css图片居中

相信很多工程师都搜索过css图片居中的方法吧&#xff0c;但总是出现各种各样的问题。其实css图片居中分为很多种情况 第一种&#xff1a;已知父元素的高度&#xff0c;单独设置文字水平垂直居中&#xff0c;我们只需要设置css样式line-hight:同父元素高度&#xff0c;text-alig…

html中如何居中

第一步&#xff1a;打开网页编辑器&#xff0c;新建一个网页文件。 第二步&#xff1a;我们编写两个div标签用来做一个对比演示&#xff0c;既嵌套式div。 第三步&#xff1a;首先我想让最外层的div进行真正意义上的居中——既在浏览器页面水平方向和垂直方向都居中显示。 …

HTML中进行居中设置

html居中的方法如下&#xff1a; 1、打开HTML的编辑器。 2、找到需要居中的图片或者文字。 3、在body里面&#xff0c;设置CSS样式。 4、添加样式为&#xff1a;text-align&#xff1a;center &#xff1b;即可。 超文本标记语言&#xff08;Hyper Text Markup Language&a…

div居中

HTML的div居中 一、margin:0px auto; 给需要居中的div设置一个宽度&#xff0c;然后设置元素的上下外边距为 相等 左右外边距为 auto&#xff0c;比如&#xff0c;margin:0px auto。 则可以实现 div 居中显示。 对于浮动元素&#xff0c;设置其左右外边距为关键字 auto 是无效…

HTML元素居中(文字居中,块居中【垂直/水平居中】)

一、文字、行内元素水平居中 给父级属性设置text-align: center即可 HTML代码: <div><p>p</p></div> <div><span>span</span></div> <div><a href"#">a</a></div>CSS代码&#xff1a; …

html让文字居中

html让文字居中的方法&#xff1a;1、给文本所在标签加CSS属性值“text-align:center”&#xff1b;2、在行内标签或行内块级标签中加CSS属性值“text-align:left”。 本文操作环境&#xff1a;windows7系统、HTML5&&CSS3版、Dell G3电脑。 两种情况&#xff1a;1、文…

word文字居中怎么设置

电脑编辑文档时&#xff0c;想要把文字内容居中显示&#xff0c;那么word怎么居中文字&#xff0c;今天和大家一起学习下。 方法1 1&#xff0c;打开新建的word文档&#xff0c;输入文字。 2&#xff0c;选中文字&#xff0c;然后点击工具栏的“居中对齐”&#xff0c;确定即…

8种css居中实现的详细实现方式了

这是一篇关于居中对齐方式的总结 开篇之前&#xff0c;先问一下大家都知道几种居中的实现方式&#xff1f; 面试时答出来两三个就不错了&#xff0c;就怕面试官还让你继续说。今天就来总结一下这些居中的方式 使用flex布局设置居中。使用flex 时也能通过给子项设置margin: au…

各种居中方法汇总(究极版)

本文部分参考文章&#xff1a;https://github.com/ljianshu/Blog/issues/29 每一部分后另附相关实践代码 前言 本文主要介绍水平居中&#xff0c;垂直居中&#xff0c;还有水平垂直居中各种办法&#xff0c;思维导图如下&#xff1a; 一、水平居中 1.行内元素水平居中 利用…

css居中大全(文字居中、块居中、水平居中、垂直居中)

css居中 一、水平居中 1.块本身水平居中 div{width: 100px;height: 100px;border: 1px solid #000;margin:auto;} <div>我本身水平居中 </div>div的margin不设值怎么表示&#xff1a;不设值&#xff0c;也是auto /*给块居中 上20&#xff0c;左右居中&#xff…

Skipped breakpoint at because it happened inside debugger evaluation

问题描述&#xff1a; 在多线程项目中&#xff0c;在idea中打断点时&#xff0c;有时会遇到下面这种情况&#xff1a; idea左下角出现一行红底或者绿底文字提示&#xff1a; Skipped breakpoint at because it happened inside debugger evaluation 然后我们能感受到的就是…

IDEA Debug出现:Skipped breakpoint at because it happened inside debugger evaluation

在使用IDEA debug功能时&#xff0c;没有出现断点处程序变量快照&#xff0c;而是进入了如下debug界面&#xff0c;并打开了URL类加载程序&#xff1a; 网上有一些方法&#xff0c;但其实都没用&#xff0c;或者说没有从根本上解决这个问题&#xff0c;下面给出我的方法。 ok&a…

ssh上传公钥报 All keys were skipped because they already exist on the remote system.

项目需要用Ansible进行多台服务器部署&#xff0c;服务器申请下来给app用户赋了权限后root用户就被上收了。 在做ssh免密登陆时报错&#xff1a; sudo ssh-copy-id -i /root/.ssh/id_rsa.pub app22.10.206.140 试了很多方法都没用&#xff0c;结果在140服务器新建/root/.ssh这…

创建vue项目的时候报错:Skipped git commit due to missing username and email in git config.

创建vue项目的时候报错&#xff1a; WARN Skipped git commit due to missing username and email in git config, or failed to sign commit. You will need to perform the initial commit yourself. 原因&#xff1a; git 进行初始化提交 没有绑定 对应的 git用户名和邮箱 …

Skipped breakpoint because it happened inside debugger evaluation

在debug项目时总是会莫名其妙的多出system.out的数据&#xff0c;莫名其妙&#xff0c;搜到这篇文章&#xff0c;帮助很大&#xff0c;转载一下 解决Skipped breakpoint at %code reference% because it happened inside debugger evaluation的通用方法。 先尝试去掉勾选 Ena…