Hiding web part properties based on screen size

Daniel Frye asked on January 14, 2020 23:31

Is there a way to hide a we part based on screen size using the visibility field? I'm wanting the user to get two different elements of the website based on screen size.. mobile or desktop. Obviously just doing a display none is the quick way out by just using css... But ideally I would like the "mobile" element to not even show or load on the website when using desktop.

Recent Answers


Sean Wright answered on January 15, 2020 04:29

@Daniel

TLDR; nothing in Kentico supports this, namely because browsers don't support it.

While you could possibly accomplish this, you'd have to implement it yourself and I don't think it's worth your effort pursuing it.

Browsers don't send the screen size with normal HTTP requests. Tools like Google Analytics collect this information manually and send it to the server. However, this information can only be collected once the browser has loaded the page, which means you can't respond, with the first request to the server, with content hidden/displayed based on resolution.

What you are looking to do is exactly what media queries in CSS were designed to handle.

Your other option is load the data/HTML via XHR/Fetch on the client side once you have the resolution information. You could use MediaQuery Event Listeners to trigger something when the resolution changes.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on January 15, 2020 08:47

What about using device profiles and conditional layouts?

0 votesVote for this answer Mark as a Correct answer

Sean Wright answered on January 15, 2020 14:37 (last edited on January 15, 2020 14:40)

@Juraj

Don't device profiles use User-Agent sniffing? That would be different than hiding something based on screen resolution.

It also looks like some browser vendors are trying to move away from User Agents.

@Daniel

Juraj's recommendation is probably the closest thing to server-side conditional rendering based on resolution, however there are caveats with it (if a new device/browser uses a different User-Agent, it won't work - it also isn't an option for Kentico 12 MVC)

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on January 15, 2020 15:10

@Sean - well he mentioned "mobile vs. desktop", while the screen resolution is one of the parameters here, if they want to use different web parts for mobile devices and for desktops, I would use device profiles. Otherwise, maybe some custom macro in the web part visibility property which will be checking the screen resolution - not sure if it is even possible. But makes no sense to hide something if I resize a window on my desktop :-)

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.