html - 在所选选项卡中添加下划线粘性边框

标签 html css frontend

HTML

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'rwaea')">روايات</button>
  <button class="tablinks" onclick="openCity(event, '3am')">ثقافة عامة</button>
</div>

<div id="rwaea" class="tabcontent"></div>

<div id="3am" class="tabcontent">
  <p>Paris is the capital of France.</p> 
</div>

CSS

.tab {
  font-family:sans-serif;
  line-height:24px;
  margin-left:5px;
  overflow: hidden;
  border: 0px;
  background-color: #F0E6E7;
  width:50%;  
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
  border: 0px;
  width:200px;
  border-bottom:2px solid lightgrey;
  font-family: Sans-Serif;
  font-size: 20px;
  line-height:24px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
  border-bottom:2px solid #ca1515 ;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

我的代码工作正常,但有一个功能我想添加到我的站点,但我不知道如何添加。 我有两个选项卡,我想在单击它时向所选选项卡添加边框, 我录制视频来解释我想做什么,请查看视频了解我的意思,所选选项卡上有橙色边框,我想将此属性添加到我的选项卡 check the video

最佳答案

为了做你想做的事,你应该使用javascript。

const tabs = document.querySelectorAll(".tab");
tabs.forEach(tab => {
  tab.addEventListener('click', function() {
    const current = document.querySelector('.tab.active');
    current.classList.remove('active');
    this.classList.add("active");
  })
})
.flex {
  flex-flow: row nowrap;
  align-items: center;
}

.tab {
  display: inline-block;
  border: none;
  margin: 0 20px;
  border-bottom: 4px solid transparent;
  outline: none;
  cursor: pointer;
}

.tab.active {
  border-bottom-color: orange;
}
<ul class="flex">
  <li class="tab active">Tab 1</button>
  <li class="tab">Tab 2</button>
  <li class="tab">Tab 3</button>
</ul>

希望它能回答您的问题!

关于html - 在所选选项卡中添加下划线粘性边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58953730/

相关文章:

html固定宽度和动态布局

javascript - 将 XML 内容加载到 li

css - 为什么我的 Wordpress 主题在移动设备上丢失 CSS?

html - 菜单大小取决于屏幕分辨率

javascript - 从 HTML 按钮播放 Javascript 播放器

html - 使用 border-radius 时内容出现在一个 Angular 上

session - Yii2 Advanced 应用程序,带有子域的前端和后端的不同 session

html - 左对齐标题到 <ul> 的右边界

HTML 5 Doctype 打乱布局

html - 对于每组注册表做 - Rails