Div与定位问题:评论框无法对齐至评分下方求解
Hey there! Let's work through this layout problem you're stuck on. Aligning elements correctly and getting that comment box to sit neatly below the rating are super common frustrations, but we can sort this out with a clear structure and a few CSS fixes.
First, let's look at the code snippet you shared (I'll fill in the truncated part for context):
* { box-sizing: border-box; } body { background-image: url("background.png"); font-family: Tahoma; font-size: 8px; } .banner { margin-left: auto; margin-right: auto; /* Assuming this was cut off */ }
The main issues here are likely:
- Missing structured containers to group related elements (rating + comment box)
- No explicit layout rules to enforce vertical stacking of the comment box below the rating
- Potentially unbalanced margin/padding causing alignment drift
Working Example Solution
Here's a complete, structured implementation that fixes alignment and ensures the comment box sits below the rating, with background colors to visualize div boundaries:
<!DOCTYPE html> <html> <head> <style> * { box-sizing: border-box; } body { background-image: url("background.png"); font-family: Tahoma; font-size: 12px; /* Adjusted from 8px for readability - tweak as needed */ max-width: 700px; margin: 0 auto; padding: 20px; } /* Banner styling */ .banner { background: #e8f4f8; padding: 15px; text-align: center; margin-bottom: 20px; border-radius: 4px; } /* Rating container - groups rating elements for alignment */ .rating-section { background: #f0f0f0; padding: 12px; margin-bottom: 15px; border-radius: 4px; /* Horizontal alignment for rating elements */ display: flex; align-items: center; gap: 10px; } .rating-stars { color: #ffc107; font-size: 18px; } /* Comment box container - sits directly below rating */ .comment-section { background: #f0f0f0; padding: 12px; border-radius: 4px; } .comment-box { width: 100%; height: 100px; padding: 8px; border: 1px solid #ddd; border-radius: 4px; margin-top: 8px; font-family: inherit; font-size: inherit; } </style> </head> <body> <div class="banner">Your Banner Content Here</div> <div class="rating-section"> <span class="rating-stars">★★★★☆</span> <span>User Rating: 4/5</span> </div> <div class="comment-section"> <label for="user-comment">Leave your comment:</label> <textarea id="user-comment" class="comment-box"></textarea> </div> </body> </html>
Key Fixes Explained
- Structured Containers: We use
.rating-sectionand.comment-sectionto group related elements, making alignment and positioning predictable. - Flexbox for Rating Alignment:
display: flexin.rating-sectionensures stars and text stay neatly aligned horizontally without floating elements. - Natural Vertical Stacking: By placing
.comment-sectiondirectly after.rating-sectionin the HTML, and usingmargin-bottomon the rating container, we guarantee the comment box sits below the rating. - Centered Content:
max-width+margin: 0 autoon thebodykeeps your layout from stretching too wide and centers it on the page.
You can adjust colors, padding, font sizes, and banner content to match your original example exactly!
内容的提问来源于stack exchange,提问作者Karos papermoon

