Ability to change the websites css from an inbuilt style selecting tool - html

I want to create a tool as shown here where the user is able to change the style of the webpage dynamically.
I've searched around but am still not entirely sure on how I could do this.
Any direction on where to look to achieve something like this would be very appreciated!

First you need to create some way for the user to select the colour they want to use in the interface, and then store that value in a string, for example "userColour". You need to make sure it's a valid CSS colour value, so either '#33aa55' or 'green' (check the CSS reference for named colours which would be easier for you to implement).
If you define the HTML of the page using classes or IDs on the divs and spans etc. that the page is made up of, like this:
<div class="myColour"> your content </div>
or
<div id="myColour"> your content </div>
Then you can use Javascript to change the css attributes like this:
document.getElementsByClassName("myColour").style.background-color = userColour;
or
document.getElementById("myColour").style.background-color = userColour;
For class and id respectively.
You could also use something like jQuery which would give you much better browser compatibility and ease of DOM manipulation to apply styles to your markup dynamically, but the principles are the same.

Related

Care in creating codes to external sites

I am creating a small screen plugin that any user can implement on your website. It is similar to those chat rooms like Zopim and tawk.to, where the user takes a certain code javascript and paste on the website importing a box screen.
In my case, I am taking some precautions as:
Creating divs with unlikely names of someone using (id="____Plug___Box")
All the css of sub-divs, must first call the previous div and then the current div #___Plug___Box #BoxInside
But why am I doing this? because I have a little fear of an external CSS affect my plugin.
In my case I say to the user to implement my javascript code always in the bottom of the page (to stay away from that kind of thing), I'm doing the right way? Is there anything else I should implement in my code to prevent any interference of external CSS?
In the case of Zopim he seems to use css-inline, it would be a good thing?
User always can overwrite your code. But you can provide random id prefix and create all elements from js side. Also using inline CSS will help.
var idPrefix = 'myPl'+(Math.rand() * 1000);
$('<div/>', {
style: 'color: red;',
id: idPrefix+'-wrapper',
html: $('<span/>', {'class': idPrefix+'-header'})
});
I suggest using core classes for js manipulation and supporting classes for display that can be overwritten.
<span class="myPl-js-click-for-action myPl-css-color-red">Click this red text</span>

Storing data in a HTML element that won't display on view

Just wondering if its possible to store invisible data in a HTML tag that activates the element but doesn't show data.
For instance, if I had a <h1></h1> element and I wanted the element to be active so the DOM would think there was a real header there and therefore include any additional properties (ie padding, margins etc) is it possible to have the element active by putting in something between that tags that wont display on the view?
I remember seeing something ages ago that did this but can't remember where from...
You can use visibility: hidden CSS like so:
<h1 style="visibility:hidden">...</h1>
Check out https://developer.mozilla.org/en-US/docs/Web/CSS/visibility for information about visibility.
EDIT:
In order to hide the contents of the heading, you could use JavaScript or a library like jQuery. Let's take jQuery for example. You could do something like this: https://jsfiddle.net/yanpbw1v/. In this example, I am saving h1's data into a variable and then clearing h1's data out.

Discrepancies between source and inspected html?

I am editing a HTML website template, and I need to change the banner height so I edited external CSS. However, somehow it is taking an inline CSS height property so there is a space left in between.
Please let me know, if I have not written any inline CSS (and there is no inline CSS in html page), from where is that height property coming from.
Code I see in console is:
<div style="display: block; height: 445px;" id="camera" class="camera-wrap camera_wrap">
And my code is:
<div id="camera" class="camera-wrap">
<div data-src="images/Battery-Banner.jpg">
I have no idea why it is taking class camera_wrap twice.
Usually JS plugins put dynamic css that is calculated during runtime. It will be placed in inline style tag. Otherwise any static code will go to external css file. Try checking how plugin is calculating that height and than modify your HTML/css.
Try viewing the HTML source in your browser (not using inspect element, use view-source). This will show you the markup prior to any other client side processing aka. JavaScript. If the inline style isn't there when you view source then that indicates that it may be a rogue bit of JavaScript that is adding it in.
In any case can you please provide more information on the issue? Possibly a little more background on what type of website, what parts it has CSS, JS etc. With more information we may be able to help more.
If your source is showing 1 class, and when you are using inspect element it is showing other classes, then it is definitely added by js/jquery plugin.
If you want to overwrite other class css properties, either use !important in your class or use deeper dom traversing like #camera.camera-wrap{}. Than this will be given higher priority. Try which works for you.

Adding custom CSS classes to different HTML elements output by Wordpress/Genesis

I'm working on integrating the Gridiculous grid with the Genesis Framework. Genesis already has some nice responsiveness built in, but I'd like to be able to quickly adjust my layout and get the nice fluidity that Gridiculous offers.
To do that, I'd like to be able to add custom classes to the major layout elements such as content, and sidebar. So, I'd like to be able to add a CSS class of .c3, .c8 etc to specific HTML elements. I need to be able to take
<div class="primary-sidebar">
and make it so that it's
<div class="primary-sidebar c3">
Am I barking up the wrong tree here? What path would be best to take to add specific classes like this to different elements? Body classes and post classes won't really work for this.
If I understand your question, I think you're definitely on the right track. You will have to write those multiple classes, of course, on your own so that the applied properties can take place. I would say start with whatever the Gridiculous supplies you in the CSS, and then add any additional styling in your own stylesheet.
In short, you would have to include the primary-sidebar c3 class in some stylesheet, but you can just copy the primary-sidebar properties given, and add your own customization to them. Hope that helps!

is it true that HTML still has a role in page layout?

I think the ideal is to use CSS purely for the layout and presentation, and HTML for the content. But let's say, the company wants to change a "Related articles" box from the bottom of the page to the top of the page. In such case, won't using CSS alone be not an ideal solution, but is better to alter the HTML as well? So as things are right now, HTML still takes a role in the page layout and presentation? Thanks.
Things still appear in the same order as they are in the html - it's not as restrictive as that as we can use absolute and relative positions, but those are undesirable - it's better to use to dom flow to handle placement, and that means yes, you should move the node in the html.
As Jason said, CSS is for styling the content, the content itself and its order is defined by the data (html), as order is necessary for the context of information, so it lies firmly in the 'data' part of what we do rather than the 'display'
EDIT:
I should say this: If you want your data to be totally independent of the display, you should consider defining your pages as xml only and using xsl to define the layout. xsl combines with css to completely abstract the display away from the data.
It does on two levels:
Firstly, the order of elements is still important. CSS floats are used a lot for layout but they also require elements to be in a certain order to get things in the right place. For example, lets say you have two buttons:
<input type="button" value="Click Me">
<input type="button" value="No, Click Me!">
These are next to each other. Lets say someone asks you to move the second button to the far right. This is how you do it:
<input type="button" value="No, Click Me!" style="float: right">
<input type="button" value="Click Me">
If you don't do this, the second (floated) button will appear below the other.
The second way HTML is still important is that there are still things that you need HTML tables for that can't be done in pure CSS at all, in a browser-compatible way (meaning IE6 support generally) or easily. This isn't something the pure CSS zealots like to hear but, like it or not, in the real world it's still true.
This is especially true with HTML emails. If you thought browser support for CSS was bad, mail program support is so much worse. Generally speaking you avoid CSS altogether with HTML emails and just pretend like its still 1999.
HTML still defines the hierarchy for elements.
HTML divides your page in logical sections. CSS then applies a certain look/feel/style to those sections.
If you want to change your page layout to include a section inside another one, you have no choice but to modify your HTML because HTML has a role on page layout.
You can actually move blocks around using nothing but CSS. The compromise always boils down to how good your CSS skills are and how much compatibility with older browsers you're after or care about. There are limits to what CSS can do, so yes, HTML definitely still has a role to play.
it is possible to change the "source order" of divs or use css to change positions. But if its more practical to just change the html, then there's no other way round it. At the end of the day, if its more important content then the source should reflect it for semantic reasons.