0

HTML 代码

<header>
  <h1>Event Heading</h1>
  <div class="meta">09 JUL 2014</div>
  <div class="textblock">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</div>
</header>

问题

我有这个无法编辑/重新排列的 HTML 结构。我想定位 h1、div.meta 和 div.textblock 如下图所示。

由于 HTML 的顺序,我无法按照我想要的方式使用浮动来解决它。

插图

试图达到这个结果

4

3 回答 3

2

This can be achieved with absolute positioning:

header { 
  position: relative;
  min-height: 100px; }

div.meta {
    position: absolute;
    width: 100px; height:100px;
    top:0; left:0;
    border: 1px solid red; }

header h1 {
  margin-left: 120px;
  border-bottom: 2px solid red; }

header div.textblock { margin-left: 120px; }

See fiddle: http://jsfiddle.net/utsKx/

You can change the div.meta widths and h1/textblock margin-left to percentages if you want a responsive layout.

EDIT Added min-height to header to ensure div.meta never falls outside the parent header block. (Thanks for MarcAudet for pointing this out)

于 2013-09-03T14:20:41.017 回答
0

你可以使用这个演示

代码笔演示

HTML

 <header>
  <div class="meta L">09 JUL 2014</div>
  <h1 class="R">Event Heading</h1>
  <div class="textblock R">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</div>
</header>

CSS

header
{
  width:650px;
  display:inline-block;
}
.meta
{
  width:150px;
  height:150px;
  border:1px solid red;
  margin:5px;
  font-size:22px;
  text-align:center;
}
h1,.textblock
{
  width:400px;
  text-align:left;
  border:1px solid red;
}
h1
{
  color:#B1003B;
  margin-top:5px;
}
.textblock
{
  margin-top:-22px;
}
.L
{
  float:left;
}
.R
{
  float:right;
}
.C1
{
  color:#000000;
  font-weight:bold;
  font-size:36px;
}
.C2,.C3
{
  color:#777777;
}

jQuery

var str = $(".meta").html();
s = str.split(' ');
$(".meta").html("<span class='C1'>"+s[0]+"</span></br><span class='C2'>"+s[1]+"</span></br><span class='C3'>"+s[2]+"</span>");
于 2013-09-03T15:04:12.263 回答
0

看这个例子:

代码笔示例

我想这就是你要找的!

于 2013-09-03T14:54:58.920 回答