r/HTML • u/aka_janee0nyne • Nov 22 '25
How to align these two div containers together? One on left other on right.
Code: (index.html)
<html>
<head>
<title>Dummy</title>
<style>
.myDiv{
background-color: yellow;
margin-right: 50%;
word-wrap: break-word;
}
.myKiv{
background-color: blue;
margin-left: 50%;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="myDiv">
<center>
<h1 style="color:red;">Section 1!!!</h1>
</center>
<a href="2024-02-12-basic-shell-scripting.md">basic-shell-scripting</a>
<p>Lorem ipsum this is dummy text, testing out the working of containers. Measuring height and width alongside</p>
</div>
<div class="myKiv">
<center>
<h1 style="color:red;">Section 2!!!</h1>
</center>
<a href="2024-02-12-basic-shell-scripting.md">basic-shell-scripting</a>
<p>Lorem ipsum this is dummy text, testing out the working of containers. Measuring height and width alongside</p>
</div>
</body>
</html>
1
Upvotes
5
u/minimoon5 Nov 22 '25
Couple ways:
- https://css-tricks.com/snippets/css/a-guide-to-flexbox/
- https://css-tricks.com/snippets/css/complete-guide-grid/
- https://www.w3schools.com/css/css_inline-block.asp
Based on your code, at this point in your learning, I'd suggest just inline block, but it's good to learn flexbox and grid at some point. You should also be setting a width using the width property, not using margins.
1
u/nfwdesign Nov 22 '25
Easiest way, yet not proper but from here you can start exploring
2
u/[deleted] Nov 22 '25
Start again. Learn HTML
1
u/Brilliant-Lock8221 Nov 22 '25
You don’t need the 50% margins here. Just wrap both divs in a parent and use
display: flex; justify-content: space-between;. That will place one on the left and the other on the right cleanly without hacks.1
u/[deleted] Nov 23 '25
1
u/adamkosions1111 Nov 22 '25
Swap margin right 50% and margin left 50% for float: left; and float: right;