How to make underline in css. Beautiful css underlining of elements
On this page you will find examples on how to change HTML links using CSS, which allows you to make them block links, icon links, without underlines, with rounded corners, change the distance between them and much more. In the future, based on these examples, you can create various options navigation menus for site.
Before you start studying the examples, I want to make a small clarification. In all examples, for greater clarity, a CSS pseudo-class will be used, which is used to change the styles of links when the mouse cursor hovers over them. It must be said that this practice is common and is used to one degree or another on almost all Internet sites.
In this example, we will remove the underline from the links, and then add or remove them on mouseover.
HTML and CSS example: how to remove and add underlines from links
Description of the example
- Default is all popular browsers add underlining to links, which is controlled by the CSS property. That is, by default this property, for links, has the value underline . Using this knowledge, we change the underlining of links in their different states.
Initially, the underlining of links is a solid thin line, pressed almost closely to the text. And in most cases, this option is quite sufficient. However, sometimes a design requires that links be underlined with a dotted line rather than a solid line, or that the distance from the underline to the links be greater than the standard, or that the underline itself be bolder. And sometimes you need to make some kind of exotic underlining, for example, with a wavy or multi-colored line. All these options will be discussed in this example.
HTML and CSS Example: Dotted Link Underline
Description of the example
- First, we remove the standard underline from all links, since we will use non-standard methods. Then we add a bottom border to the first link using the CSS property and make it dashed. This will be our underscore. To remove the underline when you hover over a link, we use a pseudo-class and make the background of the frame the same as the background of the page, that is, we simply hide it. In theory, it would be better to make the background of the frame transparent, but IE6 misunderstands this meaning.
- With the second link we do the same as with the first, but we just draw a solid line. To increase the distance from the text to the underline, we give the link a small bottom margin using the CSS property.
- Our third link has a multi-colored underline, so we will add it using background image. For this we use a small image, which we connect via CSS. Position the background at the bottom of the link (0 100%) and allow it to be duplicated only horizontally (repeat-x). The underline is ready, but it is too close to the text; to fix this, we add a small indent to the link below. That's it.
- In the old IE6 and IE7, there may be problems with displaying the underscore on the first and second links. To fix this, add the zoom :1 property, which includes the so-called layout. This property is invalid and only these browsers understand it, so it is better to enable it conditional comments.
Very often, to create a menu, you need to make not just text links, but block links, in which the active area will be both the text itself and a certain area around it.
Description of the example
- To create block links we use CSS property:block, which turns them into block elements that create a line break before and after them. If line breaks are not needed, you can replace the value with inline-block .
- Since our links are now blocks, if necessary, we can change their width (CSS) or height (CSS).
- In this example, we do not specify the height of the links at all, but simply give them internal padding (CSS), which expands the blocks.
Description of the example
- Everything is very simple - using the CSS property we add the desired frame to the links under the cursor. However, please note that we add exactly the same frame to regular links, but we make it the same color as the page background. That is, we simply hide the frame for the time being. This is done so that when you hover the mouse cursor, the links do not start to “jump” due to the drawing of the frame.
Instead of making the border color match the background of the page, it could be set to transparent and made transparent, but as I said, IE6 does not handle it correctly.
HTML and CSS Example: Creating 3D Links
Description of the example
- Using the CSS property we add frames to the links, and with the help we set their color. In this case, for the left and upper borders we indicate light shade colors, and for the bottom and right - dark. It is thanks to this distribution of colors that we get links that look like three-dimensional buttons.
- To make it seem like the buttons are being pressed when you hover the mouse cursor, we invert the border colors of links with a pseudo-class. For an additional click effect, set the relative positioning (CSS :relative) and make a one-pixel upward offset (CSS :-1px).
- Well, to make everything absolutely beautiful, we set the text and background color of the links under the cursor a little darker than the usual ones. Ready.
In this example, we will make links with icons that will contain not only images of these icons, but also text under them. However, in the future you can easily change them and leave, for example, only icons.
Before starting work, we will prepare several images of icons, in two sets - for regular links and links under the mouse cursor. The second set should be externally different from the first, with us this is expressed in the color palette.
In this example, we will round the corners of the links using one of the methods rounding corners, described in the next subsection. We will not consider the option using CSS 3, since everything is very simple there, but it would be better to make roundings using images.
To do this, first we will cut out several blank images in two sets - for regular links and links under the cursor. For us, it will differ in the presence/absence of shadows in the images.
absolute positioning*/ top: 0; /* zero top offset */ ) .links:before ( content: url("images/left_bok.png"); /* left side image */ left: 0; /* zero left offset */ ) .links:hover: before ( content: url("images/left_bok_hover.png"); /* image of the left side under the mouse cursor */ ) .links:after ( content: url("images/right_bok.png"); /* image of the right side * / right: 0; /* zero offset to the right */ ) .links:hover:after ( content: url("images/right_bok_hover.png"); /* image of the right side under the mouse cursor */ )