【问题标题】:Horizontally lining up images and text on a Gatsby web-page在 Gatsby 网页上水平排列图像和文本
【发布时间】:2020-12-28 15:09:19
【问题描述】:

作为 uni 项目的一部分,我的团队需要为客户开发一个事件列表网页。我在 adobe illustrator 中模拟了一个客户喜欢的设计,并且在过去一天左右的时间里试图将模型转换为代码,以便它在网页上看起来与在 illustrator 中一样。我尝试了几种不同的列表布局方式,但如果我将它放在列表元素中,图像就会消失。

我正在使用 Gatsby 和基本插件

  • gatsby-plugin-styled-components

const eventCalendar = () => {
  return (
    <Layout>
      <section className={styles.eventCalendar}>
        <li>
          <Image />
        </li>
        <ul>
          <ul>
            <li>29th Aug 2020</li>
            <li>5km - 100km</li>
          </ul>
          <ul>
            <li>BRIGHT RUNNING FESTIVAL</li>
          </ul>
          <ul>
            <li>Virtual</li>
          </ul>
          <button>Details</button>
        </ul>
      </section>
    </Layout>
  )
}

export default eventCalendar
.eventCalendar {
  width: 100%;
}

.eventCalendar ul {
  text-align: left;
}

.eventCalendar li {
  display: inline-block;
  font-size: 20px;
  padding: 12px;
}

.eventCalendar button {
  background: rgb(148, 88, 199);
  background: linear-gradient(
    90deg,
    rgba(148, 88, 199, 1) 0%,
    rgba(92, 62, 183, 1) 100%
  );
  color: white;
  font-size: 24pt;
}
  • gatsby-transformer-sharp
  • gatsby-plugin-sharp
  • gatsby-源文件系统

这就是模型的样子

这就是我能够编写的代码

这里是 JavaScript

import React from "react"
import Layout from "../components/Layout"
import Image from "../components/Images"
import styles from "../styles/event-calendar.module.css"

const eventCalendar = () => {
  return (
    <Layout>
      <section className={styles.eventCalendar}>
        <li>
          <Image />
        </li>
        <ul>
          <ul>
            <li>29th Aug 2020</li>
            <li>5km - 100km</li>
          </ul>
          <ul>
            <li>BRIGHT RUNNING FESTIVAL</li>
          </ul>
          <ul>
            <li>Virtual</li>
          </ul>
          <button>Details</button>
        </ul>
      </section>
    </Layout>
  )
}

export default eventCalendar

这里是 CSS

.eventCalendar {
  width: 100%;
}

.eventCalendar ul {
  text-align: left;
}

.eventCalendar li {
  display: inline-block;
  font-size: 20px;
  padding: 12px;
}

.eventCalendar button {
  background: rgb(148, 88, 199);
  background: linear-gradient(
    90deg,
    rgba(148, 88, 199, 1) 0%,
    rgba(92, 62, 183, 1) 100%
  );
  color: white;
  font-size: 24pt;
}

【问题讨论】:

  • 创建一个演示,展示您可以通过 CodeSandbox 编写的任何代码。
  • 我正在尝试将项目导入 CodeSandbox,但我被永久卡在“创建存储库”中
  • 只需编辑您的帖子并按 Ctrl-M,您就会看到如何将工作示例直接输入到您的代码中。让我们所有人都更轻松。 :)
  • 我已经完成了,但它只是出错,我不知道如何修复它

标签: javascript html css reactjs


【解决方案1】:

Stackoverflow: How do I ask a good question? > How to create a Minimal, Reproducible Example

下一次,“清理”你的 Q(你的问题与 react/gatsby/gatsby-plugin-styled-components 并不真正相关 => 这是一个 pure CSS 布局问题)。

此外,您应该“修复”您的标记(您使用无效的nested lists HTML 标记。复制粘贴您的 HTML 她:https://validator.w3.org/#validate_by_input)。

之前

.eventCalendar {
  width: 100%;
}

.eventCalendar ul {
  text-align: left;
}

.eventCalendar li {
  display: inline-block;
  font-size: 20px;
  padding: 12px;
}

.eventCalendar button {
  background: rgb(148, 88, 199);
  background: linear-gradient(
    90deg,
    rgba(148, 88, 199, 1) 0%,
    rgba(92, 62, 183, 1) 100%
  );
  color: white;
  font-size: 24pt;
}
<section>
  <li>
    <img src="https://picsum.photos/300/120" alt="thumb"/>
  </li>
  <ul class="eventCalendar">
    <ul>
      <li>29th Aug 2020</li>
      <li>5km - 100km</li>
    </ul>
    <ul>
      <li>BRIGHT RUNNING FESTIVAL</li>
    </ul>
    <ul>
      <li>Virtual</li>
    </ul>
    <button>Details</button>
  </ul>
</section>

之后

解决此问题的一种方法是通过flexbox。

.flex-wrapper{
  display: flex;
  width: 100%;
  border: 1px solid gray;
}

ul.eventCalendar{
  /* remove bullets */
  padding-left: 0px;
  list-style-type: none;
  /* nested flex grid */
  display: flex;
  flex-wrap: wrap;
}

.eventCalendar li{
  font-size: 14px;
  border: 1px solid gray;
}

.eventCalendar .date{
  flex-basis: auto;
  margin-right: 20px;
}

.eventCalendar .distance {
  flex-basis: auto;
}

.eventCalendar .name, .eventCalendar .location {
  flex-basis: 100%;
}

.thumb{
  padding: 7px;
}

.button_col{
  /* align the col to the right */
  margin-left: auto;
  /* v center the button */
  display: flex;
  align-items: center;
}

.btn {
  background: rgb(148, 88, 199);
  background: linear-gradient(
    90deg,
    rgba(148, 88, 199, 1) 0%,
    rgba(92, 62, 183, 1) 100%
  );
  color: white;
  font-size: 24pt;
  min-width: 150px;
}
<section class="flex-wrapper">
  <div class="thumb">
    <img src="https://picsum.photos/200/121" alt="thumb"/>
  </div>
  <ul class="eventCalendar">
    <li class="date">29th Aug 2020</li>
    <li class="distance">5km - 100km</li>
    <li class="name"><h2>Event Name</h2></li>
    <li class="location">Location (Place, State)</li>    
  </ul>
  <div class="button_col">
    <button class="btn">Details</button>  
  </div>
</section>

*** 在小屏幕上添加媒体查询(以避免“破坏布局”)。 例如在移动设备上换行(换行到多行)。

/* MOBILE */
@media screen and (max-width: 992px) {
  .flex-wrapper{
    flex-wrap: wrap;
  }
}

【讨论】:

  • 您的代码对我不起作用。但是我能够弄清楚如何实现它的一部分。谢谢
  • 我知道的唯一问题是按钮居中。当我使用您提供的代码使按钮居中时,出于某种原因,我只会影响文本,甚至无法移动按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
相关资源
最近更新 更多