5

So I have this HTML code with the following CSS.

HTML

<div class="secondpostholder">
    <div class="rightsecond">
        <h1>
            <a href="<?php the_permalink();?>" alt="<?php the_title();?>" title="<?php the_title();?>">
                <?php the_title();?>
            </a>
        </h1>
        <div class="firstmetaholder">
            <p class="secondentrymeta">
            by
                <span class="secondauthormeta">
                    <?php the_author();?>
                </span>
                • <?php the_date(); ?>
            </p>
            <p class="secondentryexcerpt">
                <?php
                $content = get_the_content();
                echo wp_trim_words( $content , '30' ); ?>
            </p>
            <div class="secondreadmoreholder">
                <a class="secondreadmorea" href="<?php the_permalink();?>" alt="<?php the_title();?>" title="<?php the_title();?>">
                    Read More
                </a>
            </div>
        </div>

    </div>

    <?php $background = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'TypeOne' ); ?>

    <div class="leftsecond" style="background-image: url('<?php echo $background[0]; ?>');">
        <a  class="leftseconda" href="<?php the_permalink();?>" alt="<?php the_title();?>" title="<?php the_title();?>">
        </a>
    </div>
</div>

CSS

secondentry {
    display:inline-block;
    width:100%;
}
.secondentryholder {
    width:100%;
    max-width:1120px;
    margin:0 auto;
    position:relative;
    height:100%;
}
.secondpostholder {
    margin-bottom:60px;
}
.rightsecond{
    width: 420px;
    right:0;
    position: absolute;
    margin-left:30px;
}
.secondpostholder {
    clear:both;
}
.leftsecond{ 
    background: #222;
    float:left;
    width:calc(100% - 450px);
    min-height:400px;
    background-repeat:no-repeat;
    display:block;
    position:relative;
    background-position:center center;
}

You may visit the website that I'm working on to have a clear picture of what's going on.

Once you opened the website, resize your browser window to 600px and check out the second post ("We found love in a hopeless place") to the sixth post ("The Undisputed Truth: What Happens If You Don’t Pay Your Credit Card Bills? ") and see what happens.

My problem is, Is there a way where in the "leftsecond" DIV would be on the top of the "rightsecond"?

I know this can easily be done by modifying the HTML structure BUT please be informed that the original CSS Style of the two divs are floating.

What I'm trying to achieve here is related to media queries, when the screen is large, the two divs are floated to each other. There's no problem with the code of this. The problem lies when the screen is small and I want to remove the FLOATS then the "leftsecond div" would be at the TOP and the "rightsecond div" at the BOTTOM.

The two divs don't have the same height. Only the "leftsecond" div has a fixed height. The rightsecond div has a fluid height which changes according to it's content.

Harry
  • 87,580
  • 25
  • 202
  • 214
Ben Daggers
  • 1,000
  • 1
  • 16
  • 51
  • [Absolute positioning like this](http://jsfiddle.net/hari_shanx/e123h638/3/)? – Harry Jul 12 '15 at 09:27
  • Hi @Harry, thanks for your help. I forgot to mention that there are several "secondpostholder" at the bottom that also contains the same #blue and #left divs. The problem is it overlaps with each other. I think there should be something to be added in the "secondpostholder" that recognizes the height of its child divs. I have updated the fiddle. Please reply as an ANSWER so I can reward your effort. – Ben Daggers Jul 12 '15 at 09:47
  • Sorry to bother but did the latest approach that I had added to my answer help? – Harry Jul 15 '15 at 17:26
  • Hi Harry, sorry for not getting back at you. I haven't tested the codes because I'm busy right now with my day job. Anyway, I plan to work on my website on the weekends so by that time I will have it tested. My apologies. – Ben Daggers Jul 16 '15 at 07:20
  • i just looked at your codes and ran the code snippet and it works. so i guess this will work too in my wordpress website.. – Ben Daggers Jul 16 '15 at 07:26
  • No worries Ben. I just wanted to check. Take your time to check on the actual website and let me know if doesn't work. If it works, don't forget to mark the answer as accepted ;) – Harry Jul 16 '15 at 08:15

2 Answers2

5

Alternate Suggestion:

This does change the HTML structure, but there doesn't seem to be any way to avoid it and this approach seems to match your expectation for both large and small screen sizes.

  • When the screen size is large, the two elements are floated next to each other with the div class='leftsecond' on the left and the div class='rightsecond' on the right side.
  • When the screen size is small, the float is removed and they get displayed one below the other as they are already block elements.

.rightsecond {
  width: 420px;
  float: right;
  margin-left: 30px;
}
.leftsecond {
  float: left;
  width: calc(100% - 450px);
  min-height: 100px;
  position: relative;
  background: url(http://lorempixel.com/500/100/nature/1);
  background-repeat: no-repeat;
  background-position: center center;
}
.secondpostholder {
  clear: both;
  border-top: 2px solid;
}
@media (max-width: 1000px) {
  .rightsecond,
  .leftsecond {
    width: 500px;
    float: none;
    position: relative;
    margin-left: 0px;
  }
  .secondpostholder {
    width: 500px;
    height: 100%;
    margin-bottom: 10px;
  }
}
<div class="secondpostholder">
  <div class="leftsecond" id="green"></div>
  <div class="rightsecond" id="blue">
    Author: User X <br/>Date: 12-July-2015 <br/>Content: Blah blah <br/>Lorem ipsum dolor sit amet consectur... <br/>Read more <br/>
  </div>
</div>
<div class="secondpostholder">
  <div class="leftsecond" id="green2"></div>
  <div class="rightsecond" id="blue2">
    Author: User X <br/>Date: 12-July-2015 <br/>Content: Blah blah <br/>Lorem ipsum dolor sit amet consectur... <br/>Read more <br/>
  </div>
</div>
<div class="secondpostholder">
  <div class="leftsecond" id="green3"></div>
  <div class="rightsecond" id="blue3">
    Author: User X <br/>Date: 12-July-2015 <br/>Content: Blah blah <br/>Lorem ipsum dolor sit amet consectur... <br/>Read more <br/>
  </div>
</div>

Original Answer v2:

Since you have stated that you have multiple such containers and that container size is determined by the height of its contents, you could use a transform: translateY(-100%) on the second div which will move it up and a reverse transform (transform: translateY(100%)) on the first div which will move it down.

This would have worked with my initial assumption that the elements were of same size. A tweaked version of this approach would have worked if the elements atleast had fixed size if not the same. But since one element has fluid size, this approach will not work.

.secondpostholder {
  width: 100%;
  height: 100%;
  position: relative;
}
.rightsecond {
  background: blue;
  width: 100px;
  height: 200px;
  transform: translateY(100%);
}
.leftsecond {
  background: green;
  width: 100px;
  height: 200px;
  transform: translateY(-100%);
}
<div class="secondpostholder">
  <div class="rightsecond" id="blue"></div>
  <div class="leftsecond" id="green"></div>
</div>
<div class="secondpostholder">
  <div class="rightsecond" id="blue2">2blue</div>
  <div class="leftsecond" id="green2">2green</div>
</div>
<div class="secondpostholder">
  <div class="rightsecond" id="blue3">3blue</div>
  <div class="leftsecond" id="green3">3green</div>
</div>

Original Answer v1:

If there was only one such block then we could have used absolute positioning like in the below snippet.

.secondpostholder {
  position: relative;
  width: 100px;
  height: 100% auto;
}
.rightsecond {
  position: absolute;
  top: 200px;
  background: blue;
  width: 100px;
  height: 200px;
}
.leftsecond {
  position: absolute;
  top: 0px;
  background: green;
  width: 100px;
  height: 200px;
}
<div class="secondpostholder">
  <div class="rightsecond" id="blue">
  </div>

  <div class="leftsecond" id="green">
  </div>
</div>
Harry
  • 87,580
  • 25
  • 202
  • 214
  • let me check on that first. BTW, they don't have the same height. Only the "leftsecond" div has a fixed height. The rightsecond div has a fluid height which changes according to it's content. To have a clear view of what i'm saying, you may visit the website that im working on. http://alphaomega.bendaggers.com/ and check the second post entitled "We found love in a hopeless place". You also need to resize your window down to 600px; – Ben Daggers Jul 12 '15 at 10:54
  • 1
    Hey Harry, thanks for your help. I've tried your code and it works. I believe changing the html structure makes it more easier. Let me reward you for your troubles. – Ben Daggers Jul 18 '15 at 08:32
  • I'll reward your +50 tomorrow. didn't know that it must be after 24hrs to before i can give it. – Ben Daggers Jul 18 '15 at 08:33
  • @BenMichaelOracion: Was on a holiday. To be very honest, I don't think this deserved a bounty because it wasn't a big/complex problem. But thank you all the same :) – Harry Jul 21 '15 at 08:31
2

When you resize the browser below 768 px, you can set the child sibling divs to have relative positioning and remove the top values. Change the order using the flexible box technique. This is supported from IE10 and other major browsers. This is not dependent on height of the sibling divs.

.secondpostholder {
  width: 100px;
  height: 100% auto;
  position: relative;
}
.rightsecond {
  background: blue;
  width: 100px;
  height: 200px;
  position: absolute;
  top: 200px;
}
.leftsecond {
  background: green;
  width: 100px;
  height: 200px;
  position: absolute;
  top: 0px;
}
@media (max-width: 768px) {
  .secondpostholder {
    display: flex;
    display: -ms-flex;
    flex-direction: column;
  }
  .rightsecond {
    position: relative;
    order: 2;
    top: 0;
  }
  .leftsecond {
    position: relative;
    order: 1;
  }
}
<div class="secondpostholder">
  <div class="rightsecond" id="blue">
    1blue
  </div>

  <div class="leftsecond" id="green">
    1green
  </div>
</div>

<div class="secondpostholder">
  <div class="rightsecond" id="blue">
    2blue
  </div>

  <div class="leftsecond" id="green">
    2green
  </div>
</div>

<div class="secondpostholder">
  <div class="rightsecond" id="blue">
    3blue
  </div>

  <div class="leftsecond" id="green">
    3green
  </div>
</div>
m4n0
  • 29,823
  • 27
  • 76
  • 89
  • Hey @Manoj, Thanks for your help. let me check on that first. Btw, have you check the website that I'm currently working on? it's http://alphaomega.bendaggers.com/ so you'll have a better picture that i'm working on. It's the second post entitled "We found love in a hopeless place" then resize your window to 600px; – Ben Daggers Jul 12 '15 at 11:03
  • Yes, please take your time to check if this solution works fine for you. – m4n0 Jul 12 '15 at 11:04
  • 1
    Nice one @ManojKumar. If IE9 and lower support is not required then this is the best. – Harry Jul 12 '15 at 11:10
  • yeah. i tried using ie8 and lower and it didnt worked. if it's just a perfect world, i would use flex. – Ben Daggers Jul 12 '15 at 11:40
  • @Harry Thanks! :) Ben, you need to go with Harry's first solution. I think second one will not work for you since it has fluid height. – m4n0 Jul 12 '15 at 11:46
  • @Down voter Can you comment about what is wrong with this so that I can improve the solution? – m4n0 Jul 16 '15 at 15:19