Attempting to output a URL using CSS for printing purposes

My current code involves working with the CSS print feature. When I implement the following code, a URL is visible next to the KRON4 in two instances:

1) Viewing the page in the browser window 2) Clicking the print button on the browser.

I aim to display the URL only when scenario 2 occurs - clicking the print button on the browser window. To achieve this, I replaced the content of the <style> tag as shown below with a specific media query:

<style type="text/css">
    @media print body {
        a[href^="http://"]:after {
            content: " (" attr(href) ")";
            color: blue;
            font-size: small;
        }
    }
</style>

Despite implementing the above style content, the URL does not appear as intended during printing.

Below is the provided code snippet without the media query:

<!DOCTYPE html>
<html lang="en>
   <head>
      <title>Print Media and Mobile Devices</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <style type="text/css">
         a[href^="http://"]:after{
         content: " (" attr(href) ")";
         color: blue;
         font-size: small;
         }
      </style>
   </head>
   <body>
      <p>
         <a href="http://kron4.com/">KRON 4</a> 
      </p>
   </body>
</html>

Answer №1

Your CSS code is almost complete and should work perfectly if you include the following:

display: inline-block;

This means your final CSS should look like this:

@media print body {

a[href^="http://"]:after{
content: " (" attr(href) ")";
color: blue;
display: inline-block;
font-size: small;
        }

        }
</style>

You can view a working example on JsFiddle. This code will function properly in all browsers.

Answer №2

Here's my unique take on the situation, showcasing a different method demonstrated in this CodePen

The concept involves hiding the link initially and revealing it only during the print process. I've successfully tested the functionality on CodePen (Chrome Print Preview), but it's advisable to verify compatibility across various browsers. Additionally, consider applying some specific styling like float:left to the .hide class for optimal positioning of the link.

This clever workaround can be implemented as follows:

.hide {
  display:none;
}

@media print {
.hide {
    display:inline-block;
  }
}

I trust you'll find this approach helpful,

Samantha

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Receiving a NaN output rather than a numerical value

Experiencing what seems to be a straightforward syntax error, but I'm unable to pinpoint it. Controller: $scope.startCounter = 3; $scope.startTimeouter = function (number) { $scope.startCounter = number - 1; mytimeouter = $t ...

javascript variable pointing to an unknown object

Below is the essential code snippet to consider: JS function ToggleRow(objTwistie, objRow) { if (objRow.style.display == "none") { objRow.style.display = ""; objTwistie.src = "../Images/SectionDown.gif"; } else { objRow.style.display = "n ...

The data type 'Event' cannot be assigned to the data type 'string' in this context

Recently diving into Angular, I came across a stumbling block while working through the hero tutorial. The error message that popped up was: Type 'Event' is not assignable to type 'string' You can see the error replicated here. ...

Guide to extracting HTML-containing JSON in Swift

I have response data in an HTML page format. After converting the response data to a string, it looks like this: { "Result": { "NewsId": 10, "Body": "<p><span style=\"font-family: Verdana, 'Geneva CY', Helvetica, 'D ...

Using a loop to alter the selected value of a dropdown menu with jQuery

Presently, I am using a dropdown menu that displays all the years from 1970 to the current year. Currently, this functionality is achieved through embedded JavaScript within the HTML. My aim is to achieve the same result using an external file and jQuery, ...

Aligning radio button vertically in React using Bootstrap styling

I am struggling to align a radio button within a container inline with other buttons on the right. Can anyone provide guidance on how to resolve this issue? Here is my current setup: https://i.sstatic.net/SSWZe.png Code: import React, { Component } from ...

Having trouble accessing the input class with jQuery

When I click the "Reset To Default Settings" button in my form, I want to clear the default colors in my 4 color pickers. Each of them has an input type text field with the class anyicon-form-colorpicker. To achieve this, I locate the <a> tag and use ...

What steps can I take to prevent already selected options from being chosen in a Vue.js HTML form?

Among my json data, I have a list of all inventories: { "status": true, "data": [ { "inv_id": 1, "name": "Arts" }, { "inv_id": 2, "name": "web" }, { "inv_id": 3, "name": "mobileapp" }, { "inv_id": 4, "name": "ws" }, { "inv_id": 5, ...

Utilizing CSS3 Pseudo Elements for Styling a Graph

I'm struggling to achieve the desired look of my ancestor chart using an unordered list format. Initially, I borrowed code from CSS3 Family Tree, but found it to be more like an organizational chart rather than a family tree. For testing purposes, he ...

Using JQuery to call a PHP file and verify a unique identifier

I've been attempting to use this tutorial to check the availability of certain IDs while a user fills out forms. However, I'm working with PostgreSQL instead of MySQL and seem to be encountering issues with my PHP file. There seems to be a small ...

Problems arise with a 'back to top button'

I am currently working on a 'scroll to top button' that appears after the user has scrolled down 300px. It is functioning correctly so far. My goal now is to have this button appear at a specific element on the page. Below is the jQuery code: / ...

Leveraging HTML within jQuery UI autocomplete

In the past, I was able to include HTML in the JSON array I used for autocomplete with jQuery UI before version 1.8.4. For example: $row_array['label'] = '<span style="color: red; font-family: courier;">User, Name</span>'; ...

The art of perfect alignment: Mastering the positioning of Material-UI Grid

This issue has been resolved Hey there, I'm currently working on a React App with Material-UI and I'm trying to center a Grid item that includes a Card along with two additional Grid items. Below is the relevant code snippet. Although the Cards ...

Is it possible to only set style.marginLeft one time?

My navigation menu is positioned off-screen to the right, and I am able to hide it by adjusting the style.marginLeft property. However, when I try to reveal it by setting the property again, nothing happens. function navroll(state) { if(state == true) ...

Using jQuery to dynamically insert variables into CSS styles

I am attempting to use jQuery 3 to modify the CSS of a class and change the background color. My first step is converting an rgba color code (properties.color) to a hexadecimal code, which is functioning correctly. Next, I try to change the background co ...

The vertical lines separating the buttons are not evenly centered

I need help to center a vertical line between my buttons in the middle of the header. Currently, the line goes up and my disconnect button is not aligned properly. Can you assist me in creating a responsive design? Thank you. https://i.sstatic.net/XIeiQ4E ...

Iframe navigation tracking technology

Let's say I have an iframe element in my HTML document. <html> <body> This is my webpage <button> Button </button> <iframe src="www.example.com"></iframe> </body> </html> If a user clicks on links wi ...

"Difficulty encountered in properly closing an opening tag within a logic statement in PHP

I am facing an issue with my code. I want to display three cards in a row, and when each card is clicked, the accordion body should appear in the bottom row. However, in the first and second cards, this is not happening because the horizontal-accordion div ...

Setting the CSS property `position: absolute; bottom: 0`, will move the div to the bottom of the screen rather than the bottom of

I'm currently working on a website where I want to attach a menu-bar to the bottom of the header using #menu { position: absolute; bottom: 0; } Unfortunately, instead of sticking to the bottom of the parent div, the menu-bar is stuck to the ...

The ng-hide and ng-disable functions are ineffective

For some reason, I am struggling to get both ng-hide and ng-disable directives to work simultaneously in my table row with a button click. It seems that when I define ng-controller="FileDestroyController" alone next to one directive, the other stops functi ...