2

I was wondering if it was possible to manipulate the CSS of websites. For example color in the input fields? I had a look at several questions, but its not clear to me if it is possble at all.

For Example in Google Chrome, Whenever a field is selected it shows a golden line around the outsite. Is it possible to do this and other things in delphi, with any website. Writing CSS code that gets applied to the website opend in Twebbrowser? Just for personal viewing only

Thx

Can I change the color of an input field background with this code as well? I can change background colors and change font size, but cant seem to figure out to color in or border an input field. This is the code:

http://www.delphidabbler.com/tips/58

21
  • That's something that Chrome does, and it's not based on CSS. TWebBrowser is based on IE and doesn't highlight fields in the same way. You are relying on the rendering engine to do stuff like this. It's conceivable that one could inject some CSS in via the DOM, but it would be a mammoth task. Commented Feb 24, 2011 at 16:32
  • 1
    @David Heffernan - I am learning, and the only way that is best, is by trying. Thats what I'm doing... Commented Feb 24, 2011 at 16:47
  • 1
    @Lakkerw: I think it is more efficient to learn by yourself, and only ask questions when you really, really are stuck with something. And, as David said, you should start with simple things, not advanced things. You need to learn the basics first. Commented Feb 24, 2011 at 16:53
  • 1
    @ Andreas Rejbrand, David Heffernan - thx, I added some code if still interested. Commented Feb 24, 2011 at 17:34
  • 3
    Congratulations, you appear to have mastered the clipboard: delphidabbler.com/tips/58 Commented Feb 24, 2011 at 17:38

2 Answers 2

6

It is possible to modify CSS by adding a stylesheet from code, after the page is loaded:

var
   document: IHTMLDocument2;
   stylesheet: IHTMLStyleSheet;
   stylesheetIndex: Integer;
begin

   // Inject CSS Style Sheets
   document := webBrowser1.Document as IHTMLDocument2;

   stylesheetIndex := document.styleSheets.length;
   if stylesheetIndex > 31 then
      raise Exception.Create('Already have the maximum amount of CSS stylesheets');

   stylesheet := document.createStyleSheet('', stylesheetIndex);
   stylesheet.cssText := ...

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, im sorry I'm not sure if i understand correctly. BUt at stylesheet.csstext:= from there on you can fill in the css code that is prefered?
Yes, if you load google.com and then set cssText to 'body { background-color: black }'; you should see a black background
-1

Using @jasonpenny's answer to add a stylesheet, what you need next to change the border around the input element that has focus, is what in CSS is called the focus pseudo class or selector. For more information see these articles:

The http://www.w3schools.com site has a wealth of information on web development. It also allows you to play with many examples so you can see what the effects would be when you change things. If you are trying to learn how to do css styling you might be better of getting to grips with all the information there, instead of trying to learn programming and a programming language at the same time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.