How can I break up long words instead of overflowing? - html

How can I stop this text from overflowing?
<html>
<head>
<style type="text/css">
.sticky
{
background-color: #FCFC80;
margin: 5px;
height: 100px;
width: 135px;
}
.sticky .edit
{
vertical-align:middle;
height: 100px;
position:relative;
color:Black;
background-color:blue;
height:90px;
vertical-align:middle;
width:90px;
border-collapse:collapse;
}
</style>
</head>
<body>
<div id="note44" class="sticky">
<div id="text44" class="edit" title="Click to edit" style="">A very long word: abcdefasdfasfasd</div>
</div>
</body>
</html>

I think word-wrap is supported in most browsers?
word-wrap:break-word;

Depends on what the desired output should be, but if you want to hyphenate the word, you can use ­, that is, replace "abcdefasdfasfasd", with say, abcdef­asdfasfasd.
You could also have a look at the overflow property.

does justify do it?

There's a CSS property called word-wrap. Give it the attribute "break-word" and you should be good to go.
.break-word {
word-wrap: break-word;
}
Source: Web Designer Wall - Force text to wrap

The css mentioned above won't work in all browser's as it's non-standard.
When I run into this I usually use php's wordwrap function, but that's no good if you're not using php.
Two things which need to be pointed out:
You've defined the height of the second element twice in the css.
If you wrap text inside an element with a defined height, it could well overflow, and cause you a new set of problems.

Related

How to add space to the bottom of a page without <br>

How can I add space to the bottom of a web page, say 100 pixels, without using multiple <br> tags? Right now I have something like this:
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
and that can be annoying to read when I'm looking through code. Is there a way to do this without <br>, maybe with CSS?
You can use margin-bottom, use the class when ever you want to keep 100px gap
.bottom {
margin-bottom:100px;
}
body {
padding-bottom: 100px;
}
You can certainly do it with CSS, in the <head> of your HTML document simply add:
<style>
body{
margin-bottom: 100px;
}
</style>
You want to use CSS' padding-bottom
body {
padding-bottom: 100px;
}

How to display a long link in multiple lines via CSS?

These are codes:
<div>Hello World. http://www.newyorker.com/arts/events/2014/02/03/140203gofr_GOAT_front.</div>
div {
background: red;
width: 200px;
height:200px;
}
http://jsfiddle.net/gEDx9
This long link is displayed at 2nd line. I hope long this link can be displayed in multiple lines. I also hope this long link won't be displayed at outside of red div element. This long link should be fully displayed.
So this long link should be displayed at 1st line, 2nd line and 3rd line. May it will also be displayed at 4th line.
How can this be done via CSS?
There is a CSS Property called "word-break" which you may find useful:
div {
background: red;
width: 200px;
height: 200px;
word-break: break-all;
}
Reference: W3Schools word-break information
Just add the word-wrap-attribute this way:
div {
background: red;
width: 200px;
height:200px;
word-wrap: break-word;
}
See updated fiddle: http://jsfiddle.net/qhzKF/
If you really need to include a URL in page content, insert zero-width spaces at permissible break points. You can use the reference ​ for them, e.g.
http://​www​.newyorker​.com/​arts/​events/​2014/​02/​03/​140203gofr​_GOAT​_front
The details depend on the conventions on line breaks in URLs. The above example complies to the rules of The Chicago Manual of Style. There are other styles, too, but no reasonable style allows arbitrary breaking of URLs (which is what you would get by using word-wrap: break-word).
The proper handling of URLs in content is thus somewhat tricky, but it can be automated. However, it is best avoided by not using URLs in content unless the page content is about URLs. Normally, you should use links with descriptive link texts, “hiding” URLs into href attributes.
apply this css to your A element
a { word-wrap:break-word; }
You can use the word-wrap:break-word
CSS:
div {
background: red;
width: 200px;
height:200px;
word-wrap:break-word;
}
http://jsfiddle.net/gEDx9/3/

How can I implement the code display style in StackOverflow?

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
I have a line of code ( which is to long to be displayed in one line ) to be displayed on a web page in one line, just like above.
I don't it to be wrapped into two lines.
Can I accomplish this only using css?
To actually make this happen when you use words with spaces in between them overflow:auto is not enough, you'll also need text-overflow: nowrap.
http://jsfiddle.net/kZV3j/
Here's how SO's code block looks:
<pre>
<code>
<span>...</span>
</code>
</pre>
And the CSS:
pre {
overflow: auto;
}
Demo: http://jsfiddle.net/TfeLm/
I think you're looking for overflow:auto:
<div style="overflow:auto; width:200px;">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
Create a containing element (e.g. a div), then set some basic CSS properties on it that define a width, and handle the overflow. Like this:
HTML
<div class="short">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>​
CSS:
.short {
width:400px;
padding: 10px;
overflow-x:scroll;
}​
jsFiddle example. Works in all modern browsers and IE8.
I think you are looking for the overflow property with a auto value:
<style>pre { width: 200px; overflow: auto; }</style>
<pre><code><p>Some tooooo long text on one line</></code></pre>
Live example: http://jsbin.com/uhuveg/
I believe that you should use the following CSS
#id {
width: 400px;
padding: 5px;
background: #C3C3C3;
overflow-x: scroll
}​
See this live example

Line right after text

I'd like to have a line that starts right after my text on the same line, I've tried with the following simple code
<html><body>My Text<hr/></body></html>
It seems that <hr> is not an option because it is always on a new line and I'd like the line to start at the right of my text.
Any help ?
The <hr> has default styling that puts it on a new line. However that default styling can be over-ridden, in the same way as it can for any other element. <hr> is in essence nothing more than an empty <div> with a default border setting.
To demonstrate this, try the following:
<div>Blah blah<hr style='display:inline-block; width:100px;' />dfgdfg</div>
There are a number of ways to override the styling of <hr> to acheive your aim.
You could try using display:inline-block; along with a width setting, as I have above. The down-side of this approach is that it requires you to know the width you want, though there are ways around this - width:100%;, and the whole line in a container <div> that has overflow:hidden; might do the trick, for example:
<div style='overflow:hidden; white-space:nowrap;'>Blah blah<hr style='display:inline-block; width:100%;' /></div>
Another option would be to use float:left;. You'd need to apply this to all the elements in the line, and I dislike this option as I find that float tends to cause more problems than it solves. But try it and see if it works for you.
There are various other combinations of styles you can try - give it a go and see what works.
Using FlexBox Property this can be achieved easily.
.mytextdiv{
display:flex;
flex-direction:row;
align-items: center;
}
.mytexttitle{
flex-grow:0;
}
.divider{
flex-grow:1;
height: 1px;
background-color: #9f9f9f;
}
<div class="mytextdiv">
<div class="mytexttitle">
My Text
</div>
<div class="divider"></div>
</div>
Try this:
<html><body>My Text<hr style="float: right; width: 80%"/></body></html>
The inline CSS float: right will keep it on the same line as the text.
You'll need to adjust the width if you want it to fill the rest of the line.
Using inline or float, as far as I tested it doesn't work properly even if this was my first thought. Looking further I used the following css
hr {
bottom: 17px;
position: relative;
z-index: 1;
}
div {
background:white;
position: relative;
width: 100px;
z-index: 10;
}
html
<div>My Text</div><hr/>
Demo http://jsfiddle.net/mFEWk/
What I did, is to add position relative in both elements (to give me the advantage of z-index use). Also from the moment I had position:relative for hr I moved it from the bottom:17px. This move it above the div that contains the text. Applying z-index values and adding background:white for the div puts the text above the the line. Of course don't forget to use a width for the text, otherwise will take the whole width of the parent element.
<div style="float: left">Some text</div>
<hr style="clear: none; position: relative; top: 0.5em;">
Exactly what you want.
Try this. It works
<p style="float:left;">
Hello Text
<hr style="float:left; width: 80%"/>
</p>
You can also use this to draw a line between texts like
Hello -------------------------- Hello
The OP never specified the purpose of the line, but I wanted to share what I ended up doing when I was making an html template where the user needed a line to write on after the document was printed.
Because the hr tag defaults to its own line and defaults to being centered in the line, I decided to use a div and style it instead.
HTML
This is my text.<div class='fillLine'></div>
CSS
.fillLine {
display:inline-block;
width: 200px;
border-bottom: 1px solid black;
}
JSFiddle Demo
Style Div for Line After Text
Hope that helps anyone who had the same goal as me.
hr {
width: {so it fits on the same line as the p tag};
}
p {
float: left;
width: {enough to accomodate the hr};
}
That sort of make sense?
<p>My text</p>
<hr />
Here's one potential approach, but it has some assumptions/requirements. Your question should be edited to give more specific information about what you're building.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Blah</title>
<style type="text/css">
body {
background-color : white;
font-family : Arial;
font-size : 16px;
}
.wrap {
background: transparent url(px.png) repeat-x 0px 85%;
/* Different fonts or text sizes may require tweaking of that offset.
px.png is a one-pixel(though can be thicker if needed) image in whatever color you want the line */
}
.inner {
background-color : white;
/* Should match the background of whatever it's sitting over.
Obviously this requires a solid background. */
}
</style>
</head>
<body>
<div class="wrap"><span class="inner">Here is some text</span></div>
</body>
</html>
I used the following technique:
Give the container div a background-image with a horizontal line.
Put an element (like <h3>) in the container div (I have it on the right so float: right; )
Use the following css:
.line-container {
width: 550px;
height: 40px;
margin-top: 10px;
background-image: url("/images/horizontal_line.png");
}
.line-container h3 {
padding-left: 10px;
float: right;
background-color: white;
}
Below code did the job for me
HTML File:
----------
<p class="section-header">Details</p><hr>
CSS File:
----------
.section-header{
float: left;
font-weight: bold
}
hr{
float: left;
width: 80%;
}
INLINE:
-------
<p style="float: left;font-weight: bold">Details</p><hr style="float: left;width: 80%;">

Make an element centred when it is alone, but aligned to the right when with another element

We have a page that ordinarily has two elements arranged side-by-side. Despite exploring a few angles for this, we can't seem to make it work. We're not averse to using JavaScript, it just feels that a CSS based solution ought to be possible. Is there a way of using just CSS (and possibly extra markup if necessary) to make element2 centre when it appears on its own?
Examples
Sometimes we have two elements, side by side.
<div id="container">
<div id="element1">content</div>
<div id="element2">content</div>
</div>
But in some conditions only element2 is on the page e.g.:
<div id="container">
<div id="element2">content</div>
</div>
There is a pure css solution, however it won't work in versions of IE less than 7 because it won't understand the sibling selector (+), for that you may want to consider a JavaScript solution (perhaps Dean Edwards' IE7). Anyway, some example css:
div#element2{
width:100px;
margin:0 auto;
}
div#element1{
width:50px;
float:left;
}
div#element1 + div#element2{
width:50px;
float:left;
margin:0;
}
The key is the line div#element1 + div#element2 which selects div#element2 given that it directly follows div#element1.
I think Phil was on the right track, but you should try using the CSS last-child pseudo-class. As far as I know, first-child and last-child are the only way in CSS to approximate an if construct.
div#container div#element2:last-child {
width:100px;
margin:0 auto;
}
div#element1{
width:50px;
float:left;
}
div##element2{
width:50px;
float:left;
margin:0;
}
The CSS above basically says "if element2 is the last child element of its parent use this set of styles, otherwise use these other styles.
This should even work in IE7.
A strict CSS2 solution:
#container {
text-align:center;
}
#element1, #element2 {
display:inline-block;
}
The inner elements should layout like inline text inside #container, but remain blocks inside.
This is standard CSS, but getting browser support might take some trickery.
it's not cool solution becouse tables are not "trendy" anymore but it solves the problem completly (under all ie)
<style>
#container {
margin:0 auto;
width:100px;
}
#container table{
width: 100%;
text-align:center;
}
#element1{
background-color:#0000ff;
}
#element2 {
background-color: #ff0000;
}
</style>
<div id=container>
<table cellspacing=0 cellpadding=0>
<tr>
<td id="element1">content</td>
<td id="element2">content</td>
</tr>
</table>
</div>