gpt4 book ai didi

html - 垂直对齐 float 元素

转载 作者:太空宇宙 更新时间:2023-11-04 03:50:11 25 4
gpt4 key购买 nike

我有一个导航,其中有两个 div,一个用于 Logo ,另一个用于菜单。 Logo div 向左浮动。因此,它的父元素的高度现在与 Logo div 相同。但是, float 菜单 div 位于顶部。我想把它放在中间。我怎样才能做同样的事情?请帮助我...

我的代码如下...

HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="StyleSheet.css" rel="stylesheet" />
</head>
<body>
<div class="navigation clearfix">
<div class="logo">
<img src="logo.png" />
</div>
<div class="navigation-menu">
<a>HOME</a>
<a>HOME</a>
<a>HOME</a>
<a>HOME</a>
<a>HOME</a>
</div>
</div>
<script src="JavaScript.js"></script>
</body>
</html>

CSS

html {
height: 100%;
}

body {
height: 100%;
margin: 0;
}

p {
margin: 0;
}

img {
display: block;
}

.navigation {
background-color: yellow;
}

.logo {
float:left;
}

.clearfix:after {
content: "";
display: table;
clear: both;
}

.navigation-menu {
float:right;
background-color:red;
}

还有,这是 fiddle ......

http://jsfiddle.net/ZghVk/

最佳答案

无需定义固定高度,您可以更改布局以使用 display:table 来简化垂直对齐。

尝试将您的 CSS 更改为:

Demo Fiddle

html {
height: 100%;
}
body {
height: 100%;
margin: 0;
}
p {
margin: 0;
}
img {
display: block;
}
.navigation {
background-color: yellow;
display:table;
width:100%;
}
.logo {
display:table-cell;
}
.navigation-menu {
text-align:left;
display:table-cell;
text-align:right;
vertical-align:middle;
}
.navigation-menu a{
background-color:red;
float:right;
padding:0 5px;
}

关于html - 垂直对齐 float 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23513843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com