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

本文深入讲解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 属性规定当内容溢出元素框时发生的事情。

实例:

你提供的组件没有使用这个样式当前组件没有使用这个代码中的样式<script lang="ts" setup> import isTablet from '@/utils/device' type ModelValue = string | number | (string | number)[] type ListItem = { label: string value: string | number } & { [key: string]: any } interface Props { list: ListItem[] multiple?: boolean disabled?: boolean } const props = withDefaults(defineProps<Props>(), { list: () => [], multiple: false, disabled: false }) const modelValue = defineModel<ModelValue>({ required: true, default: '' }) const $emit = defineEmits<{ (e: 'change', value: ModelValue): void }>() const isIpad = isTablet() const getActive = (item: ListItem) => { if (props.multiple && Array.isArray(modelValue.value)) { return modelValue.value.includes(item.value) } return modelValue.value === item.value } /** * 点击项 * @param item 点击的项 */ const handleItem = ({ value }: ListItem) => { if (props.disabled) { return } if (props.multiple && Array.isArray(modelValue.value)) { const _index = modelValue.value.findIndex(v => v === value) if (_index > -1) { modelValue.value = modelValue.value.filter(v => v !== value) } else { modelValue.value = [...modelValue.value, value] } } else { if (modelValue.value === value) return modelValue.value = value } $emit('change', modelValue.value) } </script> <template> <view class="CrmSelectTag-container" :class="{ 'CrmSelectTag-container-iPad': isIpad }" > <view class="item" :class="{ active: getActive(item), disabled: disabled }" v-for="item in list" :key="item.value" @click="handleItem(item)" > <text class="item-text">{{ item.label }}</text> <view class="active-icon" v-show="getActive(item)"> <uni-icons class="icon" type="check-outline" color="#fff" size="9" /> </view> </view> </view> </template> <style lang="scss" scoped> .CrmSelectTag-container { display: flex; flex-wrap: wrap; gap: 12px; .item { width: calc(calc(100% - 24px) / 3); // width: 109px; box-sizing: border-box; border: 1px solid transparent; background-color: $uni-separator-bg; border-radius: $uni-border-radius-sm; font-size: $uni-font-size-15; color: $uni-text-color; font-weight: 400; position: relative; } .item-text { display: block; width: 100%; text-align: center; padding: $uni-spacing-col-xs 4px; box-sizing: border-box; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; word-break: break-all; } .active { background-color: $uni-text-color-primary-light; color: $uni-primary; border-color: $uni-primary; } .disabled { color: $uni-text-color-extra; } .active-icon { position: absolute; bottom: -1px; right: -1px; width: 0; height: 0; border: 8px solid transparent; border-bottom-color: $uni-primary; border-right-color: $uni-primary; border-bottom-right-radius: $uni-border-radius-sm; .icon { position: absolute; right: -8px; bottom: -12px; } } } .CrmSelectTag-container-iPad { .item { width: 109px; } } </style>
10-30
参考当前组件的样式和选中样式重新提供一个组件,要求 1、可多选,仅有两个选项,选项创建商机和营销拜访 2、都没选中返回值是0,单独选中创建商机返回值是1,单独选中营销拜访返回值是2,都选中返回值是3 3、可设置某个值是否展示并且可设置某个不可选 4、可以设置默认选中的值 type ModelValue = string | number | (string | number)[] type ListItem = { label: string value: string | number } & { [key: string]: any } interface Props { list: ListItem[] multiple?: boolean disabled?: boolean } const props = withDefaults(defineProps(), { list: () => [], multiple: false, disabled: false }) const modelValue = defineModel({ required: true, default: ‘’ }) const $emit = defineEmits<{ (e: ‘change’, value: ModelValue): void }>() const isIpad = isTablet() const getActive = (item: ListItem) => { if (props.multiple && Array.isArray(modelValue.value)) { return modelValue.value.includes(item.value) } return modelValue.value === item.value } /** 点击项 @param item 点击的项 */ const handleItem = ({ value }: ListItem) => { if (props.disabled) { return } if (props.multiple && Array.isArray(modelValue.value)) { const _index = modelValue.value.findIndex(v => v === value) if (_index > -1) { modelValue.value = modelValue.value.filter(v => v !== value) } else { modelValue.value = […modelValue.value, value] } } else { if (modelValue.value === value) return modelValue.value = value } $emit(‘change’, modelValue.value) } <template> <view class="CrmSelectTag-container" :class="{ 'CrmSelectTag-container-iPad': isIpad }" > <view class="item" :class="{ active: getActive(item), disabled: disabled }" v-for="item in list" :key="item.value" @click="handleItem(item)" > <text class="item-text">{{ item.label }}</text> <view class="active-icon" v-show="getActive(item)"> <uni-icons class="icon" type="check-outline" color="#fff" size="9" /> </view> </view> </view> </template> <style lang="scss" scoped> .CrmSelectTag-container { display: flex; flex-wrap: wrap; gap: 12px; .item { width: calc(calc(100% - 24px) / 3); // width: 109px; box-sizing: border-box; border: 1px solid transparent; background-color: $uni-separator-bg; border-radius: $uni-border-radius-sm; font-size: $uni-font-size-15; color: $uni-text-color; font-weight: 400; position: relative; } .item-text { display: block; width: 100%; text-align: center; padding: $uni-spacing-col-xs 4px; box-sizing: border-box; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; word-break: break-all; } .active { background-color: $uni-text-color-primary-light; color: $uni-primary; border-color: $uni-primary; } .disabled { color: $uni-text-color-extra; } .active-icon { position: absolute; bottom: -1px; right: -1px; width: 0; height: 0; border: 8px solid transparent; border-bottom-color: $uni-primary; border-right-color: $uni-primary; border-bottom-right-radius: $uni-border-radius-sm; .icon { position: absolute; right: -8px; bottom: -12px; } } } .CrmSelectTag-container-iPad { .item { width: 109px; } } </style>
最新发布
10-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值