Generating a stylesheet based on inline HTML styling? - html

I've been working on styling different parts of my website for a while, however I have yet to put my inline styles into a stylesheet. I was wondering if a tool exists to parse an HTML file and generate a stylesheet from it. For example, here is a snippet of my website:
<div class="block" style="border:1px solid">
<img id="profile-pic" style="float:left;border:0px"/>
</div>
And I would like to be able to generate this:
.block {
border: 1px solid;
}
#profile-pic {
float: left;
border: 0px;
}
Does something like this exist?

Here, I wrote a function to do it (the specificity won't be perfect, but it'll get you started):
function getInlineStyles() {
var stylesList = "",
thisElement,
style,
className,
id;
$("*", "body").each(function () {
thisElement = $(this);
style = thisElement.attr("style");
className = thisElement.attr("class");
id = thisElement.attr("id");
if (id !== undefined) {
stylesList += " #" + id;
}
if (className !== undefined) {
stylesList += " ." + className;
}
if (id !== undefined || className !== undefined) {
stylesList += "{";
}
if (style !== undefined) {
stylesList += style;
}
if (id !== undefined || className !== undefined) {
stylesList += "}";
}
});
return stylesList;
}

Try with Firebug. In CSS part you have something like this:
element.style {
border: 1px solid;
}
for <div class="block">
Just copy this in .block { } and that's it! It's not too easy, but it can help.

No, there isn't such tool. What do you do when there's more than one class? Or a class and an ID? Or an element who's a child of an element with an ID?
No, HTML is to complex to be parsed in this way to create efficient CSS code. You'll need to do so manually.
The general approach on this, is to take an inline-css heavy page, and download it locally. Then, reconstruct the page from scratch. The bright side is that you only need to do the CSS once. Once you have a global stylesheet, the rest of the pages will fall right into place.

Related

How to select elements by css property

I have a third party library that add texts to my angular app, and I want to style it, unfortunately there is no class or specific element name to do css selector by.
My question is if is it possible to do css selector based on css property.
For example select all elements that are bold
I tried this but doesn't work and I get SassError: Expected identifier
ngx-contentful-rich-text {
line-height: 2rem;
*[font-weight=700] {
margin-top: 2rem;
}
}
You might want to use this:
var paragraphs = document.getElementsByTagName('p');
function change() {
var paragraphs = document.getElementsByTagName('p');
for (i = 0; i < paragraphs.length; i++) {
if (getComputedStyle(paragraphs[i]).fontWeight == 700) {
paragraphs[i].style.backgroundColor = '#00ff00';
}
}
}
Snippet:
function change() {
var paragraphs = document.getElementsByTagName('p');
for (i = 0; i < paragraphs.length; i++) {
if (getComputedStyle(paragraphs[i]).fontWeight == 700) {
paragraphs[i].style.backgroundColor = '#00ff00';
}
}
}
<button onclick="change()">Change!</button>
<p style="font-weight:400;">Will not change.</p>
<p style="font-weight:700;">Will change.</p>

Making Div Hide after its been shown

So I have it so far when you click on an image it shows a Div. I just need it so that when you click on the image again and the div is being shown then it will hide.
Here is my code:
Style and script
<style type="text/css">
.show{display:block;}
.hide{display:none;}
</style>
<script type="text/javascript">
function showImg()
{
var obj=document.getElementById('calcShow');
obj.className = 'show';
}
</script>
Here is the HTML
<li data-value="iconchange"><img src="modules/icons/icons/calculator.png" width="65" onclick = "showImg()" class="calculator"></li>
And the Div
<div id="calcShow" class="hide"><br><br><br>
<?php
include("modules/calc/calc.html");
?>
</div>
What you want to do is create a conditional that checks whether the <div> is currently shown or not. Depending on the visibility, change the class that's associated to the <div>:
function toggleImg() {
var obj = document.getElementById('calcShow');
if (obj.className == 'show') {
obj.className = 'hide';
}
else if (obj.className == 'hide') {
obj.className = 'show';
}
}
.show {
display: block;
}
.hide {
display: none;
}
<div id="calcShow" class="hide">Hidden</div>
<br />
<img src="http://placehold.it/100" onclick="toggleImg()">
Note that you don't actually have to use classes for this, as this can be done directly through the .style.display property:
function toggleImg() {
var obj = document.getElementById('calcShow');
if (obj.style.display == 'none') {
obj.style.display = 'block';
}
else if (obj.style.display == 'block') {
obj.style.display = 'none';
}
}
<div id="calcShow" style="display: block">Hidden</div>
<br />
<img src="http://placehold.it/100" onclick="toggleImg()">
Hope this helps! :)
Solution with Jquery:
HTML:
<li data-value="iconchange"><img src="modules/icons/icons/calculator.png" width="65" class="calculator"></li>
The DIV:
<div id="calcShow"><br><br><br>
<?php
include("modules/calc/calc.html");
?>
</div>
The Function:
$('.calculator').click(function{
var isvisible = $('#calcShow').is(":visible");
if(isvisible == true){
$('#calcShow').hide();
}
else{
$('#calcShow').show();
}
})
This is basically one of the easiest way using pure javascript,
function showImage(){
var obj = document.getElementById('calcShow');
if(obj.style.display !== "block"){
obj.style.display = "block";
}else{
obj.style.display = "none";
}
}
Instead of creating separate functions for showing and hiding the image, you could create a function that toggles the value. You can use the ternary operator to decide whether you want to set the className to show or hide.
The ternary operator works like so, condition ? ifTrue : ifFalse.
The condition will be obj.className === "show". If this is true, then it should switch from its current value of "show" to a value of "hide". However, if it's false, then it's current value is "hide" and so its new value should be set to "true".
function showOrHide() {
var obj = document.getElementById('calcShow');
obj.className = (obj.className === "show" ? "hide" : "show");
}
Then you can simply set the onClick attribute to call this function.

Mail merge: can't append images from template

I'm trying to create a mail merge function to create a document based on a simple template.
I tried to use the following function to copy the template elements but I'm having problems with the (inline) images, they appear always as PARAGRAPH and not INLINE_IMAGE and the following icon appears instead of the images:
Here's the code:
function appendToDoc(src, dst) {
// iterate accross the elements in the source adding to the destination
for (var i = 0; i < src.getNumChildren(); i++) {
appendElementToDoc(dst, src.getChild(i));
}
}
function appendElementToDoc(doc, object)
{
var element = object.copy();
var type = object.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
doc.appendParagraph(element);
} else if (type == DocumentApp.ElementType.TABLE) {
doc.appendTable(element);
} else if (type== DocumentApp.ElementType.INLINE_IMAGE) { // This is never called :(
var blob = element.asInlineImage().getBlob();
doc.appendImage(blob);
}
}
Any ideas on how to solve this? Thanks in advance!
As per my knowledge, inline images are included in a paragraph so we have to check for Image type in the Paragraph.
So the code for checking that would be this way:
if (type == DocumentApp.ElementType.PARAGRAPH) {
if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
doc.appendImage(blob);
}
else doc.appendParagraph(element.asParagraph());
}
Hope that helps!

What is the alternative of placeholder in html for xhtml?

What is the XHTML equivalent of HTML placeholder.
placeholder is new in HTML 5.
There is no equivalent in XHTML 1.x (which is the XML version of HTML 4.x)
It is, of course, available in the XML serialisation of HTML 5.
Failing that, you would have to fake it using JavaScript (which is probably best done by absolutely positioning a second <label> element under the <input> which you set to have a transparent background colour unless it has a value or the focus).
I did a experiment to simulate the placeholder using xhtml+js+css as happen on HTML5 placeholder property.
xhtml:
<input id="textbox-obj" type="text" class="search-txt" data-placeholder="Search" title="Search something here">
Javascript (jquery):
//Function to place the textbox cursor to the begining
function moveCursorToStart(el) {
if (typeof el.selectionStart == "number") {
el.selectionStart = el.selectionEnd = 0;
} else if (typeof el.createTextRange != "undefined") {
el.focus();
var range = el.createTextRange();
range.collapse(true);
range.select();
}
}
function initSearchTextPlaceholder(textBox) {
textBox.focus(function() {
if( $(this).val() == $(this).attr('data-placeholder') ) {
moveCursorToStart(this);
// To fix Chrome's bug
window.setTimeout(function() {
moveCursorToStart(this);
}, 1);
}
}).blur(function() {
if( $(this).val() == $(this).attr('data-placeholder') || $(this).val() == '' ) {
$(this).addClass('placeholder').val($(this).attr('data-placeholder'));
}
}).on('keypress', function() {
if( $(this).val() == $(this).attr('data-placeholder') ) {
$(this).removeClass('placeholder').val('');
}
}
).blur();
}
initSearchTextPlaceholder($("#textbox-obj"));
CSS
.search-txt {
color: #333;
}
.search-txt.placeholder {
color: #8d8d8d;
}

Auto hide rows in HTML table when entire row is empty

In a HTML table how can I auto hide an entire row if all the cells (columns) within that row are empty?
I presume there is something I can add to the tag that would do this, but I cannot seem to find a solution anywhere.
In HTML, you can use the hidden attribute, as in <tr hidden>, but this is an HTML5 novelty and has limited browser support. But if you can directly change the HTML markup, the best way to hide an element is to remove it.
Assuming you want something that still lets you have the row there in the markup, for some reason, then you can use JavaScript e.g. as follows:
<script>
function emptyCellsOnly(row) {
var cells = row.cells;
for(var j = 0; j < cells.length; j++) {
if(cells[j].innerHTML !== '') {
return false;
}
}
return true;
}
var rows = document.getElementsByTagName('tr');
for(var i = 0; i < rows.length; i++) {
if(emptyCellsOnly(rows[i])) {
rows[i].style.display = 'none';
}
}
</script>
The test if(cells[j].innerHTML !== '') checks whether cell is completely empty, as in <td></td>. A space character, or a line break, is not counted as empty. If they should be, modify the condition as needed.
The code rows[i].style.display = 'none' hides the row by setting its display property to none, so CSS-enabled browsers will show the page as if the element were not there, but it is still accessible to scripts, etc. You could alternative remove the element completely from the DOM.
You can use a javascript like this
$('tr').each(function() {
if($(this).find('td').length == 0) {
$(this).hide();
}
});
After some research I found this sollution, which has the advantage over empty-cells:hide, that it completely removes the space an empty cell would take up.
<style type="text/css">
table {
border-spacing: 0px; // removes spaces between empty cells
border: 1px solid black;
}
tr, td {
border-style: none; // see note below
padding: 0px; // removes spaces between empty cells
line-height: 2em; // give the text some space inside its cell
height: 0px; // set the size of empty cells to 0
}
</style>
Unfortunately you have to set border-style: none;, else the borders of empty cells will be painted anyway (which results in thick lines).
I tried additional code like:
td:empty {
display: none;
border-style: none;
}
But in my table of 2 columns it removes the borders of either the left or the right column, but never of both...
Any hint at how to remove the borders of empty rows would be appreciated.
in my case cells have Spaces tabs . they are not data.
areAllCellsEmpty : true if, all cells.textContent are empty or Whitespace
innerHTML didnt give the result i wanted.
usage:
hideEmptyRows_ofTableById("myTable_id"); /* <- your table id here */
code:
function isEmptyOrSpaces(str){
return str === null || str.trim() === '' ;
}
function areAllCellsEmpty(row) {
var cells = row.cells;
var anyCellFull = false;
for(var j = 0; j < cells.length; j++) {
if( ! isEmptyOrSpaces(cells[j].textContent) ) {
anyCellFull =true;
break;
}
}
return !anyCellFull;
}
function hideEmptyRows_ofTableById(elem_id){
var table = document.getElementById(elem_id);
var rows = table.getElementsByTagName('tr');
for(var i = 0; i < rows.length; i++) {
if( areAllCellsEmpty(rows[i]) ) {
rows[i].style.display = 'none';
}
}
}