纵向排列文字以及禁止文字被选中

本文介绍了CSS中的writing-mode属性,用于控制文本的书写方向,并提供了详细的代码示例。此外,还介绍了user-select属性,用于控制用户是否能够选中页面上的文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面:内容包括demo代码,应用和定义,以及参考文献,本文主要内容是本人学习过程中遇到css的零碎知识点1(就是以前自己不知道的),我都记下来了,需要的朋友可以过来参考下,喜欢的可以点个赞,希望对大家有所帮助。

writing-mode: tb-rl; (纵向排列文字)
应用:


Paste_Image.png

说明:
设置或检索对象的内容块固有的书写方向。西方语言一般都是 lr-tb 的书写方式,但是亚洲语言 lr-tb | tb-rl 的书写方式都有。
作为IE的私有属性之一,IE5.5率先实现了 writing-mode ,后期被w3c采纳成标准属性;
此属性效果不能被累加使用。例如,父对象的此属性值设为 tb-rl ,子对象再设置该属性将不起作用,仍应用父对象的设置。
对应的脚本特性为writingMode。

demo代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"/>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>demo</title>
    <style type="text/css">
        .verticle-mode {
            writing-mode: tb-rl;
            -webkit-writing-mode: vertical-rl;
            writing-mode: vertical-rl;
        }
        /* IE7比较弱,需要做点额外的动作 */
        .verticle-mode {
            *width: 120px;
        }
        .verticle-mode h4,
        .verticle-mode p {
            *display: inline;
            *writing-mode: tb-rl;
        }
        .verticle-mode h4 {
            *float:rightright;
        }

    </style>
</head>
<body>
<div class="verticle-mode">
    <h4>咏柳</h4>
    <p>碧玉妆成一树高,<br>万条垂下绿丝绦。<br>不知细叶谁裁出,<br>二月春风似剪刀。</p>
</div>
</body>
</html>

还有一种解决方法是设置div的宽度小一点,字会被挤下去,但是我个人不喜欢这种处理方式,觉得low。
想要再深入了解的童鞋,可以点这边:
张鑫旭大神的文章:改变CSS世界纵横规则的writing-mode属性,http://www.zhangxinxu.com/wordpress/2016/04/css-writing-mode/
css3参考手册:http://www.css88.com/book/css/properties/writing-modes/writing-mode.htm

user-select: none; (禁止文字选中)
说明:
设置或检索是否允许用户选中文本。

应用(点击按钮,点快的时候文字会被选中,很丑,):


Paste_Image.png

现象需求:html中可能有些地方不想让用户复制文字,或是用a标签做了个点击按钮,点快的时候文字会被选中,很丑,这个时候可以使用下面的方案禁止文字选中。

user-select取值:
none:文本不能被选择
text:可以选择文本
all:当所有内容作为一个整体时可以被选择。如果双击或者在上下文上点击子元素,那么被选择的部分将是以该子元素向上回溯的最高祖先元素。
element:可以选择文本,但选择范围受元素边界的约束
(css3参考手册:http://www.css88.com/book/css/properties/user-interface/user-select.htm)

兼容:不同的浏览器设置的内容不一样,user-select不是一个W3C的标准,浏览器的支持不完成,需要对不同的浏览器进行调整。

body{

-moz-user-select:none;/*火狐*/

-webkit-user-select:none;/*webkit浏览器*/

-ms-user-select:none;/*IE10*/

-khtml-user-select:none;/*早期浏览器*/

user-select:none;

}

demo代码

<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8" />
<title>user-select_CSS参考手册_web前端开发参考手册系列</title>
<meta name="author" content="Joy Du(飘零雾雨), dooyoe@gmail.com, www.doyoe.com" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<style>
.test{padding:10px;-webkit-user-select:none;-moz-user-select:none;-o-user-select:none;user-select:none;background:#eee;}
</style>
</head>
<body>
<div class="test" onselectstart="return false;" unselectable="on">选择我试试,你会发现怎么也选择不到我,哈哈哈哈</div>
</body>
</html>

注意:IE6-9不支持该属性,但支持使用标签属性 onselectstart="return false;" 来达到 user-select:none 的效果;Safari和Chrome也支持该标签属性;
直到Opera12.5仍然不支持该属性,但和IE6-9一样,也支持使用私有的标签属性 unselectable="on" 来达到 user-select:none 的效果;unselectable 的另一个值是 off;
除Chrome和Safari外,在其它浏览器中,如果将文本设置为 -ms-user-select:none;,则用户将无法在该文本块中开始选择文本。不过,如果用户在页面的其他区域开始选择文本,则用户仍然可以继续选择将文本设置为 -ms-user-select:none; 的区域文本;
对应的脚本特性为userSelect。

参考文献:1.http://www.css88.com/book/css/properties/user-interface/user-select.htm
2.http://www.cnblogs.com/codebook/p/5924073.html
最后又到了观众朋友们最喜欢的求赞求关注环节:希望看完的朋友点个喜欢,想关注我这个菜鸡是如何成长的也可以关注一下我,基本上每个月都不会少于十五篇文章(看到干货我也会进行分享)。然后github也互相加个star。码字不易,感谢支持,感激不尽!

dsl-radio.vue 文件代码 <template> <view class="dsl-radio cursor-pointer" @tap.stop="wrapperClickHandler" :style="[radioStyle]" :class="[ `dsl-radio-label--${parentData.iconPlacement}`, parentData.borderBottom && parentData.placement === 'column' && 'dsl-border-bottom' ]"> <view class="dsl-radio__icon-wrap cursor-pointer" @tap.stop="iconClickHandler" :class="iconClasses" :style="[iconWrapStyle]"> <!-- <slot name="icon" :elIconSize="elIconSize" :elIconColor="elIconColor"> --> <dsl-icon class="dsl-radio__icon-wrap__icon" name="check-white" :size="elIconSize" :color="elIconColor" /> <!-- </slot> --> </view> <view class="dsl-radio__label-wrap cursor-pointer" @tap.stop="labelClickHandler"> <slot name="label" :label="label" :elDisabled="elDisabled"> <text class="dsl-radio__text" :style="{ color: elDisabled ? elInactiveColor : elLabelColor, fontSize: elLabelSize, lineHeight: elLabelSize }" >{{ label }}</text > </slot> </view> </view> </template> <script> import { props } from './props' import { addUnit, addStyle, deepMerge } from '../../libs/function/index' /** * radio 单选框 * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配dsl-radio-group使用 * @property {String | Number} name radio的名称 * @property {Boolean} disabled 是否禁用 * @property {String | Boolean} labelDisabled 是否禁止点击提示语选中单选框 * @property {String} activeColor 选中时的颜色,如设置parent的active-color将失效 * @property {String} inactiveColor 未选中的颜色 * @property {String | Number} iconSize 图标大小,单位px * @property {String | Number} labelSize label字体大小,单位px * @property {String | Number} label label提示文字,因为nvue下,直接slot进来的文字,由于特殊的结构,无法修改样式 * @property {String | Number} size 整体的大小 * @property {String} iconColor 图标颜色 * @property {String} labelColor label的颜色 * @property {Object} customStyle 组件的样式,对象形式 * * @event {Function} change 某个radio状态发生变化时触发(选中状态) * @example <dsl-radio :labelDisabled="false">门掩黄昏,无计留春住</dsl-radio> */ export default { name: 'dsl-radio', mixins: [props], data() { return { checked: false, // 父组件的默认值,因为头条小程序不支持在computed中使用this.parent.shape的形式 // 故只能使用如此方法 parentData: { iconSize: 24, labelDisabled: null, disabled: null, // shape: null, activeColor: null, inactiveColor: null, size: 20, value: null, modelValue: null, iconColor: null, placement: 'row', borderBottom: false, iconPlacement: 'left' } } }, computed: { // 是否禁用,如果父组件dsl-raios-group禁用的话,将会忽略子组件的配置 elDisabled() { return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false }, // 是否禁用label点击 elLabelDisabled() { return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled : false }, // 组件尺寸,对应size的值,默认值为20px elSize() { return this.size ? this.size : this.parentData.size ? this.parentData.size : 20 }, // 组件的勾选图标的尺寸,默认24px elIconSize() { return this.iconSize ? this.iconSize : this.parentData.iconSize ? this.parentData.iconSize : 24 }, // 组件选中激活时的颜色 elActiveColor() { return this.activeColor ? this.activeColor : this.parentData.activeColor ? this.parentData.activeColor : '#14C780' }, // 组件禁选未选中文案颜色 elInactiveColor() { return this.inactiveColor ? this.inactiveColor : this.parentData.inactiveColor ? this.parentData.inactiveColor : '#B5B8C0' }, // label的颜色 elLabelColor() { return this.labelColor ? this.labelColor : this.parentData.labelColor ? this.parentData.labelColor : '#34373D' }, // 组件的形状 // elShape() { // return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle') // }, // label大小 elLabelSize() { return addUnit(this.labelSize ? this.labelSize : this.parentData.labelSize ? this.parentData.labelSize : '16') }, elIconColor() { const iconColor = this.iconColor ? this.iconColor : this.parentData.iconColor ? this.parentData.iconColor : '#ffffff' // 图标的颜色 // if (this.elDisabled) { // disabled状态下,已勾选的radio图标改为elInactiveColor return this.checked ? iconColor : 'transparent' // } else { // return this.checked ? iconColor : 'transparent' // } }, iconClasses() { let classes = [] // 组件的形状 // classes.push('u-radio__icon-wrap--' + this.elShape) classes.push('dsl-radio__icon-wrap--circle') if (this.elDisabled) { classes.push('dsl-radio__icon-wrap--disabled') } if (this.checked) { classes.push('dsl-radio__icon-wrap--checked') } if (this.checked && this.elDisabled) { classes.push('dsl-radio__icon-wrap--disabled--checked') } // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效 // #ifdef MP-ALIPAY || MP-TOUTIAO classes = classes.join(' ') // #endif return classes }, iconWrapStyle() { // radio的整体样式 const style = {} // style.backgroundColor = this.checked && !this.elDisabled ? this.elActiveColor : '#ffffff' style.width = addUnit(this.elSize) style.height = addUnit(this.elSize) // 如果是图标在右边的话,移除它的右边距 if (this.parentData.iconPlacement === 'right') { style.marginRight = 0 } return style }, radioStyle() { const style = {} // if(this.parentData.borderBottom && this.parentData.placement === 'row') { // console.error('检测到您将borderBottom设置为true,需要同时将dsl-radio-group的placement设置为column才有效') // } // 当父组件设置了显示下边框并且排列形式为纵向时,给内容和边框之间加上一定间隔 // if(this.parentData.borderBottom && this.parentData.placement === 'column') { // // ios像素密度高,需要多一点的距离 // style.paddingBottom = uni.$u.os() === 'ios' ? '12px' : '8px' // } return deepMerge(style, addStyle(this.customStyle)) } }, mounted() { this.init() }, emits: ['change'], methods: { init() { // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用 this.updateParentData() // if (!this.parent) { // console.error('dsl-radio必须搭配dsl-radio-group组件使用') // } // 设置初始化时,是否默认选中的状态 // #ifdef VUE3 this.checked = this.name === this.parentData.modelValue // #endif // #ifdef VUE2 this.checked = this.name === this.parentData.value // #endif }, updateParentData() { this.getParentData('dsl-radio-group') }, // 点击图标 iconClickHandler(e) { this.preventEvent(e) // 如果整体被禁用,不允许被点击 if (!this.elDisabled) { this.setRadioCheckedStatus() } }, // 横向两端排列时,点击组件即可触发选中事件 wrapperClickHandler(e) { this.parentData.iconPlacement === 'right' && this.iconClickHandler(e) }, // 点击label labelClickHandler(e) { this.preventEvent(e) // 如果按钮整体被禁用或者label被禁用,则不允许点击文字修改状态 if (!this.elLabelDisabled && !this.elDisabled) { this.setRadioCheckedStatus() } }, emitEvent() { // dsl-radio的checked不为true时(意味着未选中),才发出事件,避免多次点击触发事件 if (!this.checked) { this.$emit('change', this.name) // 尝试调用dsl-form的验证方法,进行一定延迟,否则微信小程序更新可能会不及时 // this.$nextTick(() => { // uni.$u.formValidate(this, 'change') // }) } }, // 改变组件选中状态 // 这里的改变的依据是,更改本组件的checked值为true,同时通过父组件遍历所有dsl-radio实例 // 将本组件外的其他dsl-radio的checked都设置为false(都被取消选中状态),因而只剩下一个为选中状态 setRadioCheckedStatus() { this.emitEvent() // 将本组件标记为选中状态 this.checked = true typeof this.parent.unCheckedOther === 'function' && this.parent.unCheckedOther(this) } } } </script> <style lang="scss" scoped> @import '../../libs/css/components.scss'; .dsl-radio { /* #ifndef APP-NVUE */ @include flex(row); /* #endif */ overflow: hidden; flex-direction: row; align-items: center !important; margin-bottom: 5px; margin-top: 5px; &-label--left { flex-direction: row; } &-label--right { flex-direction: row-reverse; justify-content: space-between; } &__icon-wrap { /* #ifndef APP-NVUE */ box-sizing: border-box; // nvue下,border-color过渡有问题 transition-property: border-color, background-color, color; transition-duration: 0.2s; /* #endif */ @include flex; align-items: center; justify-content: center; color: transparent; text-align: center; margin-right: $dsl-radio-wrap-margin-right; // font-size: $dsl-radio-wrap-font-size; border-width: $dsl-radio-wrap-border-width; border-color: $dsl-radio-wrap-border-color; border-style: solid; // padding-top: 1.5px; /* #ifdef MP-TOUTIAO */ // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下 // &__icon { // line-height: $dsl-radio-line-height; // } /* #endif */ &--circle { border-radius: $dsl-radio-circle-border-radius; } // &--square { // border-radius: $dsl-radio-square-border-radius; // } &--checked { color: $dsl-radio-checked-color; background-color: $dsl-radio-checked-background-color; border-color: $dsl-radio-checked-border-color; } &--disabled { background-color: $dsl-radio-disabled-background-color !important; border-color: $dsl-radio-disabled-border-color; } &--disabled--checked { color: $dsl-radio-disabled--checked-color !important; background-color: $dsl-radio-disabled--checked-color !important; border-color: $dsl-radio-disabled--checked-color !important; } } &__label { /* #ifndef APP-NVUE */ word-wrap: break-word; /* #endif */ margin-left: $dsl-radio-label-margin-left; margin-right: $dsl-radio-label-margin-right; color: $dsl-radio-label-color; // font-size: $dsl-radio-label-font-size; &--disabled { color: $dsl-radio-label-disabled-color; } } &__label-wrap{ display: flex; align-items: center; /* 确保文字垂直居中 */ height: 100%; /* 继承父容器高度 */ /* 修复文字在安卓偏上的问题 */ position: relative; top: 0; } &__text { /* 移除行高设置,使用flex居中 */ line-height: normal !important; /* 确保文字垂直居中 */ display: flex; align-items: center; } } /* #ifdef IOS */ .dsl-radio { padding: 10px 0; /* iOS需要更多垂直空间 */ &__icon-wrap { transform: translateY(0.5px); /* 微调图标垂直位置 */ } } /* #endif */ </style> props.js 文件代码 // import { defineMixin } from '../../libs/vue' import defProps from './radio.js' export const props = ({ props: { // radio的名称 name: { type: [String, Number, Boolean], default: () => defProps.radio.name }, // 是否禁用 disabled: { type: [String, Boolean], default: () => defProps.radio.disabled }, // 是否禁止点击提示语选中单选框 labelDisabled: { type: [String, Boolean], default: () => defProps.radio.labelDisabled }, // 选中状态下的颜色,如设置此值,将会覆盖parent的activeColor值 activeColor: { type: String, default: () => defProps.radio.activeColor }, // 未选中的颜色 inactiveColor: { type: String, default: () => defProps.radio.inactiveColor }, // 图标的大小,单位px iconSize: { type: [String, Number], default: () => defProps.radio.iconSize }, // label的字体大小,px单位 labelSize: { type: [String, Number], default: () => defProps.radio.labelSize }, // label提示文字,因为nvue下,直接slot进来的文字,由于特殊的结构,无法修改样式 label: { type: [String, Number], default: () => defProps.radio.label }, // 整体的大小 size: { type: [String, Number], default: () => defProps.radio.size }, // 图标颜色 color: { type: String, default: () => defProps.radio.color }, // label的颜色 labelColor: { type: String, default: () => defProps.radio.labelColor }, // 图标颜色 iconColor: { type: String, default: () => defProps.radio.iconColor } } }) radio.js文件代码 export default { // radio组件 radio: { name: '', // shape: '', disabled: false, labelDisabled: false, activeColor: '#14C780', inactiveColor: '#B5B8C0', iconSize: 24, labelSize: 16, label: '', labelColor: '#34373D', size: 20, iconColor: '#FFFFFF', placement: 'row' } } 在以上代码中修改,新增dsl-radio单独的使用的v-model v-model需要兼容vue2和vue3的写法 ,不要多加是否单独使用的参数,因为代码中注释了这一行代码 // if (!this.parent) { // console.error('dsl-radio必须搭配dsl-radio-group组件使用') // },判断!this.parent就是单独使用 也不用使用isvue2来判断是不是,可以在props.js中使用// #ifdef VUE3// #endif // #ifdef VUE2// #endif判断
最新发布
06-27
### CSS实现文字纵向排列的方法 通过多种方式可以利用CSS来实现文字纵向排列。以下是几种常见的方法及其特点: #### 方法一:使用 `writing-mode` 属性 `writing-mode` 是一种专门用于控制文本方向的属性,能够轻松实现文字纵向排列。 - **垂直从右到左** 设置 `writing-mode: vertical-rl;` 可以让文字按照从右到左的方向进行竖直排列[^1]。 ```css .tips-view { writing-mode: vertical-rl; } ``` - **垂直从左到右** 如果希望文字从左向右排列,则可设置为 `writing-mode: vertical-lr;`[^3]。 ```css .vertical-text-left-to-right { writing-mode: vertical-lr; } ``` #### 方法二:借助容器宽度与断词处理 这种方法适用于某些特定场景下需要逐字符换行的情况。通过设定固定的宽度并启用自动换行功能,可以让单个单词中的字母逐一换行显示。 - 设置固定宽度以及字体大小,并开启强制换行选项: ```css h1 { width: 20px; font-size: 20px; word-wrap: break-word; } ``` 此法适合短语或者标志性的大写字母展示需求[^2]。 #### 方法三:Flexbox布局配合书写模式调整 当追求更灵活可控的效果时,结合 Flexbox 布局模型和写作模式能带来更好的体验。例如下面的例子展示了如何创建一个既支持水平又兼容垂直居中的组件结构。 - 使用 Flex 容器定义项目分布规则的同时指定子项沿纵轴对齐的方式: ```css .tips-view { display: flex; flex-direction: column; justify-content: center; align-items: center; /* 添加必要的写入模式 */ writing-mode: vertical-rl; /* 其他样式配置 */ width: 170px; height: 254px; line-height: normal; text-align: center; font-size: 30px; } ``` 上述代码片段不仅实现了基本的文字竖排效果,还兼顾了整体视觉上的中心化定位。 以上就是关于如何运用CSS达成不同形式下的文字纵向排列介绍。每种方案都有其适用范围,在实际开发过程中可以根据具体业务逻辑选取最合适的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值