无法弄清楚为什么布局不正确

时间:2016-10-18 07:59:26

标签: javascript html css

我正在构建日历网页。在通过javascript插入新创建的元素之前,布局很好。但是在添加javascript代码之后,第一行中的数字开始表现得很奇怪。我无法弄清楚为什么。这是代码。

the screenshot

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDate = new Date(y, m, 1);
var lastDate = new Date(y, m + 1, 0);

var firstDay = firstDate.getDay();

var days  = document.querySelector(".days");
for (var i = 0; i < helper(firstDay); i++){
   var spaceContainer = document.createElement('LI');
   var spaceholder = document.createTextNode(" ");
   spaceContainer.appendChild(spaceholder); 
   days.insertBefore(spaceContainer, days.firstChild);
}


// get the number right 
function helper(n){
	if (n === 0){
		return 6;
	} else {
		return n - 1;
	}
}
* {box-sizing:border-box;}
ul {list-style-type: none;}
body {font-family: Verdana,sans-serif;}

.month {
    padding: 70px 25px;
    width: 100%;
    background: #1abc9c;
}

.month ul {
    margin: 0;
    padding: 0;
}

.month ul li {
    color: white;
    font-size: 20px;
    text-transform: uppercase;
    letter-spacing: 3px;
}

.month .prev {
    float: left;
    padding-top: 10px;
}

.month .next {
    float: right;
    padding-top: 10px;
}

.weekdays {
    margin: 0;
    padding: 10px 0;
    background-color: #ddd;
}

.weekdays li {
    display: inline-block;
    width: 13.6%;
    color: #666;
    text-align: center;
}

.days {
    padding: 10px 0;
    background: #eee;
    margin: 0;
}

.days li {
    list-style-type: none;
    display: inline-block;
    width: 13.6%;
    text-align: center;
    margin-bottom: 5px;
    font-size:12px;
    color: #777;
}

.days li .active {
    padding: 5px;
    background: #1abc9c;
    color: white !important
}

/* Add media queries for smaller screens */
@media screen and (max-width:720px) {
    .weekdays li, .days li {width: 13.1%;}
}

@media screen and (max-width: 420px) {
    .weekdays li, .days li {width: 12.5%;}
    .days li .active {padding: 2px;}
}

@media screen and (max-width: 290px) {
    .weekdays li, .days li {width: 12.2%;}
}
<h1>CSS Calendar</h1>

<div class="month">
  <ul>
    <li class="prev">❮</li>
    <li class="next">❯</li>
    <li style="text-align:center">
      August<br>
      <span style="font-size:18px">2016</span>
    </li>
  </ul>
</div>

<ul class="weekdays">
  <li>Mo</li>
  <li>Tu</li>
  <li>We</li>
  <li>Th</li>
  <li>Fr</li>
  <li>Sa</li>
  <li>Su</li>
</ul>

<ul class="days">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li><span class="active">10</span></li>
  <li>11</li>
  <li>12</li>
  <li>13</li>
  <li>14</li>
  <li>15</li>
  <li>16</li>
  <li>17</li>
  <li>18</li>
  <li>19</li>
  <li>20</li>
  <li>21</li>
  <li>22</li>
  <li>23</li>
  <li>24</li>
  <li>25</li>
  <li>26</li>
  <li>27</li>
  <li>28</li>
  <li>29</li>
  <li>30</li>
  <li>31</li>
</ul>

3 个答案:

答案 0 :(得分:1)

这是因为你在列表项上使用的display: inline-block;和第一行中没有文字的li项。
我建议你尝试使用float: left;,在这种情况下不要忘记clearfix。请记住,您还需要对周末项目使用float和clearfix,这样您才能正确对齐它们。

以下是codepen

作为另一种选择,您可能想尝试使用flexbox。这是另一个codepen

在这种情况下不需要clerfix。只需在工作日和课程日中删除ul中的display: inline-block,并将这些样式应用于现有代码。

    .days {
  display: flex;
  flex-wrap: wrap;
}

.weekdays  {
  display: flex;
}

答案 1 :(得分:1)

您在列表项上使用inline-block。这种风格inline-block创造了一些额外的空间。有很多方法可以删除这个空间。

一种方法是在父级上设置font-size: 0letter-spacing: -4px并重置子级,如下所示。

.days {
    letter-spacing: -4px;
    font-size: 0;
}
.days li {
    display: inline-block;
    letter-spacing: 0;
    font-size: 12px;
}

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDate = new Date(y, m, 1);
var lastDate = new Date(y, m + 1, 0);

var firstDay = firstDate.getDay();

var days  = document.querySelector(".days");
for (var i = 0; i < helper(firstDay); i++){
   var spaceContainer = document.createElement('LI');
   var spaceholder = document.createTextNode(" ");
   spaceContainer.appendChild(spaceholder); 
   days.insertBefore(spaceContainer, days.firstChild);
}


// get the number right 
function helper(n){
	if (n === 0){
		return 6;
	} else {
		return n - 1;
	}
}
* {box-sizing:border-box;}
ul {list-style-type: none;}
body {font-family: Verdana,sans-serif;}

.month {
    padding: 70px 25px;
    width: 100%;
    background: #1abc9c;
}

.month ul {
    margin: 0;
    padding: 0;
}

.month ul li {
    color: white;
    font-size: 20px;
    text-transform: uppercase;
    letter-spacing: 3px;
}

.month .prev {
    float: left;
    padding-top: 10px;
}

.month .next {
    float: right;
    padding-top: 10px;
}

.weekdays {
    margin: 0;
    padding: 10px 0;
    background-color: #ddd;
}

.weekdays li {
    display: inline-block;
    width: 13.6%;
    color: #666;
    text-align: center;
}

.days {
    padding: 10px 0;
    background: #eee;
    letter-spacing: -4px;
    font-size: 0;
    margin: 0;
}

.days li {
    list-style-type: none;
    display: inline-block;
    letter-spacing: 0;
    width: 13.6%;
    text-align: center;
    margin-bottom: 5px;
    font-size:12px;
    color: #777;
}

.days li .active {
    padding: 5px;
    background: #1abc9c;
    color: white !important
}

/* Add media queries for smaller screens */
@media screen and (max-width:720px) {
    .weekdays li, .days li {width: 13.1%;}
}

@media screen and (max-width: 420px) {
    .weekdays li, .days li {width: 12.5%;}
    .days li .active {padding: 2px;}
}

@media screen and (max-width: 290px) {
    .weekdays li, .days li {width: 12.2%;}
}
<h1>CSS Calendar</h1>

<div class="month">
  <ul>
    <li class="prev">❮</li>
    <li class="next">❯</li>
    <li style="text-align:center">
      August<br>
      <span style="font-size:18px">2016</span>
    </li>
  </ul>
</div>

<ul class="weekdays">
  <li>Mo</li>
  <li>Tu</li>
  <li>We</li>
  <li>Th</li>
  <li>Fr</li>
  <li>Sa</li>
  <li>Su</li>
</ul>

<ul class="days">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li><span class="active">10</span></li>
  <li>11</li>
  <li>12</li>
  <li>13</li>
  <li>14</li>
  <li>15</li>
  <li>16</li>
  <li>17</li>
  <li>18</li>
  <li>19</li>
  <li>20</li>
  <li>21</li>
  <li>22</li>
  <li>23</li>
  <li>24</li>
  <li>25</li>
  <li>26</li>
  <li>27</li>
  <li>28</li>
  <li>29</li>
  <li>30</li>
  <li>31</li>
</ul>

答案 2 :(得分:1)

display: inline-block替换为float:left,并将clearfix类添加到ul

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDate = new Date(y, m, 1);
var lastDate = new Date(y, m + 1, 0);

var firstDay = firstDate.getDay();

var days  = document.querySelector(".days");
for (var i = 0; i < helper(firstDay); i++){
   var spaceContainer = document.createElement('LI');
   var spaceholder = document.createTextNode(" ");
   spaceContainer.appendChild(spaceholder); 
   days.insertBefore(spaceContainer, days.firstChild);
}


// get the number right 
function helper(n){
	if (n === 0){
		return 6;
	} else {
		return n - 1;
	}
}
* {box-sizing:border-box;}
ul {list-style-type: none;}
body {font-family: Verdana,sans-serif;}

.month {
    padding: 70px 25px;
    width: 100%;
    background: #1abc9c;
}

.month ul {
    margin: 0;
    padding: 0;
}

.month ul li {
    color: white;
    font-size: 20px;
    text-transform: uppercase;
    letter-spacing: 3px;
}

.month .prev {
    float: left;
    padding-top: 10px;
}

.month .next {
    float: right;
    padding-top: 10px;
}

.weekdays {
    margin: 0;
    padding: 10px 0;
    background-color: #ddd;
}

.weekdays li {
    float: left;
    width: 13.6%;
    color: #666;
    text-align: center;
}

.days {
    padding: 10px 0;
    background: #eee;
    margin: 0;
}

.days li {
    list-style-type: none;
    float: left;
    width: 13.6%;
    text-align: center;
    margin-bottom: 5px;
    font-size:12px;
    color: #777;
}

.clearfix:after {
   visibility: hidden;
   display: block;
   font-size: 0;
   content: " ";
   clear: both;
   height: 0;
}

.days li .active {
    padding: 5px;
    background: #1abc9c;
    color: white !important
}

/* Add media queries for smaller screens */
@media screen and (max-width:720px) {
    .weekdays li, .days li {width: 13.1%;}
}

@media screen and (max-width: 420px) {
    .weekdays li, .days li {width: 12.5%;}
    .days li .active {padding: 2px;}
}

@media screen and (max-width: 290px) {
    .weekdays li, .days li {width: 12.2%;}
}
<h1>CSS Calendar</h1>

<div class="month">
  <ul>
    <li class="prev">❮</li>
    <li class="next">❯</li>
    <li style="text-align:center">
      August<br>
      <span style="font-size:18px">2016</span>
    </li>
  </ul>
</div>

<ul class="weekdays clearfix">
  <li>Mo</li>
  <li>Tu</li>
  <li>We</li>
  <li>Th</li>
  <li>Fr</li>
  <li>Sa</li>
  <li>Su</li>
</ul>

<ul class="days clearfix">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li><span class="active">10</span></li>
  <li>11</li>
  <li>12</li>
  <li>13</li>
  <li>14</li>
  <li>15</li>
  <li>16</li>
  <li>17</li>
  <li>18</li>
  <li>19</li>
  <li>20</li>
  <li>21</li>
  <li>22</li>
  <li>23</li>
  <li>24</li>
  <li>25</li>
  <li>26</li>
  <li>27</li>
  <li>28</li>
  <li>29</li>
  <li>30</li>
  <li>31</li>
</ul>