热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在DIV内垂直居中UL-CenteringVerticallyanULinsideaDIV

iamtryingtomakeanavigationmenuinsidea200pxx200pxsquare,thisnavigationlist(UL)chang

i am trying to make a navigation menu inside a 200px x 200px square, this navigation list (UL) changes from square (200px) to square in 2 rows, like a table, it has some transitions and a lot more stuff going on, but i don't think that affects the vertically centering i want to accomplish. (i've seen the other answes for this question, but they don't fit this specific scenario, line-height doesn't work, etc.) these multiple menus are the ones i want to be aligned depending on every element i add, if i just add 1 element then it looks like the center square, of course there is a limit of elements too that can fit on the square, i have a maximum already ( 5 elements ) according to my actual HTML - CSS

我试图在200px x 200px的正方形内制作导航菜单,这个导航列表(UL)从正方形(200px)变为2行的正方形,就像一张桌子,它有一些过渡和更多的东西,但是我认为这不会影响我想要完成的垂直居中。 (我已经看到了这个问题的其他答案,但它们不适合这个特定情况,行高不起作用等)这些多个菜单是我想要根据我添加的每个元素对齐的,如果我只添加1个元素,那么它看起来像中心方块,当然也有一个元素的限制,可以放在广场上,根据我的实际HTML,我已经有一个最大值(5个元素) - CSS

This is what i want to accomplish

这就是我想要完成的

the html markup is something like this

html标记是这样的

I know i am not giving too much info, but i can't really put the code as it is, it's work for a private company, but i hope you understand my scenario, the menu on top of the img it has an RGB alpha transition that makes it appear on top of on :hover, but again, i think the important thing is to align every button vertically like that, withouth recurring to special "fixes" for every different section using "position: relative; top: 30px;" i could do that, but i want to understand and see if i can do it without too much trouble, and adding the elements i want to and get automatically aligned in perfect center.

我知道我没有提供太多的信息,但我不能真正把代码放在原处,它适用于私人公司,但我希望你了解我的情况,img上面的菜单有一个RGB alpha过渡使它出现在on:hover上,但同样,我认为最重要的是将每个按钮垂直对齐,而不是使用“position:relative; top:30px;对于每个不同的部分重复特殊的”修复“ “我可以做到这一点,但我想了解并看看我是否可以毫不费力地做到这一点,并添加我想要的元素并在完美的中心自动对齐。

Thank you so much for your help. (english is not my first language so i hope it is understandable)

非常感谢你的帮助。 (英语不是我的第一语言,所以我希望它是可以理解的)

2 个解决方案

#1


6  

The trick for centering something vertically includes two simple steps:

垂直居中的技巧包括两个简单的步骤:

  1. Move the content to its top half of its height using transform: translateY(-50%).

    使用transform:translateY(-50%)将内容移动到其高度的上半部分。

    enter image description here

  2. Apply top: 50%. The element must be positioned relatively for this to work.

    适用顶部:50%。必须相对定位元素才能使其工作。

    enter image description here

There are ofcourse many ways to do this.

有很多方法可以做到这一点。

body {
  background: #333333;
}
.main-container {
  width: 640px;
  margin: 0 auto;
}
.container {
  display: inline-block;
  vertical-align: top;
  width: 200px;
  height: 250px;
  background: #0077A3;
  margin: 5px;
}
ul {
  position: relative;
  padding: 0;
  list-style: none;
  transform: translateY(-50%);
  top: 50%;
}
li {
  text-align: center;
  height: 25px;
  margin: 10px;
}
span {
  background-color: #00C430;
  padding: 5px 10px 5px 10px;
  color: white;
  text-transform: uppercase;
  font-weight: bold;
}
  • Element 1
  • Element 2
  • Element 3
  • Element 1
  • Element 1
  • Element 2

#2


6  

You can use the Centering in the Unknown approach.

您可以使用未知方法中心。

Use HTML like

使用HTML之类的

  • Element 1
  • Element 2
  • Element 3

To center ul vertically inside .container, use

要在容器内垂直居中,请使用

.container:before {
  content: '';
  height: 100%;
}
.container:before, ul {
  display: inline-block;
  vertical-align: middle;
}

body {
  background: #333333;
}
.main-container {
  width: 610px;
  margin: 0 auto;
}
.container {
  display: inline-block;
  text-align: center;
  vertical-align: top;
  width: 200px;
  height: 250px;
  background: #0077A3;
}
.container:before {
  content: '';
  height: 100%;
}
.container:before, ul {
  display: inline-block;
  vertical-align: middle;
}
ul {
  padding: 0;
  list-style: none;
}
li {
  margin: 10px;
  padding: 5px 10px;
  background-color: #00C430;
  color: white;
  text-transform: uppercase;
  font-weight: bold;
}
  • Element 1
  • Element 2
  • Element 3
  • Element 1
  • Element 1
  • Element 2


推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • JavaScript和HTML之间的交互是经由过程事宜完成的。事宜:文档或浏览器窗口中发作的一些特定的交互霎时。能够运用侦听器(或处置惩罚递次来预订事宜),以便事宜发作时实行相应的 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • Java图形化计算器设计与实现
    本文介绍了使用Java编程语言设计和实现图形化计算器的方法。通过使用swing包和awt包中的组件,作者创建了一个具有按钮监听器和自定义界面尺寸和布局的计算器。文章还分享了在图形化界面设计中的一些心得体会。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
author-avatar
拍友2502935047
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有