function toggleDivs() {
var container = document.querySelector(".container");
var top = document.querySelector(".top");
var bottom = document.querySelector(".bottom");
if (container.style.flexDirection === "column") {
container.style.flexDirection = "row";
top.style.flex = "3";
bottom.style.flex = "7";
top.style.height = "auto";
bottom.style.height = "auto";
} else {
container.style.flexDirection = "column";
top.style.flex = "3";
bottom.style.flex = "7";
top.style.height = "100%";
bottom.style.height = "100%";
}
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.top {
flex: 3;
background-color: blue;
border: 1px solid black;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.bottom {
flex: 7;
background-color: green;
border: 1px solid black;
}
input[type="checkbox"] {
height: 0;
width: 0;
visibility: hidden;
}
label {
cursor: pointer;
text-indent: -9999px;
width: 50px;
height: 25px;
background-color: grey;
display: block;
border-radius: 100px;
position: relative;
}
label:before {
content: "";
position: absolute;
top: 1px;
left: 1px;
width: 23px;
height: 23px;
background-color: #fff;
border-radius: 90px;
transition: 0.3s;
}
input:checked + label:before {
left: calc(100% - 1px);
transform: translateX(-100%);
}
.toggle-button {
margin-top: 10px;
padding: 10px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.toggle-button:hover {
background-color: #3e8e41;
}
input[type="text"] {
margin-bottom: 10px; /* added this */
}
@media screen and (min-width: 768px) {
.container {
flex-direction: row;
}
input[type="text"] {
margin-bottom: 0; /* added this */
}
}
<input type="checkbox" id="toggle-switch" onclick="toggleDivs()" />
<label for="toggle-switch"></label>
<div class="container">
<div class="top">
<input type="text" placeholder="Text box 1" />
<input type="text" placeholder="Text box 2" />
<input type="text" placeholder="Text box 3" />
<input type="text" placeholder="Text box 4" />
<input type="text" placeholder="Text box 5" />
<input type="text" placeholder="Text box 6" />
<input type="text" placeholder="Text box 7" />
<input type="text" placeholder="Text box 8" />
</div>
<div class="bottom"></div>
</div>
as we can see in above code we are able to toggle div using toggle switch. Im looking to fix this code in such a way that when top div is vertical I would like to have all the textboxes one below each other and when its horizontal, then we can rearrange first 4 text boxes in single row and then next 4 text boxes and so on..
in short how we can toggle and rearrange text boxes at the same time.
toggling and rearranging fields using toggle switch.
2
Answers
When the top div is in a horizontal layout, you can modify the toggleDivs() function:-
There is additional two div elements with the class "textbox-row" to group the textboxes. When the top div is in a horizontal layout, these rows will be displayed as flex containers, allowing the textboxes to be rearranged in a row-based layout. When the top div is in a vertical layout, the "textbox-row" class is removed, and the textboxes will stack vertically as they are block elements by default.
Working Link Tested:- Codesandbox Code
I suspect that what you are trying to achieve should be done with a grid layout.
Due to the comments, I spent an hour figuring out how to modify the JavaScript to add a class instead of the inline style. The use of a media query to declare the initial
grid-template-columns
value makes the JS and CSS a bit more complex withif..else if..else
statement,.row
class and!important
flag.