- Link styles in html pages
- Default link style
- A second link style
- How to Make a Div a Clickable Link
- Create HTML
- Create CSS
- Example of making a div a clickable link:
- Result
- Second solution: making a div a clickable link by nesting it in the tag:
- HTML div style class
- Inline html div style
- Internal and external style applied for div
- HTML div style class
- ROMA
- MADRID
- SEOUL
Link styles in html pages
This article will show you how to have different link style (colors, size, effect) in different areas in the site or on the same page.
You can download a sample html file at the end of this page.
Default link style
To set an overall style for your links in site your CSS will look like this:
a:link is where you set the link colors, background color, size, etc.
a:visited is a link color, size, padding, etc for a link that has already being clicked, visited
a:hover is link settings for the moment when mouse is over the link
A second link style
Now what about if you want to have one link color in top area of your site (header) and another link color in the bottom area of your site (footer)?! In this case you need to place the html code of the link inside a
, give that div a class name then customize that class in CSS, for example here is the html code for the » header » area:
Then the CSS for the «header» area is like this. you add the dot and class name in front of the default a:link and a:hover:
.header a:link < color:#006699; font-size:14px; text-decoration:none; padding:5px; background-color:#DDEEFF; >.header a:visited < color:#003399; font-size:14px; text-decoration:none; padding:5px; background-color:#DDEEFF; >.header a:hover
For other areas of the page do the same, place links in a div, give it a class name like header, footer, left_links, top_links, etc then inside the CSS file write that class name before the a:link.
How to Make a Div a Clickable Link
The first solution uses CSS absolution position, which is a bad practice most of the time. You can scroll down to see the other solution.
Create HTML
Create CSS
- Set the position to «absolute» for the inner tag.
- Use the z-index property to place the link above all the other elements in the div.
.container < font-size: 5em; background-color: #a8a8a8; color: white; width: 8em; height: 2em; line-height: 2; text-align: center; font-family: Helvetica, Arial, sans-serif; font-weight: bold; cursor: pointer; position: relative; > .link < position: absolute; width: 100%; height: 100%; top: 0; left: 0; z-index: 1; >
Example of making a div a clickable link:
HTML> html> head> style> .container < font-size: 5em; background-color: #a8a8a8; color: white; width: 8em; height: 2em; line-height: 2; text-align: center; font-family: Helvetica, Arial, sans-serif; font-weight: bold; cursor: pointer; position: relative; > .link < position: absolute; width: 100%; height: 100%; top: 0; left: 0; z-index: 1; > style> head> body> div class="container"> W3Docs a href="https://www.w3docs.com/"> span class="link"> span> a> div> body> html>
Result
You can also use the nested inside a hyperlink.
Second solution: making a div a clickable link by nesting it in the tag:
HTML> html> head> style> a < display: block; background: orange; padding: 20px; text-align: center; > style> head> body> a href="https://www.w3docs.com/learn-html/html-introduction.html"> div> This is a clickable div. div> a> body> html>
As the div takes place inside the anchor tag, the anchor tag covers all the div’s area, and thus the div becomes a clickable link.
HTML div style class
Inserting style for div, there are three ways:
External style sheet, — In this case we make file as style.css where we add CSS style
Internal style sheet, — We add CSS style betweenthem:
Inline style,
For any on your page, you can specify any CSS property as: color, font-style, font-family, font-size, background, etc.
Example: On this window you can edit online this script,
div class and internal style.
.text font-family: Verdana, Geneva, sans-serif;
font-size: 16px;
color: #09C;
background-color: #CCC;
margin: 12px;
padding: 15px;
>
Add your text here
Note: try to avoid inline-css, you have to make external style in a separate file as: style.css
Inline html div style
Here, in the next example we show you how to use inline css style what can be applied to div box as: height, width, background and color.
Example: Edit online this window, then Execute to see how you script change page.
Add your text here
div style class, border, center, color, background color, width, align center, background image
margin top 24px margin-bottom 20px span style= font size 24px color
How to use div style in HTML
Internal and external style applied for div
You can archieve same style using Internal and External CSS
1. Internal style for html div.
Example: Edit online this window, then Execute to see how you script change page.
Here your text:
This is a box useing internal style