Home > Web Front-end > HTML Tutorial > How to make div adapt to the height and width of the content? _html/css_WEB-ITnose

How to make div adapt to the height and width of the content? _html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:19:27
Original
1642 people have browsed it

div css

There is a line of text in my div, which is long or short. I want the div to adapt its length and width according to the text.
I set the padding attribute of the div to 4px, and found that the height is 4 pixels, but the width far exceeds the length of the text. That is, although padding-left and padding-right appear to be 4px, they are not. so.
I would like to ask if anyone knows how to solve this problem? What's the reason?

Reply to discussion (solution)

Using CSS float attribute or absolute positioning can make the size of DIV adapt to the size of the content, but it will bring other problems

Set display: inline

Just don’t set the width and height attributes.

Set display: inline. This is the correct answer.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template