<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page title</title>
<style>
html,
body {
height: 100%;
background: green;
margin: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
#container {
width: 80%;
height: 90%;
background: linear-gradient(#134a3b, #145040);
display: flex;
flex-direction: column;
}
.child {
width: 80%;
height: 45%;
background: red;
align-self: center;
justify-self: center;
}
</style>
</head>
<body>
<div id="container">
<div id="inputs" class="child">
<input id="input">
</div>
<div id="outputs" class="child"></div>
</div>
</body>
</html>
Can you please help me center each child div without using margin: auto
?
I am trying to understand why my css code is not working.
2
Answers
Centering elements without using margin and position can be achieved using different CSS techniques. Here are a few alternative methods to center the child divs:
You can use display: flex on the parent container and set margin: auto on the child elements. This will automatically center the child elements within the parent container.
Another way to center the child elements within the parent container using flexbox is by setting position: relative on the parent container and using top, right, bottom, left properties on the child elements.
You can use CSS grid layout to center the child elements. Set display: grid on the parent container and use place-items: center to center the items both horizontally and vertically.
All three methods will center the child divs within the #container div without using margin and position. Choose the one that suits your needs and fits well into the overall layout of your webpage.
your body tag is missing its closing ">"
close your body and it should be centered.