I'm creating my own dropdown search bar that displays results when a user types some text.
The problem is that the result list div has to be position: absolute. This leads it to ignoring the parent col container width. How do I get results list to fit within the width of the col?
.player-search-list {
z-index: 2;
background-color: white;
position: absolute;
width: 100%;
}
<div class="row">
<div class="col-6">
<!-- some other content -->
</div>
<div class="col-6">
<div class="top-buffer">
<!-- Relative search bar that expands to bootstrap col -->
<div class="player-search-bar input-group rounded-bottom-0">
<!-- search functionality-->
</div>
</div>
<!-- ***ABSOLUTE drop down*** that doesnt fit inside bootstrap col -->
<div class="list-group player-search-list" id="playerSearch">
<div *ngFor="let result of results | slice:0:9">
<!-- drop down results list functionality -->
</div>
</div>
</div>
</div>
I used:
.player-search-list{
z-index: 2;
background-color: white;
position: absolute;
width: 100%;
left: 0;
padding-left: 15px;
padding-right: 15px;
}
The left is to center the div inside the container. The padding-left and padding-right is the same amount of the col container. This keeps the divs in the same position and size of the search bar.
In Case somebody stumbles on this question and is still looking for the result:
Your Parent DIV (in this case .col-6) must be position:relative.
The width of the search results must be set to 100%!
parent_container {
position: relative;
}
search_results {
position: absolute;
width: 100%
}
Related
I've created a sidebar, where I have three container elements. I want them to be sticky as the user scrolls through the page and when the user reaches the footer I want these elements to behave like normal elements and not overlap with each other.
Here is the code representing the problem. How to resolve this overlapping issue?
<div class="sidebar">
<div class="sidebar-subscription-form-container">
<div class="sidebar-subscription-form">
<p>I would hold all the subscription form elements</p>
</div>
</div>
<div class="social-buttons-container">
<div class="social-buttons">
<p>
I would contain all the social buttons
</p>
</div>
</div>
<div class="sidebar-banner-container">
<div class="sidebar-banner">
<p>
Here is the sidebar banners
</p>
</div>
</div>
</div>
.sidebar-subscription-form-container {
top: 0px;
height: 30px;
z-index:1;
position: sticky;
}
.social-buttons-container {
top: 400px;
height:30px;
z-index:1;
position: sticky;
}
.sidebar-banner-container {
top: 800px;
height:30px;
z-index:1;
position: sticky;
}
https://jsfiddle.net/nirmalkumar1997/L0yd5hq1/84/
Since you have hardcoded the height values of each element, why not simply offset the top property in each element by the height value times the number of elements down it is:
.sidebar-subscription-form-container {
top: 0px;
height: 30px;
z-index: 1;
position: sticky;
}
.social-buttons-container {
top: 400px;
height: 30px;
z-index: 1;
position: sticky;
top: 30px;
}
.sidebar-banner-container {
top: 800px;
height: 30px;
z-index: 1;
position: sticky;
top: 60px;
}
<div class="sidebar">
<div class="sidebar-subscription-form-container">
<div class="sidebar-subscription-form">
<p>I would hold all the subscription form elements</p>
</div>
</div>
<div class="social-buttons-container">
<div class="social-buttons">
<p>
I would contain all the social buttons
</p>
</div>
</div>
<div class="sidebar-banner-container">
<div class="sidebar-banner">
<p>
Here is the sidebar banners
</p>
</div>
</div>
</div>
I added 30px for the second element, and 60px for the 3rd element.
To make this more dynamic, you could use a CSS custom property to keep everything in sync if you make changes to height, and run a calc on the element offset * height.
::root {
--container-height: 30px
}
.sidebar-subscription-form-container {
top: calc(var(--container-height) * 0);
height: var(--container-height);
z-index: 1;
position: sticky;
}
.social-buttons-container {
top: 400px;
height: var(--container-height);
z-index: 1;
position: sticky;
top: calc(var(--container-height) * 1);
}
.sidebar-banner-container {
top: 800px;
height: var(--container-height);
z-index: 1;
position: sticky;
top: calc(var(--container-height) * 2);
}
<div class="sidebar">
<div class="sidebar-subscription-form-container">
<div class="sidebar-subscription-form">
<p>I would hold all the subscription form elements</p>
</div>
</div>
<div class="social-buttons-container">
<div class="social-buttons">
<p>
I would contain all the social buttons
</p>
</div>
</div>
<div class="sidebar-banner-container">
<div class="sidebar-banner">
<p>
Here is the sidebar banners
</p>
</div>
</div>
</div>
The problem with your code is the height of section class.
As the nearest block-ancestor to all the three containers is section, they will stick as soon as they hit the edge of the section class.
According to Mozilla Documentation
A stickily positioned element is an element whose computed position value is sticky. It's treated as relatively positioned until its containing block crosses a specified threshold (such as setting top to value other than auto) within its flow root (or the container it scrolls within), at which point it is treated as "stuck" until meeting the opposite edge of its containing block.
You can find the entire info here - Link
For achieving sticky behavior and removing overlapping of containers, you can try to increase the height of section class. For example -
.section {
height: 4000px; // Needs greater value as top is 400px and 800px for children elements.
}
This question already has answers here:
How to create a fixed sidebar layout with Bootstrap 4?
(5 answers)
Closed 3 years ago.
I have a left div with id sideNav and a right main div like this
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
}
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5" id="sideNav">
...
</div>
<!-- main content area -->
<main class="col-10">
...
</main>
</div>
The code works but the left part of main is now inside behind the sideNav. When I remove the position property of the sideNav css, my divs are displayed correctly again but sideNav is no longer fixed and scrolls with the page.
How do I keep sideNav fixed and my divs properly displayed?
The reason this isn't working for you, is because row has display type flex, so that all the cols below fit into that row.
Your cols are then blocks spaced via their given value e.g. col-3 col-4 etc
By changing the column display type (to fixed in your example) your removing it from the flex spacing, and so your main nav will move left because you haven't offset it.
To fix this, don't add padding as others have suggested, BootStrap has classes that handle this. Instead, add offset-2 to your main nav, and leave everything else as is.
Example Snippet
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5" id="sideNav">
...
</div>
<!-- main content area -->
<main class="col-10 offset-2">
...
</main>
</div>
Thats because fixed gets "ignored" by other containers. It is handelt as if it was absolute. You can give your sidebar a fixed width and your main a padding.
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
width: 350px;
}
main {
padding-left: 350px;
}
Codepen
Lets try this,
iam adding width for sideNav about 40px;the same padding-left iam put #main-content.
html
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5" id="sideNav">
<p>paragraph...</p>
</div>
<!-- main content area -->
<main class="col-10" id="main-content">
<p>paragraph...</p>
<p>paragraph...</p>
</main>
</div>
css
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100%;
width:40px;
}
#main-content{
width:100%;
float:left;
padding:0 0 0 40px;
background:yellow;
}
I saw you are using bootstrap grid, so to keep the grid working I recommend putting your fixed container in the .col-2 div
#sideNav {
background-color: #012d20;
position: fixed;
z-index: 1;
top: 0;
left: 0;
overflow: hidden;
border-right-style: groove;
height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<!-- navigation sidebar -->
<div class="col-2 pt-5">
<div id="sideNav">
...
</div>
</div>
<!-- main content area -->
<main class="col-10">
...
</main>
</div>
I have a task to build a sidebar, whose height is equal to the content one.
The more content is, the bigger sidebar is.
In reality, my sidebar is equal to the window size, not content. If I scroll the page down, there is no sidebar on it.
That's me code:
HTML:
<div class = "container" style = "height :100%">
<div class = "row" style = "height:100%">
<div class = "col-md-3 hidden-xs hidden-sm" style = "height:100%">
<div class = "sidebar">
.........
</div>
</div>
</div>
</div>
CSS:
html .sidebar,
body .sidebar {
height: 100%;
width: 100%;
background: black;
}
Why? What to do to make it work?
If I get what you mean properly, then you want the sidebar to be fixed?
In order to do this you can do something a bit like this in the css..
.sidebar {
width: 250px;
height: 100vh;
position: fixed;
top: 0;
left: 0;
background: black;
}
<div class="sidebar">
</div>
I'm having trouble setting up a "master-detail" view using Bootstrap 3. I'm attaching a picture of the specific layout I'm trying to get:
I've created a div with .row spanning the 12 columns for the header, and then another div.row with a div.col-md-4 and div.col-md-8 for the two columns. But obviously this doesn't achieve what I'm looking for. I've tried playing with the position attribute, but it seems to conflict with the Bootstrap CSS and I always just get jumbled text.
Thank you very much in advance and let me know if you'd like me to provide more details on what I'm looking for. Hopefully the pic is clear.
You are looking for
position: fixed;
This ensures that the element is always static relative to the viewport. You will then use the relative positioning properties to place it where you want it to go.
For the top element (say, your navbar), you can set:
top: 0;
For the bottom element, you can set:
bottom: 0;
So, something like this:
.row, .col-md-4, .col-md-8 {
border: 2px solid pink; /* For showing the div borders */
}
.row.main-content {
background-color: blue;
height: 300px;
margin-top: 20px; /* Needs to match the height of .row.top */
}
.row.top {
position: fixed;
top: 0;
height: 20px;
width: 100%;
background-color: red;
z-index: 9999;
}
.row.bottom {
position: fixed;
bottom: 0;
height: 20px;
width: 100%;
background-color: green;
z-index: 9999;
}
.scrollable {
overflow-y: scroll;
height: 300px; /* May need to be set somehow */
}
<div class="container-fluid"><!-- To get it to take up the whole width -->
<div class="row top">
</div><!-- row -->
<div class="row main-content">
<div class="col-md-4">
<div class="sub-header">
</div>
<div class="scrollable">
<!-- menu items here -->
</div>
<div class="sub-footer">
</div>
</div>
<div class="col-md-8 scrollable">
<!-- Product detail goes here -->
</div>
</div><!-- row -->
<div class="row bottom">
</div><!-- row bottom -->
</div><!-- container-fluid -->
Hope that helps.
I have a bar chart that includes 5 different sub-containers inside, each a different color. I want the 'chicken' sub-container to be on the bottom of the bar (zero bottom-margin), but for some reason it's not working. In fact, when I use absolute positioning for the sub-container, it doesn't even show up. (note: the height of each sub-container is determined by Javascript and I've tested the JS and it works fine so I think the issue is in the HTML/CSS).
HTML:
<div class="days">
<div class="days-container">
<div class="dairy"></div>
<div class="beef"></div>
<div class="pork"></div>
<div class="eggs"></div>
<div class="chicken"></div>
</div> <!-- end days-container-->
</div> <!-- end days -->
CSS:
.days {
float: left;
height:330px;
width: 1em;
}
.days-container {
position:relative;
height: 330px;
}
.chicken {
position: absolute;
bottom:0;
background-color: #00AAFF;
}
The reason why you don't see it is because it has a 0px width. Try giving .chicken a 100% width.