How to get two div in one line

Source: Internet
Author: User

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

    1. 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:

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.