word-wrap、word-break、white-space、word-spacing、text-overflow、overflow 的联系与区别

CSS文本布局技巧
本文深入讲解CSS3中word-wrap、word-break、white-space、word-spacing和text-overflow属性的使用方法,通过实例演示如何控制文本换行、空白处理及文本溢出显示,帮助读者掌握网页文本布局的细节。

1.CSS3 word-wrap 属性

word-wrap 属性允许长单词或 URL 地址换行到下一行。

描述
normal只在允许的断字点换行(浏览器保持默认处理)。
break-word在长单词或 URL 地址内部进行换行。

实例:

2.CSS3 word-break 属性

word-break 属性规定自动换行的处理方法。

描述
normal使用浏览器默认的换行规则。
break-all允许在单词内换行。
keep-all只能在半角空格或连字符处换行。

实例

3.CSS white-space 属性

white-space 属性设置如何处理元素内的空白。

normal默认。空白会被浏览器忽略。
pre空白会被浏览器保留。其行为方式类似 HTML 中的 <pre> 标签。
nowrap文本不会换行,文本会在在同一行上继续,直到遇到 <br> 标签为止。
pre-wrap保留空白符序列,但是正常地进行换行。
pre-line合并空白符序列,但是保留换行符。
inherit规定应该从父元素继承 white-space 属性的值。

实例:

4.CSS word-spacing 属性

word-spacing 属性增加或减少单词间的空白(即字间隔)。

normal默认。定义单词间的标准空间。
length定义单词间的固定空间。
inherit规定应该从父元素继承 word-spacing 属性的值。

实例:

5.CSS3 text-overflow 属性

text-overflow 属性规定当文本溢出包含元素时发生的事情。

实例:

6.CSS overflow 属性

overflow 属性规定当内容溢出元素框时发生的事情。

实例:

import { useEffect, useMemo, useState } from "react"; import classNames from "classnames"; import { Form, FormItem, useForm, useFormWatch, LinkButton, Input, } from "@sigma-wg/ui"; import CaptchaInput from "@/components/captcha-input"; import BlockButton from "@/components/block-button"; import LoginInput from "@/components/login-input"; import LoginServerError from "@/components/login-server-error"; import { Black2Text } from "@/components/black2-text"; import PasswordValidatePopover from "@/components/management-settings/password/password-validate-popover"; import { isPhoneNumber, phoneRegex } from "@/utils/validator"; import { useError } from "@/hooks/use-error"; import { checkCode, CheckCodeOpts, getUserNameByPhone, resetUserPassword, ResetUserPasswordOpts, } from "@/api/casdoor"; import { commonCasdoorFormFieldStyle } from "@/constants"; import { goToLoginPage } from "@/utils/login-helper"; import { useMallHost } from "@/store/host-url"; import styles from "./index.module.css"; const ForgetPassword = () => { const [form] = useForm(); const [submiting, setSubmitting] = useState<boolean>(false); const [forgetPasswordError, setError, resetError] = useError(); // 验证码 获取接口 error信息 const captcha_error = useFormWatch("captcha_error", form); const error = useMemo(() => { return forgetPasswordError || captcha_error; }, [forgetPasswordError, captcha_error]); const phone = useFormWatch("phone", form); const isPhone = useMemo(() => isPhoneNumber(phone), [phone]); const handleChangeValue = () => { resetError(); }; const mallUrl = useMallHost(); const handleClickLogin = () => { // 调用 console api接口,跳转到登录页,登录完跳到商城首页 goToLoginPage(mallUrl); }; const handleSubmit = () => { form.validateFields().then((values) => { const params: CheckCodeOpts = { username: values.phone, code: values.captcha, }; resetError(); setSubmitting(true); checkCode(params) .then(() => getUserNameByPhone(values.phone)) .then((res) => { const data: ResetUserPasswordOpts = { userName: res?.data?.data?.name, // TODO: 此处的密码需要加密,此处无法调用 get-app-login 接口,待确认 newPassword: values.password, code: values.captcha, }; return resetUserPassword(data); }) .then(() => { // 调用 console api接口,跳转到登录页,登录完跳到商城首页 goToLoginPage(mallUrl); }) .catch((err) => setError(err)) .finally(() => setSubmitting(false)); }); }; // const is_send_code = useFormWatch("is_send_code", form); const is_send_code = true; return ( <div className={styles.container}> <div className={classNames(styles.header, { [styles["has-desc"]]: is_send_code, })} > <div className={styles["header-content"]}> <div className={styles.tip}> <Black2Text>已有账号?</Black2Text> <LinkButton onClick={handleClickLogin}>快速登录</LinkButton> </div> <> {is_send_code && ( <div className={styles.description}> 系统将向您已注册的手机号发送验证码。请留意查收短信并及时输入验证码完成验证。 </div> )} </> </div> </div> <div className={styles.content}> <div className={styles.title}>忘记密码</div> <Form colon={false} onFinish={handleSubmit} size="large" form={form} onValuesChange={handleChangeValue} > <FormItem name="phone" rules={[ { required: true, pattern: phoneRegex, message: "请输入手机号" }, ]} > <LoginInput placeholder="请输入手机号" /> </FormItem> <FormItem name="captcha" rules={[{ required: true, message: "请输入验证码" }]} > <CaptchaInput isPhone={isPhone} method="forget" /> </FormItem> <PasswordValidatePopover isModal={true} placeholder="请输入登录密码" validPasswordTip="请输入合法的登录密码" showLabel={false} widthStyle={commonCasdoorFormFieldStyle} /> <FormItem name="confirm_password" dependencies={["password"]} rules={[ ({ getFieldValue }) => ({ validator(_, value) { if (getFieldValue("password") === value) { return Promise.resolve(); } return Promise.reject(new Error("两次输入的密码不一致")); }, }), ]} > <LoginInput isPassword placeholder="请再次输入登录密码" /> </FormItem> {/* 存储验证码错误信息 */} <FormItem name="captcha_error" hidden> <Input /> </FormItem> <LoginServerError error={error} /> <BlockButton htmlType="submit" style={{ marginTop: "14px" }} loading={submiting} > 登录 </BlockButton> </Form> </div> </div> ); }; export default ForgetPassword; .container { display: flex; justify-content: center; align-items: center; flex-direction: column; } .content { display: flex; flex-direction: column; justify-content: center; align-items: center; width: 320px; } .header { width: 0; /* 不影响表单的高度 */ height: 0px; } .header-content { position: relative; /* 200 = 320/2 + 40 */ left: 200px; /* 25 =( 40(title height) - 24(header height)) /2 */ top: 13px; max-width: 460px; } .title { width: 320px; font-weight: 500; font-size: 28px; color: var(--sl-text-color); line-height: 40px; letter-spacing: 2px; text-align: center; margin-bottom: 30px; } .tip { display: flex; align-items: center; } .description { height: 32px; /* 42 = 8+34 = (40(left title height 找回密码)-24(right title height 在线登录))/2+30(title margin-bottom) +( 32(form item height) - 24(height))/2 */ margin-top: 42px; /* 文本宽度不够时,自动换行 */ overflow-wrap: break-word; } 中description 没有自动换行,且宽度设置的有问题,怎么修复
最新发布
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值