We know that Div is a block-level element and is a single line. In general, two adjacent div will not be in one row
For example:
<! DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>Put two div in the same row</title></head><body><div Style="Height:40px;width:80px;background-color: #006ba4" >Div1</div><div Style="Height:40px;width:70px;background-color:chartreuse" >Div2</div></body></html>
Browser effects:
So how do you get two contiguous div in the same row?
There are two ways of
- Mode one: Using float
<! DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>Put two div in the same row</title> <style> Div{ float: left; } </style></head><body><div Style="Height:40px;width:80px;background-color: #006ba4" >Div1</div><div Style="Height:40px;width:70px;background-color:chartreuse" >Div2</div></body></html>
Operating effect:
- Mode two: Use Inline-block
<! DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>Put two div in the same row</title> <style> Div{ display: inline-block; } </style></head><body><div Style="Height:40px;width:80px;background-color: #006ba4" >Div1</div><div Style="Height:40px;width:70px;background-color:chartreuse" >Div2</div></body></html>
Operating effect:
Attention:
(1) Why use Display:inline-block instead of display:inline, because display:inline causes the height and width style of the element to fail.
(2) The results of these two methods are not exactly the same, why?
The two div in the way will have a margin of 8px in size, where does this come from? inherited from the body.
How to get two div in one line