I want to dynamically generate the text within 'content' of a CSS pseudo class in the proper way. I have filled a working jsfiddle with my example so far. This picture shows better how I want to achieve this:
This is the relevant code (it's in the fiddle also):
[part of] index.php:
<div class="checkbutton">
<input type="checkbox" value="None" id="slideThree" name="check"/>
<label for="slideThree"></label>
</div>
[part of] style.css:
.checkbutton:before
{
content: 'Hombre';
float: left;
width: 60px;
text-align: center;
/* 28 es la altura total */
font: 12px/28px Arial, sans-serif;
color: CornflowerBlue;
z-index: 0;
font-weight: bold;
}
I want to be able to reuse that code for two purposes:
Dynamically and internally translate it with PHP.
Different buttons with similar style.
This means, I want to be able to create a similar button but with different names, or just translate it without extra markup. I have no idea how to do this properly. Here's why:
The CSS is in a unique .css file, separated from the content. Obviously, this doesn't execute php.
It's not possible to have CSS pseudo elements inside inline styles
No JavaScript. While this could be achieved with javascript, I prefer to avoid it where possible.
How can I achieve it? I find it odd that I need to use CSS for
As long as you have the ability to set the content you want as an attribute, you can make use of the attr() function to abstract your styles:
http://tinker.io/bda2e
.checkbutton:before {
content: attr(data-checked);
}
<div class="checkbutton" data-checked="Hombre" data-unchecked="Mujer">
<input type="checkbox" value="None" id="slideThree" name="check"/>
<label for="slideThree"></label>
</div>
Custom attributes with the prefix of data- are a part of HTML5: Embedding custom non-visible data with the data-* attributes
After writing the question and how much trouble it took me to find a solution, I post it in case someone finds it useful. It's in this fiddle and the relevant code here:
[part of] index.php:
<div class="checkbutton">
<input type="checkbox" value="None" id="slideThree" name="check" checked />
<label for="slideThree"></label>
<!-- Now this can be generated with PHP -->
<span class = "leftcheck"><?= "Hombre"; ?></span>
<span class = "rightcheck"><?= "Mujer"; ?></span>
</div>
[part of] style.css:
.checkbutton .leftcheck
{
position: absolute;
left: 0px;
width: 50%;
text-align: center;
line-height: 28px;
color: CornflowerBlue;
}
Of course, if you have a better method, please write it as an answer.
Related
I just recently started with coding , it's a small project and it's a web tool based on php and html. I started using flexbox to have more control over where snippets are on my site. I fetch a few things from my database and list them as shown in the example below. I have to use PHP because the actual content contains various php-parts to implement the data from the database.
//this is just an example, which when run, will show the problem...
//php part
echo "<div class='shell'>";
echo "<div class='shell a'>Headline<br> Something <br> another thing
<br> a third thing </div>";
echo "<div class='shell b'><b>Headline</b><br> Something <br> another thing
<br> a third thing </div>";
echo "<div class='shell c'><p><b>Headline</b><br> Something <br> another
thing <br> a third thing </p></div>";
echo "</div>";
//css part
.shell {display:flex; margin:auto; height:auto; width:600px; border: 1px
solid black;justify-content:center;}
.a {width:auto;}
.b {width:auto;}
.c {width:auto;}
I want to use (shell b) for my boxes because without the (p) text formatting the box will only be around my actual text, example (shell c) will use space above and below the text, the border will show this. (Shell a) is there to show how it is supposed to look, and how i want (shell b) to look, with the bold headline.
The problem is, that if i format anything in any kind without wrapping it in (p) it will get its own "row" and the following text will not be listed directly below it. Is this a known problem and is there a solution to it?
I hope i could explain this so someone can understand it.
Thanks in advance.
Edit: I added a picture so the problem can be seen without running the code somewhere.
result of the code
As you use dispay: flex on all shell, it will look like this
.shell {
display: flex;
margin: auto;
height: auto;
width: 600px;
border: 1px solid black;
justify-content: center;
}
<div class='shell'>
<div class='shell a'>Headline<br> Something <br> another thing
<br> a third thing </div>
<div class='shell b'><b>Headline</b><br> Something <br> another thing
<br> a third thing </div>
<div class='shell c'>
<p><b>Headline</b><br> Something <br> another thing <br> a third thing </p>
</div>
</div>
And the reason, for the 2nd item, is the <b></b> tags, which will be treated as a flex item, and in combination with the text, which is an anonymous flex item, they will render side-by-side.
The 3rd item solves this by having a wrapper, the p.
If you only set display: flex on the outer div, it likely will look what you expect
.shell.flex {
display: flex;
margin: auto;
height: auto;
width: 600px;
justify-content: center;
}
.shell {
border: 1px solid black;
}
<div class='shell flex'>
<div class='shell a'>Headline<br> Something <br> another thing
<br> a third thing </div>
<div class='shell b'><b>Headline</b><br> Something <br> another thing
<br> a third thing </div>
<div class='shell c'>
<p><b>Headline</b><br> Something <br> another thing <br> a third thing </p>
</div>
</div>
Note, I didn't used the a/b/c classes as width: auto is the default
I have a form where I need a multiselect. For that I use Select2.
Problem is: In this form all of my fields are required, and I inform my users about that by showing an asterisk when the field is empty. However, Select2 totally ignores the fact there is a required attribute on the created select.
So my question is: How can i show an asterisk image inside a select2 dropdown? Preferably with the same method i use for my other inputs?
EDIT: I already took care of the validation part. My question is purely about informing the user what fields are required.
Example Form
HTML
<input type="text" name="name" id="name" value="" placeholder="" maxlength="255" style="width: 300px" required="required"/>
<input type="text" name="label" id="label" value="" maxlength="8" style="width: 150px" required="required"/>
<?php
echo form_multiselect('client_types[]', $client_types, set_array('client_types', null), 'class="select2" style="width: 315px;" required="required"');
?>
CSS
input:required, textarea:required {
background: #fff url(../images/red_asterisk.png) no-repeat 98% center;
}
.required_notification {
color:#d45252;
margin:5px 0 0 0;
font-size: 12px;
font-weight: bold;
}
input:required:valid, textarea:required:valid {
background: #fff url(../images/valid.png) no-repeat 98% center;
box-shadow: 0 0 5px #5cd053;
border-color: #28921f;
}
input:focus:invalid, textarea:focus:invalid {
background: #fff url(../images/invalid.png) no-repeat 98% center;
box-shadow: 0 0 5px #d45252;
border-color: #b03535
}
You can do three things:
1) Create a rule in Select 2 and copy the multiple rule. That rule adds the little x in that position so you can replace with an asterisk instead for yours. This is most likely the least preferable of the three options so I won't go into details.
2) You can use CSS on your select2 container like:
select2-container:after {
content: "*";
display: block;
position: absolute;
right: 10px;
top: 5px;
color: red;
}
select2-container { position: relative; }
Not exact code but you get my drift. If this is for specific fields you might have to add a class to that field and reflect that in your CSS or add a wrapper element around your select and target that as well.
3) If compatibility is an issue you could always use jQuery.
$(".select2-container").append("<span class="asterisk">*</span>");
Again using your selectors to get the exact element you want and add CSS for the asterisk span.
I don't know enough about select2 so if there's an option in the init for adding a required indicator that's going to be your best best so I would start researching there to see if this is something provided out of the box and if not then you can try one of the above options.
I had same problem. And i found out 2 acceptable (for me) solutions.
Use ajax to validate field is it unset\set incorrectly.
Use simple JavaScript to validate is there any information in the input.
It can be done when clicking the button (submit) or after change focus from input.
I would like to align text to the left side and the input box to the right side.
This is how my site looks like and this is how it should be: [PICTURE REMOVED]
However, I did what I wanted by using div tag inside a div tag, so, if I give background colour to it, this is how it looks like:
The problem is, after green (on the grey side) I would like to echo errors. But because of the div width of the green area, I cannot do that. Error goes under input box. So, I wonder how can I be able to show form like I did in pictures but at the same time able to show errors on the right side, as well.
So, the script calls javascript plugin (http://jqueryvalidation.org). And this plugin add error right after input box, when it sees a problem. That's why I am not able to put the errors in another div tag.
error will appear in this class: "label.error". If there is way to make this class "don't care fixed width" in css. That would work.
Basicly what you want to do is create a container <div /> element and put 2 <div /> elements inside it. Since you didn't give any code, I can only give you an example:
#container {
width: 512px;
height: 506px;
border: 3px solid green;
}
#left {
float: left;
width: 250px;
height: 500px;
border: 3px solid red;
display: block;
}
#right {
float: left;
width: 250px;
height: 500px;
border: 3px solid blue;
display: block;
}
<div id="container">
<div id="left">
Form here
</div>
<div id="right">
Errors here
</div>
</div>
This is a UI/UX issue and surprisingly I'm shocked no one mentioned using a table with div's.
Because your picture was removed I can't really compare the UI, but to make this a simple answer for Text to the left and Textbox to the right see below.
(JS is just to show how it does work)
function updateText(){
var txtStrng=document.getElementById("txtTextbox1").value;
document.getElementById("lblText").innerHTML=txtStrng;
}
<form action="" >
<table width="100%" height="71" border="0">
<tr>
<td width="45%"><div align="left"><label id="lblText">Test</label></div></td>
<td width="55%"><div align="right">
<input type="text" name="txtTextbox1" id="txtTextbox1" value="Test" onkeyup="updateText();" />
</div></td>
</tr>
</table>
</form>
I had gone through w3school and read some online resource it's really difficult for me to understand html and css, please someone help me, I am beginner
I have created this form but problem is its not coming center and not looking nice, I want to see like this
Latitude Max
[ ]
Longitude Min [ ] [ ] Longitude Max
[ ]
Latitude Min
SUBMIT
Many people told me to use dreamviewer its gui based, but I am using centos so I like to understand and write html and css code manually.
See here is my code
<!DOCTYPE html>
<html>
<body>
<form action="search.php" method="post">
Latitude Max <input type="lat_max" name="RANGE[]"></br/>
</br/>Longitude Min <input type="lon_min" name="RANGE[]">
<input type="lon_max" name="RANGE[]">Longitude Max</br/>
</br/>Latitude Min <input type="lat_min" name="RANGE[]">
</br/><input type="submit" name="formSubmit" value="Submit" />
</form>
</body>
</html>
and this form I want to call inside php script please help me.. as I beginner I am taking more than 1 day to design one html page, hard but true.
I'm infinitely sympathetic to beginners, since I was one about a year ago. It takes time, and reading tons of code. I'll give you a "solution", but spend most of the time going over the CSS portion of the fiddle. If this is your first step, you still have much to learn about reading the values from the input fields, cleaning them up, and using them.
A large program begins with a single FIDDLE.
CSS
.container {
width: 600px;
border: 1px solid black;
}
.singleinput {
width: 100%;
text-align: center;
margin-top: 25px;
}
.doubleinput {
width: 100%;
margin-top: 25px;
border: 1px solid transparent;
overflow: hidden;
}
.minlong {
float: left;
margin-left: 30px;
}
.maxlong {
float: right;
margin-right: 30px;
}
.buttondiv {
width: 80px;
margin: 20px auto;
}
input {
width: 50px;
}
Suggestions:
Learn how to use jsfiddle - you can experiment for hour after hour without have to upload your files to a server. It has all the 'stuff' (jQuery) built in.
Read about div widths, and "floating" divs.
The "overflow: hidden" is a trick for floating divs within divs. You just have to memorize it.
Play with the margins and watch what happens.
Figure out was "margin: 0px auto;" means.
Best of luck! Do NOT give up!
I think it's a good idea to make actual labels for the input. Both for property HTML and for convenient styling. Also, it's handy to embed the separate chunks in divs:
<!DOCTYPE html>
<html>
<body>
<form action="search.php" method="post">
<div class="pos lat_max">
<label for="lat_max">Latitude Max</label>
<input id="lat_max" name="RANGE[]"/>
</div>
<div class="pos lon_min">
<label for="lon_min">Longitude Min</label>
<input id="lon_min" name="RANGE[]"/>
</div>
<div class="pos lon_max">
<label for="lon_max">Longitude Max</label>
<input id="lon_max" name="RANGE[]"/>
</div>
<div class="pos lat_min">
<label for="lat_min">Latitude Min</label>
<input id="lat_min" name="RANGE[]"/>
</div>
<input type="submit" name="formSubmit" value="Submit" />
</form>
</body>
</html>
After that you can style them:
/* Start by absolutely positioning every element so you can put them
where ever you want. This is not always the right thing to do, but I think
it is for pieces of the website where you have a very specific positioning
like this. */
div.pos {
width: 10rem;
position: absolute;
}
label {
display: block;
text-align: center;
}
.pos label,
.pos input {
position: relative;
width: 100%;
}
/* Center the container of the latitude inputs */
.lat_max,
.lat_min {
left: 50%;
right: auto;
}
/* Correct the position, because the left side of the container is centered */
.lat_max *,
.lat_min * {
position: relative;
left: -50%;
}
/* Lat-min at the bottom */
.lat_min {
bottom: 0;
}
/* lon in the vertical middle of the page */
.lon_min,
.lon_max {
top: 50%;
}
/* lon max on the right */
.lon_max {
right: 0;
}
I know, this is not exactly what you described, but you can continue finetuning it.
http://jsfiddle.net/s99hU/
One important note:
position: absolute now works within the page, but actually it looks at the closest parent that has position: absolute or position: relative.
position: relative doesn't move the element itself, so you can add that to the form. This will postion the inputs in the form, rather than spread over the page. After that, you can give the form an exact width and height (especially the height is needed, otherwise the form will collapse). The inputs will then be positioned inside the form, and the form itself is a self contained block, a building stone that can be embedded and positioned inside the page however you want.
I am trying to add the new twitter button to one of my Wordpress templates. For an example, see here: http://johnkivus.com/2010/08/12/if-you-build-it/. I want the text aligned with the top edge of the button instead of the button. The code section that puts the twitter button in place is as follows:
<div class="postmeta">
<p>Tweet<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script> <?php _e("Filed under", 'studiopress'); ?> <?php the_category(', ') ?> · <?php _e("Tagged with", 'studiopress'); ?> <?php the_tags('') ?></p>
</div>
the CSS for postmeta is:
.postmeta {
font-size: 11px;
text-transform: uppercase;
margin: 0px;
padding: 5px 0px 0px 0px;
border-top: 1px solid #333333;
}
.postmeta p {
margin: 0px;
padding: 0px;
display: inline-block;
}
I'm far from an expert in CSS, so I've only tried the following things:
- adding "vertical-align: text-top;" to the css
- adding "display: inline-block;" & "vertical-align: top;"
- and, as a complete flyer, style='vertical-align: top' to the button.
What would be the easiest way to get the text aligned with the top of the button?
UPDATE
Based on a suggestion from thomasrutter, I added a style to both the button and the text. This gets me much closer to what I want, however, the elements processed via PHP commands are still showing up aligned with the bottom of the image. The link is the same to see the current state of things: http://johnkivus.com/2010/08/12/if-you-build-it/ however the code is not as follows:
<div class="postmeta">
<p>Tweet<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script> <span style="vertical-align: middle"><?php _e("Filed under", 'studiopress'); ?> <?php the_category(', ') ?> · <?php _e("Tagged with", 'studiopress'); ?> <?php the_tags('') ?></span></p>
</div>
You need to give both elements next to each other the same value for the vertical-align property.
In this case, where you are aligning a button with some text and the button is a different height to the text, I'd recommend vertical-align: middle which will need to be set on both the button and the text beside it (not on the containing block element, but on the actual inline elements including the button and the span of text beside it).
I don't predict that vertical-align: top would have the effect you desire, but you can certainly try it: you need to add that property to both the button and a span around the text that is beside it.