div height overflow in parent [duplicate] - html

This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 1 year ago.
I have 2 tabs and each tab is displaying as flex. My need is if #myTabContent overflow .main-panel due to the list, I want a scroll bar only on the list, not on the whole div.
By this way, the #myTabContent should never overflow.
Here the JSFiddle with the behavior, and the following snippet:
.main-panel{
background: lightblue;
height: 300px;
}
#myTabContent {
margin: 5px;
color: white;
background: #0007;
}
#myTabContent > div {
display: none;
}
#myTabContent .active {
display: flex;
flex-flow: column;
height: 100%;
}
.data-main {
display: flex;
flex-flow: row;
}
.data-left {
flex: 0 1 150px;
background: #a007;
}
.data-right {
flex: 1 1 auto;
background: #0a07;
}
.list-group {
overflow-y: auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<div class="main-panel">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="data-tab" data-toggle="tab" href="#data" role="tab" aria-controls="data" aria-selected="true">Data</a>
</li>
<li class="nav-item">
<a class="nav-link" id="preview-tab" data-toggle="tab" href="#preview" role="tab" aria-controls="preview" aria-selected="false">Preview</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="data" role="tabpanel" aria-labelledby="data-tab">
<h3>Data</h3>
<div class="data-main">
<div class="data-left">
<h4>Tabs</h4>
<ul class="list-group">
<li class="list-group-item list-group-item-action py-1">a</li>
<li class="list-group-item list-group-item-action py-1">b</li>
<li class="list-group-item list-group-item-action py-1">c</li>
<li class="list-group-item list-group-item-action py-1">d</li>
<li class="list-group-item list-group-item-action py-1">e</li>
<li class="list-group-item list-group-item-action py-1">f</li>
</ul>
</div>
<div class="data-right">
<h4>Sections</h4>
</div>
</div>
</div>
<div class="tab-pane fade" id="preview" role="tabpanel" aria-labelledby="preview-tab">
<h3>Preview</h3>
</div>
</div>
</div>

Can't you simply set the parent container to have this property?:
height: fit-content
This should always force your parent container to be as high as the child is.

Related

How do I make a sidenav scrollbar?

I have a sidenav using a <ul> tag. How to make sidenav show scrollbar auto?
An Example of what my sidenav looks like
html:
<section class="sticky-top">
<ul class="nav flex-column bg-secondary p-3 rounded-right col-md-3 col-lg-2 text-center min-vh-100">
<li class="nav-item">
<a class="nav-link" href="#">Test 1</a>
</li>
<li class="nav-item">
<a class="nav-link " href="#">Test 2 </a>
</li>
<li class="nav-item">
<a class="nav-link " href="#">Test 3</a>
</li>
</ul>
</section>
css:
.nav {
position: absolute;
z-index : 3;
}
.navbar {
z-index : 3;
background-color: var(--white);
}
When I applied the sidebar styling by enclosing the <ul> element in a <div> element, I got the desired result. Hovering the sidebar (when the hover event occurs) opens the scrollbar.
<div class="sidebar">
<ul class="nav flex-column bg-secondary p-3 rounded-right col-md-3 col-lg-2 text-center min-vh-100">
<!-- sidebar items -->
</ul>
</div>
.sidebar{
position: fixed;
top: 0;
bottom: 0;
background: #6C757D;
width: 140px;
}
.sidebar:hover{
overflow-y: auto
}
The executable snippet is available below. The overflow-y: auto; style comment line defined in the .sidebar:hover block has been made to automatically display the scrollbar; Added overflow-y: auto; style to .sidebar block.
.sidebar {
position: fixed;
top: 0;
bottom: 0;
background: #6C757D;
width: 140px;
overflow-y: auto;
}
.sidebar:hover {
/* overflow-y: auto */
}
.nav {
position: absolute;
z-index: 3;
}
.navbar {
z-index: 3;
background-color: var(--white);
}
.nav-link {
color: #ffffff;
font-size: 12px;
&:hover {
transition: color 0.4s ease-in;
background: rgba(255, 255, 255, 0.04);
color: #ffffff;
}
&.active {
background-color: #007bff;
}
}
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<section class="sticky-top">
<div class="sidebar">
<ul class="nav flex-column bg-secondary p-3 rounded-right col-md-3 col-lg-2 text-center min-vh-100">
<li class="nav-item"><a class="nav-link" href="#">Test</a></li>
<li class="nav-item"><a class="nav-link" href="#">Test</a></li>
<li class="nav-item"><a class="nav-link" href="#">Test</a></li>
<li class="nav-item"><a class="nav-link" href="#">Test</a></li>
<li class="nav-item"><a class="nav-link" href="#">Test</a></li>
<li class="nav-item"><a class="nav-link" href="#">Test</a></li>
</ul>
</div>
</section>
</body>

How to display scrollbar in list after certain dimension?

I have this list:
<div class="col-lg-3 col-md-6">
<ul class="nav nav-pills nav-pills-primary nav-pills-icons flex-column">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#link10">
<i class="tim-icons icon-istanbul"></i> Home
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#link11">
<i class="tim-icons icon-settings"></i> Settings
</a>
</li>
</ul>
</div>
I would like to show a scrollbar when the list reach a specific number of items eg: 3, how can I do this?
You can check the list above working in Vertical Tabs With Icons example here.
Something like this
.nav {
display: block;
flex-wrap: wrap;
padding-left: 0;
margin-bottom: 0;
list-style: none;
height: 360px;
width: 150px;
overflow-y: auto;
}
ol li, ul li {
color: #fff;
height: 118px;
}

Bootstrap Navigation with tabs

I would like to reproduce navigation control with tabs like this:
with bootstrap and with (https://fezvrasta.github.io/bootstrap-material-design/docs/4.0/material-design/navs/) - material design bootstrap...
Is there a way to put tab bars below the navbar-brand items?
I found the solution: I inspect the CSS of the library that has this and try to mimic it.
The end solution:
CSS
.my_nav {
height: auto;
display: flex;
flex-wrap: wrap;
}
.my_div {
min-height: 56px;
height: auto;
display: flex;
flex: 1;
}
.my_content {
margin: 0;
flex: 0 0 100%;
}
.nav-tabs .nav-link {
padding: 0.55em 1.15em;
}
html
<nav class="navbar navbar-expand my_nav ">
<div class="my_div">
Home
<ul id="nav-mobile" class="navbar-nav ml-auto right">
<li class="nav-item"><a class="nav-link ">Wellcome {{ user.username }}</a></li>
<li class="nav-item"><a class="nav-link ">Log Out</a></li>
</ul>
</div>
<div class="my_content">
<ul class="nav nav-tabs ">
<li class="nav-item"><a class="nav-link" >Test 1</a></li>
<li class="nav-item"><a class="nav-link" >Test 2</a></li>
<li class="nav-item"><a class="nav-link active" >Test 3</a></li>
</ul>
</div>
</nav>

Unordered-list inline vertical alignment and responsive spacing

I am trying to create a responsive navbar for mobile, but I am having trouble getting the link containers to resize properly. I also cannot seem to get the text to center itself vertically. I have tried using auto padding and margins in both cases, but they don't seem to be doing anything. I am using Bootstrap.
My questions are:
How do I center the text vertically within its container?
How do I get the width of each link's container to be the width of the text
How do I then get the spacing between each container to be the same, so that all together they fill the width of the screen?
HTML
<div class="container" id="nav-container-mobile">
<ul class="list-unstyled list-inline text-center d-flex" id="nav-list-mobile">
<li class="list-inline-item text-center nav-link-horizontal" id="nav-profile-link-mobile">
<a class="nav-link" href="#Profile">
<div class="nav-link-container-mobile">Profile</div>
</a>
</li>
<li class="list-inline-item text-center nav-link-horizontal" id="nav-experience-link-mobile">
<a class="nav-link" href="#Experience">
<div class="nav-link-container-mobile">Experience</div>
</a>
</li>
<li class="list-inline-item text-center nav-link-horizontal" id="nav-skills-link-mobile">
<a class="nav-link" href="#Skills">
<div class="nav-link-container-mobile">Skills</div>
</a>
</li>
<li class="list-inline-item text-center nav-link-horizontal" id="nav-project-link-mobile">
<a class="nav-link" href="#project">
<div class="nav-link-container-mobile">Projects</div>
</a>
</li>
<li class="list-inline-item text-center nav-link-horizontal" id="nav-contact-link-mobile">
<a class="nav-link" href="#Contact">
<div class="nav-link-container-mobile">Contact</div>
</a>
</li>
</ul>
</div>
CSS
#nav-container-mobile {
position: absolute;
width: 100%;
height: 70px;
padding: 0;
background: #292929;
z-index: 5;
}
#nav-list-mobile {
width: 100%;
height: 100%;
margin: 0;
vertical-align: middle;
}
.nav-link-horizontal {
height: 100%;
width: 15vw;
min-width: 50px;
}
.nav-link-container-mobile {
padding: auto;
height: 100%;
overflow-wrap: break-word;
}
.nav-link {
color: #aeaeae;
}
Any help or advice is appreciated.
JSFiddle
It would be simpler to use the Bootstrap 4 Nav Fill or Justify, and then just change the background color and other styles as needed.
https://www.codeply.com/go/WsWHHIfRlh
<div class="navbar navbar-expand navbar-dark" id="nav-container-mobile">
<div class="container-fluid">
<ul class="navbar-nav nav-fill w-100">
<li class="nav-item">
<a class="nav-link" href="#">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Experience</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Skills</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Projects</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</div>
#nav-container-mobile {
width: 100%;
height: 70px;
padding: 0;
background: #292929;
z-index: 5;
}
.nav-link {
color: #aeaeae;
}

Twitter Bootstrap: Center Pills

My question is how you can center the pills?
I've tried to add center block around and also to change the float:left to float:center but nothing helps.
This has gotten much simpler! You just need to use the text-center class on the container, and apply display:inline-block to the ul. Just make sure you have a line break or paragraph tag separating the nav from any other elements within the container.
Done! 2 class additions, 1 line of CSS (don't modify the bootstrap css file!).
HTML:
<div class="col-md-12 text-center">
<p>Copyright stuff</p>
<ul class="nav nav-pills center-pills">
<li>Footer nav link</li>
<li>Footer nav link</li>
</ul>
</div>
CSS:
.center-pills { display: inline-block; }
Edit 2015: As Artur Beljajev has brought up, Flexbox support is now common enough that you may want to use that instead:
.center-pills {
display: flex;
justify-content: center;
}
If you'd like variable width pills in a variable width container, I'd suggest using the inline-block display type and adding a class to the pill container.
Here is how I extended bootstrap for centering pills.
CSS:
.centered-pills { text-align:center; }
.centered-pills ul.nav-pills { display:inline-block; }
.centered-pills li { display:inline; }
.centered-pills a { float:left; }
* html .centered-pills ul.nav-pills { display:inline; } /* IE6 */
*+html .centered-pills ul.nav-pills { display:inline; } /* IE7 */
HTML:
<div class="row">
<div class="span12 centered-pills">
<ul class="nav nav-pills">
<li>derek</li>
<li>brooks</li>
<li>is</li>
<li>super</li>
<li class="active">awesome</li>
</ul>
</div>
</div>
Or flexbox approach:
.nav-pills {
display: flex;
justify-content: center;
}
If you want the pills to be centered instead of left aligned you will need to change the css. You will need to specifiy a width and change the margin to be auto.
For example:
.tabs, .pills {
margin: 0 auto;
padding: 0;
width: 400px;
}
While the answer of #minaz works for a list with a known width, i'd propose a different solution which actually works even if you change the content of the list:
.tabs, .pills {
text-align: center;
}
.tabs li, .pills li {
float: none;
}
Or in SCSS:
.tabs, .pills {
text-align: center;
li { float: none; }
}
This will center the text inside the list, and resets the float property for the list items so they are affected by the text-align property.
A simple solution with Bootstrap 4
Use justify-content-center at <ul> nav
Example:
#import url('https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="mt-5">
<ul class="nav nav-pills mb-3 justify-content-center" id="pills-tab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
</li>
</ul>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
<div class="card text-white bg-success mb-3 mx-2 px-2">
<h1>Hi</h1>
</div>
</div>
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
<div class="card text-white bg-info mb-3 mx-2 px-2">
<h1>Hi</h1>
</div>
</div>
<div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">
<div class="card text-white bg-danger mb-3 mx-2 px-2">
<h1>Ni Hao</h1>
</div>
</div>
</div>
</div>
Bootstrap 4 solution is very easy:
<ul class="nav justify-content-center">
https://getbootstrap.com/docs/4.0/components/navs/