I learned html and css a week ago. I completed my first project only to find that a div tag I used was not resizing to mobile formats. I have done some research and it seems the answer may reside with JQuery or .JS. I am working within a contained environment, WordPress.com, and I don’t know Java Script yet, but I am familiar with if then statements from studying logic for years.
So I effectively have two problems:
- Can I use JQuery with inline html: no css?
- How do I do it?
I know I am way off here. I am in the process of going through a .JS tutorial on codeacademy, but I am not finished.
Just thought I would try for advice here. I may not even be in the ballpark!
Here is my div tag and here is what I attempted:
<div style="width:950px;height:5px;background-color:#FFFFFF;"></div>
$(window).resize(function() {
if ($(this).width() < 951) {
$('.divIWantedToHide').hide(<div style="width:950px;height:5px;background-color:#FFFFFF;"></div>);
} else {
$('.divIWantedToHide').show(<div style="width:450px;height:5px;background-color:#FFFFFF;"></div>);
}
});
Javascript is kind of over-kill for this kind of thing.
I would suggest using CSS media queries.
Paste this in and it should work just fine 🙂
Instead of having your style defined in the div tag, your div now has a unique name (an id) that can be styled separately. This is incredibly useful, and most would argue necessary, once you start building more complicated pages. The @media tags do basically the same thing as your if statements, where
min-width:951px
will set the style when your window is AT LEAST 951px andmax-width:950px
sets the style when your window is AT MOST 950px. The rest of the styles that don’t change are set above ONE time because they are the same regardless of window size.And now, just for fun I’ll show you how to do it in pure Javascript as well:
http://jsfiddle.net/AfKU9/1/ (test it out by changing the preview window size)
$(‘.divIWantedToHide’).hide() will hide the div !!
In order to apply css to this div you need to use css:
If you want to append any div and apply css to it then use append/html
or
No, at wordpress.com you won’t be able to use inline JavaScript. Not in regular posts using the HTML editor nor using the Custom Design upgrade that only includes a CSS editor.
Maybe you’ll benefit from the following:
If you want to resize or apply another style to some elements adapted to the device screen size, yout can just use the @media css property.
You are trying to hide a div with class ‘.divIWantedToHide’. But your div does not have any class.
So you should add to your div the class:
And then, you can show and hide it like here: