<div class="wrapper">
<div class="box">
<div class="element" contenteditable>margin auto in position</div>
</div>
</div>
<div class="form">
<div class="control">
<input type="checkbox" class="checkbox" id="margin-left" name="margin-left">
<label for="margin-left">margin-left: auto</label>
</div>
<div class="control">
<input type="checkbox" class="checkbox" id="margin-right" name="margin-right">
<label for="margin-right">margin-right: auto</label>
</div>
<div class="control">
<input type="checkbox" class="checkbox" id="margin-top" name="margin-top">
<label for="margin-top">margin-top: auto</label>
</div>
<div class="control">
<input type="checkbox" class="checkbox" id="margin-bottom" name="margin-bottom">
<label for="margin-bottom">margin-bottom: auto</label>
</div>
</div>
@import url("https://fonts.googleapis.com/css?family=Gochi+Hand");
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
background-color: #291642;
font-family: "Gochi Hand", sans-serif;
color: #fff;
font-size: 130%;
letter-spacing: 0.1rem;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 100vw;
min-height: 100vh;
padding: 10px;
}
.form {
padding: 20px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.control {
margin: 20px 10px;
display: flex;
align-items: center;
}
.checkbox {
margin: 0 5px 0 0;
}
.wrapper {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.box {
width: 60vw;
min-height: 50vh;
border-radius: 5px;
padding: 30px;
border: 1px dashed #fff;
position: relative;
}
.element {
text-align: center;
background-color: #f36;
width: 300px;
height: 200px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 5px;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
View Compiled
const checkboxs = document.querySelectorAll(".checkbox");
const elemment = document.querySelector(".element");
checkboxs.forEach((checkbox) =>
checkbox.addEventListener("change", (e) => {
if (e.target.checked) {
if (e.target.name === "margin-left") {
elemment.style.marginLeft = `auto`;
}
if (e.target.name === "margin-right") {
elemment.style.marginRight = `auto`;
}
if (e.target.name === "margin-top") {
elemment.style.marginTop = `auto`;
}
if (e.target.name === "margin-bottom") {
elemment.style.marginBottom = `auto`;
}
} else {
if (e.target.name === "margin-left") {
elemment.style.marginLeft = `unset`;
}
if (e.target.name === "margin-right") {
elemment.style.marginRight = `unset`;
}
if (e.target.name === "margin-top") {
elemment.style.marginTop = `unset`;
}
if (e.target.name === "margin-bottom") {
elemment.style.marginBottom = `unset`;
}
}
})
);
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.