0

Here is the HTML code I'm currently practising on. The CSS is not complete since I don't know how to do what I want to :

.container {
  margin: auto;
  width: 700px;
  border: solid blue 2px;
}
.container div {
  padding: 10px 0;
  vertical-align: top;
}
#orange {
  background-color: coral;
}
#blue {
  background-color: lightblue;
}
.container > div .content {
  border: dotted black 1px;
  height: 100px;
  width: 250px;
  margin: auto;
  display: block;
  text-align: center;
}
<div class="container">
  <div id="orange">
    <div class="content">content
      <br />width: 250px</div>
  </div>
  <div id="blue">
    <div class="content">content
      <br />width: 250px</div>
  </div>
</div>

When the container is large enough, the orange and blue blocks should stand side by side, like this :

enter image description here

If I reduce the width of the container, the horizontal margin inside the orange and blue blocks will shrink until their border meet the content's border :

enter image description here

Here is what I want to obtain when I reduce a bit more the container width :

enter image description here

Does anyone know how to do this ? If possible, without JS. And the container does not depend on the screen size, so I can't use media queries based on the device width. And, of course, the solution must be compatible with as many web browsers as possible (including the latest versions of IE).

Edit: I've considered using flexbox but I was hoping I could find a solution without. By the way, I would be interested by a way to write, in the CSS code, specific rules which apply only on IE9 and below.

Edit2: It seems it is not possible to do what I want with the following constraints :

  • no JS
  • no condition on the screen size, but on the container size instead

So I guess I will have to drop at least one of these...

Eria
  • 2,653
  • 6
  • 29
  • 56
  • 3
    Explore [flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) - `display: flex` – Pugazh Aug 09 '16 at 09:19
  • 1
    use `display:inline-block` in `.container` – Gaurav Aggarwal Aug 09 '16 at 09:20
  • 1
    Use flexbox as @Pugazh suggested. Great reference in the link he attached. – Luuuud Aug 09 '16 at 09:21
  • you can also look at media queries : http://www.w3schools.com/cssref/css3_pr_mediaquery.asp or https://css-tricks.com/snippets/css/media-queries-for-standard-devices/ - If you are looking for it to work on older browsers. Flexbox is for modern browsers – Andrew Aug 09 '16 at 09:21
  • @Pugazh : yes, I've already red this article (which is very good btw). But, in a perfect worl, I'd need IE9 compatibility... And IE9 does not support flexbox. :( But if I don't find another solution, I will try to use flexbox. – Eria Aug 09 '16 at 09:24
  • @GauravAggarwal : hum... I don't see how it will help the colored blocks to take the full width. – Eria Aug 09 '16 at 09:26
  • @Eria did you read my comment in my answer? I think is the only way to achieve that without using flexbox. – Marcos Pérez Gude Aug 09 '16 at 10:00
  • @Pugazh : in case I try to use flexbox, do you know a way to write, inside the CSS code, specific rules which apply only on IE 9 and below ? – Eria Aug 09 '16 at 10:04
  • @Eria : There are 2 options: **Option 1**. Have seperate stylesheet(**.css** file) specific to IE 9 and below, reference it in HTML - http://stackoverflow.com/questions/6654423/target-ie9-only-via-css **Option 2**. Edit CSS in this way - https://css-tricks.com/snippets/css/browser-specific-hacks/ – Pugazh Aug 09 '16 at 11:13

2 Answers2

1

Solution using flexbox.

In case you wish to have styles specific to IE9 and below, there are 2 options:

  1. Have seperate stylesheet(.css file) specific to IE 9 and below, reference it in HTML - Target IE9 Only via CSS

  2. Edit CSS in this way - https://css-tricks.com/snippets/css/browser-specific-hacks

.container {
  display: flex;
  background-color: green;
  justify-content: space-around;
  padding: 10px;
}
#orange {
  background-color: coral;
  height: 100px;
  min-width: 250px;
  text-align: center;
  margin: 5px;
}
#blue {
  background-color: lightblue;
  height: 100px;
  min-width: 250px;
  text-align: center;
  margin: 5px;
}
@media (max-width: 500px) {
  .container {
    flex-flow: row wrap;
  }
}
<div class="container">
  <div id="orange">
    <div class="content">
      content
      <br/>width: 250px
    </div>
  </div>
  <div id="blue">
    <div class="content">
      content
      <br/>width: 250px
    </div>
  </div>
</div>
Community
  • 1
  • 1
Pugazh
  • 9,453
  • 5
  • 33
  • 54
  • Unless I'm mistaken, the width evaluated by the media query is the device width. But for example, the `.container` can be 50%-width on a 800px screen. Which makes itself 400px width. Will your solution work then ? – Eria Aug 09 '16 at 12:08
  • @Eria - No. If you need to apply styles depending on the size of a `.container`, you'll have to use JavaScript. – Pugazh Aug 09 '16 at 12:10
0

Just add inline-block to the two boxes that needs to be side by side, and the block element will behave as you like (take always all the available width), because that's the default behaviour of block elements.

.container { 
  margin: auto; 
  font-size: 0;
  width: 700px;
}
.container div { 
  padding: 10px 0;
  display: inline-block;
  vertical-align: top;
  font-size: 1rem;
}
#orange { background-color: coral; }
#blue { background-color: lightblue; }

.container > div .content {
  border: dotted black 1px;
  height: 100px;       
  width: 250px;
  margin: auto;
  display: block;
  text-align: center;
}
<div class="container">
  <div id="orange">
    <div class="content">content<br />width: 250px</div>
  </div>
  <div id="blue">
    <div class="content">content<br />width: 250px</div>
  </div>
</div>

While inline-block runs over the text flow with the blocks behaviours, you need to avoid the whitespaces between the boxes. That's why I set font-size:0 in the container, and reset font-size: 1rem again in the inner boxes to achieve that.

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • You can make it with mediaqueries, but it depends on screen resolution, not the width of the element. Element queries is at the moment only a javascript plugin that listen to changes in the sizes of the boxes and triggers an element. If this solution fits with your requirements (add a javascript plugin), I can add a better answer than this. – Marcos Pérez Gude Aug 09 '16 at 09:42
  • I'll wait a bit to see if someone has a brilliant idea. But if not, I think the solution will be flexbox... The fact that all the container width is always covered by both the orange and blue blocks is essential, and your solution does not achieve that. – Eria Aug 09 '16 at 10:07
  • `The fact that all the container width is always covered by both the orange and blue blocks is essential`. If you define `250px` of width... how do you expect to behave?? Really confusing. There's no brilliant idea with this. What you want to achieve have two solutions: flexbox or element queries. Element queries is fine, you can write CSS and the js engine will process it. When an element meets the condition you want make another CSS runs. – Marcos Pérez Gude Aug 09 '16 at 10:12
  • orange and blue blocks do not have a 250px width... Nevermind. But thank you for your answers. – Eria Aug 09 '16 at 13:50
  • http://elementqueries.com/ : even if I can't use this in this case, due to constraints I can't change, it's quite interesting ! – Eria Aug 09 '16 at 13:54
  • Yes, you have `.container > div .content {` with width 250px. Element queries are brilliant – Marcos Pérez Gude Aug 09 '16 at 14:05
  • yep; `.content` blocks have 250px width. Not their direct parent, which are actually the blocks colored in orange and blue... – Eria Aug 09 '16 at 14:59