Difference between display none and display block - html

What is the difference between setting a control's style display: none and display: block?

The display property defines how a certain HTML element should be displayed. Display block and none are used to show or hide html elements. You can read more about display property and available options here.
none: The element will not be displayed at all.
block: The element displayed as a block-level element (like paragraphs and headers)

Display:none; - The element is in the DOM but is NOT visible and does not take up any space unlike visibility:hidden.
Display: block; - A block element takes up the full width available and does not allow other elements to be placed beside them. Example: div

These two style properties do two different things.
display: none removes the element completely from the document. It does not take up any space, even though the HTML for it is still in the source code. (The element will generate no box at all)
display: block the element will span the full width of the space available to it. (a line break before and after the element)

You are asking about a CSS property i think. This is used to show/hide DOM elements
CSS property is display and the value is 'none', 'block', etc
Referring from : CSS Display as suggested by http://w3fools.com/
block
Object is rendered as a block element.
none
Element is not rendered. The element (it has no effect on layout); all child elements also have their display turned off. The document is rendered as though the element did not exist.
inline
Default. Object is rendered as an inline element sized by the dimensions of the content.
list-item
Internet Explorer 6 and later. Object is rendered as a block element, and a list-item marker is added.
table-header-group
Object is rendered as tHead. Table header is always displayed before all other rows and row groups, and after any top captions. The header is displayed on each document spanned by a table.
table-footer-group
Object is rendered as tFoot. Table footer is always displayed after all other rows and row groups, and before any bottom captions. The footer is displayed on each document spanned by a table.
inline-block
Object is rendered inline, but the contents of the object are rendered as a block element. Adjacent inline elements are rendered on the same line, space permitting.

Display:none; means the element will not be displayed, and Display:block; means the element is displayed as a block-level element (like paragraphs and headers).

There is another nuance to display:none; if you dynamically insert a div as a child to a parent div--and you explicitly set the visibility property of the child to "visible", the visibility property of the parent will only make the parent visible/invisible; the child will remain visible, regardless of the parent's visibility setting.
In such a case (where parent/child visibility are set by different style rules), the display:none setting on the parent will hide all the children--even if parent/children are styled independently.emphasized text

display: none means that the element is not displayed at all (so you won't see it in the example either).
display: block means that the element is displayed as a block, as paragraphs and headers have always been. A block has some whitespace above and below it and tolerates no HTML elements next to it, except when ordered otherwise

Display None: it hides the control. by setting the property of element style="display:none" element will not rendered in webpage and not take place
Display Block: Show the element on web page in block level

Display none will hide the contains, here if you apply it on div then width and height of div will also hide.
Display block will show the contains.

Related

Button is an inline element but width is working

Button is an inline element, but the width properties work on it. Why is that?
button {
width: 300px;
}
<button>asdfsdf</button>
<button>234234d</button>
Most browsers display button elements as inline-block by default, according to the (not normative) Appendix D. Default style sheet for HTML 4.
Therefore, you could expect the width property to work, as described in Calculating widths and margins - Inline-block, non-replaced.
But it's not just that. button elements are replaced elements:
In CSS, a replaced element is an element whose representation is
outside the scope of CSS. These are kind of external objects whose
representation is independent of the CSS.
Therefore, they have some special behavior. For example, independently of whether they have display: inline-block or display: inline, they are sized respecting the width property, according to Calculating widths and margins - Inline, replaced.
It's worth noting that HTML5 forces them to be displayed as inline-block anyway. This is explained in 10.5.2 Bindings - The button element:
#namespace url(http://www.w3.org/1999/xhtml);
button { binding: button; }
When the button binding applies to a button element, the element
is expected to render as an 'inline-block' box rendered as a button
whose contents are the contents of the element.
See W3C Reference (a list of HTML4 elements' default CSS styles).
button is an inline-block element (by default) that can have a width set, as opposed to inline elements.
Set min-width: 300px;.
It works to me even if the button is inline-block.

alignment of a div

What is the default behaviour of a div?
I noticed that even if a put a width for a div let's say 100px,
if i put a 2nd div with the same width will put it on the second line.so by default doesn't matter the width. it puts it on different lines?
in this case i understand the need of float.
I thought that any element i put in a html page,they will be side by side unless i add a break element or paragraph or something with that role.
Or maybe i do not use it correctly the div for this kind of alignment,but i really want to
clarify this for good.
A div element is, by default, display: block.
This value causes an element to generate a block box.
The rendering of them is described here
Block-level elements are those elements of the source document that are formatted visually as blocks (e.g., paragraphs). The following values of the 'display' property make an element block-level: 'block', 'list-item', and 'table'.
Block-level boxes are boxes that participate in a block formatting context.
and then here
In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block.
To stop this kind of rendering, you can use float to cause block level elements to bubble up beside each other. You can also modify the display property of the div.
Divs are block-level elements which mean they stack...like blocks. Although it sounds reasonable that since the width would allow them to fit side-by-side without a float, this is not how they are designed to behave.
If an element is an inline element as opposed to a block, its behavior is to fit side-by-side. You can force this behavior on a div if you would like by tying the two ideas together. You can do something like:
<div style="display:inline-block"></div>
This will allow the div to maintain its other block properties but allow it to fit inline as text and images would and, if this the your desired goal, avoid the use of float.
The DIV by default takes 100% of the screen, even if you set it width the space on the right cannot be occupied by anything.
Try this:
The way to have two div on the same line would be to make them float:
<div style = 'float:left;width:500px;background-color:red;color:white;'>Hey</div>
<div style = 'float:left;width:100px;'> There</div>

Why does display:inline cancel height?

I was making a menu with id menu which had the following set:
display: inline;
height: 200px;
Once I removed display: inline;, height worked again.
Why?
display: inline;
Are usually used to refer to text elements;
From the w3c page:
[inline] Causes an element to generate one or more inline boxes.
Therefore, the height you must set is the line-height property. From the w3c page:
On a block container element whose content is composed of inline-level elements, 'line-height' specifies the minimal height of line boxes within the element.
Notice here that you can control only the minimal height of your inline elements
Because the spec says so:
'height' … Applies to: all elements but non-replaced inline elements, table columns, and column groups
line-height, however, does apply to inline elements.
By making something display inline you are effectively making it the same as a span. As such the only valid height value is line-height since you are working on an inline element.
To render height values you would need to use a div tag or force your existing tab to render like a div / object which accepts height attributes. You can do this by setting display:block.
In essence you can render a div to work like a span by setting display:inline, and conversely render a span as effectively a div via display:block.
Span tag are meant for inline styling such as font size, colour, decoration, etc.

Fill dynamic <div>'s with content. But how to show it?

My intention is to have 4 main Nav-bars at a site. If the user hovers one, it expands and should show content. But how should I do the content part?
#bottom-menu:hover #contact{
visibility: visible ;
}
If I make it visible when the box is fully expanded, it works but there is an issue: The content, while not visible, is still there and needs room which causes the layout to collapse.
I would like to do that with pure CSS, allthough i'm aware of jQuery.
http://jsfiddle.net/RbZwx/1/
visibility: hidden causes the element to continue to consume layout space.
display:none causes it to take up no layout space.
Think "spoilers" vs "hidden".
https://developer.mozilla.org/en/CSS/visibility
The visibility CSS property is used for two things:
The hidden value hides an element but leaves space where it would have been.
The collapse value hides rows or columns of a table. It also collapses XUL elements.
https://developer.mozilla.org/en/CSS/display
The display CSS property specifies the type of rendering box used for an element. In HTML, default display property values are taken from behaviors described in the HTML specifications or from the browser/user default stylesheet. The default value in XML is inline.
In addition to the many different display box types, the value none lets you turn off the display of an element; when you use none, all child elements also have their display turned off. The document is rendered as though the element doesn't exist in the document tree.
I would still encourage you to read both of these, and to learn how they affect the page more than just for this particular case.
Use display: none. See CSS 2.1: 11.2 Visibility: the 'visibility' property:
The 'visibility' property specifies whether the boxes generated by an element are rendered. Invisible boxes still affect layout (set the 'display' property to 'none' to suppress box generation altogether). Values have the following meanings: [...]
hidden The generated box is invisible (fully transparent, nothing is drawn), but still affects layout. Furthermore, descendants of the element will be visible if they have 'visibility: visible'.

Making a class invisible

I'm want to have a div with class "a" to be invisible. I've tried already to give it the display: none; line in the CSS file but what it does is hide it, yet it doesn't catch any space and so all my other boxes don't stay in place.
Is there a way to do this using CSS?
add .a{visibility: hidden} to your CSS. More about it here:
http://reference.sitepoint.com/css/visibility
visibility:hidden should hide the element, while keeping it's space so as not to move your other elements around.
You can use visibility css property. From sitepoint reference -
This property specifies whether an
element is visible—that is, whether
the box(es) that are generated by an
element are rendered.
Note that even if a box in the normal
flow is hidden, it still affects the
layout of other elements, unlike the
behavior that occurs when we suppress
box generation altogether by setting
display to none. Descendant boxes of a
hidden box will be visible if their
visibility is set to visible, whereas
descendants of an element for which
display is set to none can never
generate boxes of their own.
More information can be found here.