Can't remove space from top of the div - html

I have one div inside that i have one label. But I am getting some space above the div. I can't remove that space any how.
Don't know what happens there.
Question: Why I am getting that space above div? How can I remove that space?
My code:
For display issue proper I had put color and border.
* {
padding: 0;
margin: 0;
}
body {
background: green;
}
label {
border: 1px solid black;
font-size: 10px;
}
<div>
<label>Some text</label>
</div>
I have tried many things but, didn't get any solution.

label is a inline element therefore add display:inline-block/block or vertical-align:top
* {
padding: 0;
margin: 0;
}
div {
font-size: 0;
/* fix inline-block gap - not needed when using block only*/
background: red
}
label {
border: 1px solid black;
font-size: 10px;
}
.l1 {
display: inline-block
}
.l2 {
display: block;
width: 9%
}
.l3 {
vertical-align: top;
}
<div>
<label class="l1">Some text</label>
</div>
<div>
<label class="l2">Some text 2</label>
</div>
<div>
<label class="l3">Some text 3</label>
</div>

Try to add vertical-align: top; for your label
* {
padding: 0;
margin: 0;
}
body {
background: green;
}
label {
vertical-align: top;
border: 1px solid black;
font-size: 10px;
}
<div>
<label>Some text</label>
</div>

This behaviour is caused because the div element has a browser-dependent default size applied to it. When it contains an element with a smaller font-size, the contained element will be placed on the baseline of the div which results in the space. To solve it, add a matching font size to the div:
div {
font-size: 10px;
}

It is coming because of the default line-height of the labels. You need to use reset.css or similar (like normalize.css) to clear the browsers default styling.
Here are some helpful reference, download and simply add them above your style sheet.
http://meyerweb.com/eric/tools/css/reset/reset.css
or
https://necolas.github.io/normalize.css/3.0.3/normalize.css

Looks like the line-height in the div is too high.
This does the trick:
div {
line-height: 10px;
}
(Of course you should reference the div with a class or id).

Inline element like span also behaves the same.
Add a float:left for the label
* {
padding: 0;
margin: 0;
}
body {
background: green;
}
label {
border: 1px solid black;
font-size: 10px;
float: left;
}
<div>
<label>Some text</label>
</div>

Related

Margin auto not being respected despite using display block

I have a div containing two label elements. Each label should be on a side of the div. As labels are inline elements, I have tried with display: block and also with display: inline-block for margins to take effect, but the result is not the expected one.
div {
color: #ffffff;
background-color: #3f3f3f;
}
label:nth-of-type(1) {
margin-left: 5px;
}
label:nth-of-type(2) {
display: block;
<!-- display: inline-block; -->
margin-right: 5px;
margin-left: auto;
}
<div>
<label>Left side label</label>
<label>right side label</label>
</div>
As you can see with the code execution, the second label is not respecting the margins and is being displayed underneath the first one.
The label must have a width and display:block to work with margin auto.
Today it's more flexibel to use flexbox for this.
div {
color: #ffffff;
background-color: #3f3f3f;
display:flex;
flex-flow: row nowrap;
justify-content: space-between;
}
label:nth-of-type(1) {
margin-left: 5px;
}
label:nth-of-type(2) {
margin-right: 5px;
}
<html>
<body>
<div>
<label>Left side label</label>
<label>right side label</label>
</div>
</body>
</html>
With more modern methods like CSS Grid or Flexbox, this can be accomplished. But my solution will be with raw CSS to keep at a similar level to OP's code.
Both labels will need to have display: inline-block applied to get both elements to be treated like block elements and remain on the same line. You'll also need to set a width to give them a container to work with when adjusting the text placement. For this example, we'll do width: 50%.
Note: inline-block elements that take up a full width: 100% will result in the labels being on separate lines unless you modify the html to remove the whitespace in between the elements. Read more why on this behavior here and a personal CodeSandbox of fixing this.
You'll notice I also removed margin-left and margin-right from the width calculation and instead used padding to result in the same spacing on the left and right.
HTML:
<body>
<div>
<!-- Remove whitespace between labels to not exceed width: 100% -->
<label>Left side label</label><label>right side label</label>
</div>
</body>
CSS:
div {
color: #ffffff;
background-color: #3f3f3f;
padding: 0 5px;
}
label {
display: inline-block;
width: 50%;
}
label:nth-of-type(1) {
text-align: left; /* Not necessary, but you can explicitly set the behavior you want. */
}
label:nth-of-type(2) {
text-align: right;
}
Codepen
you don't need to specify the display property, just let it be inline and play around with the float property to float them.
<style>
div {
color: #ffffff;
background-color: #3f3f3f;
display: block;
height: 20px;
width: 100%;
}
label:nth-of-type(1) {
margin-left: 5px;
float: left;
}
label:nth-of-type(2) {
float: right;
margin-right: 5px;
}
</style>
<html>
<body>
<div>
<label>Left side label</label>
<label>right side label</label>
</div>
</body>
</html>

textarea unexplained spacing

I have just noticed some wired spacing below a textarea, it is different in every browser. Can someone explain why it is there?
span,
textarea {
border: 1px solid black;
margin: 0;
padding: 0;
}
<textarea></textarea>
<span>test</span>
Add
vertical-align:bottom
This is because
The baseline of some replaced elements, like <textarea>, is not specified by the HTML specification, meaning that their behavior with this keyword may change from one browser to the other.
MDN Reference
span,
textarea {
border: 1px solid black;
margin: 0;
padding: 0;
vertical-align: bottom;
}
<textarea></textarea>
<span>test</span>
add this style to textarea
vertical-align: top
Add vertical-align: top to textarea.
The reason for the gap is that textarea is an inline (or inline-block) element, and the gap is the space reserved for descenders in text.
span,
textarea {
border: 1px solid black;
margin: 0;
padding: 0;
}
textarea{
vertical-align:top;
}
<textarea></textarea>
<span>test</span>
The thing with textarea in HTML is that the text is aligned right next to the bottom margin. If you wish to have it any other way, read about the vertical-align attribute in CSS.
You can use:
span,
textarea {
border: 1px solid black;
margin: 0;
padding: 0;
vertical-align: middle;
}
<textarea></textarea>
<span>test</span>

How can I get a label to right align in a table "cell" (td)?

I've got this html:
<td>
<label class="whitefont righthoralign">Search</label>
</td>
...with this CSS:
.righthoralign {
text-align: right;
}
...which does nothing - the label stays left aligned as before:
What I found here, namely this:
.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
...made things far worse.
How can I get the label to hug the right side of the cell/td in which it finds itself?
The <label> element is inline level, the width is the size of the label text by default.
Try:
label {
display: block;
text-align: right;
}
Or:
td {
text-align: right;
}
You need to add text align to the td element.
td {
text-align: right;
}
You can also remove the text align from the label as it has no effect on the element (it is inline).
Here's a fiddle: https://jsfiddle.net/iamnottony/52zbfe2e/1/

Writing Text beside Fractions in html

I previously asked this qn:How to represent fractions in html. It was about writing the numerator and denominator in html and the solution provided allows me to write fraction in the proper format.
However, I can't write text beside it. If I write text beside it, it appears together at the top or bottom. It appears below it. I want the text to appear beside it.
The code is here:
.fraction {
position: relative;
width: 1em;
font-size: 2em;
}
.numerator {
border-bottom: 2px solid black;
text-align: center;
}
.denominator {
border-right: 2px solid black;
width: 0.75em;
text-align: center;
}
<div class="fraction">
<div class="numerator">3</div>
<div class="denominator">8</div>
</div>
The text appears like this:
Is there any solution? Any solution?
Set .fraction class display property as inline-block.
I guess you also will have to fiddle with its vertical-align prop.
add these css -
.fraction {
display: inline-block;
vertical-align: top;
}
working fiddle - http://jsfiddle.net/et2Lan5k/
Add div word and set CSS
CSS:
.fraction {
position: relative;
width: 1em;
font-size: 2em;
}
.numerator {
border-bottom: 2px solid black;
text-align: center;
}
.denominator {
width: 0.75em;
text-align: center;
}
.word{
position:absolute;
top:0px;
line-height:2.5em;
left:1em;
}
HTML:
<div class="fraction">
<div class="numerator">3</div>
<div class="denominator">8</div>
<div class="word">HI</div>
</div>
https://jsfiddle.net/e9nc6gna/1/
This can be achieved with just a few lines of code :
Wrap the numerator and denominator within <span> tags.
Wrap them again within a container <span>.
Then declare the container inline-block, so that it stays on a single line.
Declare the vertical-align: middle property for the container.
Add a border at the bottom of the numerator (or at the top of the denominator) so that it looks like a fraction.
Use this code :
* {
font-size: 50px;
}
.container {
vertical-align: middle;
display: inline-block;
}
.numerator {
border-bottom: 1px solid;
}
33<span class="container"><span class="numerator">1</span><br><span>3</span></span>
Also, do not put line breaks in between these tags, otherwise, there would be unnecessary spaces within the fraction, since the <span> tags are declared as inline elements.
Use this code if you wish.
<style type="text/css">
frac {
display: inline-block;
position: relative;
vertical-align: middle;
letter-spacing: 0.001em;
text-align: center;
}
frac num { /*Numerator*/
display: block;
padding: 0.01em;
}
frac den { /*Denominator*/
border-top: thin solid black;
/* Above line is for the division line. */
}
</style>
<p>33<frac><num>1</num><den>3</den></frac> sss</p>
If you want to change font-size, then add a font-size tag inside the frac tag.

How to center align img wrapped in SPAN tag?

I am trying to center align an image that is wrapped in a <span>, but I am having trouble doing so. I have uploaded my CSS and HTML to jsfiddle: http://jsfiddle.net/7nHhu/1/
I am trying to get the image to center align itself with the content in a "block" style (ie. all text above and below it, not wrapped to the left or right)
Any help would be greatly appreciated.
.imgframe {
border: 1px solid #EAEAEA;
display: inline-block;
margin: 8px;
}
.imgframe img {
border: 1px solid #FFFFFF;
margin: 0;
background: #F6F6F6;
padding: 8px;
-moz-box-shadow: 2px 2px 5px #CCCCCC;
-webkit-box-shadow: 2px 2px 5px #CCCCCC;
}
<span class="imgframe centerimg"><img src="http://i48.tinypic.com/31368e9.jpg" /></span>​
I think it's more appropriate to use text-align for centering text rather than images. You could center an image by setting left and right margin auto.
img {
display: block;
margin-left: auto;
margin-right: auto;
height: auto;
padding-top: 10px; //margin-top doesn't work
}
Demo
Just make image wrapper block level element and text-align:center; it.
FIDDLE
or wrap it in another element if needed;
FIDDLE
In .imgframe, add width: 100%;
Given your requirements, to keep the .imgframe element in-line, to avoid it taking up the full width of the enclosing element, and working without adding wrapping elements to your mark-up, the following works:
body {
text-align: center;
}
body p {
text-align: left;
}
JS Fiddle demo.
This would, probably, be less intrusive if you had the elements from your Fiddle wrapped in a specific, target-able, element; rather than the body, as the method, above, requires you to reset the text-align for all elements contained within the body. So, personally, I'd use:
<div id="contentWrapper">
<p>...</p>
<span class="imgframe">
<img src="..." />
</span>
<p>...</p>
</div>
And:
#contentWrapper {
text-align: center;
}
#contentWrapper p {
text-align: left;
}
Just in order to minimise the amount of work required to tidy up afterwards.
span {position: absolute; top:0; left: 0; width: 100%; text-align: center;}
img {width:yourimagewidth; heigth: width:yourimageheigth}