8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

元素的对齐问题

Jaspper15325 1月前

64 0

我需要一些帮助,因为我在 HTML 和 CSS 方面完全是新手。我正在尝试创建类似这样的内容:我可以生成整个容器本身,但博客标题的位置,

我需要一些帮助,因为在 HTML 和 CSS 方面我完全是一个新手。
我正在尝试创建类似的东西:

img

我可以生成整个容器本身,但博客标题、博客发布日期、博客摘要和阅读更多按钮的布局格式都很糟糕。到目前为止,我唯一能正确放置的是 img 容器。

下面的图片是我得到的结果:

img

  body {
  margin: 0;
  font-family: "Bai Jamjuree", sans-serif;
  /* Adjust with the exact font if found */
}


/* Style for Blog Container */

.blog-container {
  width: 1240px;
  overflow: auto;
  margin-left: auto;
  position: relative;
  left: -3px;
  margin-right: auto;
  max-width: 94%;
  /* Responsive for smaller screens */
  padding: 5px 20px;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 100px;
  box-sizing: border-box;
}

.blog-post {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  margin-top: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ddd;
}

.blog-thumbnail {
  flex: 1;
}

.blog-thumbnail img {
  width: 150px;
  height: 150px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-right: 20px;
  object-fit: cover;
}

.blog-post {
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  max-width: 1200px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  text-align: left;
}

.blog-post h2 {
  font-size: 1.2em;
  margin-bottom: 20px;
  color: #333;
}

.blog-post p {
  font-size: 1em;
  color: #666;
  margin-bottom: 100px;
}

.blog-post a {
  color: #1e7cd6;
  text-decoration: none;
  font-weight: bold;
}

.blog-post a:hover {
  color: #104e8b;
}


/* Responsive Styles */

@media (max-width: 768px) {
  .blog-search-sort {
    flex-direction: column;
    gap: 10px;
  }
  .blog-post {
    width: 95%;
  }
}

.bai-jamjuree-extralight {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 200;
  font-style: normal;
}

.bai-jamjuree-light {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 300;
  font-style: normal;
}

.bai-jamjuree-regular {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 400;
  font-style: normal;
}

.bai-jamjuree-medium {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 500;
  font-style: normal;
}

.bai-jamjuree-semibold {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 600;
  font-style: normal;
}

.bai-jamjuree-bold {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 700;
  font-style: normal;
}

.bai-jamjuree-extralight-italic {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 200;
  font-style: italic;
}

.bai-jamjuree-light-italic {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 300;
  font-style: italic;
}

.bai-jamjuree-regular-italic {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 400;
  font-style: italic;
}

.bai-jamjuree-medium-italic {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 500;
  font-style: italic;
}

.bai-jamjuree-semibold-italic {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 600;
  font-style: italic;
}

.bai-jamjuree-bold-italic {
  font-family: "Bai Jamjuree", sans-serif;
  font-weight: 700;
  font-style: italic;
}

a {
  text-decoration: none;
}

a.fill-div {
  display: block;
  height: 100%;
  width: 100%;
  text-decoration: none;
)
<div class="blog-container" id="blogContainer">
  <!-- Example Blog Post -->
  <div class="blog-post" data-date="2024-08-30" onclick="';" style="cursor: pointer;">
    <div class="blog-thumbnail">
      <img src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg" alt="Thumbnail" />
    </div>
    <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
    <p>August 30, 2024</p>
    <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings</p>
    <a href="">Read more</a>
  </div>
</div>
帖子版权声明 1、本帖标题:元素的对齐问题
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Jaspper15325在本站《css》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 我认为,除非我们对您在一般情况下想要实现的布局有更多了解,否则我们无法给出完整的答案。首先,您的图片显示了图像高度和相邻文本的整齐匹配,如果 a) 没有足够的文本来垂直“填充”第二列,您希望它看起来像什么?b) 这些确实是列吗(如果文本太多,那么它会保留在该列中还是会流到图像下方)。3 列的宽度是否有限制?确实,它是 3 个固定宽度的列吗?

  • Whip 1月前 0 只看Ta
    引用 3

    我修正了 HTML CSS ,即 onclick="';" )

    也删除了 .blog-thumbnail { flex: 1; }

    CSS 修复了其他一些问题

    添加 div classes 一些 HTML .

    为现有的和新的 CSS 写了一些 classes .

    您的当前版本 code 不适用于所有屏幕尺寸。请同时进行调整。我已修复您当前的问题,并尝试使其与您添加的输出相似。

    更新内容如下 code

    body {
      margin: 0;
      font-family: "Bai Jamjuree", sans-serif;
      /* Adjust with the exact font if found */
    }
    
    /* Style for Blog Container */
    
    .blog-container {
      width: 1240px;
      overflow: auto;
      margin-left: auto;
      position: relative;
      left: -3px;
      margin-right: auto;
      max-width: 94%;
      /* Responsive for smaller screens */
      padding: 5px 20px;
      background-color: white;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      margin-bottom: 100px;
      box-sizing: border-box;
    }
    
    .blog-thumbnail img {
      width: 150px;
      height: 150px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      margin-right: 20px;
      object-fit: cover;
    }
    
    .blog-post {
      display: flex;
      justify-content: space-between;
      align-items: start;
      height: 150px;
      margin-bottom: 20px;
      margin-top: 20px;
      background-color: white;
      border: 1px solid #ddd;
      border-radius: 8px;
      padding: 20px;
      max-width: 1200px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease, box-shadow 0.3s ease;
    }
    
    .blog-img-and-text {
      display: flex;
    }
    
    .blog-post h2 {
      font-size: 1.2em;
      margin: 0 0 15px 0;
      color: #333;
    }
    
    .blog-post p {
      font-size: 1em;
      color: #666;
      margin: 0;
    }
    
    .blog-post a {
      color: #1e7cd6;
      text-decoration: none;
      font-weight: bold;
    }
    
    .blog-post a:hover {
      color: #104e8b;
    }
    
    /* Responsive Styles */
    
    @media (max-width: 768px) {
      .blog-search-sort {
        flex-direction: column;
        gap: 10px;
      }
      .blog-post {
        width: 95%;
      }
    }
    
    .bai-jamjuree-extralight {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 200;
      font-style: normal;
    }
    
    .bai-jamjuree-light {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 300;
      font-style: normal;
    }
    
    .bai-jamjuree-regular {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 400;
      font-style: normal;
    }
    
    .bai-jamjuree-medium {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 500;
      font-style: normal;
    }
    
    .bai-jamjuree-semibold {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 600;
      font-style: normal;
    }
    
    .bai-jamjuree-bold {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 700;
      font-style: normal;
    }
    
    .bai-jamjuree-extralight-italic {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 200;
      font-style: italic;
    }
    
    .bai-jamjuree-light-italic {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 300;
      font-style: italic;
    }
    
    .bai-jamjuree-regular-italic {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 400;
      font-style: italic;
    }
    
    .bai-jamjuree-medium-italic {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 500;
      font-style: italic;
    }
    
    .bai-jamjuree-semibold-italic {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 600;
      font-style: italic;
    }
    
    .bai-jamjuree-bold-italic {
      font-family: "Bai Jamjuree", sans-serif;
      font-weight: 700;
      font-style: italic;
    }
    
    a {
      text-decoration: none;
    }
    
    a.fill-div {
      display: block;
      height: 100%;
      width: 100%;
      text-decoration: none;
    }
    
    .date-and-readmore {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 100%;
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="style.css" />
      </head>
      <body>
        <div class="blog-container" id="blogContainer">
          <div
            class="blog-post"
            data-date="2024-08-30"
            onclick=""
            style="cursor: pointer"
          >
            <div class="blog-img-and-text">
              <div class="blog-thumbnail">
                <img
                  src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg"
                  alt="Thumbnail"
                />
              </div>
    
              <div>
                <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
                <p>
                  Unlock the Power of Aerial Views & Attract More Buyers to Your
                  Listings
                </p>
                <p>
                  Unlock the Power of Aerial Views & Attract More Buyers to Your
                  Listings
                </p>
              </div>
            </div>
    
            <div class="date-and-readmore">
              <p>August 30, 2024</p>
              <a href="">Read more</a>
            </div>
          </div>
        </div>
      </body>
    </html>
  • CSS-Grid 在这里是理想的选择。

    * {
      margin: 0;
      padding: 0;
      min-width: 0;
      min-height: 0;
      box-sizing: border-box;
    }
    
     ::before,
     ::after {
      box-sizing: inherit;
    }
    
    
    /* Style for Blog Container */
    
    .blog-container {
      max-width: 1240px;
      overflow: auto;
      margin-left: auto;
      position: relative;
      margin-right: auto;
      padding: 20px;
      /* Responsive for smaller screens */
      padding: 5px 20px;
      background-color: white;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      margin-bottom: 100px;
      box-sizing: border-box;
    }
    
    .blog-post {
      display: grid;
      grid-template-columns: auto 1fr auto;
      grid-template-rows: auto 1fr;
      gap: 20px;
      padding: 10px;
      border-bottom: 1px solid #ddd;
    }
    
    .blog-thumbnail {
      grid-row: span 2;
    }
    
    .blog-thumbnail img {
      width: 150px;
      height: 150px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      object-fit: cover;
    }
    
    .blog-post {
      background-color: white;
      border: 1px solid #ddd;
      border-radius: 8px;
      max-width: 1200px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease, box-shadow 0.3s ease;
      text-align: left;
    }
    
    .blog-post h2 {
      font-size: 1.2em;
      color: #333;
    }
    
    .blog-post p {
      font-size: 1em;
      color: #666;
    }
    
    .blog-post a {
      color: #1e7cd6;
      text-decoration: none;
      font-weight: bold;
      align-self: end;
    }
    
    .blog-post a:hover {
      color: #104e8b;
    }
    <div class="blog-container" id="blogContainer">
      <!-- Example Blog Post -->
      <div class="blog-post" data-date="2024-08-30" onclick="';" style="cursor: pointer;">
        <div class="blog-thumbnail">
          <img src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg" alt="Thumbnail" />
        </div>
        <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
        <p>August 30, 2024</p>
        <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings Lorem ipsum dolor sit amet consectetur adipisicing elit. Incidunt iste rerum perferendis ab voluptate optio repudiandae at, ratione inventore laboriosam officia? </p>
        <a href="">Read more</a>
      </div>
    </div>
  • 您应该稍微改变一下结构并使用语义标签。

    在您的图像中,您有 3 列可以使用 Flexbox 彼此对齐。
    语义正确的方法是使用 aside 作为图像和日期的容器并阅读更多列。您的博客文章的主要列将是 article .

    您可以 article 使用以下方法让列跨越整个可用宽度 flex-grow: 1 .

    要让日期和阅读更多列达到您想要的布局,您必须使用 flexbox flex-direction: column 。这样,您可以添加 margin-top: auto \'阅读更多\' 链接以将其推到最底部。

    PS:您不应该 margins 在 flexbox 中将列分开。 gap 而是使用!

    section.blog-post {
      display: flex;
      gap: 1.25em;
      background-color: white;
      border: 1px solid #ddd;
      border-radius: 8px;
      padding: 20px;
      max-width: 1200px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease, box-shadow 0.3s ease;
      text-align: left;
      
      aside.blog-thumbnail img {
        width: 150px;
        height: 150px;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        object-fit: cover;
      }
      
      article {
        flex-grow: 1;
        h2 {
          font-size: 1.2em;
          margin-bottom: 20px;
          color: #333;
          margin-top: 0;
        }
        p {
          font-size: 1em;
          color: #666;
          margin-bottom: 100px;
        }
      }
      
      aside.blog-date {
        display: flex;
        flex-direction: column;
        time {
          color: #666;
        }
        a {
          color: #1e7cd6;
          text-decoration: none;
          font-weight: bold;
          margin-top: auto;
        }
      }
    }
    <main class="blog-container" id="blogContainer">
      <!-- Example Blog Post -->
      <section class="blog-post" data-date="2024-08-30" onclick="';" style="cursor: pointer;">
        <aside class="blog-thumbnail">
          <img src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg" alt="Thumbnail" />
        </aside>
        <article>
          <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
          <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings</p>
        </article>
        <aside class="blog-date">
          <time datetime="2024-08-30">August 30, 2024</time>   
          <a href="">Read more</a>
        </aside>
      </section>
    </main>
  • 没有添加媒体查询,因为它们与问题本身无关。对齐与响应性无关。好的答案会专注于问题本身,并删除其他内容以保持代码简短。

  • 因为 Op 显然没有将代码缩减到必要的部分。问题不在于如何使他的代码具有响应性,而在于如何达到预期的布局。预期的布局完全取决于 3 列。

  • 您还需要一个用于排版元素的包装器。

    body {
      margin: 0;
      font-family: "Bai Jamjuree", sans-serif;
      /* Adjust with the exact font if found */
    }
    
    /* Style for Blog Container */
    
    .blog-container {
      width: 1240px;
      overflow: auto;
      margin-left: auto;
      position: relative;
      left: -3px;
      margin-right: auto;
      max-width: 94%;
      /* Responsive for smaller screens */
      padding: 5px 20px;
      background-color: white;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      margin-bottom: 100px;
      box-sizing: border-box;
    }
    
    .blog-thumbnail img {
      width: 150px;
      height: 150px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      margin-right: 20px;
      object-fit: cover;
    }
    
    .blog-post {
      background-color: white;
      border-radius: 8px;
      padding: 20px;
      max-width: 1200px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease, box-shadow 0.3s ease;
      text-align: left;
      display: flex;
      margin-bottom: 20px;
      margin-top: 20px;
      padding-bottom: 20px;
      border-bottom: 1px solid #ddd;
    }
    
    .blog-post h2 {
      font-size: 1.2em;
      margin-bottom: 20px;
      color: #333;
    }
    
    .blog-post p {
      font-size: 1em;
      color: #666;
    }
    
    .blog-post a {
      color: #1e7cd6;
      text-decoration: none;
      font-weight: bold;
    }
    
    .blog-post a:hover {
      color: #104e8b;
    }
    
    a {
      text-decoration: none;
    }
    
    /* Responsive Styles */
    
    @media (max-width: 768px) {
      .blog-post {
        flex-direction: column;
        gap: 10px;
      }
    }
    <div class="blog-container" id="blogContainer">
      <!-- Example Blog Post -->
      <div class="blog-post" data-date="2024-08-30" onclick="';" style="cursor: pointer;">
        <div class="blog-thumbnail">
          <img src="https://d150u0abw3r906.cloudfront.net/wp-content/uploads/2021/09/aerial-shot.jpg" alt="Thumbnail" />
        </div>
        <div>
          <h2>How Drone Photography Can Elevate Your Real Estate Listings</h2>
          <p>August 30, 2024</p>
          <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings</p>
          <p>Unlock the Power of Aerial Views & Attract More Buyers to Your Listings</p>
    
          <a href="">Read more</a>
        </div>
      </div>
    </div>

    此外,您的代码中有很多难闻的 CSS,例如 这样的神奇数字 94% 。尝试让您的布局流畅,而不是硬编码固定值。这篇文章可能对您有所帮助: https://csswizardry.com/2012/11/code-smells-in-css/

  • 虽然我同意 94% 并不干净,但这不是一个固定值。百分比是响应值,符合 RWD 规则。

  • 给定的代码片段没有提供所需的布局。例如,在我的笔记本电脑上,整个页面上的日期位置不正确。

  • AJJ 1月前 0 只看Ta
    引用 11

    再读一遍。我同意它不干净。但是说百分比是固定值是不正确的。它们不是,它们是响应式的。

返回
作者最近主题: