CSS实现垂直居中的常用方法总结(看了面试必过的那种)

【高频考点精讲】使用 CSS 实现垂直居中的多种方法 🧑‍🏫:全栈老李📅:2025 年 5 月🧑‍💻:前端初学者、进阶开发者🚀:本文由全栈老李原创,转载请注明出处。最近在给学员改作业时发现,很多同学还在用来垂直居中,结果发现根本不起作用(笑)。今天全栈老李就给大家好好盘一盘CSS垂直居中这个"老演员",保证你看完就能在项目里秀起来。 阅读详情

写在前面

     很多前端面试中,都会问到css设置垂直居中的方法,这里做一个总结,你这个小机灵鬼一定要学会啊。

正文

     首先,如果 .parent 的 height 不写,你只需要 padding: 10px 0; 就能将 .child 垂直居中;如果 .parent 的 height 写死了,就很难把 .child 居中,以下是垂直居中的方法。
     注意:能不写 height 就千万别写 height。
     接下来,将具体讲述几种方法:

  1. 使用table自带的功能,或者将div封装成table
 html代码
<table class="parent">
    <tr>
      <td class="child">
      一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
      </td>
    </tr>
  </table>
  
css
.parent{
	  border: 1px solid red;
	  height: 600px;
}
.child{
  	border: 1px solid green;
}

将div封装成table
html代码
<div class="table">
      <div class="td">
        <div class="child">
          一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
        </div>
    </div>
  </div>

css
div.table{
  display: table;
  border: 1px solid red;
  height: 600px;
}

div.tr{
  display: table-row;
  border: 1px solid green;
}

div.td{
  display: table-cell;
  border: 1px solid blue;
  vertical-align: middle;
}
.child{
  border: 10px solid black;
}
  1. margin-top,margin-left设置-50%
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>

css
.parent{
	  height: 600px;
	  border: 1px solid red;
	  position: relative;
}
.child{
	  border: 1px solid green;
	  width: 300px;
	  position: absolute;
	  background:red;
	  top: 50%;
	  left: 50%;
	  height: 100px;
	  margin-left: -150px;
	  margin-top: -50px;
}
  1. translate:-50%
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>

css
.parent{
	  height: 600px;
	  border: 1px solid red;
	  position: relative;
}
.child{
	  border: 1px solid green;
	  position: absolute;
	  top: 50%;
	  left: 50%;
	  transform: translate(-50%,-50%);
}
  1. absolute margin auto
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>

css
.parent{
	  height: 600px;
	  border: 1px solid red;
	  position: relative;
}
.child{
	  border: 1px solid green;
	  position: absolute;
	  width: 300px;
	  height: 200px;
	  margin: auto;
	  top: 0;
	  bottom: 0;
	  left: 0;
	  right: 0;
}
  1. :after   :before加上inline-block
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>
  
css
.parent{
	  border: 3px solid red;
	  height: 600px;
	  text-align: center;
}

.child{
	  border: 3px solid black;
	  display: inline-block;
	  width: 300px;
	  vertical-align: middle;
}

.parent:before{
	  content:'';
	  outline: 3px solid red;
	  display: inline-block;
	  height: 100%;
	  vertical-align: middle;
}
.parent:after{
	  content:'';
	  outline: 3px solid red;
	  display: inline-block;
	  height: 100%;
	  vertical-align: middle;
}
  1. 使用flex
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>
  
css
.parent{
	  height: 600px;
	  border: 3px solid red;
	  display: flex;
	  justify-content: center;
	  align-items: center;
}
.child{
	  border: 3px solid green;
	  width: 300px;
}

7.grid也是可以的(不过存在兼容问题,不推荐)

 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>
  
css
.parent{
	  height: 600px;
	  border: 3px solid red;
	  display: grid;
}
.child{
	  border: 3px solid green;
	  width:300px;
	  align-self: center;
      justify-self: center;
}
  1. 使用writing-mode
html代码
<div class="g-father">
    <div class="parent">
        <div class="child">这里垂直居中了</div>
    </div>
</div>

css
.g-father {
    writing-mode: vertical-lr;
    text-align: center;
}
.parent{
    writing-mode: horizontal-tb;
    display: inline-block;
    text-align: center;
    width: 100%;
}
.child{
    display: inline-block;
    margin: auto;
    text-align: left;
}

写在最后

     到这里,就已经讲了8种方法了,只要记住四五种,多练练,面试你没问题的。附加一张效果图:
在这里插入图片描述

面试经典题目: CSS实现水平垂直居中的几种方案 Ⅰ: 水平居中(margin: 0 auto) .horizontal-center { background-color: #809399; height: 100px; width: 200px; line-height: 100px; text-align: center; /* 水平居中 */ margin: 0 auto; } 阅读详情

相关推荐

【高频考点精讲】实现垂直居中的多种CSS方法比较与最佳实践

今天咱们聊聊前端开发中最让人头疼的问题之一——垂直居中。不知道你们有没有遇到过这种情况:明明设置了水平居中了,垂直方向怎么折腾都不对劲。全栈老李当年刚入行时,为了一个div居中能折腾一晚上,现在想想真是血泪史啊。

全栈老李的博客 816

css几种垂直居中的方式(面试常考题1)

水平垂直居中(一)定位和需要定位的元素的margin减去宽高的一半 &lt;style&gt; *{ padding: 0; margin: 0; } .box{ width: 300px; height: 300px; ba...

Judy1623 1281

CSS常见的四种垂直居中的方法

面试中不管是笔试题还是面试题,一般很容易被问到如何实现垂直水平居中,这里总结四种方法作为参考 (1)margin:auto法 css: div{ width: 400px; height: 400px; position: relative; border: 1px solid #465468; } img{ position: absolute; margin: auto; top: 0; left: 0; right: 0; bottom: 0; } html: <div> <i

孙叫兽的博客 6019

html,css常见的几种垂直居中方式

一丶什么是垂直居中   指当前标签在父级容器中垂直方向是居中显示的   实现垂直居中的几种方式:     1.table-cell+vertical-align 属性配合使用     2.absolute+transform 属性配合使用     3.display+align-items 属性配个使用     4.position+margin 属性配合使用 第一种:代码: vertical-align:属性时用来设置文本内容垂直方向的对齐方式     这里常用属性值:top  顶.

weixin_56247512的博客 4031

Lossless-Claw 插件实战:告别上下文截断,让 OpenClaw 永不遗忘

本文介绍了OpenClaw的Lossless-Claw插件,这是一个基于LCM论文的DAG摘要系统,能有效解决上下文截断问题。文章包含10个部分,从插件原理到实操指南: 核心原理:通过DAG结构持久化存储所有原始消息,将旧消息分块摘要并形成层级结构,保持上下文在模型Token限制内。 性能优势:在OOLONG基准测试中得分74.8,优于Claude Code的70.3,尤其在长上下文场景表现更佳。 功能特点: 提供lcm_grep、lcm_describe等工具主动回忆历史 摘要始终链接回原文,实现真正无损

u014798858的博客 2617

系留无人机消防车组合:高层建筑火灾的空中灭火系统解析

高层建筑火灾扑救长期面临云梯高度不足、直升机依赖条件苛刻、内攻水带难以抵达的尴尬区间。系留无人机与消防车组成的飞行灭火系统,通过地面持续供电供水、空中精准抵近喷射,重新定义了高层灭火的技术路径。其核心在于将消防车改造为集泵站、电站、指挥站于一体的地面保障平台,配合系留多旋翼、机载水炮、高压管缆及多传感器融合的瞄准闭环,实现百米级高度上的长时间悬停与灭火剂投送。系统设计需重点解决垂直供水压损、水电一体管缆拉力控制、水炮喷射反作用力补偿等工程难题。从概念演示走向实战验证的过程中,多机协同、AI辅助决策正成为下一

weixin_33912246的博客 430

点云特征提取的实战对比:Voxel-based 与 Pillar-based 在自动驾驶中的表现

本文深入对比了自动驾驶中点云特征提取的两种主流方法:Voxel-based与Pillar-based。Voxel-based方法通过三维体素化保留完整几何信息,检测精度高,但计算消耗大;Pillar-based方法将点云压缩为二维柱状特征图,牺牲部分垂直细节以换取极高的处理速度,更适合实时性要求高的量产场景。文章结合实战数据,为不同算力、精度与工程需求的场景提供了清晰的选型指南。

weixin_29045001的博客 222

CSS中 常见的几种垂直居中的方式 (目前四种,未完待续。。。)

1,flex 布局的方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>水平-垂直居中</title> <style> .con { width: 400px; height: 400px; background-color: yellow; margin: 100px auto; /* f

weixin_44684357的博客 278

基于Halium 9为小米平板4移植Ubuntu Touch:内核适配与驱动调试实战

在移动设备生态中,Linux内核作为核心,通过硬件抽象层(HAL)管理底层硬件驱动,为上层系统提供统一接口。其模块化设计允许深度定制,是实现跨平台移植的技术基础。Halium项目正是利用这一原理,构建了一个中间层,将安卓内核的硬件支持能力“翻译”成标准Linux发行版能识别的格式,从而在手机、平板等移动设备上运行Ubuntu Touch、postmarketOS等移动Linux系统,极大地拓展了老旧设备的再利用价值。这一技术为开发者提供了在常见安卓硬件上进行Linux系统开发与定制的可能,尤其在嵌入式与物联

weixin_42639246的博客 371

【SPSS实战】手动实现Quade非参数协方差分析:从数据准备到结果解读

本文详细介绍了在SPSS中手动实现Quade非参数协方差分析的实战方法。当数据不满足正态分布等参数检验假设时,通过将协变量和因变量转换为秩,并利用秩回归残差进行Kruskal-Wallis组间比较,提供了一种清晰、可操作的近似解决方案。该方法有效解决了非正态数据的协变量调整问题,适用于实验研究和调查数据分析。

bean的博客 468

面试常问:7种CSS实现元素水平垂直居中的方法

在 CSS 中对元素进行水平居中是非常简单的:如果它是一个行内元素,就对它的父容器应用text-align:center;如果它是一个块级元素,就对它自身应用margin:auto。

m0_61215908的博客 1382

ROS机器人未知环境自主探索功能包explore_lite最全源码详细解析(五)

本系列文章主要针对ROS机器人常使用的未知环境自主探索功能包explore_lite展开全源码的详细解析,并进行概括总结。 本系列文章共包含六篇文章,前五篇文章主要介绍explore_lite功能包中 explore.cpp、costmap_tools.h、frontier_search.cpp、costmap_client.cpp等源码实现文件中全部函数的详细介绍,第六篇文章进行概括总结,包含自主探索功能包explore_lite的简介,实现未知环境自主探索功能的原理及流程总结,效果演示,使用功能包exp

慕羽★的博客 1510

CSS 垂直居中的七种方法——史上最详细总结

我们在写页面时,通常会用到水平居中或垂直居中,而水平居中很好处理,不外乎就是设定 margin: 0 auto; 或是 text-align: center; “垂直居中”却比较麻烦,下面介绍七种纯 CSS 实现垂直居中的方式。 一、设定行高(line-height) 设定行高是垂直居中最简单的方式,适用于“单行”的“行内元素”(inline、inline-block),例如单行的标题,或是已经设为 inline-block 属性的 div,若将 line-height 设成和高度一样的数值,则内容的行内元

qq_37384271的博客 3万+

Cesium 实现动态加载气象数据:渲染成动态雨雪/云层效果(源码 + 原理)

本文介绍了在Cesium中实现动态天气效果的技术方案。核心思路是通过时间轴驱动多层数据动态切换,包括:1)时间维度处理,按序列加载不同气象图层;2)空间维度处理,将气象数据切分为瓦片;3)效果层实现,制作风场、降水等动态可视化效果。文章详细讲解了后端数据预处理(生成瓦片和风场JSON)、前端实现(双层雷达瓦片交替渐变、风场粒子系统)的具体方法,并提供了完整的代码示例。此外还探讨了雨雪粒子、云层效果等增强功能,以及工程化建议,为构建科学准确的动态天气可视化系统提供了实用解决方案。

专注AI大模型,软件混淆,授权 623

【SemiDrive源码分析】【X9芯片启动流程】11 - freertos_safetyos目录Cortex-R5 DIL2.bin 引导程序源代码分析

【SemiDrive源码分析】【X9芯片启动流程】11 - freertos_safetyos目录Cortex-R5 DIL2.bin 引导程序源代码分析

|~~~热爱生活、努力学习的小伙汁~~~| 2108

当面试官问起CSS布局之水平垂直居中时~~

最近面试的时候,面试官问到了CSS水平垂直居中布局的方式,对于刚入前端没多久的我,无疑是一脸懵逼,闲了赶紧查阅资料分析一波,给各位分享一下,避免落坑。 首先先说明一下html和一些基础css样式,下面就不再赘述! html <body> <div class="div1"> <div class="box size">垂直水平居中</div&g...

angula 9289

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

CSS中实现元素水平垂直居中的几种方法总结

今天我们聊一个CSS布局中一个老生常谈的问题就是元素的水平垂直居中。不管在面试还是平时写网页布局,都是经常需要使用的。 之前我也面试过别人或者被别人面试,这个问题的出现频率也是非常高的,面试官一般都会问:元素的水平垂直居中你能有几种实现方法?面试官想要你尽可能的多说实现几种方法,如果你只说一两种方法,可能就不能赢得面试官的倾心。 在实际写代码过程中,遇到各种场景的水平垂直居中,可能还要去网上百度,不能做到信手拈来,作为一个合格的“前端攻城狮”确实有点不应该。那么,这篇文章就教你在遇到这类问题时怎么征服面

jnfy的博客 1695

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【水平垂直居中布局】CSS实现水平垂直居中的5种方法(附源码)

CSS水平垂直居中的五种方法,手把手教你如何实现水平垂直居中布局,水平居中,垂直居中,子容器如何水平垂直居中,flex布局实现垂直水平居中,相对绝对定位实现水平垂直居中

赏樱看雪撸代码 1554

深度学习工具包 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")

CSS水平居中+垂直居中+水平/垂直居中的方法总结

  目录 水平居中  行内元素  块级元素 方案一:(分宽度定不定两种情况) 方案二:使用定位属性 方案三:使用flexbox布局实现(宽度定不定都可以) 垂直居中 单行的行内元素 多行的行内元素  块级元素 水平垂直居中 已知高度和宽度的元素 未知高度和宽度的元素 方案一:使用定位属性 方案二:使用flex布局实现 水平居中  行内元素 首先看它的父元...

半夏半暖半倾城的博客 20万+

CSS之水平垂直居中九种方法(有你意想不到的方法哦!)

大家好,很荣幸为大家介绍一下水平垂直居中的方法,,跟着我的步伐,跑步前进吧!</margin;padding;height;margin;;height;;

m0_74577714的博客 1378

css 实现元素水平垂直居中的几种方法

在做网页的时候经常会需要让某一个元素水平垂直居中,而要实现水平垂直居中有很多种方法,不知道你们有没遇到这种情况:方法倒是知道,甚至还知道许多种,可一到真正用到的时候,却突然在努力地回想,这或许就是人们经常说的「脑子短路」,所以,这篇文章和大家一起来温故而知新,梳理一下常见的几种元素水平垂直居中的方法,以后突然短路了,就翻出来看一看,而且万一以后面试就问到了,那就更好不过了。 div 元素的水平垂直...

LightLinV的博客 2061

css 垂直居中_css方法实现垂直居中的常用方法(布局常用)

前一段时间有个同学去面试,被面试官提问到了实现垂直居中的方法,同学当时问蒙了,就说出2个,今天就利用空暇时间总结下第一种方法对单行元素进行垂直居中1、可设置该行内元素的父元素的height与line-heigth的值相等,让行内元素垂直居中案例如下:效果如下:这个是最简单的一种方式,就是设置行高,让文字在块元素的高度相等,然后保持垂直居中状态2、针对行内元素,可通过设置vertical-alig...

weixin_39877166的博客 132

css块级元素垂直居中五种方法

目录前言正文第一种第二种第三种第四种第五种总结 前言 昨天收到京东的面试邀请,面试的是个小姐姐,第一道就是css问题,父元素高度不固定的情况下,如何让内部块元素垂直居中,当时只想到一种方法,因此记录下其他几种方法,以供后续翻看加深记忆 正文 第一种 在父元素添加display:flex;align-items:center 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl

qq_37985836的博客 4519

css水平垂直居中常用方法

css水平垂直居中在当前网页制作中可以说是非常重要了,大公司面试的时候也经常会问到这种问题,还要你给出多种方法,说出哪一种办法最好并且兼容性还要好,我给大家总结了几种水平垂直居中的方法。 第一种: 元素的水平居中 &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;&..

家有佳欣宝贝的博客 442

分享 11 种垂直居中的方法,你会用哪些?

前言之爆锤面试官神器 - CSS无论是实际开发中,亦或者是求职面试中,css 垂直居中往往都是一个绕不开的话题,其中不乏有许多面试者在多次双重尝受打击之后,而没有一个很好的反击点,刚好结合...

前端达人 5786
上一篇: 排序算法大集合:插入类(直接插入、折半插入、希尔)、交换类(冒泡、快排)、选择类。反正你想要的我都要。
下一篇: 浅谈最小(代价)生成树(普利姆算法和克鲁斯卡尔算法)
Two_Bits
博客等级 码龄8年 25粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值