如何在CSS中对齐旋转的文本和图标

huangapple go评论50阅读模式
英文:

How to align rotated text and icons in CSS

问题

这是您提供的CSS和HTML代码,其中您遇到了对齐问题。您的目标是创建一个固定的侧边栏,其中包含一些图标和旋转的文本,它们都应该在一列中。以下是您的问题的翻译部分:

.Sidebar {
    position: fixed;
    top: 50%;
    height: 300px;
}

#S1 {
    position: absolute;
    left: 20%;
    top: 10%;
}

#S2 {
    position: absolute;
    left: 20%;
    top: 20%;
}

#Follow {
    transform: rotate(-90deg);
    font-size: 12px;
    position: absolute;
    left: 20%;
    top: 50%;
    font-family: Montserrat;
    text-transform: uppercase;
    overflow: hidden;
    white-space: nowrap;
}

这段CSS代码用于样式化您的侧边栏元素,但您遇到了文本对齐问题,文本偏离得太远。

英文:

I am trying to teach myself CSS and I am struggling with alignment. What I want to achieve is to have a fixed sidebar that has some icons and a rotated text, all of them should be in a column.

My code:

HTML:

<div class="Sidebar">
	<div id="S1" class="SBlock">
		<a href="https://twitter.com"><img src="Twitter_Logo.png" width=10px height=10px></a>
	</div>
	<div id="S2" class="SBlock">
		<a href="https://linkedin.com"><img src="Linkedin_Logo.png" width=10px height=10px></a>
	</div>
	<div id="Follow" class="SBlock">
		Follow Us
	</div>
</div>

CSS:

.Sidebar {
	position: fixed;
	top: 50%;
	height: 300px;
}

#S1 {
	position: absolute;
	left: 20%;
	top: 10%;
}

#S2 {
	position: absolute;
	left: 20%;
	top: 20%;
}

#Follow {
	transform: rotate(-90deg);
	font-size: 12px;
	position: absolute;
	left: 20%;
	top: 50%;
	font-family: Montserrat;
	text-transform: uppercase;
	overflow: hidden;
	white-space: nowrap;
}

Which produces this:

Result:

It works fine for the icons, I assume that is because they are the same size, but the text is way further to the right. Any ideas?

答案1

得分: 1

.Sidebar {
    position: fixed;
    top: 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.SBlock {
    margin: 10px 0;
}

#Follow {
    transform: rotate(-90deg);
    font-size: 12px;
    font-family: Montserrat;
    text-transform: uppercase;
    white-space: nowrap;
    writing-mode: vertical-rl;
    text-orientation: mixed;
}

请注意,上面的文本是CSS代码,已经翻译好了,没有别的内容。

英文:
   .Sidebar {
      position: fixed;
      top: 50%;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    
    .SBlock {
      margin: 10px 0;
    }
    
    #Follow {
      transform: rotate(-90deg);
      font-size: 12px;
      font-family: Montserrat;
      text-transform: uppercase;
      white-space: nowrap;
      writing-mode: vertical-rl;
      text-orientation: mixed;
    }

Try this...

答案2

得分: 0

尝试使用 writing-mode: sideways-lr 代替 transform: rotate(-90deg),以避免额外的空间。而且您不需要使用 position: absolute 来使所有项目在侧边栏中对齐。

阅读更多关于 writing-mode

.Sidebar {
  position: fixed;
  height: 300px;
  display: flex;
  flex-direction: column;
  background: red;
  align-items: center;
  width: 50px;
}

#Follow {
  font-size: 12px;
  font-family: Montserrat;
  color: blue;
  writing-mode: sideways-lr;
}
<div class="Sidebar">
    <div id="S1" class="SBlock">
        <a href="https://twitter.com"><img src="Twitter_Logo.png" width=10px height=10px></a>
    </div>
    <div id="S2" class="SBlock">
        <a href="https://linkedin.com"><img src="Linkedin_Logo.png" width=10px height=10px></a>
    </div>
    <div id="Follow" class="SBlock">
        Follow Us
    </div>
</div>
英文:

Try to use writing-mode: sideways-lr instead of transform: rotate(-90deg) to avoid the extra space. And you don't need position: absolute to get all items align in the sidebar.

Read more about writing-mode.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.Sidebar {
  position: fixed;
  height: 300px;
  display: flex;
  flex-direction: column;
  background: red;
  align-items: center;
  width: 50px;
}

#Follow {
  font-size: 12px;
  font-family: Montserrat;
  color: blue;
  writing-mode: sideways-lr;
}

<!-- language: lang-html -->

&lt;div class=&quot;Sidebar&quot;&gt;
	&lt;div id=&quot;S1&quot; class=&quot;SBlock&quot;&gt;
		&lt;a href=&quot;https://twitter.com&quot;&gt;&lt;img src=&quot;Twitter_Logo.png&quot; width=10px height=10px&gt;&lt;/a&gt;
	&lt;/div&gt;
	&lt;div id=&quot;S2&quot; class=&quot;SBlock&quot;&gt;
		&lt;a href=&quot;https://linkedin.com&quot;&gt;&lt;img src=&quot;Linkedin_Logo.png&quot; width=10px height=10px&gt;&lt;/a&gt;
	&lt;/div&gt;
	&lt;div id=&quot;Follow&quot; class=&quot;SBlock&quot;&gt;
		Follow Us
	&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: -1

我建议你使用Flexbox,它非常适合设计布局,而且使用它非常容易进行对齐。这篇文章是一个很好的起点:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

英文:

I'd suggest you use Flexbox which is so nice for designing layouts also, alignment is so easy using it.
This article is a good point to start:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

huangapple
  • 本文由 发表于 2023年2月18日 02:15:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定