CSS Spacing with Divs and Floats - html

I have the following html:
<div class="count-unit">
<div class="count-digit digit0"></div>
<div class="count-digit digit6"></div>
</div>
<div class="count-unit">
<div class="count-digit digit0"></div>
<div class="count-digit digit4"></div>
</div>
<div class="count-unit">
<div class="count-digit digit4"></div>
<div class="count-digit digit2"></div>
</div>
<div class="count-unit">
<div class="count-digit digit3"></div>
<div class="count-digit digit9"></div>
</div>
Tied to each ".count-digit" is a sprite (viz. background image) which represents a png of a numeric digit. I'm trying to get the sprites to show horizontally with spacing, like this:
06 04 42 39
The CSS I'm using looks like this:
.count-unit
{
margin: 0 20px 0 20px;
padding: 0 20px 0 20px;
}
.count-digit {
background-image : url(Images/numbers.png);
background-color : transparent;
background-repeat : no-repeat;
float: left;
}
.digit0 {
height : 44px;
width : 30px;
background-position : -0px -0px;
}
Only one of the sample digits (".digit0") is shown. As can be seen, I'm trying to put the spacing around each "numeric" image-pair with padding or margins on the containing "count-unit" div. It isn't working. The "float: left" on the ".count-digit" is bypassing margin and padding settings.
How should I fix this? I tend to think I need to kill the floats, but the alternative "display: inline" prevents the sprites from showing.
Worse, though these sprites work on IE8 and Chrome, they aren't showing when I turn on IE8 compatability mode. I'm not sure what that is about. Any ideas?

The float: left property shouldn't ignore margins or padding. I think you should have specify floating for the .count-unit class though.
.count-unit {
margin: ...
padding: ...
float: left;
}
I've tried it here, and there doesn't seem to be any problem: http://jsfiddle.net/QGZjn/1/

Related

How to get 2 pictures to appear side by side is this html example?

How do I get 2 pictures to appear side by side in this particular html example?
Here is my fiddle
What I want is to align pictures side by side in html, and similarly for the h1 tag above and the p tag below the pic.
illustration of what I want:
title0------------title1
pic0--------------pic1
word0-------------word1
^^^^^^^^^^^^^^^^^^^^^^^^^
This is an example of what I want fiddle, but here it doesn't work when I want add the h1 tag above and the p tag below the picture. I do, however like the way margin-right can control the lateral distance between the pics.
Here is a similar question but this is slightly different.
EDIT1 here is the bootstrap version mentioned below
EDIT2 here are other solutions from below
Amitesh Kumar - https://jsfiddle.net/HattrickNZ/ko1qsbom/9/
YoYo - https://jsfiddle.net/ThetHlaing10/ko1qsbom/2/
Michael_B - https://jsfiddle.net/HattrickNZ/ko1qsbom/8/
BTruong - https://jsfiddle.net/ko1qsbom/6/
they all offer a solution but I think the bootstrap version is the best as it handles when the screen width is resized the best.tks
You can use display:inline-block; to set the element to just use the width they have. Normally, h1 or div are the display:block; elements.
Here is the fiddle for you.
What you can do is put title0, pic0, and word0 in a div and add a class to the div so you can float it to the left using css. On the other side you have title1, pic1, and word1 in a div that has a class that would float it to the right.
Here's the float in work:
.leftBlock {
float: left;
}
.rightBlock {
float: right;
}
Check out this jsfiddle: https://jsfiddle.net/ko1qsbom/6/
Also more information on floats: https://developer.mozilla.org/en-US/docs/Web/CSS/float
Side-by-side positioning is simple and easy with flexbox.
Here's all you need:
#container {
display: flex;
text-align: center; /* optional */
}
<div id="container">
<section>
<h1 class="left">title0 </h1>
<img class="left" src="img_tree.png" alt="pic0" style="width:304px;height:228px;">
<p class="left"><a>word0</a></p>
</section>
<section>
<h1 class="right">title1 </h1>
<img class="right" src="img_tree.png" alt="pic1" style="width:304px;height:228px;">
<p class="right"><a>word0</a></p>
</section>
</div>
There are various options for aligning the two sections in the row (center, space-between, flex-start, etc.). See here for details: https://stackoverflow.com/a/33856609/3597276
Learn more about flexbox here: A Complete Guide to Flexbox
Note that flexbox is supported by all major browsers, except IE 8 & 9. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add all the prefixes you need, post your CSS in the left panel here: Autoprefixer.
Try This give them witdth total width should be less then 100% and float:left
HTML
<h1>Hello World!</h1>
<p>W3Schools background image example.</p>
<p>The background image is only showing once, but it is disturbing the reader!</p>
<!--<h1 class="left">title0 </h1> -->
<img class = "left" src="img_tree.png" alt="pic0" style="width:304px;height:228px;">
<!-- <p class="left"><a>word0</a></p> -->
<!-- <h1 class="right">title1 </h1> -->
<img class = "right" src="img_tree.png" alt="pic1" style="width:304px;height:228px;">
<!-- <p class="right"><a>word0</a></p> -->
CSS
/*
img.right{
float: left;
margin-right: 300px;
}
*/
h1.left p1.left {
text-align: left;
float:left
}
h1.right p1.right{
text-align: right;
}
.div1 {
width:40%;
float: left;
}
.div2 {
width:40%;
float: left;
}
you can use display:inline-block; also

space between two columns div

I have the following code. Can someone please suggest the right way to give space between two column classes ?
<div class="row">
<div class="column"></div>
<div class="column"></div>
</div>
.row {
clear: both;
}
.column {
width: 50%;
box-sizing:border-box;
padding: 10px;
float: left;
}
1 Using margin (Recommended way)
.column {
margin: 10px; /* or what ever */
}
2 Using Line Breaks (Not recommended)
Using this:
<div class="column"></div>
<br />
<div class="column"></div>
3 Padding (that you're using)
Padding would add a space inside the element. Which won't do the trick. You need to use margin instead of padding.
try
<div...></div> <div...></div>
if that does not work or has a compatibility issue
then try:
<div class="column" id="1"></div>
<div class="column" id="2"></div>
CSS:
div.column#1{position:fixed/relative/absolute;left: #px or #% ;top: #px or #%;}
note: ".column" is the class; the "#1" is the id; position should have only one of the listed values and there may be a few others; Left defines the # of px or % from the left of the document; and top defines the # of px or % from the Top of the document;
NOTE:I have only tested this method with Firefox. so other browsers may have other parameters.

Removing the rectangle from top of web page

This one is taking a lot of time as I can not understand the problem associated with.I am still a newcomer to CSS and don't understand the problem completely .
This is the piece of code from CSS which controls the background of the web page
#intro1 {
height:1000px;
background: #000 url('../innerimages/slide4.jpg');
background-position:center 0px;
background-repeat:no-repeat;
background-size:cover;
}
at this point the web page looks something like this
now if i change the color to fff.i.e
#intro1 {
height:1000px;
background: #fff url('../innerimages/slide4.jpg');
background-position:center 0px;
background-repeat:no-repeat;
background-size:cover;
}
the page looks like this
and this is the html part
<div id="verticalScrollArea" style="display: block; top: -65.27584886649873px;">
<section id="intro1" data-navigation-tag="About SVP 69" style="display: block; background-position: 100% 44.8228px;">
<div id="intro1content" class="content" style="display: block; margin-top: 61.86937866666666px;">
<div class="contentData"> <div class="background">SOME TEXT <br>
<img src="./69 SVP_files/know-more.png">
</div> </div>
<div class="scrollDown"> <span>SOME TEXT</span> <img src="./69 SVP_files/scroll-down.png"></div>
</div>
</section>
What I want is the image to be in full screen .I can not figure it out where am I going wrong in removing the back/white strip ?
How it can be removed
You have to remove margin-top from your #intro1's inline CSS, you can use padding-top instead of margin
The problem is being caused by this line of code:
<div id="intro1content" class="content" style="display: block; margin-top: 61.86937866666666px;">
Specifically, the margin-top that you are applying inline is causing this colored strip to show. Just remove this inline css for margin-top.
There in this ID:
remove the margin-top: 61.86937866666666px;
I don't find any problem with the header section. Only one glitch I saw that a white strange line was appearing. for better explanation check the screenshot.
let me know this is what you want?
okay do you want like this? check the update screenshot?

How can I avoid hard coding line height while vertically centering element in a div?

I have a checkbox next to 3 lines of text. I wish to center the checkbox vertically against these lines of text:
A
[] B
C
I'm attempting to do this via div containers while resisting the immense temptation to revert to tables. Here's my code so far:
<div style="overflow:auto;">
<div style="height:57px; float:left;margin-right:15px;">
<input style="vertical-align:middle;height:100%" type="checkbox"
name="theCheckbox" id="checkboxId">
</div>
<div style="float:left;">
A<br/>
B<br/>
C
</div>
</div>
JSFiddle
While the above 'works', I'm not happy about the hard coded height. Changing 57px to 100% makes the checkbox disappear (computed height becomes 0). Removing the height style from the div alltogether also results in a disappearing checkbox. Can anyone suggest improvments or alternative solutions to achieve my goal?
EDIT: I have to support IE7+ amongst other browsers.
You could treat the elements as a table (without actually using a table) like this:
HTML
<div id="container">
<div class="tableCell">
<input type="checkbox" name="theCheckbox" id="checkboxId">
</div>
<div class="tableCell">A<br/>B<br/>C</div>
</div>
CSS
#container { display: table; }
.tableCell {
display: table-cell;
vertical-align: middle; }
See the fiddle here: http://jsfiddle.net/QpnkV/2/
For backwards compatibility think about using scripts in your dochead like this:
<!--[if lt IE 8]><script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script><![endif]-->
<!--[if IE 8]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
How about this?
HTML:
<input type="checkbox" name="theCheckbox" id="checkboxId"/>
<div id ="try">
A<br/>
B<br/>
C
</div>
CSS:
#checkboxId{
position:relative;
vertical-align:middle;
}
#try{
position:relative;
display:inline-block;
vertical-align:middle;
}
Here is the JSFiddle
You can position the checkbox vertically using absolute positioning.
For your HTML, you can simplify it as follows:
<div class="wrap">
<input class="control" type="checkbox" name="theCheckbox" id="checkboxId">
<div class="label">A
<br/>B
<br/>C
<br/>D</div>
</div>
and apply the following CSS:
.wrap {
border: 1px dotted gray;
position: relative;
overflow: auto; /* triggers hasLayout in IE7 */
}
.control {
position: absolute;
left: 0;
top: 0;
bottom: 0;
margin: auto;
}
.label {
margin-left: 20px;
}
Demo Fiddle: http://jsfiddle.net/audetwebdesign/N23qr/
The tradeoff here is that you need to hard code a value for margin-left on the .label container, which is less restrictive than specifying a height value.
Note About IE7
To get position: relative to work correctly for .wrap, you need to make sure that IE7 invokes the hasLayout property, which can be effected by applying overflow: auto. For more details, see: IE7 relative/absolute positioning bug with dynamically modified page content and specifically, http://www.satzansatz.de/cssd/onhavinglayout.html#rp

CSS Background-Image not showing up

I am trying to setup background images using CSS but I can't seem to get the images to populate correctly.
Here is the CSS for what I want to do
a.fb {
background-image: url('img/Facebook.png');
}
a.fb:hover {
background-image: url('img/FacebookHover.png');
}
Here is the html code that I am using, I have tried a couple of different ways to populate the images with no luck
<div class="footer">
<a class="fb" href="http://www.facebook.com" target="_blank"></a>
</div>
Any help would be greatly appreciated
Okay added the following and still not go any other thoughts
a.fb {
display:block;
width: 33px;
height: 33px
background-image: url('img/Facebook.png');
}
EDIT: Yup got it working now forgot the ; after height, but no I get a white border around it and tried setting border: none; no luck
a.fb {
border: none;
display:block;
width: 33px;
height: 33px;
background-image: url('img/Facebook.png');
}
An anchor tag by default shows as an inline elements, so it depends on its content in order to get a height and width. To do what you want, you should add some styles: display:block; width: 20px; height: 20px.
You could also change the aproach completely and use html + mouseover and mouseout events:
<div class="footer">
<a class="fb" href="http://www.facebook.com" target="_blank">
<img src="http://www.facebook.com/images/fb_icon_325x325.png" alt="fb" name="fb" width="33px" height="33px" name="image_name" onmouseover="fb.src='http://goo.gl/cxiR7'; fb.width='38'; fb.height='38';" onmouseout="fb.src='http://www.facebook.com/images/fb_icon_325x325.png'; fb.width='33'; fb.height='33';" />
</a>
</div>
Here is a jsBin: http://jsbin.com/onehuw/1/edit
background-image only draws in the space that the element occupies. Your a tag has no content, and therefore it's width is 0. You'll not see any content (and background) until you give it at least some width (and height if needed).
You need to add padding to the <a> tag otherwise it has a width and height of 0 for example:
a.fb {
padding: 20px;
background-image: url('img/Facebook.png');
}
a.fb:hover {
background-image: url('img/FacebookHover.png');
}
You could also just set the width and height of the anchor