【问题标题】:aligning right but not breaking the vertical align右对齐但不破坏垂直对齐
【发布时间】:2015-08-24 22:20:15
【问题描述】:

我仍然有很多麻烦,现在甚至很难描述这个问题。黄色文本区域应位于粉色 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>

【问题讨论】:

    标签: html css alignment


    【解决方案1】:

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

    .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. */
    }
    

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

    这就是你的 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.

    【讨论】:

    • 我明白了 - 边距:0 是错误之一,我删除了它,但问题仍然存在,文本 xxxxxxxxxxxx 就在下方 - 它应该垂直居中在中间div 并浮动在右侧。我怎样才能做到这一点?带有 home-featured-class-3 的 div 上方的换行符在哪里?
    • @user3760941 查看更新后的问题。换行符生成需要删除的空白字符。
    猜你喜欢
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多