1

I want to align my stuff as:

┌────┬─────────────────────────────────┐
│ No │  Some possible long content     │
│    ├─────────────────────────────────┤
│    │  Not so long stuff              │
└────┴─────────────────────────────────┘

Using tables would be like 5 minutes using rowspan=2 but using divs I don't know where to start. I think divs should should be used since this is not tabular data. Is just a way of layout a counter, a possible lone title and its subtitle (not that long):

<div id="container">
    <div class="div1">No</div>
    <div class="div2">Some possible long content</div>
    <div class="div3">Not so long stuff</div>
</div>

1 width is constant but its height should be the sum of 2 and 3 height. 3 always contain one line of code but 2 might contain more than 1 line.

Any help is appreciated

Sleek Geek
  • 4,638
  • 3
  • 27
  • 42
nacho4d
  • 43,720
  • 45
  • 157
  • 240

3 Answers3

6

If you can change the HTML structure to wrap the right side divs then flexbox can do that.

.container {
  width: 50%;
  margin: auto;
  border: 1px solid grey;
  display: flex;
}
.div1,
.right {
  display: flex;
  flex-direction: column;
}
.div1,
.right > div {
  padding: 1em;
}
.div1 {
  background: orange;
}
.div2 {
  background: pink;
}
.div3 {
  background: lightblue;
}
<div class="container">
  <div class="div1">No</div>
  <div class="right">
    <div class="div2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eveniet explicabo omnis, suscipit laborum officiis, eius sint ipsam doloremque magnam reiciendis corporis repudiandae ducimus laboriosam ex! Porro dolores aut vitae. Assumenda ut laborum dolor
      fugiat ullam nihil enim sapiente ex vel praesentium quo dolores distinctio, aliquid perferendis non accusamus dolorum excepturi?</div>
    <div class="div3">Not so long stuff</div>
  </div>
</div>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
  • I added all the vendor stuff for flex as in your other answer (http://stackoverflow.com/a/31788693/149008) plus `-ms-flex-direction` and `-webkit-flex-direction` and I worked! Thanks. – nacho4d Aug 06 '15 at 12:53
2

You could use absolute positioning, like so: https://jsfiddle.net/sueba6t2/1/

Based on a modified HTML markup (where I renamed some classes to be compliant), here is the css:

.container {
    position: relative;
}
.d1 {
    height: 100%;
    position: absolute;
    width: 3em;
}
.d2, .d3 {
    margin-left: 3em;
}

Or, if you can change the markup and like newer, fancy stuff, use flex: https://jsfiddle.net/k7e01o29/

HTML:

<div>
    <nav>No</nav>
    <article>
        <section>Long content</section>
        <footer>Not long</footer>
    </article>
</div>

CSS:

div {
    display: flex;
    flex-direction: row;
}
article {
    width: 100%;
}

Be aware that the flexible box module is not supported in all browsers. If you want to support IE10 or below, you're going to have to use polyfills, which might be undesirable. Flex is much more powerful than what you are trying to achieve, but it has downsides when it comes to browser support.

Refer to http://caniuse.com/#search=flex for more details.

Xr.
  • 1,410
  • 13
  • 23
  • 1
    Just fyi: `float` should not be used in combination with `position: absolute;`, it is nonsensical. Instead, make it stick to any side of the parent box using `top`, `right`, `bottom` or `left` which you give a value of pixels, `em`s or a percentage. – klaar Aug 06 '15 at 12:28
  • You're completely right. I first tried with floats, changed my mind and left the code pending... – Xr. Aug 06 '15 at 12:30
  • check out my answers, it has solved my life problems ;) – Muhammad Umer Aug 06 '15 at 12:54
2

You can use css's display:table and display:table-cell

http://jsfiddle.net/8qv9z762/

#container {
    display: table;
    background-color: #dda;
    height: 1px; /*weird but important forgot why*/
}
.r {
    display: table-cell;
}
.div1 {
    height: 100%;
}
.div1, .div2, .div3 {
    border: solid 1px;
    padding: 5px 8px;
}

enter image description here

This has way better support across browser, flexbox is still not supported everywhere.

The table-cell is supported all the way to ie8, whereas, flex is supported only by ie11+ (last two versions)

Only draw back of this technique is you can't have margins. However, that's not a problem as you can have another child container that can be a main wrapper inside table-cell div and it can have margin.

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
  • 1
    Paulie_D's answer works in all browsers (by adding the vendor stuff) but this looked a simpler answer :) Thanks! – nacho4d Aug 06 '15 at 13:07
  • yea almost... it wont work in ie8 and ie9 but not sure if they even matter – Muhammad Umer Aug 06 '15 at 13:09
  • This is a nice fallback considering the compatibility, but it's actually almost as ugly as using tables, because you could have just used tables in HTML and left out these bits of CSS instead. Good thing about this, though, is that you can swap the CSS with something more nice, so it's acceptable. – klaar Aug 06 '15 at 13:24
  • without flexbox trying to achieve all properties of a table layout using only css but not display table would require javascript's help. Beside it's not ugly as using tables, html structure isn't ruined due to using display: table or so on. It's no more dirty than using display:inline-block – Muhammad Umer Aug 06 '15 at 13:36