CSS that creates a repeated background image sourced from an 'image map'

I have created an image map with coordinates and dimensions specified (apologies for the rough drawing...)

https://i.sstatic.net/CxQAk.png

The numbers represent x-coordinate, y-coordinate, width, and height. For instance, 5, 5, 25, 25 indicates that the image is positioned at (5, 5) with a width of 25px and height of 25px.

(the size of the last image is incorrect. The correct size should be 100, 5, 5, 25.)

Now, I aim to create

https://i.sstatic.net/es9RE.png

using the aforementioned image map with a width of 100px and height of 25px.

Currently, I am using the following code for the image map:

#aDiv {
    width: 25px; height 25px;
    background-image:url('path');
    background-repeat:no-repeat;
    background-position:-5px -5px;
}

with just one tag

<div></div>

for the background.

However, I am unsure how to create something similar to the second image using the first image map. How can I achieve this?

The code above shows my current progress. At this point, I am completely lost. My latest attempt was:

#one { 
    display:inline-block;
    width:25px; height:25px;
    background-image:url('path');
    background-repeat:no-repeat;
    background-position:-5px -5px;
}

#two { 
    display:inline-block;
    width:25px; height:25px;
    background-image:url('path');
    background-repeat:no-repeat;
    background-position:-100px -5px;
}


#three { 
    display:inline-block;
    width:5px; height:25px;
    background-image:url('path');
    background-repeat:no-repeat;
    background-position:-50px -5px;
}

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>

However, this method only adds up to 75 pixels in total width. I need to replicate #two, essentially recreating the third section of the image map.

Answer №1

Understanding your needs can be challenging, so here are two versions to provide some visual aids for finding a suitable solution.

To illustrate the division and positioning of the image map, I have included borders around each div element.

Version 1: Using three divs with the same map, arranged in a way that appears as one, where the second div is double the width of the others.

div {
    float: left;                /* temporary, for side-by-side display */
    border: 1px solid black;    /* temporary, for demonstration purposes */
    width: 100px; 
    height: 100px;
    background-size: 400px 100px;
    background-repeat: no-repeat;
    background-image: url(https://i.sstatic.net/Qsnl6.png);
    background-position: 0 0;
}
div:nth-child(2) {
    width: 200px; 
    background-position: -100px 0;
}
div:nth-child(3) {
    background-position: -300px 0;
}
<div></div>
<div></div>
<div></div>


Version 2: This time with four divs instead.

div {
    float: left;                /* temporary, for side-by-side display */
    border: 1px solid black;    /* temporary, for demonstration purposes */
    width: 100px; 
    height: 100px;
    background-size: 400px 100px;
    background-repeat: no-repeat;
    background-image: url(https://i.sstatic.net/Qsnl6.png);
    background-position: 0 0;
}
div:nth-child(2) {
    background-position: -100px 0;
}
div:nth-child(3) {
    background-position: -200px 0;
}
div:nth-child(4) {
    background-position: -300px 0;
}
<div></div>
<div></div>
<div></div>
<div></div>

Answer №2

Since your CSS skills are strong, the key advice I have for you is to customize an HTML structure that suits your requirements:

<div class="widget">
    <div class="widget__left"></div>
    <div class="widget__middle"></div>
    <div class="widget__right"></div>
</div>

By doing this, you can easily apply CSS styles to target .widget__left, .widget__middle, and .widget__right.

Answer №3

To enhance the appearance of our background, we can utilize pseudo-elements and make use of multiple divs for a simpler approach.

When using transform: scaleX(-1), remember that it will flip the pseudo-element. Pay close attention to background positioning.

In this scenario, :before holds the first image, aDIV contains the third image (----), and :after displays the flipped version of the first image. By setting z-index:-1, we position the middle image behind the other two backgrounds, effectively eliminating any visible vertical edges through this workaround.

#aDIV:before
{
content:" ";
width:75px;
height:25px;
background-image:url('path');
background-repeat:no-repeat;
background-position:-5px -5px;
}    

 #aDIV:after
{
content:" ";
width:75px;
height:25px;
background-image:url('path');
background-repeat:no-repeat;
background-position:45px -5px;
transform: scaleX(-1);
}

#aDIV
{
 width:75px;
 height:25px;
 z-index:-1;
 background-image:url('path');
 background-repeat:no-repeat;
 background-position:20px -5px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

GWT encrypted CSS class names not being correctly applied

I recently encountered a strange issue with GWT styles. While using UiBinder and programmatically styling my GWT widgets, I ran into the following scenario: <ui:UiBinder xmlns:ui="..." xmlns:g="..."> <ui:style src="bindings.css"/> ...

Does "br" not play well with flexbox?

After creating a table using flexbox, I made an interesting observation: the br element seems to have no effect within the flexbox. For example: .flexbox { display: flex; flex-wrap: wrap; border: 2px solid red; padding: 2px; } .item { width: ...

How can one retrieve the 'alt' attribute text from an image tag on a webpage using the JSOUP library in an Android application?

Extracting the 'alt' text from an 'img' tag in Android after clicking a button is causing issues. The attempted code is not working as expected. Here is the snippet of the code that was used: Document doc = Jsoup.connect(url).get(); ...

Creating HTML or PHP pages using jQuery

Is it possible to generate a file using jQuery? I am creating a website maker. On the left side, I have a list of elements such as: ---------------------------------------------------------------- Elements Attri ...

Using various alignment options for images on mobile devices

I've designed a theme with two columns, one for text and the other for images. I've structured it in HTML to display as: Text - Img Img - Text Text - Img Img - Text Text - Img Img - Text However, for mobile responsiveness, I want it to display ...

Tips for choosing every <div> within a specific class

I'm trying to create multiple boxes within a class, all with the same background color. I've written code for 6 boxes in the "color-board" class, but when I go to select them, only 1 box is displaying... Below is the code for the class and how I ...

jquery allows you to toggle the visibility of content

There are two divs with a button positioned above them. Initially, only one div, the catalogusOrderBox, is visible. When the button named manualButton is clicked, it should display the manualOrderBox div while hiding the catalogusOrderBox div. The text on ...

``There seems to be an issue with the functionality of the href link

Currently, I am utilizing purecss to design colorful buttons on my website. However, I am encountering an issue where the href link is not clickable even though the button displays correctly. <button class='pure-u-1 pure-button pure-button-primary ...

The 'function' is not defined in Edge and Explorer browsers

I am currently working on a web page in SharePoint and have encountered an issue where the onclick referenced function throws an error only in Internet Explorer 11 and Microsoft Edge. However, in Chrome, Firefox, and Opera, everything works perfectly fine. ...

@page Css not displaying properly on the Mozilla Firefox browser

In order to fulfill my requirement, I need to ensure that there is a consistent 10cm margin on all pages when printing. Printing on my web page involves utilizing the window.print() function. However, due to the dynamic nature of the content, the number o ...

The JavaScript functions are not loading within the onload event handler

I've got an HTML document that contains some Script sections. I've consolidated all the functions within a single Script Tag. Now, I want to be able to utilize these methods in both the onload and onclick events. Below is the HTML file with all t ...

Unable to retrieve the sum total of all product items and display it in the designated text element

My product items are dynamically generated in a list. I have used the calculateItemTotal() method to determine the total for each item. Now, I need to sum up all these item totals and display the result in the total text field. However, instead of showing ...

JavaScript powered simple window

I am currently working on a basic modal that will open and display content. Unfortunately, I cannot add images to help illustrate the design, so I'll do my best to describe it in detail. The modal is linked to an image slider where the heading change ...

Hover background color not being applied to child elements within Button element in Firefox

Ensuring that my product is SEO-friendly, I incorporated semantic markup using the button element and schema attributes. To separate individual items, I utilized "span" elements while setting hover effects on selected items by adding display:block. This a ...

What is the best way to customize the color scheme of a Bootstrap 4 range slider?

https://i.sstatic.net/MBona.png Looking for suggestions on how to customize the colors of ranges in Bootstrap 4 and change the blue thumb color to 'gray'. Below is the example HTML input code: <p id="slider" class="range-field"> <in ...

Tips for creating a div that slides from the right side to the left side

I received a code from someone and I need help with making the sliding div slide from right to left. Essentially, I want the div to hide on the right side and slowly move to the left within a width of 300px. Here is the HTML code: <a id="loginPanel"&g ...

How to create an HTML hyperlink in CakePHP version 2.2.1 and above?

Is there an easy way to create an HTML link using the HtmlHelper class in CakePHP 2.2.1? Let's say I have a route set up that maps /finest-perfumes-ever-2012 to the Perfumes/Index Controller/Action. I want the generated link to be: somedomain.com/f ...

Align the top navigation centered horizontally, with the logo on the left and language selection on the right

Is there a way to center my menu horizontally while keeping the logo aligned to the left and language selection to the right, all with consistent proportions when resizing? I've tried various suggestions but nothing seems to work for my navigation bar ...

The footer menu fails to glide upwards

I designed a top menu for my website that includes 2 key features: Sticky navigation bar when scrolling Slide toggle navbar You can view the demo here: JSBIN However, I am facing 2 issues: When the page is at the top and the menu is in its default pos ...

How to Extract Text Content Excluding the Wrapping Tag from an Element Using JSoup

How can I take the text 'Some random text' and enclose it within a span tag? For example, if I have the following element: <div> Some random text 1 <a href="some_url">Go to Link</a> </div> <div> <spa ...