【发布时间】:2021-06-19 14:31:29
【问题描述】:
我正在寻找一种在 CSS(或 JS,但最好是 CSS)中定义文本开始换行的断点的方法。我正在使用 React 17/CRA 和 CSS 模块。
我有一个包含两个内容的标题栏的 React 应用程序。左侧是h1 标记中的应用程序的三字标题。右侧是登录用户的头像和姓名,由span 中的几个元素组成。如果我缩小 viewframe,首先内容会溢出,然后,如果我进一步缩小它,应用程序的标题就会开始换行。
我希望标题在溢出发生之前换行,以便所有内容尽可能长时间地留在屏幕上。我所做的所有谷歌搜索都只提供了overflow-wrap 或word-break 的信息,这不是我想要的。文字按我想要的方式换行,我宁愿早点这样做。
我的组件的代码是:
import React from 'react'
import anonymousAvatar from './anonymousAvatar.jpg'
import styles from './dashboardHeader.module.css'
const DashboardHeader ({data}) => (
<div className={styles.root}>
<div className={styles.bar}>
<span className={styles.headerContainer}>
<h1 className={styles.header}>Three Word Title</h1>
</span>
<span className={styles.profile}>
<div className={styles.profileText}>
<p className={styles.textTop}>{data.name}</p>
<p className={styles.textBottom}>{data.email}</p>
</div>
<img className={styles.avatar} src={data.image_url || anonymousAvatar} alt='User Avatar' referrerPolicy='no-referrer' />
</span>
</div>
</div>
)
export default DashboardHeader
我目前拥有的 CSS 模块是:
.root {
position: fixed;
top: 0;
width: 100%;
}
.bar {
width: 100%;
height: 64px;
padding: 12px 16px;
background-color: #000;
color: #fff;
display: flex;
justify-content: space-between;
}
.headerContainer {
display: grid;
align-items: center;
}
.header {
font-family: 'Cinzel Decorative', sans-serif;
margin: 0;
font-size: 1.5rem;
}
.profile {
background-color: #000;
border: none;
display: grid;
align-items: center;
grid-template-columns: 2fr 1fr;
max-width: 30%;
cursor: pointer;
margin-right: 16px;
}
.profileText {
display: grid;
}
.textTop, .textBottom {
font-family: 'Quattrocento Sans', Arial, Helvetica, sans-serif;
font-size: 1.025rem;
color: #fff;
text-align: right;
margin: auto 0;
}
.textTop {
margin-bottom: 2px;
}
.textBottom {
margin-top: 2px;
}
.avatar {
height: 64px;
width: 64px;
border-radius: 50%;
box-shadow: 0px 0px 12px #fff;
margin-left: 24px;
margin-right: 16px;
}
【问题讨论】:
-
在浏览器开发工具中找到宽度,然后为它设置一个媒体查询?
-
我要问的是在媒体查询中放入什么来强制文本在特定断点处换行。
-
1) 减少断点处元素的宽度(推荐)或2)放一个
<br>在带有display: none的元素中,直到触发断点。例如:three word <br style='display:none' > title然后在断点处将其设置为display: block。 -
谢谢!我不得不做一些差事,但我回来后会试试的。如果可行,您可以将其作为答案,我会接受:)
-
好的,我添加了一个答案,其中包含提到的第一个解决方案的示例。希望这会有所帮助。
标签: javascript html css