我有一个希望是一个简单的问题,我只是在脑海里过于复杂。
Here is a Fiddle to demonstrate my issue.
在上面的小提琴中,您将看到弹出菜单设置。在顶部选项中,您将看到放大的第一个选项,它具有损坏的浮动。这是我的问题所在。 我需要这些元素并排放置,而不会破坏持有它们的列表容器。
我知道white-space:nowrap
正在导致容器空间的中断,但是,每当我尝试删除该元素时,我似乎无法让我的浮动得到尊重。我确信这是一个简单的错过,因为他会考虑围绕它的事情,但是对此的任何进步都是值得赞赏的,并且希望对某人来说很容易。
CSS:
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */
#nav{margin:0;padding:0;position:relative;float:left;}
#nav li {list-style:none;}
#nav a {
color:#fff;
display:block;
line-height:2.4;
padding:0 1em;
text-decoration:none;
border-bottom:1px solid #eee;
}
/* FLYOUT */
.sub {
top:0;
margin:0;
padding:0;
left:100%;
display:none;
min-height:100%;
position:absolute;
white-space:nowrap;
border-left:2px solid #eee;
}
#nav li:hover .sub {
display:block;
}
#nav li:hover .sub .sub {
display:none;
}
#nav .sub li:hover .sub{
display:block;
}
.sub a:hover {
color:#fff;
background:#333;
}
.sub img { min-width:120px; float:left; }
.sub .content { float:left; }
/*-- COLORS NOT NEEDED ON LIVE--*/
.first{background:#aaa;}
.second {background:#bbb;}
.third {background:#ccc;}
.fourth {background:#ddd;}
.fifth {background:#aaa;}
.sixth {background:#bbb;}
.seventh {background:#ccc;}
.eighth {background:#ddd;}
.nineth {background:#aaa;}
.tenth {background:#bbb;}
.all {background:#000}
<div id="nav">
<li><a class="first" href="#">Level 1 Option</a>
<ul class="sub first">
<li class="clearfix"><a href="#">
<div class="float">
<img src="https://placeholdit.imgix.net/~text?txtsize=20&txt=120%C3%9780&w=120&h=80" alt=""></div>
<div class="">
<h5>This will be a title</h5>
<p>
<strong>Sub-title</strong><br>
Specs<br>
<strong>Sub-title</strong><br>
Specs
</p>
</div>
</a>
<ul class="sub first">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 1</a></li>
</ul>
</li>
<li><a href="#">Option 2</a>
<ul class="sub first">
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="second" href="#">Level 1 Option</a>
<ul class="sub second">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="third" href="#">Level 1 Option</a>
<ul class="sub third">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="fourth" href="#">Level 1 Option</a>
<ul class="sub fourth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="fifth" href="#">Level 1 Option</a>
<ul class="sub fifth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="sixth" href="#">Level 1 Option</a>
<ul class="sub sixth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="seventh" href="#">Level 1 Option</a>
<ul class="sub seventh">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="eighth" href="#">Light Duty</a>
<ul class="sub eighth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="ninth" href="#">Level 1 Option</a>
<ul class="sub ninth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="tenth" href="#">Level 1 Option</a>
<ul class="sub tenth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="all" href="#">View All</a>
</li>
</div>
答案 0 :(得分:3)
从float div中删除浮动左侧的浮动。并在那边增加一些余量。
CSS:
.sub img {
min-width: 120px;
/* float: left; */ //Remove this line
margin-top: 10px;
}
答案 1 :(得分:2)
请参阅此fiddle。
实现所需布局的一种方法是为图像容器和标题/子标题的容器设置inline-block
。
vertical-align: top;
是必要的,以确保两个容器在其顶部边缘对齐。
为了简单起见,我保留了现有的float
课程。 (请注意,此类需要添加到标题/子标题/规范的容器中。)
.float {
display: inline-block;
vertical-align: top;
}
答案 2 :(得分:1)
请检查此代码,我认为您正在寻找此代码,您不需要将宽度设置为.sub
我已添加新分区并添加一些属性
.float , .inline-block {
display:inline-block;
vertical-align:top;
}
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix {
display: inline-block;
}
/* start commented backslash hack \*/
* html .clearfix {
height: 1%;
}
.clearfix {
display: block;
}
/* close commented backslash hack */
#nav {
margin: 0;
padding: 0;
position: relative;
float: left;
}
#nav li {
list-style: none;
}
#nav a {
color: #fff;
display: block;
line-height: 2.4;
padding: 0 1em;
text-decoration: none;
border-bottom: 1px solid #eee;
}
/* FLYOUT */
.sub {
top: 0;
margin: 0;
padding: 0;
left: 100%;
display: none;
min-height: 100%;
position: absolute;
white-space: nowrap;
border-left: 2px solid #eee;
}
#nav li:hover .sub {
display: block;
}
#nav li:hover .sub .sub {
display: none;
}
#nav .sub li:hover .sub {
display: block;
}
.sub a:hover {
color: #fff;
background: #333;
}
.sub img {
min-width: 120px;
float: left;
}
.sub .content {
float: left;
}
/*-- COLORS NOT NEEDED ON LIVE--*/
.first {
background: #aaa;
}
.second {
background: #bbb;
}
.third {
background: #ccc;
}
.fourth {
background: #ddd;
}
.fifth {
background: #aaa;
}
.sixth {
background: #bbb;
}
.seventh {
background: #ccc;
}
.eighth {
background: #ddd;
}
.nineth {
background: #aaa;
}
.tenth {
background: #bbb;
}
.all {
background: #000
}
.float , .inline-block {
display:inline-block;
vertical-align:top;
}
&#13;
<div id="nav">
<li><a class="first" href="#">Level 1 Option</a>
<ul class="sub first">
<li class="clearfix"><a href="#">
<div class="float"> <img src="https://placeholdit.imgix.net/~text?txtsize=20&txt=120%C3%9780&w=120&h=80" alt=""></div>
<div class="inline-block"><h5>This will be a title</h5></div>
<div class="">
<p> <strong>Sub-title</strong><br>
Specs<br>
<strong>Sub-title</strong><br>
Specs </p>
</div>
</a>
<ul class="sub first">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 1</a></li>
</ul>
</li>
<li><a href="#">Option 2</a>
<ul class="sub first">
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="second" href="#">Level 1 Option</a>
<ul class="sub second">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="third" href="#">Level 1 Option</a>
<ul class="sub third">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="fourth" href="#">Level 1 Option</a>
<ul class="sub fourth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="fifth" href="#">Level 1 Option</a>
<ul class="sub fifth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="sixth" href="#">Level 1 Option</a>
<ul class="sub sixth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="seventh" href="#">Level 1 Option</a>
<ul class="sub seventh">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="eighth" href="#">Light Duty</a>
<ul class="sub eighth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="ninth" href="#">Level 1 Option</a>
<ul class="sub ninth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="tenth" href="#">Level 1 Option</a>
<ul class="sub tenth">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
</ul>
</li>
<li><a class="all" href="#">View All</a> </li>
</div>
&#13;