简体   繁体   English

在canvas元素内移动文本并使其居中

[英]move text inside canvas element and make it centered

I am trying to move my div s with the texts hallo1 and hallo2 to my canvas element and make it centered. 我正在尝试将带有文本hallo1hallo2 div移动到我的canvas元素上并使其居中。
Does anyone have and idea on how to achieve this? 是否有人对如何实现这一目标有想法?

Below is code: 下面是代码:

<canvas id="canvas1">hi</canvas>

<div> 
    hallo1
</div>
<div>
  hallo2
</div>
<canvas id="canvas2"></canvas>

<div> 
    hallo1
</div>
<div>
  hallo2
</div>
<canvas id="canvas3"></canvas>

<div> 
    hallo1
</div>
<div>
  hallo2
</div>
<canvas id="canvas4"></canvas>

<div> 
    hallo1
</div>
<div>
  hallo2
</div>

put everything in a div and center it with text-align:center; 将所有内容放入div并使用text-align:center; . If you still need it to be on the left side of the screen use float:left; 如果仍然需要它在屏幕float:left;使用float:left; , like this: , 像这样:

fiddle 小提琴

#everything {
  text-align:center;
  float:left;
}

One posibility without modifying your layout is simply adjusting the position: 一种可能的情况是,无需修改布局即可简单地调整位置:

div {
    position: relative;
    left: 80px;
    top: -125px;
}

fiddle 小提琴

A version that adjusts to different text widths is to use 13ruce1337 answer and adjust it vertically 适应不同文本宽度的版本是使用13ruce1337答案并垂直调整

#everything {
    text-align:center;
    float:left;
    position: static;
}
div {
    position: relative;
    top: -125px;
}

I have to redefine everything position, because the style to the text div would modify it 我必须重新定义所有位置,因为文本div的样式会对其进行修改

fiddle 2 小提琴2

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

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