Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I also want a margin-left on the left-aligned text.

This is my current approach:

HTML:

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

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.

Thank you really much :)

like image 976
LastSecondsToLive Avatar asked Dec 08 '25 07:12

LastSecondsToLive


2 Answers

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>
like image 131
Mani Avatar answered Dec 09 '25 20:12

Mani


My two cents.

CSS code:

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

#wrapper {
  width: 100%;
}

HTML code:

<div id="wrapper">
  <h1 id="left">LEFT</h1>
  <h1 id="center">CENTER</h1>
</div>
like image 42
DeepCode Avatar answered Dec 09 '25 19:12

DeepCode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!