You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Div与定位问题:评论框无法对齐至评分下方求解

Fixing Layout Alignment & Comment Box Placement Issues

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-section and .comment-section to group related elements, making alignment and positioning predictable.
  • Flexbox for Rating Alignment: display: flex in .rating-section ensures stars and text stay neatly aligned horizontally without floating elements.
  • Natural Vertical Stacking: By placing .comment-section directly after .rating-section in the HTML, and using margin-bottom on the rating container, we guarantee the comment box sits below the rating.
  • Centered Content: max-width + margin: 0 auto on the body keeps 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:47:53