I am creating an html5 file that is meant to be printed. One part of the printed output is a form to be manually filled out on the printed output. I want to create underlines under the blanks as is normal on such a form.
The example code at http://www.w3schools.com/cssref/tryit.asp?filename=trycss_dim_height allows me to define a width of a element, and apply a border-bottom css property to it. The output is a faux underline the width of the element, even if there is no text in the element.
The same code in both Safari and Chrome on OS X Yosemite does not show any bottom border.
Is there a way to generate an underline of a specified length?
make a custom tag element called 'sp' eg. <p>THIS IS A <sp/> SPACE</p>
CSS for the new 'sp' tag:
sp:before {
content: '\2003\2003\2003\2003\2003'; // '\2003' is the ISO code used in CSS for a 4 unit wide whitespace. Here I use 5 of them.
text-decoration:underline;
}
Add according to the width you need (example below):
sp:before {
content: '\2003\2003\2003\2003\2003';
text-decoration:underline;
}
<p>THIS IS A <sp/> SPACE</p>
<p>THIS IS A <sp/><sp/> SPACE</p>
<p>THIS IS A <sp/><sp/><sp/> SPACE</p>
Does something like this work for you?
.underline {
width: 100px;
border-bottom: 1px solid;
display: inline-block;
}
bla <span class="underline"></span> bla
However, instead of giving the underline a fixed width, I would suggest you adapt the width of the underlined area dynamically according to the container's width.
Set a width, ideally a percentage so it can adapt if the page size or layout changes. 100% if your container is doing layout, some other percent as appropriate. You can use inches (in), centimeters (cm), and so on, but you will need to test it. Just do not use pixels (px). Also, use pt for the border line as that is something a printer generally renders consistently.
#media print {
input[type=text] {
border-bottom: 1pt solid #000;
width: 50%; /* use a % value, or maybe in, but not px */
}
}
This only fires when the page is printed. It removes the border from any <input type="text">.
Since I misunderstood your question at first, I initially wrote CSS to underline only text that was already entered and to keep the width of the text. So I'll put that here anyway.
#media print {
input[type=text] {
border: none;
text-decoration: underline;
}
}
It underlines the text in the field, which takes its length from the width of the text anyway.
In both cases, no custom elements, no special classes, no script.
EDIT: I should have qualified, I come from a world where forms that are printed are usually backed by an on-screen form, so I started there.
Related
I started learning how to write code within the last few weeks and I have been trying to improve and expand as well as put in to practice some of what I have learned by creating a text-based, web browser game.
The problem I am having is when I create a RNG and try to add a confined border it expands to fill a horizontal section of the webpage.
I have tried using some border measurement instructions but nothing seems to affect it. I have tried searching various web pages for a solution as well but nothing.
I have posted what I have written so far:
<style>
.bordered {
width 50px;
height 50px;
padding 25px;
border: 3px solid black;
}
</style>
<section>
<div class="bordered">
<p id="one"; width 50px;></p>
<button onclick="random()">Random</button>
<script>
function random(){
document.getElementById("one").innerHTML = Math.floor(Math.random() * 100);
}
</script>
Any suggestions?
The problem is that you have invalid HTML / CSS:
Your line of code <p id="one"; width 50px;></p> has a few errors; attributes should not have semicolons after them, and you width needs an equals sign. Ideally it would have quotation marks as well, as <p id="one" width="50px"></p>. Also note that setting inline rules like this is generally considered bad practise, and they carry the second-highest level of specificity. CSS should be used in most cases instead.
Your CSS rules need to have colons separating the properties from their values. For example, width 50px should be width: 50px.
Both the inline style and the CSS styles are invalid, and subsequently neither attempt to set width to 50px is getting applied. Because the element is block-level, it expands to the maximum width by default.
Fixing up the above two issues will confine your border's width, though as an additional nitpick you should avoid using the inline event handler onclick. Instead, you should separate the markup form the logic by making use of JavaScript's addEventListener() method.
This can all be seen in the following:
document.getElementsByTagName('button')[0].addEventListener("click", function() {
document.getElementById("one").innerHTML = Math.floor(Math.random() * 100);
});
.bordered {
width: 50px;
height: 50px;
padding: 25px;
border: 3px solid black;
}
<section>
<div class="bordered">
<p id="one"></p>
<button>Random</button>
</div>
</section>
Hope this helps!
I want to:
be able to style some text on my HTML page so that a certain background color only covers the text and not beyond it.
Ideally I would like to control this from one div.
Here is my jsfiddle of the below:
#edit_this_div {
min-width: 0px;
background-color: yellow;
}
#bad_way {
background-color: yellow;
display: inline-block
}
<div id="edit_this_div">Please edit this div to there isn't extra yellow background without manually setting the width.</div>
<br>
<div id="bad_way">This is the inefficient and manual way.</div>
What I tried:
The way I thought of accomplishing this is to set the div as an inline block, which I've also shown in my jsfiddle. However, I rather not do this because I feel it would complicate things; when I did this my block started jumping around and combining with other elements. I don't plan to have any other elements with the div so I am fine with it staying as a block that takes up the whole line on the screen.
With the display of block, I also tried setting the padding and minimum widths but it doesn't have an effect laterally for removing the extra color that spills past the text.
It is generally recommended that you put text into appropriate block tags, i.e. <p>...</p>, <h1>...</h1>, <blockquote>...</blockquote>, etc.
If you did that, it would be easy, for example:
<div id="edit_this_div">
<p>Please edit this div to there isn't extra yellow background without manually setting the width.</p>
</div>
Then the CSS:
#edit_this_div p {
background-color: yellow;
display: inline;
}
Even cleaner would be to use both <p>-tags as well as additional inline tags, for example <span>-tags:
<div id="edit_this_div">
<p><span>Please edit this div to there isn't extra yellow background without manually setting the width.</span></p>
</div>
CSS:
#edit_this_div p span {
background-color: yellow;
display: inline;
}
What you need is <mark></mark> tag, like this:
<p>Do not forget to buy <mark>milk</mark> today.</p>
Here's a fiddle for you:
http://jsfiddle.net/am9rzfmd/
The default css settings for this tag are:
mark {
background-color: yellow;
color: black;
}
So you don't have to explicitly define the css, only just in case you need to change the color.
Update
As misterManSam pointed out:
Be aware that the element has a special semantic meaning and
shouldn't be used if you just want "to make my text a yellow
background"
Change it from a div to a span and it will only stretch its width to the contents within it.
<body>
<span id="edit_this_div">Please edit this div to there isn't extra yellow background without manually setting the width.</span>
<br>
<br>
<span id="bad_way">This is the inefficient and manual way.</span>
</body>
http://jsfiddle.net/bbv5ryhk/
Here's an example of code to place a border around span tags on hover:
CSS
p {
background-color: #def;
width: 137px; /* chosen so the text *just* fits, may need to alter
* for different browser or OS
*/
}
span {
margin: 0;
}
span:hover {
margin: -2px;
border: 2px solid #336;
}
HTML
<p>
<span>hover</span> <span>over</span> <span>the</span> <span>words</span>
</p>
(See demo at: http://jsfiddle.net/sS7vY/)
It uses a -ve margin to compensate for the border and avoid the text shifting position on hover.
On Firefox, hovering over the very last word causes it to wrap over to the next line, which I want to avoid. On Chrome it behaves as I intended and never wraps.
Is this a Firefox bug that needs reporting?
Is there a way to prevent this wrapping in Firefox, in a way that works for arbitrary text? (i.e. adding a couple more pixels width to the outer <p> is not a valid solution!)
Not sure if it's a bug in either browser as I'm not familiar with the inline box model, but using an outline instead of a border seems to work well as outlines don't affect box sizing, even on inline-level boxes:
span:hover {
outline: 2px solid #336;
}
I forded a working solution of your's : jsfiddle.net/dgY4J
It seems to be a mixed of 'box-sizing' and available width situation.
Also, if you use the css box-sizing, you won't have to deal with borders with the negative margins.
One last tip : chosen so the text just fits, may need to alter for different browser or OS || it will do the oposite. No browsers render font type the same.
I want to have a ▲▼ symbol to denote that a specific table column is sortable (e.g. name▲▼ but with the arrows on top of each other). To get that onto one line I have used a list:
<table class="test">
<tr><th>test <ul><li>▲</li><li>▼</li></ul></th></tr>
</table>
The arrows are too big and spaced badly. So, to style the result I used:
th ul{
display:inline-block;
}
.test th ul{
font-size:5px;
margin:0;
padding:0;
}
.test th li{
font-size:5px;
margin:0;
padding:0;
}
This was meant to make the combined character smaller, but font-size seems to have no effect at all on the size of the symbols. How should this be done?
Edit: It now seems that this works perfectly in opera and that this is some firefox (my version is the 32 bit linux 20.0 one) specific issue. In fact it just affects arrow sizes:
<p>▲</p>
body{
font-size:5px;
}
This still results in large arrows. Can anyone else confirm that it's a firefox only issue?
This is just weird, I'll probably just go with using an image.
Maybe this will help. Use a icon font instead of html symbols. https://github.com/aristath/elusive-iconfont
You can also visit a demo page at: http://shoestrap.org/downloads/elusive-icons-webfont/
Alternatively, try http://fortawesome.github.io/Font-Awesome/
Both font sets have up/down carets, but not in one symbol.
You could try the following:
<table class="test">
<tr><th>test <span class="icon-sortable">▲<br>▼</span></th></tr>
</table>
with the following CSS:
th {
outline: 1px dotted blue;
font-size: 1.00em;
}
.icon-sortable {
outline: 1px dotted gray;
display: inline-block;
font-size: 0.50em;
vertical-align: middle;
}
I would use an inline-block to position the two arrows, a bit easier to style and simpler mark-up (fewer tags).
You can set a font-size for the icon either using relative or absolute units depending on your site's styling.
Use vertical-align to position the icon vertically, I used middle, but top, bottom, baseline and other options are available, again depends on your preference.
If you need to move the two symbols close together, you need to wrap them in another tag and adjust the positioning.
I constructed two examples, one basic and the other fancy with more tags to control arrow positioning.
You can also adjust the padding, width, margin of the inline-block for a lot more control.
Fiddle: http://jsfiddle.net/audetwebdesign/XPQPh/
You may have a minimum font size setting in Firefox, preventing the effect of setting font size to 5 pixels. In general, if you need a font size that small, you really need a different approach.
The size of the characters ▲▼ greatly varies by font, and so does their spacing, so you should have a font-family setting for them that suits your needs, possibly a rather large list of fonts, just to be sure (after all, no font is present in all computers).
How to automatically change the space between the letters.I want the text to take up the entire width of the div. Text is not static. (Always changing text, can be 123" or "text text"...)
<style type="text/css">
#menu{
width: 200px;
background-color: #000;
color: #336699;
font-size: 16px;
letter-spacing: 100%;
}
</style>
<body>
<div id="menu">
tekstas
</div>
EDIT: Unfortunately this only changes word spacing, not letter spacing. There is not way to do kerning in CSS. Possibly CSS3, however.
This is easily accomplished with the text-align: justify CSS attribute:
#menu
{
width: 200px;
background-color: #000;
color: #336699;
font-size: 16px;
text-align: justify;
}
There is no way of doing this purely with CSS. The letter-spacing attribute doesn't take percent values. text-align: justify won't work either because it only affects the space between words, not the font kerning and it also only applies to those rows of text that are followed by another row.
You could try using JS to do this by counting the number of characters in a particular div and then calculate the needed space between the characters so it would fill out the width, but this solution would only work right with mono-spaced fonts (fonts that have the same width for all the characters).
Here's a solution will not work for everyone, but it turned out to solve the problem for me: if you are displaying a short amount of headline text, you can put a space between every character of every word "L i k e t h i s".
For my particular design, this happens to look fine, and of course it allows align: justify to fully do its magic within the div.