简体   繁体   English

使按钮保持HTML对齐

[英]Keep buttons alignment in HTML

Everytime when I press on Button 1 , Button 2 and Button 3 are going below Button 1 (same with Button 2 ). 每当我按下Button 1Button 2Button 3移到Button 1下方(与Button 2相同)。 Here is a working snippet 这是一个工作片段

I want something like this to happen (everytime I press on a new button, the new text overrides the previous text, and the alignment is kept). 我希望这样的事情发生(每次我按下新按钮时,新文本都会覆盖以前的文本,并保持对齐)。

我希望这样的事情发生

What do I need to change in order to achieve what I want? 为了实现我想要的东西,我需要更改什么? Thank you for your time! 感谢您的时间!

Structure you html like this. 这样构造您的html。 在此处输入图片说明

Place the text inside the container element for each button. 将文本放置在每个按钮的容器元素内。

Set the container elements display to inline-block 将容器元素显示设置为内联块

Here is the code for you : 这是给您的代码:

 function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } 
 body { font-family: Arial; } .tab { overflow: hidden; } .tab button { cursor: pointer; padding: 6px 10px; transition: 0.3s; font-size: 17px; } .tabcontent { display: none; padding: 6px 12px; border-top: none; } 
 <meta name="viewport" content="width=device-width, initial-scale=1"> <div class="tab"> <button class="tablinks" onclick="openCity(event, 'London')">London</button> <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button> <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button> </div> <div id="London" class="tabcontent"> <h3>London</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Paris</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>Tokyo</h3> <p>Tokyo is the capital of Japan.</p> </div> 

You need to set the div's which are containing the text to display: inline-block; 您需要设置包含要显示的文本的div:inline-block;

div {
  display: inline-block
}

Here is the updated code snippet. 是更新的代码段。 I've restructured the html and took the text div at the end after buttons are added. 在添加按钮之后,我对html进行了重组,并在最后添加了文本div。 This way the buttons remain in same position. 这样,按钮可保持在相同位置。 For replacing the text, I've used a variable which is changed on button click and the value is updated on html. 为了替换文本,我使用了一个变量,该变量在单击按钮时更改,并且该值在html上更新。

Hope this is what you wanted to achieve. 希望这是您想要实现的目标。

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

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