简体   繁体   English

文本和另一个内的div的垂直对齐

[英]Vertical alignment of text and a div inside another one

HTML HTML

<div id="divTop">divTop</div>
<div id="divBottom">divBottom
<div id="divCenter">divCenter</div>
</div>

CSS CSS

html, body{
    height:100%;
}
#divTop{
    height:50px;
    background:#008080;
    color:#ffffff;
    text-align:center;
    vertical-align:middle;
}
#divBottom{
    height:100%;
    text-align:center;
    vertical-align:middle;
    border:thick solid blue;
}
#divCenter{
    width:50vmin;
    height:50vmin;
   border:medium solid red;     
}

I want: divCenter to be centered both horizontally and vertically inside divBottom and also text inside all divs to be vertically centered. 我想要: divCenterdivBottom内水平和垂直divBottom ,并且在所有div内的文本垂直居中。
Please, help. 请帮忙。

jsfiddle is here jsfiddle在这里

Hi SunSky I forked your fiddle and made this one, you can use transformation properties to rotate the text. 嗨,SunSky,我把您的小提琴分了出来,做了一本,您可以使用transformation属性来旋转文本。 This should work in webkit browsers, mozilla, opera and IE9. 这应该可以在webkit浏览器,mozilla,opera和IE9中使用。 I didn't try it on earlier versions of IE but it should work at least in IE versions 7 and 8. The transform property works with the browsers mentioned except <=IE8 for IE 7,8 I put css property writing-mode:tb-lr; 我没有尝试在早期版本的IE浏览器,但它应该工作至少在IE版本7和8的transform属性可用于IE 7,8我把CSS属性除了<=提到IE8浏览器writing-mode:tb-lr; and text-orientation:sideways-lr; text-orientation:sideways-lr; to get the desired effect in these older IE browsers. 在这些较旧的IE浏览器中获得理想的效果。 You will have to play with those values to get what you really want. 您将必须运用这些价值观来获得自己真正想要的。 With transformations it's easier since you only have to rotate it to the point you want it, if you want to center it or move it from the edges of the divCenter just use paddings , to center the box you use margin-left:auto; 使用transformations更容易,因为您只需将其旋转到所需的点,如果要使其居中或从divCenter的边缘移动,只需使用paddings即可使框居中,您可以使用margin-left:auto; and margin-right:auto; margin-right:auto; for these to work you need to have a width set you can use 100% here's the jsfiddle so you can see the code, and also here's W3 writing-mode documentation . 为了使它们起作用,您需要设置width您可以使用jsfiddle的 100%,以便您可以看到代码,以及W3写作模式文档 I hope this helps if you have any more questions or comments feel free to ask/comment. 我希望这对您有任何疑问或意见的问题有帮助。

EDIT: 编辑:

I did not read the question carefully I will leave the info behind just as additional info on text transformations to create vertical text. 我没有仔细阅读问题,我将把信息留在后面,就像其他有关创建垂直文本的文本转换信息一样。 To center vertical text you can check out thi link It's about how to vertically align and how not do it the final examples are proably the one's you're looking for, if that does not work there's some questions on Stackoverflow that can help you here are the links: 要使垂直文本居中,您可以查看该链接, 这是关于如何垂直对齐以及如何不对齐的最终示例很可能是您所要查找的示例,如果这样做不起作用,那么Stackoverflow上存在一些问题可以为您提供帮助链接:

Vertically Align text in a Div Has some links to more information. Div中的文本垂直对齐具有一些指向更多信息的链接。

How do I vertically align text next to an image with CSS? 如何使用CSS在图像旁边垂直对齐文本? For this one check the second answer it's simple and gives some good tips and ways of archieveing what you want. 对于这一点,检查第二个答案很简单,并提供了一些很好的技巧和方法来存档所需的内容。 I hope this helps and that this is what you really wanted. 我希望这会有所帮助,这就是您真正想要的。 Here's the latests jsfiddle it should work like you want, what you have to do is set a height and width to center vertically then put 50% for the top property and then for the margin-top property put the negative half of the height of the div you want to center. 这是最新的jsfiddle,它应该可以按您想要的方式工作,您需要做的是将heightwidth设置为垂直居中,然后将top属性设置为50%,然后将margin-top属性设置为负height的一半。您想居中的div If you need to apply this alignment to all divs just use the CSS properties in each corresponding div or for easier code create a class ( HTML <div class="foo"></div> CSS .foo{} ) with these properties and just add it to elements that you want the text vertically aligned. 如果您需要将此对齐方式应用于所有divs只需在每个对应的div使用CSS属性,或者为了简化代码,使用这些属性创建一个HTML <div class="foo"></div> CSS .foo{} ),只需将其添加到您希望文本垂直对齐的元素即可。

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

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