How to relatively align text with CSS?

♀尐吖头ヾ 提交于 2021-01-27 17:03:10

问题


I seek to align equal signs in my equations. I tried span and divs, but the alignment ends if LHS content size > RHS's:

https://jsfiddle.net/gytmx256/6/

<span class="ctr">
    <span class="lft">\(1+2\ \) </span> \(=\) 
    <span class="lft"> \(\ 3\)</span>
</span>
<br>
<span class="ctr">
    <span class="lft">\(1+5\ \) </span> \(=\) 
    <span class="lft"> \(\ 6 + 2 - 2\)</span>
</span>
<br>
<span class="ctr">
    <span class="lft">\(1 + 3 + 2\ \) </span> \(=\) 
    <span class="lft"> \(\ 6 + 2 - 2\)</span>
</span>

.ctr{position: absolute; left: 40%;}
.lft{position: relative; display: inline;}

How can I align equal signs for any equation size? Help is appreciated.


回答1:


One option is that you can simply use table and float left side items to right i.e. .lft {float:right:}

Check here working example: https://jsfiddle.net/r6bxgem3/

Table will look like below:

.lft {
  float: right;
  display: inline;
}

* {
  box-sizing: border-box;
}

.row {
  display: flex;
  flex-wrap: wrap;
}

.main {
  flex: 80%;
  background-color: white;
  padding: 20px;
  overflow: hidden;
  position: relative;
}

.left {
  flex: 20%;
  background-color: #f1f1f1;
  padding: 20px;
}
<head>
  <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-MML-AM_CHTML"></script>
</head>

<div class="row">
  <div class="left">Stuff</div>
  <div class="main">

      <table><tr><td><span class="lft">
     
      \(1+2\ \)                                      <!--LHS-->
      </span></td><td>\(=\) </td><td>                <!--Equal sign-->
      \(\ 3\)                                        <!--RHS-->
      </td></tr><tr><td><span class="lft">

      \(1+5\ \)                                                         
      </span></td><td>\(=\)</td><td>
      \(\ 6+2-2\)                                                            
      </td></tr><tr><td><span class="lft">

      \(1+3+2\ \)                                                         
      </span></td><td>\(=\)</td><td>
      \(\ 6+2-2\)                                                           
      </td></tr>

      </table>

 </div></div>

Hope this will be helpful.




回答2:


.ctr {
    margin-left: auto;
    margin-right: auto;
}
<span class="ctr"><span class="lft">\(1+2\ \) </span> \(=\) <span class="lft"> \(\ 3\)</span></span><br>
<span class="ctr"><span class="lft">\(1+5\ \) </span> \(=\) <span class="lft"> \(\ 6 + 2 - 2\)</span></span><br>
<span class="ctr"><span class="lft">\(1 + 3 + 2\ \) </span> \(=\) <span class="lft"> \(\ 6 + 2 - 2\)</span></span>

What you can do is this:

This will do two things: make the <span>s centered, and also make them responsive.



来源:https://stackoverflow.com/questions/52087925/how-to-relatively-align-text-with-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!