Making rightmost column fixed position in Bootstrap 4

Making rightmost column fixed position in Bootstrap 4



I'm trying to have the right column in Bootstrap 4 grid system fixed using position: fixed.


position: fixed



Here's a demo and it's code.



I want the button group on the right to stick to the viewport when I scroll the document. I have tried adding position: fixed; this doesn't work as intended - it causes the button group to overlay on top of the window like this.


position: fixed



I have already seen this answer on making columns fixed in Bootstrap, but the solutions won't work when the position of the columns is interchanged. Few of the solutions there will cause the right column to overlay on top of the left column in small screens, which is undesirable. In such cases, when viewed on small screens, I want the columns to be stacked (with the sticky feature disabled).



I prefer CSS solutions but if possible, I'd also like to know how it's done in JS.




2 Answers
2



Place your buttons inside another div and then add a style of position: fixed to that div like so:


div


position: fixed


<div class="col-md-3" align="center" style="margin-bottom: 40px;/* position: fixed; */">
<div style="position: fixed;">
<button class="btn btn-success btn-custom">
<span>Button 1</span>
</button>
<br>
<button class="btn btn-warning btn-custom">
<span>Button 2</span>
</button>
<button class="btn btn-danger btn-custom">
<span>Button 3</span>
</button>
</div>
</div>



You can make the style a css class and make it show only on big screens


css






Small correction to your answer. If we use an inner div and add a position: fixed, it'll cause the button group to become larger than its parent - col-md-3 in this case. Fix would be to set the width of the outer div to some value, use the same width for the inner buttons and make the inner div inherit width from its parent using width: inherit.

– rvadaga
Sep 16 '18 at 8:34


position: fixed


col-md-3


width: inherit



Bootstrap 4 has the position-fixed class for this...


position-fixed


<div class="container">
<div class="row">
<div class="col-md-9">
..
</div>
<div class="col-md-3" align="center">
<div class="position-fixed">
<button class="btn btn-success btn-custom">
<span>Button 1</span>
</button>
<br>
<button class="btn btn-warning btn-custom">
<span>Button 2</span>
</button>
<button class="btn btn-danger btn-custom">
<span>Button 3</span>
</button>
</div>
</div>
</div>
</div>



https://www.codeply.com/go/exOfOB2Igy



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)