返回介绍

Styling

发布于 2025-02-27 23:45:49 字数 3214 浏览 0 评论 0 收藏 0

We have seen that different HTML elements display different behavior. Some are displayed as blocks, others inline. Some add styling, such as <strong> making its content bold and <a> making it blue and underlining it.

The way an <img> tag shows an image or an <a> tag causes a link to be followed when it is clicked is strongly tied to the element type. But the default styling associated with an element, such as the text color or underline, can be changed by us. Here is an example using the style property:

<p><a href=".">Normal link</a></p>
<p><a href="." style="color: green">Green link</a></p>

The second link will be green instead of the default link color.

A normal and a green link

A style attribute may contain one or more declarations, which are a property (such as color ) followed by a colon and a value (such as green ). When there is more than one declaration, they must be separated by semicolons, as in "color: red; border: none" .

There are a lot of aspects that can be influenced by styling. For example, the display property controls whether an element is displayed as a block or an inline element.

This text is displayed <strong>inline</strong>,
<strong style="display: block">as a block</strong>, and
<strong style="display: none">not at all</strong>.

The block tag will end up on its own line since block elements are not displayed inline with the text around them. The last tag is not displayed at all— display: none prevents an element from showing up on the screen. This is a way to hide elements. It is often preferable to removing them from the document entirely because it makes it easy to reveal them again at a later time.

Different display styles

JavaScript code can directly manipulate the style of an element through the node’s style property. This property holds an object that has properties for all possible style properties. The values of these properties are strings, which we can write to in order to change a particular aspect of the element’s style.

<p id="para" style="color: purple">
  Pretty text
</p>

<script>
  var para = document.getElementById("para");
  console.log(para.style.color);
  para.style.color = "magenta";
</script>

Some style property names contain dashes, such as font-family . Because such property names are awkward to work with in JavaScript (you’d have to say style["font-family"] ), the property names in the style object for such properties have their dashes removed and the letters that follow them capitalized ( style.fontFamily ).

This is a book about getting computers to do what you want them to do. Computers are about as common as screwdrivers today, but they contain a lot more hidden complexity and thus are harder to operate and understand. To many, they remain alien, slightly threatening things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文