Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Answered css numbering and cross referencing

JohanHu

New Coder
Hi all,
I have succeeded with css numbering. But now, I would like to be able to reference to the whole numbering (like Figure 1). Here is the code:

HTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test Report</title>
        <style type="text/css">
            body {
                counter-reset: fignum;
            }
            body figcaption {
                counter-increment: fignum 1;
            }
            body figcaption:before {
                content: "Figure " counter(fignum) ". ";
            }
        </style>
    </head>
    <body>
        <p>
            <figure>
                <img   src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/car.svg"/>
                <figcaption   id="test_fig">A nice car made in SVG.</figcaption>
            </figure>
        </p>
        <p>
            Here is a reference to an SVG, see <a href="#test_fig"></a>.
        </p>
    </body>
</html>

I would like the reference to be printed out (automatically of course): "Here is a reference to an SVG, see Figure 1.
Is this possible in html/css?
And how is it done?
Thanks!
/Johan
 
Solution
The problem is quite easily solved by javascript!

HTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test Report</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
    <body>
        <figure>
            <img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/car.svg"/>
            <figcaption id="test_fig">A nice car made in SVG.</figcaption>
        </figure>
        <p>
            Here is a reference to an SVG, see <a href="#test_fig"></a>.
        </p>
        <script>
            $(document).ready(function(){
                $("figcaption").each(function(index,value){...
Hi @Johna,
In my browser the code render to a figure with a caption (with an ID) that is automatically numbered to Figure 1. The image is shown in the html line below:
HTML:
<img   src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/car.svg"/>

The problem is that the cross reference I would like to use below the <figcaption> should automatically render to Figure 1 when using the same ID.
Thanks!
/Johan
 
The problem is quite easily solved by javascript!

HTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test Report</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
    <body>
        <figure>
            <img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/car.svg"/>
            <figcaption id="test_fig">A nice car made in SVG.</figcaption>
        </figure>
        <p>
            Here is a reference to an SVG, see <a href="#test_fig"></a>.
        </p>
        <script>
            $(document).ready(function(){
                $("figcaption").each(function(index,value){
                    $(this).prepend("Figure "+(++index)+". ");
                    $('[href="#'+$(this).attr("id")+'"]').text("Figure "+index)
                });
            });
        </script>
    </body>
</html>

Hope that this can help someone!
/Johan
 
Solution

Latest posts

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom