Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style a div to have a background color for the entire width of the content, and not just for the width of the display?

I have an HTML page that for the sake of this question looks like this:

<html>
<head>
<style>
div { width: 100%; }
.success { background-color: #ccffcc; }
</style>
</head>
<body>
<div class="success">
<nobr>This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line. This is a very long line.</nobr>
</div>
</body>
</html>

Note the "very long line", and the background color of that div.

My problem (and I bet it is a basic one) is that the background-color stops at the edge of the screen. When I scroll out to the right to see the rest of the text, the rest of the text background is white.

Basically I want my div to behave like this:

  1. To have the specified background color
  2. To minimum have the same width as the screen, even if the text within is just a few words
  3. To follow the width of the text, if it is more than the width of the screen
  4. Optionally (and I know this is really a different, follow-up, question), if I have more than one such div, following the first, is there a way to have the two follow the width of the widest div automatically?

Did that make any sense?

Is there any way to do this?


I have set up a test page here, which, if you view this on iPhone, although a small font, shows the problem: http://www.vkarlsen.no/test/test.html

I saw the following questions listed as potential duplicates/suggestions by SO, here's what I noticed when I tried the information within:

  • iPad background for div blocks not spanning entire width of screen

    Tried the suggested <meta ... viewport .../> tag, did not make a difference (it is present in the test page right now.)

  • Background color stretches accross entire width of ul

    <div>s are already block elements

  • WebKit doesn't paint background-color for entire width of final inline list item

    Tried setting the div to display: inline-block; but this did not appear to change anything

like image 548
Lasse V. Karlsen Avatar asked Jan 02 '12 20:01

Lasse V. Karlsen


People also ask

How do I make a div cover the whole 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.

How do I set a background color for the width of text?

Put the text in an inline element, such as a <span> . And then apply the background color on the inline element. An inline element is as big as its contents is, so that should do it for you.

Can a div have a background color?

To set a background color for a div or related element in CSS, you use the background-color property. While setting this background color, your creativity is your limit as to how far you want to go.


2 Answers

The problem seems to be that block elements only scale up to 100% of their containing element, no matter how big their content is—it just overflows. However, making them inline-block elements apparently resizes their width to their actual content.

HTML:

<div id="container">
    <div class="wide">
        foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
    </div>
    <div class="wide">
        bar
    </div>
</div>

CSS:

.wide { min-width: 100%; display: inline-block; background-color: yellow; }
#container { display: inline-block; }

(The containerelement addresses your follow-up question to make the second div as big as the previous one, and not just the screen width.)

I also set up a JS fiddle showing my demo code.

If you run into any troubles (esp. cross-browser issues) with inline-block, looking at Block-level elements within display: inline-block might help.

like image 116
Jan Pöschko Avatar answered Sep 28 '22 08:09

Jan Pöschko


black magic:

<style>
body { float:left;}
.success { background-color: #ccffcc;}
</style>

If anyone has a clear explanation of why this works, please comment. I think it has something to do with a side effect of the float that removes the constraint that the body must fit into the page width.

like image 25
user372719 Avatar answered Sep 28 '22 08:09

user372719