I'm currently working on a CSS-based 3-column layout that has the following requirements:
On the desktop…
…all columns are shown.
On mobile devices…
…only the middle column is shown.
…the left column can slide in from the left, triggered by a swipe or a tap on a button.
…the right column can slide in from the right, triggered by a swipe or a tap on a button.
Independent of the device…
…the middle column contains three rows: The main header, a sub header, and the actual content.
…the main header scrolls away when scrolling down.
…the subheader is sticky on the top of the screen when scrolling down.
Now I tried to implement this:
Creating a 3-column layout and hiding the left and right columns is easy, using Bootstrap.
Having three rows in the middle column is easy, too.
To make the subheader sticky, I have two options:
Use position: sticky (best solution in technical terms, but not supported by any browser).
Use a script, attach to the scroll event and change to position: fixed on demand. This is what Bootstrap offers OOTB with its affix plugin. Using this plugin, it's an easy task, too.
Creating two sidebars and sliding them in is easy as well, using something such as Snap.js.
Problems start when I want to combine the sticky subheader with the sliding sidebars: The affix plugin simply stops working, and the subheader is not sticky any more. Basically, the problem comes down to issues with CSS transform and position: fixed, see Eric Meyer's awesome blog post on this and this answer.
One option to solve this could be to put the headers above the area where the sidebars slide in, so that they are not affected, but this is not what I want: I want the sidebar to push away everything.
How can I solve this? Is this possible at all?
Consider this post:
Applying snap.js to my main content wrapper seems to break *some* of my jQuery functions
Bootstraps affix.js listens to the $(window).on('scroll'... event. Snap.js seems to change the scrollable element from the "window" element to the element where you added the "snap-content" class. I dont see any other solution as to write the sticky functionality provided by bootstrap yourself.
Use this as a reference. Based on your current scroll position (in pixels) you can add css attributes or even whole css classes to the element you want to make sticky:
jQuery(document).ready(function ($) {
$('.snap-content').scroll(function () {
if ($(this).scrollTop() > 140) {
if ($("#navbar").css('position') !== 'fixed') {
$("#navbar").css("position", "fixed");
}
} else {
if ($("#navbar").css('position') !== 'static') {
$("#navbar").css("position", "static");
}
}
});
});
Related
I'm using Astra template with Elementor plugin.
I've set up my navbar to be fixed - to scroll alongside the webpage, but now my items are appearing under it. And i'm not talking about the z-index issue, but the first thing that comes after navbar - breadcrumbs + title are both under navbar.
.main-header-bar-wrap{
position:fixed;
top:0;
width:100%;
}
I solved the issue using --
padding-top:100px;
But i don't really think that's the best practice.
Is there any better solution?
Thank you!
There's not really one foolproof way of doing this unfortunately. Fixed elements are taken completely out of the flow of the page and how it renders so don't take up any space. https://developer.mozilla.org/en-US/docs/web/css/position#fixed
The way you've done it is one option, another is to change the padding to match the height on resizing the window (to make sure the height is always correct).
e.g. something like:
$(window).resize(function () {
$(".main").css("padding-top",$(".main-header-bar-wrap").outerHeight());
})
The other option is to create a hidden duplicate of the header, with position: relative and visibility: hidden, which will take up the required space but not be visible. Just make sure to add the aria-hidden="true" property so people with screen readers don't end up with a duplicate menu.
You could do this with js as follows:
$( ".main-header-bar-wrap" ).after(
$(".main-header-bar-wrap").clone().addClass("spacer").attr("aria-hidden","true")
);
This will duplicate the header and add the class spacer to the second version so you can style it separately with the visibility and position properties, along with the aria-hidden attribute.
I want to create a div with CSS (preferably without any library) whose contents can be slided right/left on mouse-click if the content of the div is larger than the div width. Like below:
I tried with overflow-scroll but that gives a scroll-bar which is not what I want. Is it possible to create this with CSS and JavaScript (AngularJS, which I am using in my app) combination, without using any other library? I can use jQuery if it can done using that.
Update: Created a working demo after #Shnibble's answer.
$("#left").mousedown(function() {
var y = $("#content").offset();
$("#content").animate({
left: 0
}, function callback() {
$("#left").fadeOut();
});
});
$("#right").mousedown(function() {
var y = $("#content").offset();
$("#left").show();
$("#content").animate({
left: y.left - 200
});
$("#info").text(y.left - 100);
});
I'm not sure I know exactly what you want, do you want the <> arrows to scroll the content or do you want to click+drag the content left and right? Both can be accomplished with JQuery.
The former can be done by having the parent container positioned as relative and the content within positioned as absolute. Then you can use JQuery to alter the left offset of the content +/- depending on which arrow is clicked.
For the latter, you would do the same setup but check the mouse coordinates on click and apply the difference to the content left attribute until the mouse button is released.
As for making all of this only happen if the content overflows, you will need to do some checking with JQuery again to measure the width of the content and compare it to the parent container and only allow one of the above functions to run if the content is wider.
Alternatively, you co use overflow-x: auto and then style the scrollbar to fit in with your theme better? See here: https://css-tricks.com/custom-scrollbars-in-webkit/.
Hi,
I am trying to built a chrome app and decided to use the theme SB Admin 2 for this purpose. I modified the theme to look something like what is show in the image above. The three boxes represents three bootstrap rows. I want row 3 to be fixed to the bottom of the page.Tried suggestions i got from stack overflow[added class "navbar-fixed-bottom" to the row], was able to get the row fixed to the bottom of the page but the footer was overflowing to the left side navigation bar.
What can be done to achieve this.
You can try setting the "left" for the footer. But without any code, we can't help you and we need to guess.
.footer
{
left: 200px; /* Use the width of your left menu */
}
How do I produce a footer that spans the whole width of the screen (despite screen resolution) and stays at the bottom of the page?
When I search for this I get people providing code for sticky footers (footers that hoover with the page), and saying use absolute positioning (which I hear is a no no).
A perfect example of what I mean is the one of the sites twitter bootstrap cites as an example (https://www.gathercontent.com/). On that site the footer (which looks like a hero unit or something) is always on the bottom of the page.
If you are using bootstrap, this example shows how you can do it. The name is misleading on the site, but it stays at the bottom of the page if there isn't much content. It also moves down the page if there is a lot of content (so doesn't stick).
The footer:
http://getbootstrap.com/docs/4.0/examples/sticky-footer-navbar/
The CSS:
http://getbootstrap.com/docs/4.0/examples/sticky-footer-navbar/sticky-footer-navbar.css
Make sure not to forget the
html {
position: relative;
min-height: 100%;
}
Use this jquery:
$(window).load(function () {
$('.footer').width($(window).width());
$(window).resize(function () {
$('.footer').width($(window).width());
});
});
Make sure you have a js file though
I was looking for something similar as I did not want the footer to hover. The footer was blocking the pull down menu elements on small mobile devices. All I did was to remove "navbar-fixed-bottom" from the footer class.
I changed
<div class = "footer navbar-fixed-bottom"></div>
to
<div class = "footer"></div>
I am trying to help a graphic designer with her website http://designingforgood.tumblr.com/
She wanted the SnapWidget which is the grid of six photos on the bottom left so I placed it there. My concern is that it gets cut off in smaller windows which may mean that on smaller screens it will also not be displayed properly.
Also, in different browsers the distance from the blue box above the photo grid changes. It looks further away in firefox than in chrome. I worry that in some other browser it may even end up overlapping.
I searched for an answer on w3schools but didn't find what I was looking for. I also searched for similar questions here on stackoverflow.
Well you have inline styles on the div containing your photos that is settings its position as fixed. Your sidebar is also set to fixed. The only way to make sure that section is scrollable is by removing both of those values and fixing it up from there.
Remove position fixed from all divs inside the sidebar.
Wrap both sidebar and thumbnails inside another div and give an id sidebar-container to this div. You should end up with this main structure for your sidebar:
<div id="sidebar-container">
<div id="blue-box">...</div>
<div id="photo-grid">...</div>
</div>
Fix any css problems you have. Make sure website looks good in both situations, when sidebar has fixed position and when it hasn't. Maybe you want to float the sidebar to work well in both cases.
Use jQuery (I see you load it anyways) to determine if the height of the #sidebar-container is less than the height of the window and only then, we add position:fixed;.
The jQuery code might be like this:
function checkHeight() {
var sidebarHeight = jQuery("#sidebar-container").height();
var windowHeight = jQuery(window).height();
if( sidebarHeight <= windowHeight ) {
jQuery("#sidebar-container").css({ 'position' : 'fixed' });
} else {
jQuery("#sidebar-container").removeAttr("style");
}
}
jQuery(document).ready(checkHeight);
jQuery(window).resize(checkHeight);
So, users with big screens will benefit from the fixed sidebar and users with small screens will have the ability to view the whole sidebar by scrolling down.