reactjs 如何在单个元素内的每个句子后添加间隔|一个元素中的行高值不同

7hiiyaii  于 2022-12-22  发布在  React
关注(0)|答案(2)|浏览(105)

我正在开发一个类似google keep with react的notes应用程序,我的单个note元素的html-css是:

const NoteList = ({searchTerm}) => {
    const [notes, setNotes] = useState([]);

    useEffect(() => {
        async function fetchNotes(){
            const response = await fetch("url")
            const notes = await response.json();

            setNotes(notes)
        };
        fetchNotes();
    }, []);

    return (<div className="note-list">
        {notes.map((note) => (
            <div key={note.id}>
                <Note
                    id={note.id}
                    title={note.title}
                    content={note.content}
                />
            </div>
        ))}
    </div>);
};
.card{
  width: 100%;
  height: fit-content;
  background-color: var(--card-bg);
  border-radius: 5px;
  box-shadow: 0 2px 4px 0 var(--card-shadow);
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  line-height: 1.2;
}

而我从JSON得到的数据是这样的:

[{
    "id": 31,
    "title": "Personal Development Goals",
    "content": "1. Read one book per month\n2. Practice mindfulness meditation daily\n3. Take a public speaking course\n4. Learn a new skill or hobby\n5. Volunteer regularly\n6. Start a gratitude journal\n7. Set and track personal fitness goals"
}]

输出如下所示。

我想在每一个完成的句子后面有更多的空间。每行的高度不一样(不是行高)。但是句子之间要有更大的间隔。

这应该是输出

我所尝试的

我试过很多行和字母属性。

.card{
    line-height: 1.2;
    line-break: loose;
    letter-spacing: 0.3px;
    word-spacing: 1px;
}

但它没有达到我想要的。
我想要的

如果你仔细看,你会发现红色的gap,比blue小,我试过line-heightword-wrap,但是我找不到任何属性可以做到这一点。
我看过一些在线解决方案,但是他们在一个元素中使用了多个span。但是我的数据不是静态的,它是从API中以JSON检索的。
先谢了。
我是react的新手。

s2j5cfk0

s2j5cfk01#

将每个"goal" Package 在p标签中,然后对p标签边距应用css规则。示例:
网页:

<p>goal 1</p> 
<p>goal 2</p>

等等...
那么...
CSS:

p {
    margin-top: use px, em, or rem. whichever works best for you and play around with the sizes until you like the look.; }

这将在每个p标记上应用边距,强制标记之间留有空格,而不是单独的行。

uwopmtnx

uwopmtnx2#

让你尝试使用多个p
例如:

<div class="card">
  <h5 class="card-title">Personal Development Goals</h5>
  <div class="card-content">
    <p>1. Read one book per month on personal development or self-improvement.</p>
    <p>2. Exercise at least 3 times per week.</p>
    <p>3. Practice mindfulness meditation for at least 15 minutes per day.</p>
    <p>4. Learn a new skill or take a course to improve my career prospects.</p>
    <p>5. Volunteer at least once per month to give back to my community.</p>
    <p>6. Set aside time each week to work on personal projects and hobbies.</p>
    <p>7. Practice gratitude by writing in a journal every day.</p>
    <p>8. Improve my time management skills by setting and sticking to a daily schedule.</p>
    <p>9. Set and work towards specific, measurable, achievable, relevant, and time-bound (SMART) goals.</p>
    <p>10. Seek out opportunities for personal growth and development, such as attending workshops or events.</p>
  </div>
</div>

您可以更好地控制自己的操作,您还可以使用CSS功能来选择孩子:

.card > p {
  font-size: 18px;
  color: blue;
}

相关问题