三行代码实现div内元素垂直居中

告别PPT卡顿!用LaTeX Beamer制作学术报告幻灯片的保姆级教程(附Overleaf在线配置) 本文提供了一份详细的LaTeX Beamer教程,帮助学术研究者告别PPT卡顿问题。通过Overleaf在线配置和Beamer的强大功能,实现公式精确排版、参考文献自动管理及跨平台显示一致性,打造专业高效的学术报告幻灯片。 阅读详情

在前端编写中,经常会遇到div内部元素需要居中效果,主要实现方式为:

1.水平居中:margin: 0 auto;

2.垂直居中,

position: relative;
top: 50%;
transform: translateY(-50%);

Demo如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.div1 {
				border: 1px solid red;
				height: 200px;
				width: 200px;
			}
			.div2 {
				border: 1px solid green;
				height: 100px;
				width: 100px;
				margin: 0 auto;
				position: relative;
				top: 50%;
				transform: translateY(-50%);
			}
		</style>
	</head>

	<body>
		<div class="div1">
			<div class="div2">

			</div>
		</div>
	</body>
</html>

(1)未设置Position:relative之前,内部的元素贴在外部元素的顶端。

(2)设置了相对定位,并位移父容器的50%的高度。即向下平移50%。


(3)设置向上位移50%自身高度,完成居中效果。

CSS+DIV布局(满屏自适应三行布局,兼容FF,div垂直居中) 带DOCTYPE的写法。。 ------------------ CSS+DIV写的,上下固定高度,中间自动适应高度满屏布局的页面~~写了好久的。ie6还有点问题。。。 兼容IE7,IE8,FF,Opera,Safari,谷歌浏览器 ------------------------- 附送一个 DIV垂直居中的页面~~~ 立即下载

相关推荐

PRIDE PPP-AR软件实战:从ZTD解算到PWV反演的全流程避坑指南

本文详细解析了使用PRIDE PPP-AR软件进行GNSS数据处理的全流程,重点涵盖从ZTD解算到PWV反演的关键步骤与精度评估。文章深入剖析了软件部署、数据准备、参数配置中的常见“坑点”,并提供了与IGS ZTD及ERA5产品进行交叉验证的实用方法,旨在帮助用户高效、准确地获取高精度大气可降水量产品。

p8q9r0的博客 625

web中的各种居中

居中是前端开发过程中最常见的布局,但是很多人都不清不楚,每次就靠试一试,今天就拎一拎~~ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>学习居中</title> <style> .father { height: 200px; wid...

要和代码过一辈子的博客 1万+

DIY等离子环:从Class-E电路到七彩辉光,手把手教你打造炫酷科学玩具

本文手把手教你DIY一个炫酷的等离子环科学玩具。从解析等离子体辉光放电原理入手,详细介绍了核心的Class-E自激振荡电路设计、关键元件选型与参数计算,并提供了从物料准备、电路搭建、腔体密封到上电调试的全流程指南。通过调制电路或填充不同气体,还能实现七彩变换与交互效果,是学习高频电路与电磁场知识的绝佳实践项目。

weixin_29305337的博客 647

table让元素垂直居中的布局小技巧

通过给父元素设置display:table,然后给子元素设置display:table-cell,vertical-align:middle可以实现子元素的内容水平垂直居中,下面代码实例: html> lang="en"> charset="UTF-8"> Title type="text/css" rel="stylesheet"> .table

web_hua的博客 8334

Dify 从入门到熟悉100 天的学习大纲

理解 Dify 的核心概念,熟悉操作界面,并能创建简单的 AI 应用。祝你在这 100 天的旅程中学有所成,成为一名优秀的 AI 应用开发者!掌握生产环境部署、监控、调优,并关注 Dify 生态和前沿动态。探索多模态、Agent、API 集成、性能优化等高级主题。掌握工作流编排、高级提示词技巧、知识库优化等核心功能。

IT深耕十余载,大道之简 1747

Mixly/Arduino实战:WS2812点阵动态显示自定义图案与汉字

本文手把手教你使用Mixly和Arduino驱动WS2812点阵屏,实现自定义图案与汉字的动态显示。内容涵盖硬件连接、取模软件设置、高效数据处理技巧(巧用Excel)、Mixly图形化编程及Arduino代码解析,并深入探讨动态显示与滚动汉字原理,最后提供常见问题排查与性能优化心得,助你从零开始轻松玩转智能硬件。

weixin_29185199的博客 459

3行Css实现div水平、垂直居中

HTML部分:<div class="container"> <!-- This is your content ... --> </div>Css部分:就像下面,只需要给.container三行css规则就可以使它在水平方向和垂直方向均处于居中。.container { display: flex; justify-content: center; al

Dragon.G 1067

Ghost卷积原理与构建YOLOv11的GhostNet变体

本文探讨了在边缘设备部署YOLOv11时使用Ghost卷积进行模型轻量化的实践经验。针对Jetson Nano的4GB内存限制,作者通过替换Backbone中的3x3标准卷积为Ghost卷积,实现了参数减少28%、推理速度提升38%,仅损失1.1%mAP的优化效果。文章详细介绍了Ghost卷积的实现原理、在YOLOv11中的具体替换策略,以及训练调参和部署优化的关键技巧。作者强调轻量化是计算效率与模型性能的权衡,建议工程团队保留原模型和Ghost变体两个版本,根据实际需求灵活切换。Ghost卷积的核心价值在

as1225120794的博客 171

div水平垂直居中

使用flex 主要就三行:display: flex;align-items: center; // 水平居中justify-content: center; // 垂直居中

frt的博客 201

Kimodo 入门教程(一)英伟达开源人形机器人动捕数据集训练运动学动作扩散模型

Kimodo是一个基于700小时商用光学动作捕捉数据集训练的运动学动作扩散模型,能够生成高质量的人体和机器人3D动作。该模型支持通过文本提示和多种约束条件(如关键帧、末端执行器位置等)进行控制,并提供数字人类和类人机器人两种模型变体。安装方式包括软件包安装和源代码安装两种,需要配置HuggingFace令牌以访问文本编码器。推荐使用GPU加速,最低需要17GB显存。项目提供命令行生成和交互式演示两种使用方式,演示程序支持时间轴编辑和实时3D可视化。模型分为商用友好版和受限版,其中SMPL-X版本需要额外申请

2506_90492529的博客 1971

FPGA&ASIC笔面试题(四):编写Verilog代码描述跨时钟域信号传输,快时钟域到慢时钟域(补充)

      跨时钟域处理从快时钟域到慢时钟域,如果是下面第一个图,cklb则可以采样到signal_a_in,但是如果只有单脉冲,如第二个图,则不能确保采样掉signal_a_in。这个时候用两级触发器同步是没有用的。 代码如下: //Synchronous module Sync_Pulse( input clka, input clkb, input rst_n, input pulse_ina, output pulse_outb, ou

千歌叹尽执夏的博客 2518

css 文本和div垂直居中方法汇总

本文总结了垂直居中的各种方法,包括单行文本垂直居中、多行文本垂直居中以及div垂直居中,列出了多种情况对应的方法。读者可以根据实际需要选择合适的方法。

诗渊的博客 20万+

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

CSS中实现div元素水平垂直居中

CSS中实现div元素水平垂直居中

qq_45442879的博客 1433

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

div块水平居中,垂直居中

水平居中 一个div想要水平居中于它的父div中只需要给它加css属性margin:0 auto; 即可 <!DOCTYPE html> <html> <head> <title></title> </head> <style> #box{ width: 200px; height: 200px; bord...

Yan 小铁匠 7686

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

解决div中元素垂直居中的五种方法

假设div的高度为200px,字体大小为20px 方法一:利用padding内边距 我要垂直居中 .type1{ border: 1px solid blue; font-size: 20px; padding: 90px 0; line-height: 1; } 注意此时不用设置div的高度height:200p

NananananA' township 6973

div内元素水平、垂直居中

div内元素水平、垂直居中

wsdhla的专栏 1万+

css元素水平垂直居中

/图片与文字垂直居中,但会有少量偏差,再通过。在应用一的基础上,给子项(元素) 添加 vertical-align: middle;//子元素相对父元素水平(主轴)居中。//子元素相对父元素垂直(交叉轴)居中。这种写法,在没有指定子元素宽高的情况下,也能让其在父容器中垂直居中。//给父元素加,弹性布局。margin-left:- 自身宽度一半;margin-top:-自身高度一半;

博哥的二进制世界 2050

实现元素的水平垂直居中:全面详解

实现元素的水平垂直居中:全面详解

嗨,欢迎来到我的 CSDN 博客小天地!一名深耕多年的技术发烧友。在这里,我将把日常工作中积累的宝贵经验,从复杂架构设计的精妙之处,到代码优化的实战技巧,毫无保留地分享给大家。 7129

div在html垂直居中,div 垂直居中(css div水平垂直居中六种方法)

div垂直居中div垂直居中代码示例如下:前台效果如下图所示:如图所示,div中的文字垂直居中。其中主要的属性就是line-height。line-height属性设置行间的间隔(行高即垂直方向)。line-height属性会影响行框的规划。在应用到一个块级元素时,它定义了该元素中基线之间的最小间隔而不是最大间隔。line-height与font-size的计算值之差(在CSS中成为“行间距”)分...

weixin_29119475的博客 4232

【CSS】让div在另一个div中垂直居中

垂直居中是布局中十分常见的效果之一,如果我们想实现下图的效果: 首先我们写两个div:(下面的介绍中,我们称外层div为父div,里面的div为子div)。 <body> <div class="content"> <div>我是需要垂直居中的div</div> </div> </b...

qing_小诺的博客 8962

div内部元素实现水平垂直居中总结

(1)flex弹性布局 第一种实现办法(align-self: center): .box{ width: 300px; height: 300px; background-color: red; /*弹性布局,子元素float,vertical-align,clear失效*/ display:flex; } .box_child{ width: 200...

Johan666的博客 2212

div内元素居中(水平居中、垂直居中、水平和垂直都居中)

1. 水平和垂直都居中 效果图 相关代码 <!-- html相关代码 --> <div id="outer"> <div id="insider"></div> </div> /* css相关代码 */ html, body { width: 100%; height: 100%; } ...

Pei71320的博客 6075

html div中元素垂直居中,HTML/CSS:div水平与元素垂直居中(2)

单个div水平居中:设置margin的左右边距为自动div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中的效果代码:HTMLCSS#parent {margin: 0 auto;}多个div水平居中:设置display的属性为inline-block多个div水平居中,text-align和vertica...

weixin_39603823的博客 734

div中input垂直居中的方法

div中input垂直居中的方法 1. absolute+transfrom <div> <input type="text"/> </div> <style> div{ position: relative; height: 44px; background-color: #fe3c35; } input{ position: absolute; top: 50%; transform: translateY(-50%

q850593913的博客 9398

如何让一个div盒子水平垂直居中(实现代码如下:)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-...

weixin_43748930的博客 2086
上一篇: 史上最全的CSS hack方式一览
下一篇: 关于获取到页面顶部的距离的兼容问题
水果控丶
博客等级 码龄16年 1粉丝 2原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值