過去に同じ内容の記事を書きましたが、内容が古くなっているので新しく作り直しました。
新着情報などでよく見る、日付・カテゴリ・タイトルを並べて表示する形式です。
スマホで見た場合は、タイトルが改行されます。
◆まずはデモページをご覧ください。
デモページを見る
HTML
<h2 class="news_title">お知らせ</h2>
<div class="news_list">
<ul>
<li>
<span class="date">2021.00.00</span>
<a href="#"><span class="category">長いカテゴリ</span></a>
<span class="news_text"><a href="#">長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。</a></span>
</li>
<li>
<span class="date">2021.00.00</span>
<a href="#"><span class="category">カテゴリ</span></a>
<span class="news_text"><a href="#">テキストが入ります。テキストが入ります。</a></span>
</li>
<li>
<span class="date">2021.00.00</span>
<a href="#"><span class="category">長いカテゴリ</span></a>
<span class="news_text"><a href="#">長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。長いテキストが入ります。</a></span>
</li>
<li>
<span class="date">2021.00.00</span>
<a href="#"><span class="category">カテゴリ</span></a>
<span class="news_text"><a href="#">テキストが入ります。テキストが入ります。</a></span>
</li>
</ul>
</div>
<p class="news_btn"><a href="#" class="btn">一覧を見る</a></p>
</section>
HTMLは「ul」「li」で作っています。
CSS
width: 90%;
max-width:1000px;
margin: 50px auto;
}
.news_title {
color: #24b224;
text-align: center;
font-size: 20px;
font-weight: bold;
}
.news_list {
margin: 20px 0;
border-top: 1px dashed #24b224;
}
.news_list li {
padding: 15px 5px;
border-bottom: 1px dashed #2eb087;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
.date {
font-size: 16px;
margin-right: 10px;
}
.category {
display: inline-block;
color: #fff;
width: 120px;
font-size: 14px;
text-align: center;
margin-right: 10px;
padding: 2px 10px;
background-color: #75b46e;
}
.news_text a {
color: #333;
}
.news_btn {
text-align: center;
margin-top: 50px;
}
.news_btn a {
color: #75b46e;
}
.btn {
color: #75b46e;
padding: 10px 50px;
background: #fff;
border: 2px solid #75b46e;
border-radius: 50px;
transition: all 0.5s;
}
.btn:hover {
color: #fff;
background: #75b46e;
}
.btn:after {
font-family: 'Font Awesome 5 Free';
content: "\f0da";
font-weight: 900;
padding-left: 10px;
}
@media screen and (max-width: 559px) {
.news_container {
width: 100%;
padding: 0 20px;
}
.news_text a {
display: block;
margin-top: 10px;
text-overflow: ellipsis;
overflow: hidden;
}
}
横幅など、サイズを指定している個所は任意で変更してください。単位は「px」にしています。
長いテキストになった場合のために「text-overflow: ellipsis;」を指定しています。
これは、表示領域を超えた場合の表示形式で、今回の「ellipsis」は省略記号の「…」を表示します。
スマホでは、タイトル部分の「span」を「display: block;」にすることで、改行されるようになります。
メディアクエリのサイズは任意で変更してください。
デモページのファイル一式をご希望の方はダウンロードできますので、よろしければどうぞ。
【 css47.zip(2KB) 】