简体   繁体   English

如何在 CSS 中居中标签标签?

[英]How can I center tab labels in CSS?

How can I put the 3 label buttons (tab one, two and three) in the middle above the content?如何将 3 个标签按钮(标签一、二和三)放在内容上方的中间? they are now aligned to the left.它们现在向左对齐。 on top of that I would also appreciate a solution to hide the content in case a user is on his phone.最重要的是,我还希望有一个解决方案来隐藏内容,以防用户使用手机。 I hope that this is not too hard.我希望这不会太难。

 .tabs { position:absolute; bottom:0; display: flex; flex-wrap: wrap; } .tabs label { order: 1; display: block; padding: 1rem 2rem; margin-right: 0.2rem; cursor: pointer; background: #90CAF9; font-weight: bold; transition: background ease 0.2s; } .tabs .tab { order: 99; flex-grow: 1; width: 100%; display: none; padding: 1rem; background: #fff; } .tabs input[type="radio"] { display: none; } .tabs input[type="radio"]:checked + label { background: #fff; } .tabs input[type="radio"]:checked + label + .tab { display: block; } @media (max-width: 45em) { .tabs .tab, .tabs label { order: initial; } .tabs label { width: 100%; margin-right: 0; margin-top: 0.2rem; } }
 <div class="tabs"> <input type="radio" name="tabs" id="tabone" checked="checked"> <label for="tabone">Tab One</label> <div class="tab"> <h1>Tab One Content</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div> <input type="radio" name="tabs" id="tabtwo"> <label for="tabtwo">Tab Two</label> <div class="tab"> <h1>Tab Two Content</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div> <input type="radio" name="tabs" id="tabthree"> <label for="tabthree">Tab Three</label> <div class="tab"> <h1>Tab Three Content</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div> </div>

Use Flexbox's justify-content: center on .tabs with width: 100% to align the tabs in center of the page, like:使用 Flexbox 的justify-content: center on .tabs with width: 100%对齐页面中心的选项卡,例如:

.tabs {
  width: 100%;
  justify-content: center;
}

For closing tab on mobile devices, use jQuery to remove the checked attribute from #tabone , like:要在移动设备上关闭选项卡,请使用 jQuery 从#tabone删除checked属性,例如:

/* If mobile, remove checked attribute (Media Query in JS) */
if (window.matchMedia("(max-width: 45em)").matches) {
  $('#tabone').removeAttr('checked');
}

Have a look at the snippet below ( use full page view below to see it in center ):看看下面的片段(使用下面的全页视图在中心查看):

 /* If mobile, remove checked attribute */ if (window.matchMedia("(max-width: 45em)").matches) { $('#tabone').removeAttr('checked'); }
 .tabs { position:absolute; bottom:0; display: flex; width: 100%; justify-content: center; flex-wrap: wrap; } .tabs label { order: 1; display: block; padding: 1rem 2rem; margin-right: 0.2rem; cursor: pointer; background: #90CAF9; font-weight: bold; transition: background ease 0.2s; } .tabs .tab { order: 99; flex-grow: 1; width: 100%; display: none; padding: 1rem; background: #fff; } .tabs input[type="radio"] { display: none; } .tabs input[type="radio"]:checked + label { background: #fff; } .tabs input[type="radio"]:checked + label + .tab { display: block; } @media (max-width: 45em) { .tabs .tab, .tabs label { order: initial; } .tabs label { width: 100%; margin-right: 0; margin-top: 0.2rem; } }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tabs"> <input type="radio" name="tabs" id="tabone" checked="checked"> <label for="tabone">Tab One</label> <div class="tab"> <h1>Tab One Content</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div> <input type="radio" name="tabs" id="tabtwo"> <label for="tabtwo">Tab Two</label> <div class="tab"> <h1>Tab Two Content</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div> <input type="radio" name="tabs" id="tabthree"> <label for="tabthree">Tab Three</label> <div class="tab"> <h1>Tab Three Content</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </div> </div>

Hope this helps!希望这可以帮助!

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

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