How to achieve horizontal positioning of three divs in HTML?
P粉419164700
2023-08-21 19:20:03
<p>I'm creating a sample website with three horizontal partitions.
I want the leftmost partition to be 25% wide, the middle partition to be 50% wide, and the right partition to be 25% wide so that the partitions fill 100% of the space horizontally. </p>
<pre class="brush:php;toolbar:false;"><html>
<title>
Website title
</title>
<div id="the whole thing" style="height:100%; width:100%" >
<div id="leftThing" style="position: relative; width:25%; background-color:blue;">
left menu
</div>
<div id="content" style="position: relative; width:50%; background-color:green;">
random content
</div>
<div id="rightThing" style="position: relative; width:25%; background-color:yellow;">
right menu
</div>
</div>
</html></pre>
<p>https://i.stack.imgur.com/NZDJe.jpg</p>
<p>When I execute this code, the divs appear overlapping. I want them to appear side by side! </p>
<p>What should I do? </p>
I know this is a very old question. I'm posting a solution to this problem here, using FlexBox. Here is the solution:
Just add
display:flex
to the container! No need to use floats.I recommend not using floats to handle this situation, I prefer to use
inline-block
.A few more points to consider:
<head>
and<body>
doctype
Here is a better way to format the document:
There is also a jsFiddle for reference.