実現方法
CSSのみで実現可能です。
親要素の大きさを変えるのではなく、子要素の大きさを1つ辺り決める事で解決します。
nth-child()
などはとても便利なので良く調べる事を勧めます。
HTML
HTML
1 <nav>
2 <ul id="pagenav">
3 <li class="item"><a href="hogehoge.php">メニュー1</a></li>
4 <li class="item"><a href="hogehoge.php">メニュー2</a></li>
5 <li class="item"><a href="hogehoge.php">メニュー3</a></li>
6 </ul>
7 </nav>
CSS
CSS
1 * {
2 padding: 0;
3 margin: 0;
4 }
5
6 #pagenav {
7 width: auto;
8 display: flex;
9 list-style: none;
10 text-align: center;
11 }
12
13 /* 1つの場合 */
14 .item:only-child {
15 width: 100%;
16 }
17
18 /* 2つの場合 */
19 .item:first-child:nth-last-child(2),
20 .item:first-child:nth-last-child(2) ~ .item {
21 width: calc(100%/2);
22 }
23
24 /* 3つの場合 */
25 .item:first-child:nth-last-child(3),
26 .item:first-child:nth-last-child(3) ~ .item {
27 width: calc(100%/3);
28 }
29
30 /* 4つの場合 */
31 .item:first-child:nth-last-child(4),
32 .item:first-child:nth-last-child(4) ~ .item {
33 width: calc(100%/4);
34 }
35
36 /* 5つの場合 */
37 .item:first-child:nth-last-child(5),
38 .item:first-child:nth-last-child(5) ~ .item {
39 width: calc(100%/5);
40 }
41
42 /* 6つの場合 */
43 .item:first-child:nth-last-child(6),
44 .item:first-child:nth-last-child(6) ~ .item {
45 width: calc(100%/6);
46 }
参考文献
下記のサイトを参考にすると分かりやすいと思います。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/22 15:39