简体   繁体   English

HTML / CSS-左对齐并居中同一行

[英]HTML/CSS - Left Align and center on same line

I want to left align some text and center another Text on the same line in HTML and CSS. 我想在HTML和CSS的同一行上左对齐某些文本并将另一文本居中。 I also want a margin-left on the left-aligned text. 我还希望在左对齐的文本上留一个margin-left

This is my current approach: 这是我目前的方法:

HTML : HTML

<div id="wrapper">
  <h1 class="align-left">LEFT</h1>
  <h1>CENTER</h1>
</div>

CSS : CSS

.align-left {
  float: left;
  margin-left: 20px;
}

#wrapper {
  text-align: center;
}

This works with left and right align, but the margin also pushes the centered text. 这适用于左对齐和右对齐,但是页边距也会推动居中的文本。 I think this is because the float: left keeps the left-aligned text in the page flow. 我认为这是因为float: left在页面流中保留了左对齐的文本。

Thank you really much :) 非常感谢你:)

Use like this 这样使用

<div id="wrapper">
  <h1 class="align-left">LEFT</h1>
  <h1 class="align-center">CENTER</h1>
</div>
<style>
h1.align-left {
    text-align:left;
    padding:0;
    margin:0;
    position:absolute;
}

h1.align-center{
  text-align: center;
}
</style>

Other way: 另一种方式:

<div id="wrapper">
  <h1 class="align-left">LEFT</h1>
  <h1 class="align-center">CENTER</h1>
</div>
<style>
h1.align-left{
    padding:0;
    margin:0;
    position:absolute;
}
#wrapper {
    text-align:left;
}

h1.align-center{
  text-align: center;
}
</style>

You just need to adjust the HTML: 您只需要调整HTML:

<div id="wrapper">
  <h1><span class="align-left">LEFT</span>CENTER</h1>
</div>

jsFiddle Demo jsFiddle演示

If you do not want the center text adjusting as the left text gets longer, use positioning CSS (with the same HTML I've posted). 如果您不希望随着左文本变长而调整中心文本,请使用定位CSS(使用我发布的相同HTML)。

.align-left {
  position: absolute;
  left: 20px;
}

#wrapper {
  text-align: center;
  position: relative;
}

jsFiddle Demo for positioning jsFiddle演示用于定位

Be aware... if you don't want the center text to adjust, it is possible that the text is going to overlap. 要知道......如果你不想中心的文本进行调整, 可能是文本会重叠。

My two cents. 我的两分钱。

CSS code: CSS代码:

h1{
  display: inline-block;
}
#center{
  text-align: center;
  width: 80%;
}

#wrapper {
  width: 100%;
}

HTML code: HTML代码:

<div id="wrapper">
  <h1 id="left">LEFT</h1>
  <h1 id="center">CENTER</h1>
</div>

You can achieve it with the code below. 您可以使用以下代码来实现。 The order of the articles tags won't matter, the css takes care to position them correctly. 文章标签的顺序无关紧要,css会小心地正确放置它们。

 <!DOCTYPE html> <html> <head> <style type="text/css"> section { border: 1px solid #ff0000; overflow: hidden; position: relative; width: 100%; } section article.txt { border: 1px solid green; max-width: 35%; } section article.left { float: left; } section article.right { float: right; } section article.center { --width: 300px; position: absolute; margin: 0 50%; left: calc((var(--width) / 2) - var(--width)); width: var(--width); } </style> </head> <body> <section> <article class="txt left">Text LEFT side</article> <article class="txt right">Text RIGHT side</article> <article class="txt center">Text CENTER</article> </section> </body> </html> 

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

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