Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you put a display:block on an "a" tag that is inside a list?

Tags:

css

block

I am trying to understand image sprites on CSS, and one line of code looks like this.

#navlist li, #navlist a{height:44px;display:block;}

I am just wondering what effect does display:block has on "a", I know that it is for the "a" tag since the link won't work if I removed "a" and conversely if I removed "display:block", I'm just wondering why it should be "display:block".

like image 978
Johnsy Avatar asked May 20 '13 06:05

Johnsy


People also ask

What is the purpose of display block?

display: block An element that has the display property set to block starts on a new line and takes up the available screen width. You can specify the width and height properties for such elements. Examples of elements that are at block-level by default are <div> , <section> , <p> , and lots more.

Why display block is used in HTML?

The display property specifies if/how an element is displayed. Every HTML element has a default display value depending on what type of element it is. The default display value for most elements is block or inline . This panel contains a <div> element, which is hidden by default ( display: none ).

What is use of display block in CSS?

The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex. Formally, the display property sets an element's inner and outer display types.

What is used to display a block element?

Block-level Elements Two commonly used block elements are: <p> and <div> . The <p> element defines a paragraph in an HTML document. The <div> element defines a division or a section in an HTML document. The <p> element is a block-level element.


2 Answers

To make the inline element (a, span etc) to behave like a box model element (div, p, h1 etc), in other words, to make the a tag behave like a div tag.

Inline elements can live side by side on the same line, for example if you write

<a href="example.com">Link1</a> <a href="example.com">Link2</a>

they will appear like Link1 Link2 but box model elements can't live in the same line, for example, if you write something like

<div>Box1</div><div>Box2</div>

they will appear like

Box1
Box2

Both divs will occupy the whole space around them (even if they are smaller in width). In a list, for example,

<li><a href="">Home</a></li>

If the list width is 300px then the a tag will not cover the full width of the li's width because by default the a tag is inline and using display:block will make the a element to occupy the full width of the li, even if it's not that wide.

There are more to say about this, I've just gave you an example, you should read more. Check this link and also Check this example.

like image 182
The Alpha Avatar answered Sep 30 '22 05:09

The Alpha


The w3schools explanation for display:block is as fallow

The element is displayed as a block-level element (like paragraphs and headers) and you can check the display behavior here

practically mostly we use display:block in four situations

  1. Element doesn't contains any content, but need to show as fix size block. eg: link with background image but no text between open and close anchor tags.
  2. Element need to show in fix size ignore the auto size according to content.
  3. There are set of elements and each should display in each line (one element per line).
  4. To implement show hide we can use diaply:none and display:block

But link functionality doesn't have any relationship with the display or CSS, link showld work interdependently, CSS wrote for the anchor tag, just for style the link.

here is some more details about display https://developer.mozilla.org/en-US/docs/Web/CSS/display

like image 37
Nayana Adassuriya Avatar answered Sep 30 '22 06:09

Nayana Adassuriya