CSS Tips and Tricks: Add External URLs to Print Stylesheets

Corey Schafer · Beginner ·💻 AI-Assisted Coding ·11y ago

Key Takeaways

CSS print stylesheets can be enhanced with external URLs using the :after pseudo-element and attribute selectors, allowing for a more user-friendly print experience by including relevant link information.

Full Transcript

hey how's it going guys i wanted to make a quick video here and show you a neat trick that you can add to your css files when making your print style sheets if you don't know what a print style sheet is pretty much it is a media query but instead of being a media query for screen sizes it's a media query for what your page looks like when someone prints it out the reason that you would want something like this is if i have a test website running on my local server over here um if someone printed out this page there's a lot of information here that they wouldn't care to see uh when printed out like this navigation menu here uh this sidebar with featured posts and things like that and also anything that you might have in a footer area with you know things like this it's all would just be junk when printed out you really just want to focus uh solely on the content so whenever i print this test page here you can see that my print style sheet has removed the navigation menu here it has centered the site logo taken out this sidebar and also removed all the footer information here that was getting in the way so what i wanted to show you guys was a lot of people suggest that it's a best practice to add in the urls of links into your print style sheet and the reason that they suggest doing this is if i zoom in here if you look at this resources section that i added here to the bottom of this article then if someone printed this out and it's just on paper they may be curious as to where these links actually go and if you add the url onto the page then they'll be able to uh you know type those urls in themselves and don't have to go back to your website and refine that article again so i'm going to show you a few different ways to do this and ultimately how i ended up doing it on my website now the easiest way we can do this is to just target every anchor tag on the page that has an href attribute and the way we do that is we would just search for our anchors with and whenever i open up this brackets now it means that we're now searching for attributes and type in href so any anchor tag that has an attribute with an href and then we'll put in this after tag and we want this content after the anchors with hrefs to be and i'm just going to put hard code in a parenthesis here and then after i close off this quote then i'm going to put in attribute href and then i'm going to hard code in the closing parentheses and then end that with a semicolon and let's save that and let's take a look at what this looks like in our print style sheet so let me reload this and then open up the print preview you can see this isn't exactly what we're looking for if we zoom in here you can see that it prints out every link on the page with an href and that's is what we targeted but you can see that those links include the links and the header here it also includes author information a lot of things that the reader most likely doesn't care about they probably just want to see external links to your website so these are really the only ones that we would want printed out in our print style sheet so let's see if we can do this a little bit better now what some people suggest to do here is to target uh any anchor tags with an href that starts with and this carrot sign equals means that hrefs that start with and then they will type in here http colon and that will target any href that starts with http now this would work great if every internal link on our website was relative but if you hard code in the links with the full web address or if you're using a framework like wordpress or something like that then sometimes those will put in the full web address for you so let me show you what i mean by this if i inspect one of my links over here then you can see within the anchor tag the href even though it's internal to my website it still has the http and this links to localhost because i'm running this on my local web server so this approach really isn't going to work for us this only works if every internal link for your website is relative so what other options do we have here well if i pull up this tab here there is a css selector called not and what not does is it takes a css selector and then it applies styles to everything that is not that selector so it'd be easiest just for me to show you with an example here so in our example with our links right after the anchor tag here if i put in colon not then open up a parenthesis and we want the print parentheses to go around our href here so let's close that there and what this currently says is that we want all links that do not have an href that starts with http and what we want this to be is we don't want any links that start with our internal website here so in this case i'm running this website on a local web server right now so this will be http localhost now if i was on my live website then i would change this domain to whatever my live website url would be so if we save that and then we reload this page over here now if i pull up the print style sheet then you can see that it no longer adds urls into the header or any of these internal links like like this author url leave a comment none of that stuff but if we scroll down here to the bottom then you can see that it does add in the urls uh to these links that i have to these external links here that i have listed in the resources and that's exactly what we wanted so let me go ahead and zoom in there so you guys can read so yeah that's exactly what we were hoping for so this is the best way that i've found that i can remove internal links from my css print style sheets so i hope this was useful for you guys if you guys have any questions or if you know any alternate ways of doing this then go ahead and leave those in the comments below be sure to subscribe for future tips and tutorials and thank you for watching

Original Description

Printing out URLs can be a nice edition to print stylesheets. When someone prints your article or blog post, you want them to know where your links would have taken them so that they can investigate those sources further if they desire. However, you don't want to include every link in the print stylesheet. Your internal links that loop back to your own website aren't necessary and will bog down the print stylesheet with a lot of unneeded text. So how do we add only the external links to the print stylesheet? In this video, we will cover several techniques you can use in order to achieve this. ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Corey Schafer · Corey Schafer · 15 of 60

1 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
22 JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
30 Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
36 Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
60 SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

This video teaches how to add external URLs to print stylesheets using CSS, making it easier for users to access relevant information when printing web pages. The technique involves using the :after pseudo-element and attribute selectors to target specific links.

Key Takeaways
  1. Create a print stylesheet using a media query
  2. Target anchor tags with an href attribute using CSS selectors
  3. Use the :after pseudo-element to add URL information after links
  4. Use the not selector to exclude internal links from the print stylesheet
  5. Test and refine the print stylesheet
💡 Using the not selector to exclude internal links from the print stylesheet is a key technique to achieve the desired outcome.

Related AI Lessons

Up next
Azure Security Priorities for 2026: Identity, Governance, AI Security & Zero Trust
Valto Microsoft Specialists
Watch →