简体   繁体   English

定位绝对定位的旋转文本

[英]Positioning an absolute positioned rotated text

I am having a really tough time positioning a transformed, rotated text. 我在定位转换后的旋转文本时非常困难。 Both vertically and horizontally, I cannot figure out how to position the rotated text rotateText , so that it is bottom: 60px and left: 10% within the relative container #tslotBlock1inner . 在垂直和水平方向上,我都无法弄清楚如何定位旋转后的文本rotateText ,以使其在相对容器#tslotBlock1inner bottom: 60pxleft: 10%

Then if you click on the jsfiddle link below and alter the viewport for 640px or less, you will see that the rotated text completely goes away, even with left:30% . 然后,如果单击下面的jsfiddle链接并将视口更改为640px或更小,您将看到旋转的文本完全消失了,即使left:30%

I have included an image below of how I would like it positioned. 我在下面添加了一张图片,说明它的位置。 Can anyone help and explain what I am doing wrong? 谁能帮忙解释我做错了什么?

Jsfiddle link to see mobile version jsfiddle链接查看移动版本

在此处输入图片说明

 .sec90 { width: 90%; margin: 20px auto 60px auto; } .rotateText { position: absolute; top: 50%; bottom: 60px; left: 2%; -webkit-transform: rotate(-90deg) translateX(-50%);transform: rotate(-90deg) translateX(-50%); font-family: 'Nunito', sans-serif; letter-spacing: .2rem; font-size: 2rem; color: #b82222; text-transform: uppercase; } #tslotSec { margin: 60px auto; } #tslotBlock1 { display: inline-block; vertical-align: top; } #tslotBlock1 { width: 70%; } #tslotBlock1:after { content: ''; display: inline-block; background: #b82222; width: 8px; height: 70%; vertical-align: middle; } #tslotBlock1inner { padding-right: 10%; position: relative; } #tslotBlock1inner img { width: 35%; height: auto; margin-left: 30%; } .dG { font-size: 1.1rem; line-height: 1.5em; font-family: 'Nunito', sans-serif; } /*---------------------------------------------- MEDIA QUERY 640 --------------------------------------------*/ @media screen and (max-width:640px) { .rotateText { bottom: 0px; left: 30%; -webkit-transform: rotate(-90deg) translateX(0%);transform: rotate(-90deg) translateX(0%); font-size: 1.5rem; } } 
 <section class="sec90" id="tslotSec"> <div id="tslotBlock1"> <div id="tslotBlock1inner"> <h2 class="blockTG">Advantages</h2> <p class="dG"> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." </p> <p class="dG"> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." </p> <div class="rotateText">Advantages</div> <img src="https://boygeniusreport.files.wordpress.com/2016/11/puppy-dog.jpg?quality=98&strip=all&w=782"> </div> </div> </section> 

I prefer to avoid position: absolute; 我宁愿避免这样的position: absolute; for elements that are part of a page's content, so I used float instead. 对于页面内容的一部分,因此我改用float I also set transform-origin since it's often easier to figure out positioning relative to a fixed point rather than the middle of an object's bounding box: 我还设置了transform-origin因为通常更容易找出相对于固定点的位置,而不是对象边界框的中间位置:

.rotateText {
    float: left;
    transform-origin: top left;
    -webkit-transform: rotate(-90deg) translateX(-100%);
    transform: rotate(-90deg) translateX(-100%);
    font-family: 'Nunito', sans-serif;
    letter-spacing: .2rem;
    font-size: 2rem;
    color: #b82222;
    text-transform: uppercase;
}

( Modified fiddle ) 修饰小提琴

I got it by updating your transforms and removing the positioning 我是通过更新您的变换并删除定位来获得的

.rotateText {
position: absolute;
-webkit-transform: rotate(-90deg) translate(-100px, -100px);
transform: rotate(-90deg) translate(-100px, -100px);
font-family: 'Nunito', sans-serif;
letter-spacing: .2rem;
font-size: 2rem;
color: #b82222;
text-transform: uppercase;
}

I agree with DavidW. 我同意DavidW。 I added some positioning and a bottom attribute to keep the text down beside the image. 我添加了一些定位和bottom属性,以使文本在图像旁边保持向下。

.rotateText {
    float: left;
    position: relative;
    bottom: 0px;
    transform-origin: top left;
    -webkit-transform: rotate(-90deg) translateX(-100%);
    transform: rotate(-90deg) translateX(-100%);
    font-family: 'Nunito', sans-serif;
    letter-spacing: .2rem;
    font-size: 2rem;
    color: #b82222;
    text-transform: uppercase;
}

This is kind of a different approach, but does not depend on magic numbers/sizes, using css writing-mode : 这是一种不同的方法,但是不使用幻数/大小,使用css Writing -mode

 .rotated-text { writing-mode: vertical-lr; transform: rotate(180deg); font-size: 60px; text-transform: uppercase; display: inline-block; background: cyan; } .flex { display: flex; } .image { flex-grow: 1; background: url(https://boygeniusreport.files.wordpress.com/2016/11/puppy-dog.jpg?quality=98&strip=all&w=782); background-position: center; background-repeat: no-repeat; background-size: contain; } 
 <p> LAKJSDLKJASLDJKALSDJLASKDLAKSJDLKJASLDKASLDJLASKDJLASKJDLASDJ </p> <div class="flex"> <div class="rotated-text">Advantages</div> <div class="image" /> </div> 

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

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