0

Issue

I'm trying to design a basic calendar layout using HTML/CSS. Another application would be an image gallery. The point is, I want to be able to set the row "width" in columns using just CSS, but it is not working.

This is the desired calendar look I'm trying to achieve with CSS:

Desired "calendar" look

NOTE: The most promising work-around I have found is to wrap an inline-block unordered list in a container div, and set the width of the div so that it gives you the desired row length. However, this is not a sufficient solution for my purposes. It doesn't address insert a line break to manually split rows.

My code

Version 1

This version of my calendar works, but it uses fixed <br> tags in the HTML, which doesn't allow for easily changing a number in the CSS to change the row width.

span.cell {
    display: table-cell;
    border-collapse: collapse;
    height: 2.2em;
    border: solid black 1px;
    width: 30px;
}

span.cell:nth-child(8n+7) {
    background-color: yellowgreen;
}

br {
    content: "";
    margin: 0;
}
<span class='cell'>1 </span>
<span class='cell'>2 </span>
<span class='cell'>3 </span>
<span class='cell'>4 </span>
<span class='cell'>5 </span>
<span class='cell'>6 </span>
<span class='cell'>7 </span>
<br>
<span class='cell'>8 </span>
<span class='cell'>9 </span>
<span class='cell'>10 </span>
<span class='cell'>11 </span>
<span class='cell'>12 </span>
<span class='cell'>13 </span>
<span class='cell'>14 </span>
<br>
<span class='cell'>15 </span>
<span class='cell'>16 </span>
<span class='cell'>17 </span>
<span class='cell'>18 </span>
<span class='cell'>19 </span>
<span class='cell'>20 </span>
<span class='cell'>21 </span>
<br>
<span class='cell'>22 </span>
<span class='cell'>23 </span>
<span class='cell'>24 </span>
<span class='cell'>25 </span>
<span class='cell'>26 </span>
<span class='cell'>27 </span>
<span class='cell'>28 </span>
<br>
<span class='cell'>29 </span>
<span class='cell'>30 </span>
<span class='cell'>31 </span>

Version 2

Version 2 attempts to accomplish the same using pure CSS, but so far, has been unsuccessful. Here are a few of the challenges with this attempt:

Here is the incorrect result I am getting with this version:

enter image description here

  1. The spans display inline even though I have display: table-cell set.
  2. Here we are using span.cell:nth-child(8n):after to insert a blank line, as per this post (but it isn't working): Line break (like <br>) using only css
  3. Have tried Chrome, Firefox, and IE, all to no avail.

span.cell {
    display: table-cell;
    border-collapse: collapse;
    height: 2.2em;
    border: solid black 1px;
}

span.cell:nth-child(7n) {
    background-color: yellowgreen;
}

span.cell:nth-child(8n):after {
    content: "\a";
    white-space: pre;
}   

br {
    content: "";
    margin: 0;
}
<div id="cal1">

<span class='cell' style='width:30px'>1 </span>
<span class='cell' style='width:30px'>2 </span>
<span class='cell' style='width:30px'>3 </span>
<span class='cell' style='width:30px'>4 </span>
<span class='cell' style='width:30px'>5 </span>
<span class='cell' style='width:30px'>6 </span>
<span class='cell' style='width:30px'>7 </span>
<span class='cell' style='width:30px'>8 </span>
<span class='cell' style='width:30px'>9 </span>
<span class='cell' style='width:30px'>10 </span>
<span class='cell' style='width:30px'>11 </span>
<span class='cell' style='width:30px'>12 </span>
<span class='cell' style='width:30px'>13 </span>
<span class='cell' style='width:30px'>14 </span>
<span class='cell' style='width:30px'>15 </span>
<span class='cell' style='width:30px'>16 </span>
<span class='cell' style='width:30px'>17 </span>
<span class='cell' style='width:30px'>18 </span>
<span class='cell' style='width:30px'>19 </span>
<span class='cell' style='width:30px'>20 </span>
<span class='cell' style='width:30px'>21 </span>
<span class='cell' style='width:30px'>22 </span>
<span class='cell' style='width:30px'>23 </span>
<span class='cell' style='width:30px'>24 </span>
<span class='cell' style='width:30px'>25 </span>
<span class='cell' style='width:30px'>26 </span>
<span class='cell' style='width:30px'>27 </span>
<span class='cell' style='width:30px'>28 </span>
<span class='cell' style='width:30px'>29 </span>
<span class='cell' style='width:30px'>30 </span>
<span class='cell' style='width:30px'>31 </span>

</div><!-- END #cal1 div -->

What I've tried that hasn't worked

  1. I found the following links in my search, but none of them provide a working solution (although many of them purported to or came close):

My questions

  1. Is what I'm trying to do possible (split a row of spans or an unordered list without using a container div, or adding extra markup, using only CSS)? If not, why? If so, how?
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1

As jameswassinger posted, it can be done with floats.

Since you want it injected via CSS, here is a snippet doing so

Remember that

clear left: Requires that the top border edge of the box be below the bottom outer edge of any left-floating boxes that resulted from elements earlier in the source document.

w3c reference

span.cell {
  float: left;
    height: 2.2em;
    border: solid black 1px;
}

span.cell:nth-child(7n) {
    background-color: yellowgreen;
}
span.cell:nth-child(7n+1) {
  clear: left;
}
<div id="cal1">

<span class='cell' style='width:30px'>1 </span>
<span class='cell' style='width:30px'>2 </span>
<span class='cell' style='width:30px'>3 </span>
<span class='cell' style='width:30px'>4 </span>
<span class='cell' style='width:30px'>5 </span>
<span class='cell' style='width:30px'>6 </span>
<span class='cell' style='width:30px'>7 </span>
<span class='cell' style='width:30px'>8 </span>
<span class='cell' style='width:30px'>9 </span>
<span class='cell' style='width:30px'>10 </span>
<span class='cell' style='width:30px'>11 </span>
<span class='cell' style='width:30px'>12 </span>
<span class='cell' style='width:30px'>13 </span>
<span class='cell' style='width:30px'>14 </span>
<span class='cell' style='width:30px'>15 </span>
<span class='cell' style='width:30px'>16 </span>
<span class='cell' style='width:30px'>17 </span>
<span class='cell' style='width:30px'>18 </span>
<span class='cell' style='width:30px'>19 </span>
<span class='cell' style='width:30px'>20 </span>
<span class='cell' style='width:30px'>21 </span>
<span class='cell' style='width:30px'>22 </span>
<span class='cell' style='width:30px'>23 </span>
<span class='cell' style='width:30px'>24 </span>
<span class='cell' style='width:30px'>25 </span>
<span class='cell' style='width:30px'>26 </span>
<span class='cell' style='width:30px'>27 </span>
<span class='cell' style='width:30px'>28 </span>
<span class='cell' style='width:30px'>29 </span>
<span class='cell' style='width:30px'>30 </span>
<span class='cell' style='width:30px'>31 </span>

</div><!-- END #cal1 div -->
vals
  • 61,425
  • 11
  • 89
  • 138
0

I believe you can achieve the desired look and effects you want by floating the cell elements to the left. To achieve the "line break" via CSS use the clear attribute clear: both;

Here is a jsFiddle example.

Let me know if this answer helps.

.day {
  float: left;
  display: table-cell;
  border-collapse: collapse;
  height: 2.2em;
  border: solid black 1px;
  width: 30px;
}

.colored {
  background: #8AC627;
}
.clear {
  clear: both;
}
<span class="day">1</span>
<span class="day">2</span>
<span class="day">3</span>
<span class="day">4</span>
<span class="day">5</span>
<span class="day">6</span>
<span class="day colored">7</span>
<div class="clear"></div>
<span class="day">8</span>
<span class="day">9</span>
<span class="day">10</span>
<span class="day">11</span>
<span class="day">12</span>
<span class="day">13</span>
<span class="day colored">14</span>
<div class="clear"></div>
<span class="day">15</span>
<span class="day">16</span>
<span class="day">17</span>
<span class="day">18</span>
<span class="day">19</span>
<span class="day">20</span>
<span class="day colored">21</span>
<div class="clear"></div>
<span class="day">22</span>
<span class="day">23</span>
<span class="day">24</span>
<span class="day">25</span>
<span class="day">26</span>
<span class="day">27</span>
<span class="day colored">28</span>
<div class="clear"></div>
<span class="day">29</span>
<span class="day">30</span>
<span class="day">31</span>
jameswassinger
  • 373
  • 3
  • 9
  • Thanks for your answer James! Unfortunately, it doesn't quite answer my dilemma because it uses more code (your "clear" class) in the markup (HTML). I'm looking for an answer that involves injecting row breaks with CSS. – Eric Hepperle - CodeSlayer2010 Jan 24 '17 at 16:40