如何用HTML/CSS美观展示数据库中1-5个可变数据?
Hey there! I see your current layout is a bit messy with inline styles and generic elements—let's refactor it to be cleaner, more semantic, and responsive. Here's a better approach using semantic HTML, Flexbox for layout, and reusable CSS classes:
The Problem with Your Current Code
Your inline styles make maintenance a headache, the rotated headers are misaligned, and using spans for list items isn't semantic. Plus, empty items would still show up, which isn't ideal for your variable 1-5 data points.
Solution: Semantic HTML + Flexbox Layout
Step 1: HTML Structure (Semantic & Clean)
We'll use semantic elements like <section> for the container, <h3> for headers, and <ul>/<li> for lists—this makes the markup more readable and accessible.
<section class="dual-list-container"> <!-- Left List --> <div class="list-column"> <h3 class="rotated-header"> List1: {{ en.form.nbTotalCon }} </h3> <ul class="item-list"> <li class="list-item">{{ en.form.part1 }}</li> <li class="list-item">{{ en.form.part2 }}</li> <li class="list-item">{{ en.form.part3 }}</li> <li class="list-item">{{ en.form.part4 }}</li> <li class="list-item">{{ en.form.part5 }}</li> </ul> </div> <!-- Right List --> <div class="list-column"> <h3 class="rotated-header"> Projects: {{ en.form.nbTotalProj }} </h3> <ul class="item-list"> <li class="list-item">{{ en.form.nomProj1 }}</li> <li class="list-item">{{ en.form.nomProj2 }}</li> <li class="list-item">{{ en.form.nomProj3 }}</li> <li class="list-item">{{ en.form.nomProj4 }}</li> <li class="list-item">{{ en.form.nomProje5 }}</li> </ul> </div> </section>
Step 2: CSS (Flexible & Responsive)
We'll use Flexbox to align columns, properly position rotated headers, add alternating list item backgrounds, and automatically hide empty items.
/* Main container for both columns */ .dual-list-container { display: flex; gap: 2rem; /* Space between columns */ margin: 5% auto; max-width: 1200px; /* Prevent over-stretching on wide screens */ padding: 0 1rem; } /* Each column takes equal space */ .list-column { flex: 1; display: flex; align-items: flex-start; gap: 1.5rem; } /* Rotated header styling */ .rotated-header { background-color: gray; color: white; padding: 1rem; margin: 0; width: 50px; /* Fixed width after rotation */ height: 175px; display: flex; align-items: center; justify-content: center; transform: rotate(-90deg); transform-origin: center center; text-align: center; } /* List base styling */ .item-list { list-style: none; padding: 0; margin: 0; flex: 1; } /* List items with alternating backgrounds */ .list-item { padding: 0.5rem 1rem; margin-bottom: 0.3rem; } .list-item:nth-child(odd) { background-color: darkgray; } /* Hide empty list items automatically */ .list-item:empty { display: none; } /* Responsive adjustment for mobile */ @media (max-width: 768px) { .dual-list-container { flex-direction: column; } .rotated-header { transform: rotate(0deg); width: auto; height: auto; } .list-column { flex-direction: column; align-items: flex-start; } }
Key Improvements:
- Semantic HTML:
<ul>/<li>makes your list structure clear for browsers and other developers. - Flexbox Alignment: Columns stay evenly spaced and aligned, even if content lengths differ.
- Automatic Empty Item Hiding: The
:emptypseudo-class hides any list items without data—perfect for your 1-5 variable entries. - Alternating Backgrounds:
nth-child(odd)handles the stripe pattern automatically, no manual class additions needed. - Responsive Design: Switches to a vertical stack on mobile with non-rotated headers for better usability.
- Maintainable CSS: Reusable classes mean you can tweak styles across the entire layout in one place.
This will give you a clean, professional layout that adapts to your variable data count and works well on all screen sizes!
内容的提问来源于stack exchange,提问作者nassim nassim

