简体   繁体   English

在CSS / HTML中定位和对齐文本

[英]Positioning and aligning text in CSS/HTML

I am new to HTML and CSS and am having trouble positioning and aligning simple text. 我是HTML和CSS的新手,在定位和对齐简单文本时遇到麻烦。 I am trying to put the text on the right half of the page and have the text be lined up vertically along the first letter of every line. 我正在尝试将文本放在页面的右半部分,并使文本沿每行的第一个字母垂直排列。 Right now, the header is all the way on the right side of the page, half cut off. 现在,页眉一直在页面的右侧,一半被截断。 The rest of the the rest of the text is not stacked up on top of one another. 其余文本中的其余部分并未相互堆叠。 Rather, there is one block of text on the left, with the other two stacked on top of each other on the right. 相反,在左侧有一个文本块,而在右侧则是其他两个文本堆叠在一起。 I have been stuck on this for hours and have read every article on positioning and alignment, but I can't seem to get it. 我已经在这个问题上停留了几个小时,并且已经阅读了有关定位和对齐的每篇文章,但是我似乎无法理解。 Thanks for the help! 谢谢您的帮助!

 <div class="aboutme"><h3 id="hello">Header</h3></div> <div class="aboutme"><p id="school">text</p> </div> <div class="aboutme"><p id="personal"> text</p> </div> <div class="aboutme"><p id="thanks"> text</p> </div> 

 .aboutme > *{ font-family: Avenir Next; display: inline-block; text-align:left; float:left; margin: 0.5em 0.5em 0.5em 0.5em; position:relative; left:4em; } #hello{ font-size: 3em; color: #282828; right: 3.47em; font-weight: lighter; position:relative; text-align: left; float: right; margin: 1em 1em 0em 1em; } #school { width: 30em; clear:right; } #personal { width: 30em; clear:right; } #thanks { width: 30em; clear:right; } 

You need to add margin-left: 50% to the about me class and delete the other positioning you have added. 您需要在about me类中添加margin-left: 50% ,并删除您添加的其他位置。

 .aboutme > *{ font-family: Avenir Next; display: block; margin-left: 50% } #hello{ font-size: 3em; color: #282828; font-weight: lighter; } #school { width: 30em; } #personal { width: 30em; } #thanks { width: 30em; } 
 <div class="aboutme"><h3 id="hello">Header</h3></div> <div class="aboutme"><p id="school">text</p> </div> <div class="aboutme"><p id="personal"> text</p> </div> <div class="aboutme"><p id="thanks"> text</p> </div> 

Link to JSFiddle: https://jsfiddle.net/dLy932Lh/ 链接到JSFiddle: https ://jsfiddle.net/dLy932Lh/

EDIT: Adding a div to the left side 编辑:将div添加到左侧

 #leftHalf { display: inline-block; width: 50%; height: 200px; padding: 0; margin: 0; } #rightHalf { display: inline-block; width: calc(50% - 10px); height: auto; padding: 0; margin: 0; background-color: #FFF; } .aboutme > *{ font-family: Avenir Next; display: block; } #hello{ font-size: 3em; color: #282828; font-weight: lighter; } #school { width: 30em; } #personal { width: 30em; } #thanks { width: 30em; } 
 <div id="container"> <div id="leftHalf"> </div> <div id="rightHalf"> <div class="aboutme"> <h3 id="hello">Header</h3> </div> <div class="aboutme"><p id="school">text</p> </div> <div class="aboutme"> <p id="personal">text</p> </div> <div class="aboutme"> <p id="thanks">text</p> </div> </div> </div> 

It is important to have the display set to inline-block for the left and right divs so that they can both be on the same line (hence the inline) and they can have heights and widths (hence the block). 重要的是,将显示设置为左右div的行内块,以便它们都可以在同一行上(因此行内),并且它们可以具有高度和宽度(因此行内)。

Put any content you want on the left side of the page in the left div and make sure that it's width is not more than 50%. 将您想要的任何内容放在页面的左侧div中,并确保其宽度不超过50%。

I hope this solution works for you! 希望此解决方案对您有用!

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

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