Home >Web Front-end >CSS Tutorial >How to set the width of div with css

How to set the width of div with css

王林
王林Original
2021-05-18 16:26:076916browse

The way to set the width of a div in css is to add the width attribute to the div and set the attribute value to an appropriate value, such as [width:100px;]. We can also set the percentage width of the div, such as [width:50%;].

How to set the width of div with css

The operating environment of this article: windows10 system, css 3, thinkpad t480 computer.

In fact, it is very simple for us to set the width of a div, because there is a ready-made attribute width in CSS, which is used to set the width of the element.

Attribute value:

  • auto Default value. The browser can calculate the actual width.

  • #length Use px, cm and other units to define the width.

  • % Defines the width as a percentage based on the width of the containing block (parent element).

  • #inherit Specifies that the value of the width attribute should be inherited from the parent element.

Sample code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title></title> 
<style>
img.normal
{
	height:auto;
}
img.big
{
	height:120px;
}
p.ex
{
	height:100px;
	width:100px;
}
</style>
</head>

<body>
<img class="normal" src="logocss.gif" width="95" height="84" /><br>
<img class="big" src="logocss.gif" width="95" height="84" />
<p class="ex">这个段落的高和宽是 100px.</p>
<p>这是段落中的一些文本。这是段落中的一些文本。
这是段落中的一些文本。这是段落中的一些文本。
这是段落中的一些文本。这是段落中的一些文本.</p>
</body>
</html>

Related video sharing: css video tutorial

The above is the detailed content of How to set the width of div 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