skrln
skrln

Reputation: 542

Positioning of HTML elements with css

HTML Code

<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>

Issue

I have this HTML structure which I can not edit/rearrange. I would like to position the h1, div.meta and div.textblock is as in the picture below.

I can't work it out with floats the way I want to because of the sequence of the HTML.

Illustration

Trying to achieve this result

Upvotes: 0

Views: 92

Answers (3)

Mariam
Mariam

Reputation: 1

You can use this demo

Code Pen Demo

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>");

Upvotes: 0

Saif Lacrimosa
Saif Lacrimosa

Reputation: 35

See this example:

Codepen Example

I think this is what you're looking for!

Upvotes: 0

CaribouCode
CaribouCode

Reputation: 14398

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)

Upvotes: 2

Related Questions