Home  >  Article  >  Web Front-end  >  How to make two divs display on the same line with css

How to make two divs display on the same line with css

青灯夜游
青灯夜游Original
2021-03-05 11:58:1961022browse

How to display two divs on the same line: 1. Set the "display:inline;" or "display:inline-block;" style to the two div elements to convert them into inline elements or inline Block elements will do. 2. Set the "float:left;" style to the two div elements.

How to make two divs display on the same line with css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

In HTML, the div element is a block element, and each block-level element occupies its own line; therefore, multiple div elements cannot be displayed on one line.

How to make two divs display on the same line with css

So how to make two divs display on the same line? Let me introduce it to you below.

Method 1: Use the display attribute to convert it into an inline element or an inline block element

The display attribute is used to define the type of display box generated by the element when creating a layout.

  • display:inline;: The element will be displayed as an inline element, with no line breaks before and after the element.

  • display:inline-block;: The element will be displayed as an inline block element, with no line breaks before and after the element.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<style type="text/css">
			div{
				width: 200px;
				border: 1px solid red;
				display: inline-block;
				/*display:inline;*/
			}
		</style>
	</head>
	<body>
		<div>div测试文本!</div>
		<div>div测试文本!</div>
	</body>
</html>

Rendering:

How to make two divs display on the same line with css

[Recommended tutorial: CSS Video Tutorial

Method 2: Use float to display Div side by side

We can solve the problem of not displaying side by side by setting a float attribute on the div, as long as you If the total width of the side-by-side div boxes is less than or equal to the width of the outermost box, multiple div objects can be displayed side by side.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<style type="text/css">
			div{
				width: 200px;
				border: 1px solid red;
				float: left;
			}
		</style>
	</head>
	<body>
		<div>div测试文本!</div>
		<div>div测试文本!</div>
	</body>
</html>

Rendering:

How to make two divs display on the same line with css

The float attribute defines in which direction the element floats. Historically this property has always been applied to images, causing the text to wrap around the image, but in CSS, any element can be floated. A floated element creates a block-level box, regardless of what type of element it is.

If floating non-replaced elements, specify an explicit width; otherwise, they are made as narrow as possible.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to make two divs display on the same line with css. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn