我正在尝试垂直对齐导航栏上的图标,我尝试使用此方法:
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
这是我的代码:
HTML
<div class="navbar">
<nav>
<ul>
<% if logged_in? %>
<li><%= link_to "IOAKA", dashboard_path %></li>
<li><%= link_to(image_tag("icon_ioaka.png", alt: "geometry IOAKA icon",
:class => "icon", :id => "ioaka_icon2"), dashboard_path) %></li>
<li><%= link_to(image_tag("icon_settings.png", alt: "geometry settings icon",
:class => "icon", :id => "settings_icon"), edit_user_path(current_user)) %></li>
<li><%= link_to(image_tag("icon_logout.png", alt: "geometry logout icon",
:class => "icon", :id => "logout_icon"), logout_path, method: "delete") %></li>
<% else %>
<li><%= link_to "IOAKA", root_path %></li>
<li><%= link_to(image_tag("icon_login.png", alt: "geometry login icon",
:class => "icon", :id => "login_icon"), login_path) %></li>
<li><%= link_to(image_tag("icon_signup.png", alt: "geometry signup icon",
:class => "icon", :id => "signup_icon"), signup_path) %></li>
<li><%= link_to(image_tag("icon_ioaka.png", alt: "geometry IOAKA icon",
:class => "icon", :id => "ioaka_icon1"), root_path) %></li>
<% end %>
</ul>
</nav>
</div>
CSS
/*--------------------------------HEADER--------------------------------------*/
ul {
list-style-type: none; /* Removes the bullets. A navigation bar does not need list markers */
margin: 0; /* to remove browser default settings */
padding: 0; /* to remove browser default settings */
text-align: left; /* solves the behavior of center because of body tag text-align center */
}
li {
display: inline; /* By default, <li> elements are block elements. Here, we remove the line breaks before and after each list item, to display them on one line */
}
a:link {
text-decoration: none; /* unvisited link remove default undline */
}
a:active {
color: black; /* selected link remove default red color */
}
.icon {
float: right; /* use float to get block elements to slide next to each other */
}
#ioaka_icon1 {
height: 50px;
}
#signup_icon {
height: 44px;
}
#login_icon {
height: 50px;
}
.navbar {
max-width: 100%;
height: 65px;
line-height: 65px; /* Aligns text vertically to the div the value has to be the same as the div! */
background-color: white;
border-bottom: #cfcfcf 3px solid;
}
如果我在这种情况下使用:
.nav ul {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
这些元素似乎只有50%
到顶部(transform: translateY(-50%);
)和top: 50%
;没有做任何改变?
问题:我错过了什么,为什么它不起作用?提前谢谢!
答案 0 :(得分:2)
以下两种方法可以垂直对齐导航部分中的导航项目。
HTML (两个示例都相同)
<nav>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</nav>
方法1:Flexbox
CSS
nav {
display: flex; /* establish flex container */
align-items: center; /* center ul container vertically */
justify-content: center; /* center ul container horizontally (optional) */
}
方法2:绝对定位
nav {
position: relative; /* establish containing block (nearest positioned ancestor) for
absolute positioning */
}
ul {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
如果要垂直和水平居中导航项目,请进行此调整:
ul {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center; /* center nav items inside nav container */
width: 75%; /* to prevent overflow of nav items for demo */
}
对于涉及表属性的第三种方法,请参阅我的答案:https://stackoverflow.com/a/31977476/3597276
要了解有关flexbox的更多信息,请访问:
浏览器支持: 所有主流浏览器except IE < 10都支持Flexbox。最近的一些浏览器版本,例如Safari 8和IE10,需要vendor prefixes。要快速添加前缀,请使用Autoprefixer。 this answer。
中的更多详细信息答案 1 :(得分:1)
使用top: 50%;
是不正确的,因为即使它选择了一半的垂直但它忽略了.element
本身的高度,所以我们可以通过以下方式解决这个问题:
只有在知道高度值时手动计算 - :
#element-container {
width: 100%;
height: 150px;
background-color: orange;
text-align: center;
}
.element {
width: 50px;
height: 50px;
background-color: green;
display: inline-block;
margin-right: 20px;
/* here we set margin-top manually, so 15px container height - (50px margin-top +
50px element height) leaves us with bottom space of 50px, vertically aligned!
*/
margin-top: 50px;
}
<div id="element-container">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
利用CSS函数.calc()
(*),此方法类似于您的方法,在您有不同高度或未确定高度的情况下它非常有用 - 同样适用于宽度 - 像这样使用
#element-container {
width: 100%;
height: 150px;
background-color: orange;
text-align: center;
}
.element {
width: 50px;
height: 50px;
background-color: green;
display: inline-block;
margin-right: 20px;
position: relative;
/* 50% of vertical distance - 25px (half of the .element height) will vertically align it */
top: calc(50% - 25px);
}
<div id="element-container">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
(*)资源: