x元素直接到一个元素

时间:2014-11-28 16:38:16

标签: html css html5 css3

此刻让我发疯的事情。我想将四个较小的元素与一个大元素对齐。

+-----------------+-------------------------+   +     + 
|                 |          title          |   |     | 30px
|                 |-------------------------+   | 1   +
|                 |                         |   | 2   |
|      thumb      |          text           |   | 0   | 60px
|                 |                         |   | p   |
|                 |-------------------------+   | x   +
|                 | views |  flag  (space)  |   |     | 30px
+-----------------+-------------------------+   +     +

我在所有可以想象的组合中尝试float:leftfloat:right以及clear:rightspan以及div{display:inline}。它只是不想像我期望的那样表现。

我知道这可以通过display:table来实现,但我很好奇为什么float:leftclear:right的组合在我的情况下不起作用。

这是我的代码(对不起混淆 style.css ,但这正是它经过几个小时的工作后的样子):

row.php

<div class="wide media-library">
    <div class="inner">
        <!-- (...) -->
        <?php while($row = $result->fetch_assoc()): ?>
            <div class="row" data-id="<?php echo $row['video_id']; ?>" data-resource-id="<?php echo $row['video_resource_id']; ?>" id="<?php echo $row['video_id']; ?>">
                <div class="thumb" style="background-image: url(/<?php echo $row['slideshare_thumb_url']; ?>)"></div>
                <div class="title"><?php echo $row['name_en']; ?></div>
                <div class="text"><?php echo $row['description']; ?></div>
                <div class="views"><?php echo $row['no_of_views']; ?></div>
                <div class="flag" style="background-image: url(/<?php echo $row['flag_image_path']; ?>)"></div>
            </div>
        <?php endwhile; ?>
        <!-- (...) -->
    </div>
</div>

style.css 已损坏

.wide.media-library {
    color: #000;
}

.wide.media-library .inner {
    display: block;
    font-size: 14px;
    height: auto;
    position: relative;
}

.wide.media-library .header {
    margin-bottom: 45px;
}

.wide.media-library .header h2 {
    font-size: 26px;
    font-weight: normal;
    margin-bottom: 20px;
    margin-top: 20px;
    text-align: left;
}

.wide.media-library .header p {
    text-align: left;
    font-size: 16px;
}

.wide.media-library .filter {
    padding-bottom: 10px;
    padding-top: 10px;
}

.wide.media-library .filter input {
    background-color: rgb(200, 200, 200);
    border: 0px;
    border-radius: 12px;
    font-size: 14px;
    height: 27px;
    padding: 0;
    padding-left: 11px;
    width: 290px;
}

.wide.media-library .filter select {
    font-size: 14px;
    height: 22px;
    margin-left: 60px;
    margin-top: 4px;
    padding: 0px;
    vertical-align: top;
    width: 240px;
}

.wide.media-library .row div {
    display: inline;
}

.wide.media-library .row .thumb {
    background-size: 120px 68px;
    float: left;
    height: 120px;
    width: 68px;
}

.wide.media-library .row .title {
    float: right;
}

.wide.media-library .row .text {
    float: right;
}

.wide.media-library .row .views {
    float: right;
}

.wide.media-library .row .flag {
    background-size: 16px 12px;
    float: right;
    height: 12px;
    width: 16px;
}

JSFiddle

1 个答案:

答案 0 :(得分:1)

在这里,只需删除右侧元素上的浮点数:

使用JSFiddle:http://jsfiddle.net/c94zfn6c/

或代码段:

.wide.media-library {
	color: #000;
}
.wide.media-library .inner {
	display: block;
	font-size: 14px;
	height: auto;
	position: relative;
}
.wide.media-library .row div {
	display: block;
}
.wide.media-library .row .thumb {
	background: #CCC;
	float: left;
	height: 120px;
	width: 68px;
}
.wide.media-library .row .title {
    height: 30px;
}
.wide.media-library .row .text {
    height: 70px;
}
.wide.media-library .row .views {
    height: 20px;
    display:inline;
}
.wide.media-library .row .views:after {
    content:" |";
}
.wide.media-library .row .flag {
	background: #CCC;
	height: 20px;
	width: 16px;
    display:inline;
}

.row > div {
  margin-right:2px;
}
<div class="wide media-library">
	<div class="inner">
		<!-- (...) -->
		<div class="row" data-id="1" data-resource-id="2" id="1">
			<div class="thumb"><!-- (thumb) --></div>
			<div class="title">Just a title...</div>
			<div class="text">... with just a description.</div>
			<div class="views">1337 views</div>
			<div class="flag">(flag)</div>
		</div>
		<!-- (...) -->
	</div>
</div>