简体   繁体   中英

CSS background image for fluid layouts

I am trying to apply a background-image to a li element.

<ul>
<li class="maintablink">&nbsp;</li>
</ul>

My CSS for the same is

li.maintablink{background:url(../images/tab_content.png) no-repeat left top;}

Even adding some width:200px;height:200px above does not work.

I am using HTML5 doctype

I have 2 questions related to this;

  1. The background image is slightly bigger and does not display fully inside the li. ie it indirectly depends on the content inside my li element. I do not want to give any content inside li (except &nbsp; ) How do I fix this issue ?

  2. I am using fluid layout (everything in %, no px value)...So how do I give the width/height for this background-image...It should scale with the browser size (I know for page images, we use img{max-width:100%} )...But how do I make background-images scalable ?

It sounds like you're looking for the CSS background-size property.

For a background that is scalable according to the size of the container:

background-size: auto;

For a background that is a fixed size, regardless of the size of the container or the actual background image:

background-size: 150px 200px;

You can also use percentages, or the keywords contain or cover .

See http://www.css3.info/preview/background-size/ for more info.

Please note that background-size does have quite good browser support now, but some older browsers (in particular IE8 and earlier, but also Firefox 3.x) do not support it.

If you need to support these browsers fully, you will need to work out a fall-back solution (probably using additional markup and/or Modernizr to determine whether the feature is available).

Apply a background-image to a li element

li is not a block element so you must be change display:inline-block before you set width and height.

There is an option to set background-size in CSS you can follow this link ,. If you don't want to show your content from li element just put the text-indent:-9999px to you li css.

Check out this demo for testing background-size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM