Hiding and showing divs in pure HTML? - html

I apologize if this question doesn't have the right amount of detail; I'm very new to web development and programming.
I have three buttons situated above a div, and I want to use these buttons to change the appearance of the div. It's hard to explain, so I've made a crude wireframe which I've linked in the rest of this question.
When all three buttons are selected, I want three smaller divs to appear inside my larger div, like shown below
When two of the buttons are pressed, I want it to appear like
When only one button is pressed, I want it to appear like this.
As you can see, I want the pressing (and un-pressing) of a button to toggle the presence of the smaller divs, and if possible, for the smaller divs to be resized depending on how many buttons have been selected (i.e., when two buttons are selected, the two small divs are each half the size of the larger div, etc.). I would really love if this could be done in pure HTML, without any JavaScript or libraries like jQuery.
I feel like this is a common thing to have built, as I am certain I have seen something similar before. However, I can't find any tutorials that quite match what I am looking for, and I couldn't even find existing examples (hence me making linking those pictures). I would greatly appreciate any help, especially if you could point me to some source code or to some examples.

It's possible to achieve in CSS using some tricks like this: https://codeburst.io/awesome-checkbox-and-radio-button-css-hacks-a8c24fb15a95
Actually, your "buttons" seem to act more like radio/checkboxes as they should remain "active"? Unless you meant they have to remain pressed, but in that case it's impossible to have them all pressed at the same time ;-)
Unfortunately, you can't achieve that purely in HTML.
Also keep in mind that a CSS solution will not be great from an accessibility point of view, because you need to style checkboxes as buttons, and there's not way to modify attributes like aria-haspopup or aria-expanded to tell screen readers when the content is changed. You could use an aria-live area as a fallback but it's always better to link the button with the content is actually "opens".
Of course you can use both to support as many devices as possible.
UPDATE: to achieve that in Javascript, you would need checkboxes too, or store the state of your buttons (one click sets the state to "clicked", another one unsets it). Each button would add or remove a class on the containing DIV. Then you can use CSS to display or hide, and also set the size of your secondary divs. Something like:
.bigdiv > div {
display: none;
}
.bigdiv.button1 > div:nth-child(1) {
display: block
}
.bigdiv.button2 > div:nth-child(2) {
display: block
}
.bigdiv.button3 > div:nth-child(3) {
display: block
}
to set the sizes, you would have to rely on the combination of buttons:
.bigdiv.button1:not(.button2, .button3) > div {
width: size when only button 1 is pressed
}
.bigdiv.button1.button2:not(.button3) > div {
width: size when only button 1 and 2 are pressed
}
.bigdiv.button1.button2.button3 > div {
width: size when button 1,2 and 3 are pressed
}
Of course you'd have to cater for all the combinations of buttons, or simplifiy the logic, count the number of button pressed, and apply extra classes to .bigdiv to indicate how many are pressed, so you can handle that easily in CSS.
As you can see, there's tons of different solutions to handle that, but a mix of JS and CSS is the best as you decouple the logic from the presentation and all your JS does is adding/removing classes.

Related

Custom styled radio buttons? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hello I'm a student that is building a site for a friends business and I have learned online how to make radio buttons styled with custom images but i am trying to make a small, medium , large option to make the user able to choose the size. I know java but not much Jquery. I understand if it is not possible and I am open to other ways of accomplishing the task as long as i can make code that can check which option is selected to make it direct them to the right page/shopping-cart product.
---HERE'S AN EXAMPLE OF WHAT I HAVE SO FAR: http://squidsquadgang.com/radiobuttons.html
any help would be much appreciated!
Thank you,
Stephen T.
You have quite a bit of JavaScript doing the work on the radio buttons. The javaScript you are using is actually creating dynamic <span> elements and completely hiding the radiobuttons. While this may support old browsers better, a much more "future-proof" (one might even say "correct") way to do this is through pure CSS. It's simpler, gives you more options and customization, and it will load much faster than that giant JavaScript file.
So, while this may not be exactly what you came here looking for, I encourage you to at least give this a try.
Example: Here's what I've done.
Give it a try: go into your image editor and remove the text. Now, the text displayed on your buttons is in your HTML code. much easier to edit than images, and faster too.
The code explained: Making custom CSS radio buttons
input[type=radio] {
display:none;
}
This selects all the radio buttons on the page and gives them a display:none property. It hides the radio buttons, since you don't want to see those ugly things anymore.
input[type=radio] + label { ... }
This selects all of the radio button labels. In our HTML, we've created labels for each of the <input> elements. It's a built in feature in HTML: when labels are clicked, the radio button is automatically selected. We don't need any javascript here! Don't reinvent the wheel.
Here, we define both the background image and the style of the text to be displayed over the background image (if any). In this case, it's "XS", "S", and "M". I've used display:table-cell to display the buttons side-by-side (like in your link) and make use of the vertical-align property. If there is no text, simply change it do display:inline-block and adjust from there.
input[type=radio]:checked + label { ... }
Finally, we specify the styling for the selected option. In your case, I've moved the background image down 42px (background-position:0 -42px;). Again, you are going to want to remove the text from your image. You can look up other styling you can do - you could make the text "glow" by adding something like:
text-shadow:2px 2px 10px #FF00FF;
You can play around with the details. Even though it's not going to display the images IE8 and before, this will still nicely fall back to whatever text you have in <label>. It will just go to regular old radio buttons. People smart enough to use Chrome or keep IE updated, well, they'll get the extra eye-candy.
I guess you are asking something like this:
http://jsfiddle.net/vybEf/1/
$('.styled').click(function(){
var idS=$(this).attr('id');
var s=idS.substring(5,7);
$('#txt').css({'font-size':''+s+'px'});
$('#txt').html(''+s+'px');
});

html form spanning bootstrap tabs causing odd spacing

I have a form that spans a toggable tab left bootstrap layout. I am using the bootstrap form controls as well. Essentially, I want to have three textareas which can be individually viewed by selecting the associated tab and then on submit, I take all three of the textarea's content to perform an update with.
The code is at http://jsfiddle.net/timburgess/rhNxF/8/
The tabs behave as expected. However, when displaying a textarea in the tab-content, it is spaced quite a bit away from the tab controls. Looking at the html, I can't see anything which would cause this. I could always do a CSS hack but I am wondering if I am missing something here? I have tried setting different span2,span4,etc classes but they make no difference.
Ideally I want to have the textarea next to the tabs and the alert to the right of the textarea.
It's because of:
.form-horizontal .controls {
margin-left: 160px;
}
in bootstrap.css. You would need to overwrite that in your CSS.

How to make select inputs look the same in all browsers on all platforms?

i'm solving a problem to make select inputs look the same in all browsers (Chrome and Safari on Mac renders them differently) how to do that ?
The ONLY way to make them look the same right now would be to hide the original inputs, and replace them with appropriately styled html equivalents (of god forbig Flash objects), which would act as proxies, passing the functionality over to the hidden inputs.
That may be automated with JavaScript. But that would be WRONG. You are not supposed to force a different look on to OS styled elements of the webpage. It conflicts with a lot of usability and accessibility practices.
So, the only way is to make your design flexible enough to support differently looking control elements on a web page, and also use different stylesets for different browsers, to ease the adjustment of the styles (at the moment there are no inputs that would look and act the same on all browsers with the same style rules applied).
Unfortunately, life just kinda sucks on this one. Just wait till you need to style a file input...now that's some fun!
if you dont mind using js you can simply design your own look (a jpg img it can even be the same img as the original select element or if you wish you can model parts of it in css)
Then place a div on top of that image that div will contain the text which select element would usually contain
<div id="selectTxt" >
then set another div on top of that with the select element inside it.
<div id="transparentSelect" class="transparent">
<select id="selectCar" name="selectCar">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
</div>
Now the trick is to set the select element opacity to zero
you can do this by adding by adding a class transparent
and then applying the class to the div
.transparent
{
filter:alpha(opacity=0);
-moz-opacity: 0;
opacity: 0;
}
now the element is hidden but when you click on it the list will still show up.
So the list will always look like the default list in the browser
now use js to extract the select value every time you click on the select
and set the inner html of selectTxt div to its value.
This way you get the text of the select on top of an image you want
you can make the image animated with the hover effect in css or with js
I also make a select that looks the same in all browsers but it doesnt work when you click directly on the arrow...
so its an inferior version but if you wish to look at it here it is
http://jsfiddle.net/fiddlerOnDaRoof/LM73V/
it also lacks the arrow image but you can print screen that from your browser
good luck
You should apply a CSS to reset the styles (not just for the inputs, this is a highly recommended practice for all element so that your page looks almost the same in all browsers) there are many, just google a little, for example this one, and then apply your desired styles (border color and width, background, etc...) take a look at this tutorial on how to style form elements

How to break up HTML documents into pages for ebook?

For an iPhone ebook application I need to break arbitrarily long HTML documents up into pages which fit exactly on one screen. If I simply use UIWebView for this, the bottom-most lines tend to get displayed only partly: the rest disappears off the edge of the view.
So I assume I would need to know how many complete lines (or characters) would be displayed by the UIWebView, given the source HTML, and then feed it exactly the right amount of data. This probably involves lots of calculation, and the user also needs to be able to change fonts and sizes.
I have no idea if this is even possible, although apps like Stanza take HTML (epub) files and paginate them nicely. It's a long time since I looked at JavaScript, would that be an option worth looking at?
Any suggestions very much appreciated!
update
So I've hit upon a possible solution, using JavaScript to annotate the DOM-tree with sizes and positions of each element. It should then be possible to restructure the tree (using built-in XSLT or JavaScript), cutting it up in pages which fit exactly on the screen.
Remaining problem here is that this always breaks the page on paragraph-boundaries, since there is no access to the text at a lower level than the P-element. Perhaps this can be remedied by parsing the text into words, encapsulating each word in a SPAN-tag, repeating the measurement procedure above, and then only displaying the SPAN elements that fit onto the screen, inserting the remaining ones at the front of the next page.
All this sounds rather complicated. Am I talking any sense? Is there a simpler way?
You should look at the PagedMedia CSS module: http://www.w3.org/TR/css3-page/
CSS3 also support multicolumn layouts (google for "css3-multicol". I don't have enough Karma to include a second link here :-)
About your update: how about doing the layout of one single page, then use a DIV with overflow:hidden for the text part. Next thing would be to overlay a transparent item on top of that, that would programmatically scroll the inner content of the DIV PAGE_HEIGHT pixels up or down according to some navigation controls (or gestures).
The other option is to have a parent <div> with multiple css3 columns: link1, link2.
This works on Android:
<style type='text/css'>
div {
width: 1024px; // calculated
-webkit-column-gap: 0px;
-webkit-column-width: 320px; // calculated
}
p {
text-align: justify;
padding:10px;
}
</style>
The CSS multicol suggestions are very interesting! However, and I hope it's ok to respond with another question: how would you go from splitting one or more long <p> elements into columns to having one particular of these columns being rendered in a WebView? The DOM hasn't changed, so you can't pick out an element and render it. What am I missing?

What Html markup for a focusable TD?

I want to practive, and even best-practice, with Html+JS+CSS.
I use a one page client-only Sudoku page.
My Sudoku markup is basically a <table>, with <td>.
(I'm open to suggestions to improve this).
My requirements :
Have a cell under focus (the keyboard notion of focus) (highlighed with css to a yellow background)
Navigate through cells with arrow keys (plus Home etc).
type-in an integer value sets that value to the currently focused cell
I use an input button inside each cell.
The Javascript works fine.
My only problem is with the display.
When a cell has the focus, it's highlighted display doesn't cover the whole TD, rather only the visual space included in the input button. I have some space around the button that isn't 'yellow'.
I don't think I could go up in the CSS selection, to select the parent of the input, could I ? Such as :
input:focus '?? how to go up ??' td { background-color:yellow;
I tried a few tricks, like having always 5 characters in each button display (5 spaces when empty, changing the middle character when set), but nothing is visually satisfying.
Even worse, it is clearly against best-practices to alter the content for the sake of visualizing. That's what the MVC distinction between Html/Css/Js is for !
I already searched this site for answer, I found close but distinct questions and answer.
I'm hoping someone could help improve my page ... and my markup skill :-)
It is not possible to construct a css selector which matches a parent node dependent on a (pseudo-)class of child node.
Basically you have two options to choose from:
Try to fill the td with the input completely using height and width rules in your css.
Set 'focused' and 'unfocused' class on your tds with javascript using the onfocus and onblur events of the inputs.
Could you not use a dash of jQuery to set a .focused class and then apply some style to it?