我正在ViewPager内的WebView片段中加载一些本地HTML文件。我一直在为编号列表的缩进而挠头,随着我增加字体的大小,列表向左移动甚至移至从屏幕开始。
此演示HTML没有与之关联的CSS:
<html>
<head>
<meta name="viewport" content="user-scalable=no, width=device-width">
</head>
<body>
<p>This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. This is a simple paragraph. </p>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ol>
</body>
</html>
这是HTMLol
元素的默认行为。见小提琴。
W3C建议浏览器供应商显示ol
元素(典型的默认显示属性)是:
ol {
display: block;
list-style-type: decimal;
margin-before: 1em;
margin-after: 1em;
margin-start: 0;
margin-end: 0;
padding-start: 40px;
}
The padding-start: 40px;
is the interesting part here. Because the padding is not defined in a proportional unit (px) it is not relative to or follows the change of the font-size. It will always be 40px.
So one solution would be to set the padding-left of the ol
element to a proportional unit (em):
ol {
padding-left: 1.5em;
}
Here is the fiddle.
Here are three different approaches to set the padding:
1. Inline style
Use the style
attribute:
<ol style="padding-left: 1.5em;">
<li>Item 1</li>
<li>Item 1</li>
<!-- ... -->
</ol>
2. The style
element
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8 />
<title>your title</title>
<style>
ol {
padding-left: 1.5em;
}
</style>
</head>
<body>
<ol>
<li>Item 1</li>
<li>Item 1</li>
<!-- ... -->
</ol>
</body>
</html>
3. External style sheet
This approach is my recommendation, because you have a clear separation between markup and styles. Link an external CSS file to your HTML document.
HTML document:
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8 />
<title>your title</title>
<link rel=stylesheet href="path/to/your/css/style.css"/>
</head>
<body>
<ol>
<li>Item 1</li>
<li>Item 1</li>
<!-- ... -->
</ol>
</body>
</html>
CSS file style.css:
ol {
padding-left: 1.5em;
}
注意HTML文档中文本的方向。如果您具有从右到左的方向,则应设置padding-right而不是padding-left。分别使用<html dir=ltr>
或<html dir=rtl>
,您可以这样做:
[dir=ltr] ol {
padding-left: 1.5em;
}
[dir=rtl] ol {
padding-right: 1.5em;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句