0

I realize that I am asking two separate questions here, but given my inexperience with HTML and CSS, I get the feeling that they are connected. If nothing else, I am unsure about how to ask one question while ensuring that the answers I get will be compatible with the other.

So, here goes:

I am making a small calculator, which needs to look nice in different window sizes. I have some groupings of buttons that all need to be kept together, while the different groupings are moved around according to the window size. Below is a screenshot of "good behavior":

Screenshot 1

The content is together with headers, and there are no odd blank spaces.

Then we get the next behavior: upon a small change of window width, some material is moved down, but not the entire div - the header stays:

Screenshot 2

How do I make the header follow the content down?

Finally, when the window width gets even smaller, the header moves down as well:

enter image description here

This behavior is fine but I'd like the remaining content in the top to take advantage of the extra space and center horizontally. Any idea how? I've had a look at questions such as How do you easily horizontally center a <div> using CSS?, but they all seem to entail increasing the width of the element that I want centered, which breaks the behavior in screenshot #1.

An example of that is seen here:

screenshot 4

How do I fix screenshots #2 and #3, without affecting #1?

I have of course also made a fiddle, but I thought screenshots would make the point better:

http://jsfiddle.net/eEPmw/

in it is:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .incbut,.decbut,.rollButton{
        background-color:#FF961F;
      }
      .regular-checkbox{
        border-color:#FF961F;
      }

      body {
        background-color:#FCB41B;
        color:#753B08;
      }

      .modifiers{
        text-align:left;
        min-width:8em;
      }
      .center{
        margin-left:auto;
        margin-right:auto;
      }
      .inputField {
        max-width:2em;
        border:1px #bababa solid;
        vertical-align:middle;
        text-align:center;
      }
      .incbut,.decbut {
        border: 1px transparent solid;
        display: inline-block;
        text-align: center;
        vertical-align: middle;
        cursor: pointer;
        padding: 4px 10px;
        position: relative;
        width: 2.5em;
      }
      .rollButton{
        border: 1px transparent solid;
        display: inline-block;
        text-align: center;
        vertical-align: middle;
        cursor: pointer;
        padding: 4px 10px;
        position: relative;#FF0000
      }
      button:active{
        background-color:#FF0000;
      }
      .fieldCol{
        float:left;
        text-align:center;
        max-width=45%;
        height:9em;
      }
      input[type=number]::-webkit-inner-spin-button,
      input[type=number]::-webkit-outer-spin-button {
        -webkit-appearance: none;
        margin: 0;
      }
      .incbut,.inputField, .decbut{
        min-height:2em;
        margin-right:0.5em; 
        margin-bottom:0.3em; 
      }
      h2{
        margin:0.1em;
        font-weight:500;
      }
      h2,button, input{
        font-family:inherit;
      }
      .outputField{
        padding:2px;
        margin:3px;
        border:1px #753B08 solid;
        width=2em;
        line-height:1.7em;
        white-space:nowrap; 
      }
      label {
        display: inline;
      }
      .checkBoxLabel{
        margin-left:5px;
        font-size:90%;
      }
      .regular-checkbox {
        display: none;
      }
      .regular-checkbox + label {
        border: 3px solid #FF961F;
        padding: 9px;
        background-color: #FFFFFF;
        display: inline-block;
        position: relative; 
        vertical-align:middle;
        margin: .1em;
      }
      .regular-checkbox:checked + label {
      border: 3px solid #FF961F;
      }
      .regular-checkbox:checked + label:after {
        content: '\2716';
        font-size: 1em;
        position: absolute;
        top: 0px;
        left: 10%;
        color: #753B08;
        font-weight: 600;
      }
      .checkboxbox2{
        width:8em;
        float:left;
      }
      .colContainer{
        text-alignment:center;
      }
      div.centre{
        text-align: left;
        width: 17em;
        display: block;
        margin-left: auto;
        margin-right: auto;
      }
    </style>
  </head>
  <body style="text-align:center;font-family: 'Segoe UI', 'Open Sans', Verdana, Arial, Helvetica, sans-serif;" class="center">      


<!--
<div class="colContainer">
<div class="centre">
-->
  <section id="bal_attacker" class="fieldCol">
    <h2>Attacker:</h2>
    <label for="bal_BS1">BS:</label> <br>
    <button type="button" id="bal_BS1inc" class="incbut"> + </button> 
    <input type="number" id="bal_BS1" value="1" class="inputField"> 
    <button type="button" id="bal_BS1dec" class="decbut"> - </button> <br> 
    <label for="bal_S">S:</label> <br>
    <button type="button" id="bal_Sinc" class="incbut"> + </button> 
    <input type="number" id="bal_S" value="1" class="inputField"/>
    <button type="button" id="bal_Sdec" class="decbut"> - </button> 
  </section>
  <section id="bal_defender" class="fieldCol" >
    <h2>Defender:</h2>
    <label for="bal_T">T:</label> <br>
    <button type="button" id="bal_Tinc" class="incbut"> + </button> 
    <input type="number" id="bal_T" value="1" class="inputField"> 
    <button type="button" id="bal_Tdec" class="decbut"> - </button>  <br> 
  </section>

<!--
</div>
</div>

-->
  <div>

  <section title="bal_Modifiers" class="modifiers">
    <h2 style="text-align:center;">Modifiers:</h2><br>
    <span class="checkboxbox2">
      <input type="checkbox" id="bal_harmConv" class="regular-checkbox" /><label for="bal_harmConv"></label>
      <label for="bal_harmConv" class="checkBoxLabel">Harm. Conv.</label>
    </span>
    <span class="checkboxbox2">
      <input type="checkbox" id="bal_rrToHit" class="regular-checkbox" /><label for="bal_rrToHit"></label>
      <label for="bal_rrToHit" class="checkBoxLabel">R.r. to hit</label>
    </span>
    <span class="checkboxbox2">
      <input type="checkbox" id="bal_rrToW" class="regular-checkbox" /><label for="bal_rrToW"></label>
      <label for="bal_rrToW" class="checkBoxLabel">R.r. to wound</label>
    </span>
    <div style="clear: both;"></div>
  </section>
  <div style="clear: both;"></div>
  </body>
 </html>

I apologize that this is not quite a minimal working example - I have been trying for some time now to reduce it further, but I seem to break stuff when I try.

Community
  • 1
  • 1
Kaare
  • 531
  • 1
  • 7
  • 26
  • you need to make a 3 column layout, `col1 = attacker`, `col2 = defender`, `col3= modifiers`. That will insure that when the screen shrinks past a point all elements in that column will drop. Next you need to use percentages within your columns to allow for even distribution of the elements within, or give each col a fixed with and all the widths of the elements inside relevant to the with of the parent column. – Eric Goncalves Mar 11 '13 at 20:25

4 Answers4

1

Easy fix:

Put everything in a holder div ("wrap", some call it):

<div id="holder">
   <section> .... </section>
   <section> .... </section>
   <section> .... </section>
</div>

Then make the holder text-align:center; and the sections display:inline-block;, and likely you'll want vertical-align:top;.

#holder{
   text-align:center;
}
section{
   display:inline-block;
   vertical-align:top;
}

From your jsFidle, you'll also want to remove float:left; from .fieldCol

Here's your jsFiddle updated

DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • this solves the centering, but it seems to have the same problem with non-optimal packing as shown in screenshot #4? – Kaare Mar 11 '13 at 21:24
  • Glad to help! `display:inline-block` is not very well supported (cough..IE) If you'd like to make this work cross-browser, be sure to check out http://css-tricks.com/snippets/css/cross-browser-inline-block/ – DACrosby Mar 11 '13 at 21:41
  • "display:inline-block is not very well supported" <- why do people keep saying this nonsense? IE6 is pretty much dead in the water now and IE7 is not far behind. – cimmanon Mar 11 '13 at 23:31
  • Fair point. I guess I'm just re-iterating information from several years ago. [This browser support chart shows which browsers support it (most all do)](http://caniuse.com/inline-block). and [StatCounter shows that IE7 now falls under "other"](http://gs.statcounter.com/#browser_version-ww-monthly-201209-201302-bar), so it's really not much of a concern – DACrosby Mar 11 '13 at 23:38
  • hmm, is there a way to make the "modifiers" section switch between column and row layout depending on whether it has the room for a row? that was the strength of the float:left-solution, the vertical dimension was minimized as well. – Kaare Mar 12 '13 at 10:08
1

If you want to create this layout with the responsiveness you've specified, Flexbox is what you're looking for. I've simplified the markup a bit:

http://codepen.io/cimmanon/pen/djwsz

<div id="calculator">
    <section id="bal_attacker" class="fieldCol">
        <h2>Attacker:</h2>

        <div>
            <label for="bal_BS1">BS:</label> <br>
            <button type="button" id="bal_BS1inc" class="incbut"> + </button> 
            <input type="number" id="bal_BS1" value="1" class="inputField"> 
            <button type="button" id="bal_BS1dec" class="decbut"> - </button>
        </div>

        <div>
            <label for="bal_S">S:</label> <br>
            <button type="button" id="bal_Sinc" class="incbut"> + </button> 
            <input type="number" id="bal_S" value="1" class="inputField"/>
            <button type="button" id="bal_Sdec" class="decbut"> - </button>
        </div>
    </section>

    <section id="bal_defender" class="fieldCol" >
        <h2>Defender:</h2>

        <div>
            <label for="bal_T">T:</label> <br>
            <button type="button" id="bal_Tinc" class="incbut"> + </button> 
            <input type="number" id="bal_T" value="1" class="inputField"> 
            <button type="button" id="bal_Tdec" class="decbut"> - </button>
        </div>
    </section>

    <section title="bal_Modifiers" class="modifiers">
        <h2>Modifiers:</h2>

        <ul>
            <li><label><input type="checkbox" id="bal_harmConv" class="regular-checkbox" /> Harm. Conv.</label></li>
            <li><label><input type="checkbox" id="bal_rrToHit" class="regular-checkbox" /> R.r. to hit</label></li>
            <li><label><input type="checkbox" id="bal_rrToW" class="regular-checkbox" /> R.r. to wound</label></li>
        </ul>
    </section>
</div>

These should be all of the styles you need, I've chopped out all of the purely aesthetic styles that aren't really relevant to the layout (colors, padding, etc.):

#calculator, section.modifiers ul {
  display: -webkit-flexbox;
  display: -ms-flexbox;
  display: -webkit-flex;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}
@supports (flex-wrap: wrap) {
  #calculator, section.modifiers ul {
    display: flex;
  }
}

#calculator section {
  -webkit-flex: 1 1 30%;
  -ms-flex: 1 1 30%;
  flex: 1 1 30%;
}

section.modifiers ul {
  padding: 0;
}

section.modifiers li {
  list-style-type: none;
  -webkit-flex: 1 0 33%;
  -ms-flex: 1 0 33%;
  flex: 1 0 33%;
  white-space: pre;
}

Now the bad news is that support for Flexbox is rather poor at the moment. Right now, only Opera, Chrome, and IE10 are capable of rendering this layout.

http://caniuse.com/#feat=flexbox

cimmanon
  • 67,211
  • 17
  • 165
  • 171
0

I would put the headers in parent divs along with the elements you want them to stay with.

<div class="parent">
    <h1>Header</h1>
    <div class="content">
        //Whatever your content is
    </div>
</div>

As for centering, margin: 0 auto; is what's typically done for this, but since you're going to have a float, you're probably going to have to write some script to handle that for you based on .length() of the parent divs and the width of the grandparent container.

Plummer
  • 6,522
  • 13
  • 47
  • 75
0

Here is a quick mock can use to get started.

you need to make a 3 column layout, col1 = attacker, col2 = defender, col3= modifiers. That will insure that when the screen shrinks past a point all elements in that column will drop. Next you need to use percentages within your columns to allow for even distribution of the elements within, or give each col a fixed with and all the widths of the elements inside relevant to the with of the parent column.

demo: http://jsfiddle.net/bRRqW/

HTML

<div class="wrapper">
    <div class="col">
        <h4>Attacker:</h4>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae dignissimos voluptatem in ut minima doloremque fuga qui saepe! Esse laudantium non voluptates deleniti soluta ab praesentium velit id omnis corrupti.</p>
    </div>
    <div c**strong text**lass="col">
        <h4>Defender:</h4>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae dignissimos voluptatem in ut minima doloremque fuga qui saepe! Esse laudantium non voluptates deleniti soluta ab praesentium velit id omnis corrupti.</p>
    </div>
    <div class="col">
        <h4>Modifier:</h4>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae dignissimos voluptatem in ut minima doloremque fuga qui saepe! Esse laudantium non voluptates deleniti soluta ab praesentium velit id omnis corrupti.</p>
    </div>
</div>

CSS

.wrapper {
    max-width: 600px;   
    margin: 0 auto;
}

.col { 
    width: 33.333333%;
    float: left;
    position: relative;
    text-align: center;
    min-width: 150px;
    background: red;
}
Eric Goncalves
  • 5,253
  • 4
  • 35
  • 59
  • I can't help but notice that the columns are not in fact centering when the screen-size is changed? – Kaare Mar 11 '13 at 21:22