简体   繁体   English

CSS转换属性存在问题-无法调整背景颜色

[英]Issue with CSS transform property - Can't adjust background color

I am trying to create a box with the text on left with rotate from top to bottom but can't handle the width of the background, background color is showing additional but when I reduce the width, height is also getting reduced, here is the JSFiddle for the code I have created. 我正在尝试创建一个文本框在左侧且从上到下旋转但不能处理背景宽度的背景色显示得更多,但是当我减小宽度时,高度也会减小,这是JSFiddle用于创建的代码。

Text should also start from top width should not be showing extra. 文本也应从顶部宽度开始,不应过多显示。

HTML Code is HTML代码是

<div class="box">
  <span>Vertical Text</span>
</div>

And the CSS is CSS

.box {
  position: relative;
  width: 100%;
  float: left;
  border: 2px solid #444;
  min-height: 150px;
  box-sizing: border-box;
}
.box > span {
  position: absolute;
  left: 0;
  top: 0;
  background: #f00;
  color: #fff;
  width: 150px;
  min-height: 150px;
  font-size: 14px;
  font-family: arial;
  transform: rotate(-90deg);
}

Result I am looking is as attached below 我正在寻找的结果如下

在此处输入图片说明

Example JsFiddle box-sizing: border-box; 示例JsFiddle box-sizing: border-box; was the reason for the overflow 这是溢出的原因

text-aling: right; will set text to top 将文字置顶

 .box { position: relative; width: 100%; float: left; border: 2px solid #444; min-height: 150px; } .box > div { text-align: right; position: absolute; left: 0; top: 0; padding: 0; margin: 0; background: #f00; color: #fff; width: 20px; min-height: 150px; font-size: 14px; font-family: arial; transform: rotate(); } .box > div > p { transform: rotate(-90deg); } 
 <div class="box"> <div> <p>Text</p> </div> </div> 

You are facing such problem because of border: 2px solid #444 property, as it takes up 2px from 150px of total width in drawing the border of the div. 由于border: 2px solid #444属性,您正面临这样的问题,因为它在绘制div的边框时占总宽度的150px中的2px。 So the actual area width for the <span> would be 150px - (2px + 2px) = 146px (2px of border on either side) 因此, <span>的实际区域宽度将为150px - (2px + 2px) = 146px (2px 150px - (2px + 2px) = 146px (两边的边框150px - (2px + 2px) = 146px 2px)

If you set 如果您设定

.box > span 
{
    text-align:right; 
    width: 146px;
    min-height: 146px;
}

It would resolve the issue. 它将解决问题。

Try this, 尝试这个,

.box > span::before{
  content:'';
  position:relative;
  margin-left:70px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM