对齐但没有打破垂直对齐

时间:2015-06-09 21:33:23

标签: html css alignment

我仍然遇到很多麻烦,现在甚至很难描述问题。黄色文本区域应位于粉红色div的边缘(右侧边缘),此黄色区域div与左侧另一区域(包含绿色和红色区域)之间应有30px的边距。

如果我添加'float:right;',问题就解决了。在'home-featured-class-3'中,但这会产生一个将黄色文本区域div移到顶部的新问题。

如何让黄色文本div向右浮动但在中心垂直浮动? (不调整任何东西的高度/宽度)

HTML& CSS:

.home-featured-class-3{
	width: 630px;
	height:auto;
	margin:0 0 0 30px;
	display: inline-block;
    vertical-align: middle;
	background:#CF0;
}
.home-featured-class-4{
	width:298px;
	height:auto;
	margin:0 0px 0 0;
	border: 1px solid #211c20;
	display: inline-block;
    vertical-align: middle;
}
.home-featured-class-A{
	width:960px;
	height:auto;
	background:#C3C;
	vertical-align:middle;
}
.clear {
  clear: both;
}
h6{
	font-family:arial,verdana,helvetica,sans-serif;
	font-size:18px;
	color:#201c1f;
	text-decoration:none;
	text-transform:uppercase;
	font-weight:100;
	margin:0;
	padding:10px;
	background:#DB1D1D;
	text-align:center;
	border-top:1px solid #211c20;
}
h6 a:link{
	color:#201c1f;
	text-decoration:none;
}
h6 a:visited{
	color:#201c1f;
	text-decoration:none;
}
h6 a:hover{
	color:#201c1f;
	text-decoration:underline;
}

#home-featured-classes-wrap{
	width:auto;
	height:auto;
	margin: 30px 0 0 0;
}
.home-featured-class:hover .products {
    text-decoration: underline;
}
.home-featured-class-end{
	width:298px;
	height:auto;
	margin:0 0 0 0;
	border: 1px solid #211c20;
	float:left;
}
.home-featured-class-end:hover .products {
    text-decoration: underline;
}
<div id="home-featured-classes-wrap">

<div class="home-featured-class-A">
<div class="home-featured-class-4"><a href="products.html"><img name="RP-TEXT" src="http://upload.wikimedia.org/wikipedia/commons/d/de/Color-Green.JPG" width="298" height="148" alt="RP-TEXT" style="background-color: #3366FF" /></a>
<h6 class="products"><a href="products.html" title="RP-TEXT">RP-TEXT</a></h6>

</div>

<div class="home-featured-class-3">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</div>

<div class="clear"></div>
</div>



</div>

1 个答案:

答案 0 :(得分:2)

这是你的问题(在CSS评论中描述):

.home-featured-class-3{
    width: 630px;
    height:auto;
    margin:0 0 0 30px;      /* Here you set the margin to 30px on the left. */
    display: inline-block;
    vertical-align: middle;
    background:#CF0;
    margin: 0;              /* Here you set it back to 0 again. Simply remove this line. */
}

此外,您还必须删除<div class="home-featured-class-3">上方的换行符(或将其注释掉)。 HTML引擎创建了一个额外的空白字符,因为这个换行符只剩下大约27px的空间而不是30px。您必须删除这些换行符才能设置30px的距离。

这就是您的HTML需要:

<div id="home-featured-classes-wrap">
  <div class="home-featured-class-A">
    <div class="home-featured-class-4">
      <a href="products.html"><img name="RP-TEXT" src="http://upload.wikimedia.org/wikipedia/commons/d/de/Color-Green.JPG" width="298" height="148" alt="RP-TEXT" style="background-color: #3366FF" /></a>
      <h6 class="products"><a href="products.html" title="RP-TEXT">RP-TEXT</a></h6>
    </div><!--
    --><div class="home-featured-class-3">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</div>
    <div class="clear"></div>
  </div>
</div>

JSFiddle