I am making a application layout that consists of left and right section.
.container {
display: flex;
flex: 1;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.leftSection {
flex: 0.7;
background-color: #ccc;
}
.rightSection {
flex: 0.2;
background-color: #ccc;
}
<div class="container">
<div class="leftSection">
Left Section
</div>
<div class="rightSection">
Right Section
</div>
</div>
In this the .rightSection
div will be as it is, and need to look only into left section.
The left section will have a background image with some space in which I need to include the sub component diagram’s.
The background image will be like,
And the expected result of filling the sub components is like,
So I have tried to fill the left section like,
.container {
display: flex;
flex: 1;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.leftSection {
flex: 0.7;
}
.rightSection {
flex: 0.2;
background-color: #ccc;
}
.bgContainer {
background-image: url('https://i.stack.imgur.com/AfygH.png');
height: 401px;
width: 688px;
background-repeat: no-repeat;
position: relative;
margin: 0 auto;
}
.coil {
position: absolute;
top: 49.55%;
left: 24.3%;
}
.evaporator {
position: absolute;
top: 7.25%;
left: 54.5%;
}
.compressor {
position: absolute;
top: 53.15%;
left: 59.2%;
}
<div class="container">
<div class="leftSection">
<div class="bgContainer">
<div class="coil">
<img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
</div>
<div class="evaporator">
<img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
</div>
<div class="compressor">
<img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
</div>
</div>
</div>
<div class="rightSection">
Right Section
</div>
</div>
So far everything is fine. But I am implementing this in for tablet screen’s where the width of the device will start from 600px
to 1400px
.
So i need to make this section responsive across all the devices starting from width 600px
to 1400px
.
So I breakdown setting up some width to the parent div considering it as device width like 600
, 1024
, and 1200
(just for eg..,) I have made it like this static width.
.first {
width: 600px;
border: 2px solid #000;
margin: 30px;
}
.second {
width: 1024px;
border: 2px solid #000;
margin: 30px;
}
.third {
width: 1200px;
border: 2px solid #000;
margin: 30px;
}
.container {
display: flex;
flex: 1;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.leftSection {
flex: 0.7;
}
.rightSection {
flex: 0.2;
background-color: #ccc;
}
.bgContainer {
background-image: url('https://i.stack.imgur.com/AfygH.png');
height: 401px;
width: 688px;
background-repeat: no-repeat;
position: relative;
margin: 0 auto;
}
.coil {
position: absolute;
top: 49.55%;
left: 24.3%;
}
.evaporator {
position: absolute;
top: 7.25%;
left: 54.5%;
}
.compressor {
position: absolute;
top: 53.15%;
left: 59.2%;
}
<div class="first">
<div class="container">
<div class="leftSection">
<div class="bgContainer">
<div class="coil">
<img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
</div>
<div class="evaporator">
<img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
</div>
<div class="compressor">
<img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
</div>
</div>
</div>
<div class="rightSection">
Right Section
</div>
</div>
</div>
<div class="second">
<div class="container">
<div class="leftSection">
<div class="bgContainer">
<div class="coil">
<img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
</div>
<div class="evaporator">
<img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
</div>
<div class="compressor">
<img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
</div>
</div>
</div>
<div class="rightSection">
Right Section
</div>
</div>
</div>
<div class="third">
<div class="container">
<div class="leftSection">
<div class="bgContainer">
<div class="coil">
<img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
</div>
<div class="evaporator">
<img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
</div>
<div class="compressor">
<img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
</div>
</div>
</div>
<div class="rightSection">
Right Section
</div>
</div>
</div>
In this above snippet, inside .first
, the left section comes out and in .second
and third
also its in different position.
So finally my requirement is how to make the left section with background image to be responsive along with all tablet screens starting from 600
pixels to 1400
pixels at least.
In real application: Right now my main issue is that the design goes out of the screen’s mostly at bottom like,
I have tried using transform: scale(2.0);
but still the left section goes beyond the screen. as like the above image, should I need to calculate this scale value dynamically? Kindly please help me to get out this problem as I am struggling for very long time.
Update1:
If I give width to specific div like,
.coil {
position: absolute;
top: 49.55%;
left: 24.3%;
width: 17%;
}
.evaporator {
position: absolute;
top: 7.25%;
left: 54.5%;
width: 12%;
}
.compressor {
position: absolute;
top: 53.15%;
left: 59.2%;
width: 13%;
}
then I get issues when the device height is smaller or larger.
After the implementation,
The below screenshot is captured in the screen viewport with height: 844 and width: 1280
The below screenshot is captured in the screen viewport with height: 552 and width: 1024
4
Answers
Maybe try this CSS code for the .container class using background-size: cover:
First, we could just make the background image as normal block so that it will be the main width and height. Then we can set the width of the image to
100%
and With background image being like that, it will maintain its aspect ratio. Since you manually calculate thetop
,left
positions for the components, why not manually calculate thewidth
as well:This should be responsive for all devices. You could also automate the calculating with javascript if you really need to, but I made this solution fixed because you manually calculate the
top
,left
positions.With in document SVG, you can use the SVG as any other regular image and size it to your requirements, while still being able to access the HTML making up the SVG and modify its CSS properties.
tl;dr
display: none
in doc SVGsymbol
element with anid
in thedefs
section.<svg><use href="#the_id"></use></svg>
read
Because the entire SVG is treated as a single image, all elements in that SVG will stay in their respective locations and scale proportionally without requiring any CSS to position or resize them.
This is what I did for the snippet:
flex
andmin-width
in elements.spec
and.info
to meet your requirements.<a>
to show that you can use regular HTML tags in a SVG.(The demo uses a 3:2 ratio for the panels (the
flex
values) andmin-width: 20rem
(360px minus left/right spacing) to force the wrap).snippet