How do I position a radio button correctly? - css

http://jsfiddle.net/BD8j2/
I am trying to put a radio button to the right of the arrow picture, but it always keeps going someplace else for some reason. I want it between the arrow and the word Masculino.
I also don't want this button to really record anything, it's just there for aesthetic purposes. But I want when someone click on this box for the radio button to fill and then to link to another page.
Is this possible to do? For the whole box to be the link? I was thinking of wrappign the whole div in a <a href> which I think would work.

Try this: http://jsfiddle.net/BD8j2/8/.
(The HTML in the first version of this post is invalid (input inside a), and it is not cross-browser compatible, so I have replaced it with a JavaScript example.)
HTML:
<div class="answers">
<form>
<label class="links">
<input type="radio" name="sex" value="male" />
Masculino
</label>
</form>
</div>
<div class="answers">
<form>
<label class="links">
<input type="radio" name="sex" value="female" />
Femenino
</label>
</form>
</div>
JavaScript:
function init() {
var link_elems = getElementsByClass('links');
for(var i = 0; i < link_elems.length; i++) {
addEvent(link_elems[i], 'click', goToLink);
}
}
function goToLink(e) {
if(e.stopPropagation) {
e.stopPropagation();
} else if(typeof e.cancelBubble != 'undefined') {
e.cancelBubble();
}
e.currentTarget.children[0].checked = true;
window.location = e.currentTarget.children[1].href;
}
function addEvent(elem, event, handler) {
if(elem.addEventListener) {
elem.addEventListener(event, handler, false);
} else if(elem.attachEvent) {
elem.attachEvent('on' + event, handler);
} else {
elem['on' + event] = handler;
}
}
function getElementsByClass(className) {
if(document.getElementsByClassName) {
return document.getElementsByClassName(className);
} else if(document.all) {
var everything = document.all;
var all_length = everything.length;
var matches = [];
for (var i = 0; i < all_length; i++) {
if(everything[i].className == className) {
matches[matches.length] = everything[i];
}
}
return matches;
}
return false;
}
addEvent(window, 'load', init);
CSS:
.answers {
width: 220px;
height: 50px;
margin-top: 20px;
border-top: 1px solid black;
border-bottom: 1px solid black;
background-color: #ddd;
background-image: url('http://fortmovies.com/brazil/arrow.png');
background-repeat: no-repeat;
background-position: 0 50%;
}
.answers label {
float: left;
margin-left: 75px;
padding-top: 15px;
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
font-weight: bold
}
.answers input {
margin-right: 5px
}
In the previous version, the JavaScript required putting the link names and targets in two arrays, now I have made it automatic.

Something like http://jsfiddle.net/XEk5d/2/, the clicking part to go to another page would require JavaScript.
CSS
#answers {
width:220px;
height:50px;
background-color:#DDDDDD;
border-top:1px solid black;
border-bottom:1px solid black;
margin-top:20px;
}
#arrowcenter {
width:71px;
height:31px;
background-image:url('http://fortmovies.com/brazil/arrow.png');
background-position:0 50%;
background-repeat:no-repeat;
height:100%;
display:inline-block;
vertical-align:middle;
}
#answerstext {
background-position:0 50%;
display:inline-block;
vertical-align:middle;
}
form{
display:inline-block;
}
input{
vertical-align:middle;
}
HTML
<div id="answers">
<div id="arrowcenter"></div>
<form>
<input type="radio" name="sex" value="male" />
<div id="answerstext">Masculino</div>
</form>
</div><!-- end grayAnswer -->?

Related

Why does the :focus-within pseudo-class not work with an iframe child? [duplicate]

I have a scenario like this:
.container {
background: yellow;
padding: 40px;
}
.container:focus-within {
background: red;
}
iframe {
background: white;
}
<div class="container">
<input type="text" placeholder="Input 1">
<iframe srcdoc="<input type='text' placeholder='Input 2'>"></iframe>
</div>
As you can see, when I click on input 1 to type, the container turns red because of the :focus-within selector. But when I focus input 2 (inside the iframe), it doesn't.
Is it possible to use a CSS selector on .container for if something inside the iframe is focused? I have control over the CSS inside the iframe as well.
not by css but with jQuery, you can add class to container when input 2 focused, and remove class when it loses focus.
<style>
.container:focus-within, .container.in_iframe {
background: red;
}
</style>
<script>
$('document').ready(function () {
$('#iframe').on('load', function () {
var iframe = $('#iframe').contents();
iframe.find("input").on('focus', function () {
$('.container').addClass('in_iframe');
});
iframe.find("input").on('focusout', function () {
$('.container').removeClass('in_iframe');
});
});
});
</script>
full code: https://codepen.io/peker-ercan/pen/PdgEOy
CSS is scoped to the one document - an iframe is a separate document.
Any CSS that applies to your page that contains that iframe can't apply to elements within the iframe.
Failed attempt with :has() selector as another example:
.container {
background: yellow;
padding: 40px;
}
.container:has(:focus) {
background: red;
}
iframe {
background: white;
}
<div class="container">
<input type="text" placeholder="Input 1">
<iframe srcdoc="<input type='text' placeholder='Input 2'>"></iframe>
</div>
You will need JavaScript, here is an example that should work with an iframe of the same origin:
document.querySelectorAll('.container').forEach((container) => {
container.querySelectorAll('iframe').forEach((iframe) => {
iframeRef(iframe).addEventListener('focus', () => {
container.classList.add('container-iframe-focused-within')
})
iframeRef(iframe).addEventListener('focusout', () => {
container.classList.remove('container-frame-focused-within')
})
})
})
function iframeRef(frameRef) {
return frameRef.contentWindow ?
frameRef.contentWindow.document :
frameRef.contentDocument
}
.container {
background: yellow;
padding: 40px;
}
.container:focus-within,
.container-iframe-focused-within {
background: red;
}
iframe {
background: white;
}
<div class="container">
<input type="text" placeholder="Input 1">
<iframe srcdoc="<input type='text' placeholder='Input 2'>"></iframe>
</div>

css button is moving after adding a field in an upper list

Hello guys !
So that's the problem : I've 3 tables which are 3 items lists. I've 2 buttons (cancel/submit) below the lists. When I add a row in one of the lists : the buttons below escapes, moving right. What the heck ?
If someone has an idea of how to fix it, it'll save me some hair : https://jsfiddle.net/sqonLbv4/
Thanks a lot
HTML
<div class="cols3">
<label for="field1">Field 1</label>
<table id="tableField1"></table>
<input id="field1" name="field1" type="text" />
<button id="field1Add" name="field1Add" class="btn btnOrange" type="button" onclick="field1Add()">Add</button>
<button id="field1Delete" name="field1Delete" class="btn btnBlack" type="button" onclick="field1Delete()">Delete last</button>
</div>
<div class="cols3">
<label for="field2">Field 2</label>
<table id="tableField2"></table>
<input id="field2" name="field2" type="text" />
<button id="field2Add" name="field2Add" class="btn btnOrange" type="button" onclick="field2Add()">Add</button>
<button id="field2Delete" name="field2Delete" class="btn btnBlack" type="button" onclick="field2Delete()">Delete last</button>
</div>
<div class="cols3">
<label for="field3">Field List 3</label>
<table id="tableField3"></table>
<input id="field3" name="field3" type="text" />
<button id="field3Add" name="field3Add" class="btn btnOrange" type="button" onclick="Field3Add()">Add</button>
<button id="field3Delete" name="field3Delete" class="btn btnBlack" type="button" onclick="field3Delete()">Delete last</button>
</div>
</div>
<div id="btnsForm" class="subcontainer">
<button id="btnCancel" name="btnCancel" class="btn btnBlack" type="button">Cancel</button>
<button id="btnSubmit" name="btnSubmit" class="btn btnOrange" type="submit">Submit</button>
</div>
JS
var field1 = document.querySelector("#field1");
function field1Add() {
var table = document.querySelector("#tableField1");
var cell = table.insertRow(0).insertCell(0);
cell.innerHTML = field1.value;
}
function field1Delete() {
var table = document.querySelector("#tableField1");
table.deleteRow(0);
}
var fieldList2 = [];
var field2 = document.querySelector("#field2");
function field2Add() {
var table = document.querySelector("#tableField2");
var cell = table.insertRow(0).insertCell(0);
cell.innerHTML = field2.value;
}
function field2Delete() {
var table = document.querySelector("#tableField2");
table.deleteRow(0);
}
var fieldList3 = [];
var field3 = document.querySelector("#field3");
function field3Add() {
var table = document.querySelector("#tableField3");
var cell = table.insertRow(0).insertCell(0);
cell.innerHTML = field3.value;
}
function field3Delete() {
var table = document.querySelector("#tableField3");
table.deleteRow(0);
}
CSS (aka the devil)
box-sizing: border-box;
}
.subcontainer {
padding: 0 2rem;
}
.cols3 {
width: 33%;
float: left;
padding: 0 0.3rem 3rem;
}
.btn {
color: #fff;
font-size: 1rem;
font-weight: 700;
background-color: transparent;
border: 1px solid transparent;
border-radius: 0;
cursor: pointer;
display: inline-block;
transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
.btnCentered {
text-align: center;
}
.btnTitle {
vertical-align: middle;
margin-left: 2rem;
}
.btnOrange {
background-color: chocolate;
border-color: chocolate;
}
.btnOrange:hover {
color: #fff;
background-color: #000;
border-color: #000;
}
.btnBlack {
color: #000;
border-color: #000;
}
.btnBlack:hover {
color: #fff;
background-color: #000;
border-color: #000;
}
.btnBlack:active {
color: #f16e00;
border-color: #f16e00;
}
.btnsTable {
white-space: initial;
}
#btnsForm {
text-align: center;
}
You shouldn't be using float: left to position things next to each other in this case. In this case you'd rather want to use a flexbox. Change the css to this to have it display next to each other:
.cols3 {
width: 33%;
padding: 0 0.3rem 3rem;
}
#numbers {
display: flex;
justify-content: space-between;
}
edit: There are way better ways to position things on certain places nowadays. If you want to position multiple elements on either the X axis or Y axis there is what we call a flexbox. If you want to position multiple elements on both the X axis AND Y axis, there is a grid.
Found it !
.cols3 {
width: 33%;
padding: 0 0.3rem 3rem;
}
.subcontainer {
display: flex;
}
For the HTML part, all the titles h3 have been moved before the div subcontainer.
With that, it's all fixed.
But like Terry said, the grid properties should have been used, it's better when you try to arrange objects on X & Y position.
Thanks

CSS :focus-within selector for an element with an iframe child

I have a scenario like this:
.container {
background: yellow;
padding: 40px;
}
.container:focus-within {
background: red;
}
iframe {
background: white;
}
<div class="container">
<input type="text" placeholder="Input 1">
<iframe srcdoc="<input type='text' placeholder='Input 2'>"></iframe>
</div>
As you can see, when I click on input 1 to type, the container turns red because of the :focus-within selector. But when I focus input 2 (inside the iframe), it doesn't.
Is it possible to use a CSS selector on .container for if something inside the iframe is focused? I have control over the CSS inside the iframe as well.
not by css but with jQuery, you can add class to container when input 2 focused, and remove class when it loses focus.
<style>
.container:focus-within, .container.in_iframe {
background: red;
}
</style>
<script>
$('document').ready(function () {
$('#iframe').on('load', function () {
var iframe = $('#iframe').contents();
iframe.find("input").on('focus', function () {
$('.container').addClass('in_iframe');
});
iframe.find("input").on('focusout', function () {
$('.container').removeClass('in_iframe');
});
});
});
</script>
full code: https://codepen.io/peker-ercan/pen/PdgEOy
CSS is scoped to the one document - an iframe is a separate document.
Any CSS that applies to your page that contains that iframe can't apply to elements within the iframe.
Failed attempt with :has() selector as another example:
.container {
background: yellow;
padding: 40px;
}
.container:has(:focus) {
background: red;
}
iframe {
background: white;
}
<div class="container">
<input type="text" placeholder="Input 1">
<iframe srcdoc="<input type='text' placeholder='Input 2'>"></iframe>
</div>
You will need JavaScript, here is an example that should work with an iframe of the same origin:
document.querySelectorAll('.container').forEach((container) => {
container.querySelectorAll('iframe').forEach((iframe) => {
iframeRef(iframe).addEventListener('focus', () => {
container.classList.add('container-iframe-focused-within')
})
iframeRef(iframe).addEventListener('focusout', () => {
container.classList.remove('container-frame-focused-within')
})
})
})
function iframeRef(frameRef) {
return frameRef.contentWindow ?
frameRef.contentWindow.document :
frameRef.contentDocument
}
.container {
background: yellow;
padding: 40px;
}
.container:focus-within,
.container-iframe-focused-within {
background: red;
}
iframe {
background: white;
}
<div class="container">
<input type="text" placeholder="Input 1">
<iframe srcdoc="<input type='text' placeholder='Input 2'>"></iframe>
</div>

Remove form label when input is active/focus

Trying to remove a icon when the input is active/focused. The problem is that :after do not work. When I click on the input the label is still visible.
Tried with content: none; but that didn't work.
#search-label {
position: relative;
font-weight: normal;
}
#search-label:before {
content:"\f002";
font-family: FontAwesome;
font-size: 115%;
position: absolute;
color: #dddddd;
top: 5px;
left: 8px;
}
Edit:
<div class="form-group">
<label id="search-label">
<input id="search-form" class="form-control" placeholder="{% trans "Search" %}" type="text" name="q" value="{{ request.REQUEST.q }}">
</label>
</div>
Can I do this with CSS or do I need JavaScript for this?
Image of it: http://imgur.com/a/pA1uN
It really depends on your markup, and if you have the freedom and/or ability to modify it.
Yes, the markup can be changed: CSS-only solution
If your label is a sibling and occurs after the input, you can toggle its display status based on the state of the input element itself, for example:
.input-row {
position: relative;
}
.input-row label {
position: absolute;
left: 0;
}
.input-row label::before {
transition: 1s;
/* Just to show how the toggle works */
}
.input-row label.icon1::before {
content: 'icon1';
}
.input-row label.icon2::before {
content: 'icon2';
}
.input-row input {
padding: 0 2rem 0;
}
.input-row input:focus + label::before,
.input-row input:active + label::before {
opacity: 0; /* To visually hide the label's pseudoelement */
pointer-events: none; /* To prevent any cursor-based interaction */
}
<form>
<div class="input-row">
<input type="text" name="input1" id="input1" />
<label for="input1" class="icon1"></label>
</div>
<div class="input-row">
<input type="text" name="input2" id="input2" />
<label for="input1" class="icon2"></label>
</div>
</form>
No, the markup cannot be changed: JS-only solution
If the markup cannot be changed, you will be forced to use a JS-only solution. This is because CSS does not have the ability to traverse backwards (i.e. select previous siblings) or up the parent node (i.e. select the wrapping elements). Remember that as pseudo-elements are not part of the DOM, you cannot select them via JS. Instead, you will have to toggle a class in order to perform the hiding/showing.
In my proof-of-concept example below using the markup that you have provided after updating your question, you can see how jQuery can be used to achieve the desired function. You can of course rewrite the JS function in native JS ;)
$(function() {
$('.form-group :input')
.focus(function() {
$(this).closest('label').addClass('hide');
})
.blur(function() {
$(this).closest('label').removeClass('hide');
});
});
label {
position: relative;
font-weight: normal;
}
label::before {
content: "icon";
font-family: FontAwesome;
font-size: 115%;
position: absolute;
top: 5px;
left: 8px;
transition: 1s; /* Added just to show the effect of toggling opacity */
}
label.hide::before {
opacity: 0; /* To visually hide the label's pseudoelement */
pointer-events: none; /* To prevent any cursor-based interaction */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label id="search-label">
<input id="search-form" class="form-control" placeholder="Search" type="text" name="q" value="">
</label>
</div>
Maybe someone will find this useful:
$('.form-group')
.focus(function() {
$(this).prev('label').addClass('u-hidden');
})
.blur(function() {
n=$(this).val();
if (n.length<1){
$(this).prev('label').removeClass('u-hidden');}else{
$(this).prev('label').addClass('u-hidden');
}
});
A jQuery solution would look something like this:
$("input").focus(function(){
$("span#your_icon").hide();
}).blur(function(){
$("span#your_icon").show();
});
A JS solution would look like this:
document.getElementById("myInput").addEventListener("focus", function(){
document.getElementById("#your_icon").style.display = 'none';
});
document.getElementById("myInput").addEventListener("blur", function(){
document.getElementById("#your_icon").style.display = 'inline';
});
Instead of label you can use a background image and do somthing like this
.input-search {
background:url('http://www.aljanaedu.com/Limitless/images/icons/14x14/search.png') no-repeat left 10px center;
}
.input-search:focus {
background-image:none;
}
Working Fiddle here
albeit a few years late to the question... but I found the best article that worked wonders for me, using jQuery, this article shows us how to essentially add a class to the label's parent container (div) if the input field is either selected/focused AND if the field has a value, OR remove said class if the input field is NOT focused AND empty. https://knowbility.org/blog/2019/animating-form-labels
$("form input").on("blur input focus", function() {
var $field = $(this).closest(".field");
if (this.value) {
$field.addClass("filled");
} else {
$field.removeClass("filled");
}
});
$("form input").on("focus", function() {
var $field = $(this).closest(".field");
if (this) {
$field.addClass("filled");
} else {
$field.removeClass("filled");
}
});

CSS positioning of JS dropdown - relative to fluid searchbar width

I'm working on a searchbar that has a js dropdown for categories. The searchbar needs to be responsive to width changes and at the moment the general layout is fine. I'm having one problem of positioning the dropdown menu relative to the category button. As it is now, the dropdown pushes the searchbar content down.
I have applied z-index, but no joy.
I have put the category dropdown within the new-sb-container, but still no joy. Any help greatly appreciated.
<div id="new-sb-container">
<div id="search">
<form id="search-form" method="post" action="">
<input type="text" name="q" id="query" placeholder="Search..."/>
<div id="search-category" title='Select a search category'>All Categories</div>
<div id="search-category-menu">
<label id='search-options'>Search Options:</label><br/>
<input type="radio" name="search_category" id='all_categories' value="all" checked><label for='all_categories'>All Categories</label><br/>
<input type="radio" name="search_category" id='antiques' value="antiques"><label for='antiques'>Antiques</label><br/>
</div> <!--end category menu-->
</form>
</div> <!--end search container-->
<div id="search-submit"><form><input type="submit" id='search-submit' value="Search"></form> </div>
</div><!--end sb container-->
#new-sb-container{
clear:both;
overflow:hidden;
width: 60%;
margin-top: 20px;
}
div#search
{
overflow:hidden;
width: 70%;
background-color: #FFFFFF;
border: 1px solid #CCCCCC;
border-top-left-radius: 7px;
border-bottom-left-radius: 7px;
padding-left: 10px;
float:left;
z-index: 1;
position:relative;
}
#search-form{
position: relative;
float:left;
width: 100%;
z-index: 0;
}
input#query{
position:relative;
float:left;
width: 60%;
height: 27px;
border: 1px solid #FFFFFF;
overflow:hidden;
}
div#search-category /*grey category box*/
{
border-radius: 7px;
font-family: Verdana,Arial,sans-serif;
font-size: 11px;
display:inline;
background-color: #CCCCCC;
padding-top: 5px;
padding-bottom: 5px;
padding-left: 7px;
padding-right: 7px;
cursor: default;
margin-right:5px;
float:right;
}
div#search-category-menu
{
float:right;
position:relative;
width: 150px;
background-color: #FFFFFF;
border: 1px solid #000000;
padding: 8px;
margin-top: 10px;
z-index:99;
float:right;
}
div#search-submit{
position:relative;
float:left;
width:20px;
}
As the code stands the search-category-menu is relative to search-form > search > sb-new container.
and the jq form if this help...
$(document).ready(function()
{
function search_autocomplete(selector, tags, default_value)
{
$('#' + selector).focus(function()
{
if($('#' + selector).val() == default_value)
{
$('#' + selector).val('');
}
});
$('#' + selector).blur(function()
{
if($('#' + selector).val() == '')
{
$('#' + selector).val(default_value);
}
});
var search = $('#' + selector).autocomplete(
{
source: tags,
timeout: 0,
select: function(event, ui)
{
var selected_object = ui.item;
var search = selected_object.value;
search = search.replace(/\s+/g, '-').toLowerCase();
var category = $('input[name="search_category"]:checked').val();
$("#search-form").attr("action", "/search/" + search + "/category/" + category);
$("#search-form").submit();
}
});
$('#' + selector).data('ui-autocomplete')._renderItem = function (ul, item)
{
var re = new RegExp('^' + this.term, "i");
var t = item.label.replace(re, "<span id='dropdown-item-highlight'>" + "$&" + "</span>");
return $('<li></li>')
.data("item.autocomplete", item)
.append('<a> ' + t + '<span id="dropdown-category"> in ' + item.type + '</span></a>')
.addClass( 'dropdown-item' )
.appendTo(ul);
$('<li></li>').append('<a>Hide suggestion</a>').appendTo(ul);
};
$('#' + selector).data('ui-autocomplete')._renderMenu = function (ul, items)
{
var that = this;
$.each( items, function( index, item )
{
that._renderItemData( ul, item );
});
$(ul)
.attr( 'tabindex', -1 )
.addClass('dropdown-menu');
$('<li></li>')
.append('<a>Hide Suggestion</a>')
.addClass( 'dropdown-suggestion' )
.appendTo(ul);
};
}
$('#search-category-menu').hide();
$('#search-category-menu input[type="radio"]').click(function()
{
var category = $(this).next("label").text();
$('#search-category').text(category);
$('#search-category-menu').hide();
//not focusing by selector
$('#query').focus();
});
$('#search-category').click(function()
{
if ($('#search-category-menu').is(":visible"))
{
$('#search-category-menu').hide("fast");
}
else
{
$('#search-category-menu').show("fast");
}
});
var availableTags = [{"label" : "XBOX 360", "type":"Electronics"},
{"label":"XBOX One", "type":"Electronics"},
{"label":"Nike", "type":"Clothing & Footwear"}];
search_autocomplete('query', availableTags, 'Search...');
});
Fixed with the folowing
take category drop down out of container.
place a span element with width at similar percentage as category button movement.
make drop down relative to absolute span
apply z indexes to span and drop down.
code below
<div id="new-sb-container">
<div id="search">
<form id="search-form" method="post" action="">
<input type="text" name="q" id="query" placeholder="Search..."/>
<div id="search-category" title='Select a search category'>All Categories</div>
</form>
</div> <!--end search container-->
<div id="search-submit"><form><input type="submit" id='search-submit' value="Search"></form></div>
</div><!--end sb container-->
<span id = "menuplacer">
<div id="search-category-menu">
<label id='search-options'>Search Options:</label><br/>
<input type="radio" name="search_category" id='all_categories' value="all" checked><label for='all_categories'>All Categories</label><br/>
<input type="radio" name="search_category" id='antiques' value="antiques"><label for='antiques'>Antiques</label><br/>
</div> <!--end category menu-->
</span>
#menuplacer{
clear:both;
width:41%;
position: absolute;
}
div#search-category-menu
{
width: 150px;
background-color: #FFFFFF;
border: 1px solid #000000;
padding: 8px;
margin-top: 10px;
z-index:1000;
position:relative;
float:right;
}

Resources