简体   繁体   English

想要在 css 中对齐同一行中的两个单词

[英]Want to align two words in the same line in css

I have two words that will change after some JavaScript coding.我有两个词会在一些 JavaScript 编码后发生变化。 They will be arrays.它们将是数组。 I can perform the JavaScript coding well, but I want them to be aligned instead of one above the other:我可以很好地执行 JavaScript 编码,但我希望它们对齐而不是一个在另一个之上:

<div class="testArrayDiv">   
    <input type="text" id="testArrayText" value="aqui" class="testArrayText">
    <p id="pTextIng" class="pTextIng">Inglés</p> 
    <p id="pTextPor" class="pTextPor">Português</p>    
    <button id="btnArray" class="btnArray">Test Array</button>
</div>    

My CSS coding:我的 CSS 编码:

.pTextIng {
    margin-left: 45%;
}

.pTextPor {
    margin-left: 45%;
}

The words "Inglés" and "Português" will become columns after some Javascript commands, but they are not aligned and I want them to be aligned. “Inglés”和“Português”这两个词将在一些 Javascript 命令之后变成列,但它们没有对齐,我希望它们对齐。

 .pTextIng { border:1px solid black; width:80px; display:inline-block; } .pTextPor { border:1px solid red; width:80px; display:inline-block; }
 <div class="testArrayDiv"> <input type="text" id="testArrayText" value="aqui" class="testArrayText"> <p id="pTextIng" class="pTextIng">Inglés</p> <p id="pTextPor" class="pTextPor">Português</p> <button id="btnArray" class="btnArray">Test Array</button> </div>

<p> automatically inserts a line break after the text. <p>自动在文本后插入换行符。 If you do not want this you should use <span> .如果你不想要这个,你应该使用<span> I assume you want both words on the same line as your input field, so you should put both <span> elements inside a <div> and apply margin-left to that.我假设您希望两个单词与输入字段位于同一行,因此您应该将两个<span>元素放在<div>并对其应用margin-left

HTML: HTML:

<div class="indentation">
    <span id="pTextIng" class="pTextIng">Inglés</span> 
    <span id="pTextPor" class="pTextPor">Português</span>
</div>

CSS: CSS:

.indentation {
    display: inline-block;
    margin-left: 45%;
}

If you make a div with both p inside like:如果你用两个p制作一个div像:

<div class="testArrayDiv">   
    <input type="text" id="testArrayText" value="aqui" class="testArrayText">
    <div class="idiomas">
        <p id="pTextIng" class="pTextIng">Inglés</p> 
        <p id="pTextPor" class="pTextPor">Português</p>  
    </div>  
    <button id="btnArray" class="btnArray">Test Array</button>
</div> 

And then you make this div element flex and flex-direction: row然后你让这个div元素flexflex-direction: row

.idiomas{
    display: flex;
    flex-direction: row;
}

You need to add vertical-align: top;您需要添加vertical-align:top; to fix the issue解决问题

.testAlign p {
    display: inline-block;
    vertical-align: top;
    /* other styles */
}

and also that <p> tags need to be inside a <div>而且<p>标签需要在<div>

<div class = "testAlign">    
    <p id="pTextIng" class="pTextIng">Inglés</p> 
    <p id="pTextPor" class="pTextPor">Português</p>
</div>

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

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