-1

I'm reproducing a site in order to practice CSS and HTML, but I got stuck on something. I tried for hours and searched everywhere for a solution in order to split a section in half and add a row with two columns inside, like you can see in this image:

enter image description here Also tried to look at their source, but they have tons of classes in all kind of files, which makes it pretty hard to figure out how they did it.

/**** Standard stuff ****/

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
html,
body {
  background-color: #fff;
  color: #555;
  font-family: 'Lato', 'Arial', sans-serif;
  font-weight: 300;
  font-size: 20px;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
}
.section {
  clear: both;
  padding: 0px;
  margin: 0px;
}
.clearfix {
  zoom: 1;
}
.clearfix:after {
  content: '.';
  clear: both;
  display: block;
  height: 0;
  visibility: hidden;
}
/**** Row ****/

.row {
  max-width: 1140px;
  margin: 0 auto;
}
.row:before,
.row:after {
  content: "";
  display: table;
}
.row:after {
  clear: both;
}
/**** Cols ****/

.col {
  display: block;
  float: left;
  margin: 1% 0 1% 1.6%;
}
.col:first-child {
  margin-left: 0;
}
.span-1-of-2 {
  width: 49.2%;
}
/**** Styling ****/

.first-half {
  width: 50%;
  background-color: #ff0000;
  height: 500px;
  float: left;
}
.second-half {
  width: 50%;
  float: right;
  height: 500px;
}
.section-split {
  position: relative;
}
.section-split .row {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.left-text {
  padding-right: 15%;
}
.right-text {
  padding-left: 15%;
}
.right-text,
.left-text {
  color: #fff;
}
<section class="section-split clearfix">
  <div class="first-half"></div>
  <div class="second-half">
    <img src="http://placehold.it/950x500" alt="">
  </div>
  <div class="row">
    <div class="col span-1-of-2">
      <div class="left-text">

        <p>Case Study - Macaw Mobile App We recently restructured Macaws strategy which lead to an increase in sales and traffic for the brand. Downloads and turnover skyrocketed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
          eu fugiat nulla pariatur. Play Showcase</p>
      </div>
    </div>

    <div class="col span-1-of-2">
      <div class="right-text">

        <p>Case Study - Macaw Mobile App We recently restructured Macaws strategy which lead to an increase in sales and traffic for the brand. Downloads and turnover skyrocketed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
          eu fugiat nulla pariatur. Play Showcase</p>

      </div>
    </div>
  </div>
</section>

Here's what I've managed to do: http://codepen.io/anon/pen/jyvBvo. Even though it's similar to what I want, I highly doubt that that's the proper way to do it. I think the code is pretty "ugly".

I'm also trying to make it responsive, which makes it even more challenging.

Any help and guidance will be appreciated!

Thank you!

Radu B
  • 103
  • 2
  • 10
  • Remove the row with the content and put the content inside the first half and second half. If you can use Flex property then what you are looking for is pretty easy. – Aslam Feb 08 '17 at 04:43
  • From your current html code, the row has to be positioned `absolute` to achieve your requirements. But then page in your picture probably has the content within the section / halves. –  Feb 08 '17 at 04:49
  • @AnuragDaolagajao Thanks for your reply! I had this .section-split .row{ position:absolute; but the rest was used in a wrong way anyway... Flex solved the issue, though. – Radu B Feb 08 '17 at 05:16
  • @hunzaboy Thank you for your reply! Yep, the flex solution pointed out by Michael here did the trick! – Radu B Feb 08 '17 at 05:18

1 Answers1

1

Flexbox makes layouts like this pretty easy. To create the split plane, just have a flex parent with 2 flex children, and by default they will display in a flex row like this. Then you can make the flex children flex parents as well and shift around the content in them via with flex properties.

/**** Standard stuff ****/

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

section {
  display: flex;
  color: #fff;
  min-height: 100vh;
}

.item {
  flex-basis: 0;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 4em;
}

.pri {
  background: red;
  align-items: flex-end;
}

.sec {
  background: url('http://placehold.it/950x500') center center no-repeat;
  background-size: cover;
}

.item p {
  width: 66%;
}

@media (max-width: 767px) {
  section { flex-direction: column; }
  /* or you can use this 
  section { display: block; } */
}
<section>

  <div class="item pri">
    <p>Case Study - Macaw Mobile App We recently restructured Macaws strategy which lead to an increase in sales and traffic for the brand. Downloads and turnover skyrocketed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eufugiat
      nulla pariatur. Play Showcase</p>
  </div>

  <div class="item sec">
    <p>Case Study - Macaw Mobile App</p>

    <p>We recently restructured Macaws strategy which lead to an increase in sales and traffic for the brand. Downloads and turnover skyrocketed.</p>

    <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Play Showcase</p>

  </div>

</section>
Michael Coker
  • 52,626
  • 5
  • 64
  • 64
  • Thank you! This did the trick quite easily. I need to get into this flex stuff :D. One more question, though, if it's not too much to explain. From the courses I took, I've learned to make responsive designs using a grid file, with the use of columns. In the "flex" case, since I'm not using the columns anymore, how could I make the left side and right side go under each other (used to define this like .col{width:100%;} and problem solved) at a small breaking point on mobiles, like max-width:767px;? Now they just stretch vertically. Thank you! – Radu B Feb 08 '17 at 05:13
  • @RaduB sure, define a media query and either add `section { flex-direction: column; }`, or just apply `section { display: block; }`. Updated my answer. – Michael Coker Feb 08 '17 at 05:16
  • This was way easier than I expected :). Thanks a lot! I appreciate it! Should I start learning another technique for responsive design, like this flex thing, and give up on the grid system? – Radu B Feb 08 '17 at 05:24
  • @RaduB depends, what is the grid thing? You should learn flexbox, along with every other part of CSS :) – Michael Coker Feb 08 '17 at 05:28
  • I'm currently using a responsive grid system downloaded from responsivegridsystem(dot)com. Don't know how efficient is that really... And yes, I'll definitely get into flexbox. Hopefully I'm not missing on any other CSS goodies :). – Radu B Feb 08 '17 at 05:40
  • @RaduB ah yeah responsivegridsystem is a CSS framework. Or in other words, someone else's CSS. Like bootstrap or semantic or material or something. Flexbox is actual CSS. Knowing CSS is always better than learning someone else's CSS. If you wanted to learn a framework, I would use Bootstrap or a bigger one than repsonsivegridsystem, but if you really want to learn CSS, don't use a framework until you are great with CSS or you'll use it as a crutch and will be a half-assed CSS guy - like all of the people that make SO posts who can't figure out some super simple CSS thing because... – Michael Coker Feb 08 '17 at 05:47
  • @RaduB they learned bootstrap and barely know any CSS, so they're stuck in the mud as soon as they need to do something that the framework doesn't provide for them. – Michael Coker Feb 08 '17 at 05:47
  • Yep, you're right. Well, I never thought this would be a short and easy ride, so I have my patience with me. Hopefully my brain will pitch in and help me learn all this, even if it takes a long time :D. You've been of great help! Thanks! – Radu B Feb 08 '17 at 06:06