Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making span tag cover entire width of div

Tags:

html

css

width

I have the following structure for html:-

<div>
  <span>
    <a href="wherever">Wherever</a>
  </span>
</div>

If I want the span to cover the entire width of the div, how can I do that in chrome?

I tried in css using

span{
  background:#FFF;
  width:100%;
}

It works in firefox and ie but not in chrome.

like image 370
user1439090 Avatar asked Feb 07 '13 08:02

user1439090


People also ask

How do I span a div to an entire width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

Can a span wrap a div?

The <span> element shows the inline portion of a document. The <div> elements show a block-level portion of a document. A div is a block-level element and a span is an inline element. The div should be used to wrap sections of a document, while use spans to wrap small portions of text, images, etc.

Can we give width to a span tag?

The main thing to remember about a span element is that it is an inline element, which means that you will not be able to set the width or height of it as is. So to give our span element a width, we must convert it to a block, or inline-block element.


3 Answers

span elements are inline. Inline elements adjust their width automatically and ignore your width: expressions.

Make it block-level:

span {
    display: block;
}

But then it's basically just a <div>.

like image 128
Blender Avatar answered Oct 08 '22 13:10

Blender


There are two ways to resolve your issue.

As span tags adjust their width according to the contents in it. So to assign width to this tag we will use float left with width.

span{
 background:#FFF;
 width:100%;
 float: left;
}

secod way is to use display block with the code

 span{
 background:#FFF;
 width:100%;
 display: block;
}
like image 4
aishazafar Avatar answered Oct 08 '22 13:10

aishazafar


Change span's CSS to:

span {
display: block;
}
like image 1
KBN Avatar answered Oct 08 '22 11:10

KBN