V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fancymf
V2EX  ›  前端开发

左右文本对齐问题。

  •  
  •   fancymf · 11 天前 · 509 次点击






    B 文本内每段文本长度不一,怎么取最长的那段文本右边框贴到 B 的右边框,然后 B 文本内其他文本都跟那个最长的文本左对齐。


    应该怎么做到?
    7 条回复    2025-06-27 17:08:41 +08:00
    fancymf
        1
    fancymf  
    OP
       11 天前



    大概 B 那边这样对齐
    Yanlongli
        2
    Yanlongli  
       11 天前
    width: max-content;
    text-align-last: justify;
    liyinhe
        3
    liyinhe  
       11 天前
    A 的文本设置 width:fit-content;text-align: left;试一下,不能解决别骂
    Yanlongli
        4
    Yanlongli  
       11 天前   ❤️ 1
    @Yanlongli 啊理解错了,那就这样

    <style>
    .test {
    width: 200px;
    border: red 1px solid;
    display: flex;
    justify-content: flex-end;
    }

    .test2 {
    border: yellow 1px solid;
    width: max-content;
    }
    </style>
    <div class="test">
    <div class="test2">
    <p>啊朋友你好</p>
    <p>啊朋友再见吧再见</p>
    <p>啊朋友</p>
    </div>
    </div>
    zbinlin
        5
    zbinlin  
       11 天前
    你要在 B 里再用一个 div 包裹住那些文本段。
    iMiata
        6
    iMiata  
       11 天前
    <style>
    .root-container {
    width: 100%;
    height: 400px;
    border: dashed 1px #ccc;
    text-align: center;
    }
    .half-container {
    width: 40%;
    height: 100%;
    display: inline-block;
    vertical-align: top;
    border: dashed 1px #f00;
    }
    #B.half-container {
    text-align: right;
    }
    #B .inner-container {
    width: fit-content;
    margin-left: auto;
    margin-right: 0;
    text-align: left;
    border: dashed 1px #00f;
    }
    </style>
    <div class="root-container">
    <div class="half-container" id="A"></div>
    <div class="half-container" id="B">
    <div class="inner-container">
    <p>1234</p>
    <p>1234567890</p>
    <p>qwertyuiopasdfghjklzxcvbnm</p>
    <p>1234</p>
    <p>1234567890</p>
    </div>
    </div>
    </div>
    fancymf
        7
    fancymf  
    OP
       11 天前
    @Yanlongli 的答案是我需要的,我已经将项目改好了,再次感谢。又学会了一个新知识 :width: max-content;

    一开始我是没有 AB 这样的分离,完全是每段文本一行,后来需求改了,从每行一段文本改成每行两段文本展示,脑子就转不过来,问 ai 也一直没出现理想的效果。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1056 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:51 · PVG 06:51 · LAX 15:51 · JFK 18:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.