The width of the div can be set through the width attribute of css. The css width attribute is used to set the width of an element. You only need to add the "width: width value;" style to the div element to set the width of the div.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
The width of the div can be set through the width attribute of css. The css width property sets the width of an element.
Let’s learn more about it through code examples:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style type="text/css"> div{ border: 1px solid red; margin: 10px 0; } #div1{ width: 300px; } #div2{ width: 50%; } </style> </head> <body> <div>这是一个示例div标签,默认宽度。</div> <div id="div1">这是一个示例div标签,宽度为300px。</div> <div id="div2">这是一个示例div标签,宽度为50%。</div> </body> </html>
Rendering:
Description:
The width attribute defines the width of the element's content area. Padding, borders and margins can be added outside the content area.
Inline non-replaced elements will ignore this attribute.
Possible values:
Value | Description |
---|---|
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). |
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of What properties of css can be used to set the width of a div?. For more information, please follow other related articles on the PHP Chinese website!