linebox without space overflows from the width of its parent element - html

<!DOCTYPE HTML>
<html>
<head>
<style>
#test{
width: 10px;
background-color: blue;
}
</style>
</head>
<body>
<p id="test">a a a a a a a a a</p>
In this example linebox follows the width of the #test
<!DOCTYPE HTML>
<html>
<head>
<style>
#test{
width: 10px;
background-color: blue;
}
</style>
</head>
<body>
<p id="test">aaaaaaaaaaaaaaaaa</p>
</body>
</html>
but with this second code the linebox overflows from the width of the #test
what the deal with the space between linebox

I think you can use
#test{
...
word-wrap:break-word;
}
DEMO and see this too.

Related

Why isn't my text-align property applying to my span? [duplicate]

This question already has answers here:
How to align this span to the right of the div?
(6 answers)
Closed 1 year ago.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.right {
text-align: right;
}
</style>
</head>
<body>
<span class="right">test</span>
</body>
</html>
I want 'test' to be displayed at right, but it didn't work. If I change span to div, it works. Why is that? I want span, but not div. In the following example, I want 'name' to be displayed at the right of the page, but at the same line as 'age'. But the 'span' tag doesn't do the job.
<span>age</span> <span class="right">name</span>
The <span> element is an inline container and it occupies the space based on content. Where as <div> is a block element and it occupies the parent container fully. So if you want it to work with <span>
you can set the display: block:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.right {
text-align: right;
display: block;
}
</style>
</head>
<body>
<span class="right">test</span>
</body>
</html>
You can use float instead of text-align with span
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.right {
float: right;
}
</style>
</head>
<body>
<span class="right">test</span>
</body>
</html>
Same for the second example:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.right {
float: right;
}
</style>
</head>
<body>
<span>age</span> <span class="right">name</span>
</body>
</html>
span is an inline element. Its width is that of its content. So while the content of the span is being aligned to the right, it's unnoticeable.
divs, on the other hand, are block elements, and will span the width of the container.
if you want to align the span to the right, you should to wrap it in a div and apply text-align:right to it:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.right {
text-align: right;
width:100%;
}
</style>
</head>
<body>
<div class="right"><span>test</span></div>
</body>
</html>
The reason is because a <div> has a default display property of block, but <span> has a default display property of inline.
Change your .right class to include display: block; and it will work for both.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.right {
display: block;
text-align: right;
}
</style>
</head>
<body>
<span class="right">test</span>
</body>
</html>
<span> documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span

Background Image not displaying on child element when parent already has background image

This is a sample code to show you what i want to do. I have a background image on body and a background image on h1 but the background image on h1 isn't rendering!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style>
body {
background: url("https://www.free-css.com/assets/files/free-css-templates/preview/page169/art-school/images/bg.jpg") center 0 no-repeat;
}
h1 {
background-image: url("https://www.free-css.com/assets/files/free-css-templates/preview/page169/art-school/images/block-1-shadow.png");
}
</style>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>
Your image in h1 is here, it is just that basicaly, it is pretty much transparent.
I made 2 demo, in the first I added the line below and you can see the grey shadow from it which is your image.
background-position: center;
In Demo 2, I add the background image in a pseudo-element :after to add a background behind the image and show it better.
DEMO 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style>
body {
background: url("https://www.free-css.com/assets/files/free-css-templates/preview/page169/art-school/images/bg.jpg") center 0 no-repeat;
}
h1{
background-image: url("https://www.free-css.com/assets/files/free-css-templates/preview/page169/art-school/images/block-1-shadow.png");
background-position: center;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>
DEMO 2
h1{
background:cornsilk;
position:relative;
}
h1:after {
content:"";
display:block;
position: absolute;
left:0;
top:0;
width:100%;
height:100%;
background-image: url("https://www.free-css.com/assets/files/free-css-templates/preview/page169/art-school/images/block-1-shadow.png");
z-index:1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style>
body {
background: url("https://www.free-css.com/assets/files/free-css-templates/preview/page169/art-school/images/bg.jpg") center 0 no-repeat;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>

How to set background to div

I tried to set image in background of a div but disappeared! I want the background image covers all page, thanks.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
div {
width:100%;
height: 100%;
background-image: url('https://static.vecteezy.com/system/resources/previews/000/094/491/original/polygonal-texture-background-vector.jpg');
}
</style>
</head>
<body>
<div></div>
</body>
</html>
Just attach it to the body directly :
body {
background-image: url("img_link");
}
Hope this helps.
.img{
width:100px;
height:100px;
}
<div>
<img src="img1.jpg"/>
</div>
for this make parents 100% by:
html,body{width:100%;height: 100%;}

how to change the position of boxes?

this is my code, i want to create box in box in css and html
but when i made it,i couldn't move the boxes, i need a margin from top to put the boxes in the center of the box or moving them in to the main box,how can i do this?
code:
<html >
<head>
<style>
ui {
background-color:red;
padding:100px 100px;
}
div1{
background-color:white;
padding:50px 50px;
}
div2{
background-color:yellow;
padding:50px 50px;
}
</style>
</head>
<body>
<ui>
<div1>hello</div1>
<div2>bye</div2>
</ui>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<img src="w3css.gif" width="100" height="140">
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>
</body>
</html>
Produces: http://www.w3schools.com/css/tryit.asp?filename=trycss_zindex

clickable div with image centered vertically and horizontally

I need to create a clickable div with an image (of variable size, but that is smaller than the div) centered both horizontally and vertically within the div.
I made the div clickable with
#image-box a { display: block; height: 100%; width: 100%; }
but can't seem to center the image vertically.
Try this adjusting width and height of your a element as explained in the comments:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Centered Clickable Image</title>
<style type="text/css" media="screen">
#image-box {
position:absolute;
top:0; bottom:0; left:0; right:0;
margin:auto;
border: 1px solid #999;
text-align: center;
}
#image-box a {display:block; position:absolute;
top:0; bottom:0; left:0; right:0;
margin:auto; text-align: center;
}
/* insert the same width and height of your-nice-img.png */
#image-box a {width:339px; height:472px; border: 2px solid red;}
</style>
</head>
<body>
<div id="image-box">
<a href="#">
<img src="your-nice-image.png" alt="image to center"/>
</a>
</div>
</body>
</html>
NOTES:
Borders are needed only for visual debug, You can delete them at any time.
The trick here is that you use an absolute positioned div (#image-box) with a fixed width and height.
If you set the #image-box a top and bottom position to zero the rule margin:auto puts #image-box a element in a middle position (on the vertical axis) because it has a fixed height, .
If you can or like to solve It using jQuery, try this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Centered Image</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<div id="image-box">
<a href="#">
<img src="canning.png" alt="image to center"/>
</a>
</div>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$(window).resize(function(){
$('#image-box a').css({
position:'absolute',
left: ($(window).width() - $('#image-box a img').outerWidth())/2,
top: ($(window).height() - $('#image-box a img').outerHeight())/2
});
});
// first run
$(window).resize();
});
</script>
</body>
</html>