So, I have a little bit of a weird idea. The background image for the landing of my (work in progress) website is a building I made in blender, and my idea is to put a simple div exactly over the door of the building, which will redirect the user from the landing page to the real homepage upon click.
I have two different versions of this background image, that I switch between via media query: a standard (horizontal) version for devices that have a landscape orientation, and an aptly cut (vertical) version for devices that have a portrait orientation. This means that the position of the door is wildly inconsistent between devices.
Now, the issue is that I’m going insane trying to find a solution to position this "door" div with good responsiveness. Any tricks to avoid using a billion media queries? If not, at least what’s the smartest way to go about those queries? (Using percentages? Using vh and vw? Using pixels?)
Thanks in advance.
3
Answers
I think you have to use @media unless you use tools like bootstrap or tailwind.
I know there are standards though to make it a little easier.
Mobile: 320-480px
Tablet: 481-768px
Desktop: 769-1024px
Xl Desktop: 1201px and up
I use Tailwind and for this you write inline css like md: lg: xl: instead of pixels
What front end framework are you using?
You could try and build that with a display of grid.
Also there’s a kinda new way instead of @media which is @container . you can check this video i think it can be really helpfull.
The way to do this is by creating an SVG which contains both your image and its clickable areas (implemented as SVG shapes or paths). This ensures that both the image and its clickable areas will stay in sync as the SVG scales, stretches or crops.
Let’s start with the image. For this example, I’ve chosen an image from picsum.photos which contains three doors:
It’s image number 819, and I’ll scale it down to 1000 pixels wide:
http://picsum.photos/id/819/1000
The first step is to define an SVG
<image>
element to reference this, setting the width and height to match the intrinsic width and height of the image. This image happens to be square, so both width and height are 1000 pixels.Then set the SVG viewbox to match.
Then define the clickable areas within this space. We can use any SVG shapes or paths to do this; in this case the doors are simple rectangles, so we can use
<rect>
. An SVG editing application can help make this step easy, particularly if you are needing more complex shapes for the clickable areas.Finally, we need to wrap each
<rect>
in an anchor<a>
tag, so that we can use it as a link. Usually you would just set thehref
attribute to specify the link destination, but in this snippet I am using Javascript to trigger an alert box. Run it and tap on each of the three doors.The final step is to make the SVG cover the viewport. With an image, this would be achieved by styling the image with
object-fit
andobject-position
, but these don’t work with inline SVG. Similar effects can be achieved using SVG’spreserveAspectRatio
attribute; the following value is the equivalent ofobject-fit: cover
combined withobject-position: center bottom
:Here is a snippet to demonstrate the finished solution. After running it, use the full page link to properly test the responsive behaviour.
It’s also worth taking the ImageMapper website for a spin; it is a code generator which does the hard work for you with a sweet point-and-click interface.