How come the paragraph shifts downward when I use a jQuery custom plugin to add the image source?

Check out the code below:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
    <title>Box using Plugin</title>
    <script>
        (function ($) {
            $.fn.boxPlugin = function (options) {
                //default values
                var settings = $.extend({
                    color: "green",
                    width: "100px",
                    height: "100px",
                    backgroundColor: "black",
                    imageURL: "",
                    display: "inline-block"

                }, options);

                $(this).addClass('boxAdded').css({ "color": settings.color, "width": settings.width, "height": settings.height, "background": settings.backgroundColor, "display": settings.display }).find('img').attr('src', settings.imageURL);
            }
        }(jQuery));
    </script>
    <style>
        p {
            margin: 0px;
        }
    </style>
    <script>
        $(function () {
            $(".box").boxPlugin({ width: "200px", height: "200px", backgroundColor: "lightblue" });
            $("#boxOne").boxPlugin({ width: "200px", height: "200px", backgroundColor: "lightblue", imageURL: "https://www.w3schools.com/images/w3schools_green.jpg" });
        })
    </script>
</head>

<body>
    <p class="box"></p>
    <p class="box"></p>
    <p class="box"></p>
    <p id="boxOne">
        <img src="" />
    </p>
</body>

</html>

If I don't include the imageURL option in the settings, everything works fine with the width and height set as expected.

However, when I add the imageURL option, the image links correctly but the paragraph moves down slightly. Can someone explain why this is happening? It's quite puzzling!

Thank you.

Answer №1

Revise your CSS as follows :

p {
   margin: 0px;
   vertical-align: top;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
    <title>Box using Plugin</title>
    <script>
        (function ($) {
            $.fn.boxPlugin = function (options) {
                //default values
                var settings = $.extend({
                    color: "green",
                    width: "100px",
                    height: "100px",
                    backgroundColor: "black",
                    imageURL: "",
                    display: "inline-block"

                }, options);

                $(this).addClass('boxAdded').css({ "color": settings.color, "width": settings.width, "height": settings.height, "background": settings.backgroundColor, "display": settings.display }).find('img').attr('src', settings.imageURL);
            }
        }(jQuery));
    </script>
    <style>
        p {
            margin: 0px;
            vertical-align: top;
        }
    </style>
    <script>
        $(function () {
            $(".box").boxPlugin({ width: "200px", height: "200px", backgroundColor: "lightblue" });
            $("#boxOne").boxPlugin({ width: "200px", height: "200px", backgroundColor: "lightblue", imageURL: "https://www.w3schools.com/images/w3schools_green.jpg" });
        })
    </script>
</head>

<body>
    <p class="box"></p>
    <p class="box"></p>
    <p class="box"></p>
    <p id="boxOne">
        <img src="" />
    </p>
</body>

</html>

Answer №2

To ensure that the image fits within the box, you must set display:block; for the image as all your blocks are currently set to inline-block.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
    <title>Box using Plugin</title>
    <script>
        (function ($) {
            $.fn.boxPlugin = function (options) {
                //default values
                var settings = $.extend({
                    color: "green",
                    width: "100px",
                    height: "100px",
                    backgroundColor: "black",
                    imageURL: "",
                    display: "inline-block"

                }, options);

                $(this).addClass('boxAdded').css({ 
                "color": settings.color, 
                "width": settings.width, 
                "height": settings.height, 
                "background": settings.backgroundColor, 
                "display": settings.display }).find('img').attr('src', settings.imageURL).css('display',settings.imgdisplay);
            }
        }(jQuery));
    </script>
    <style>
        p {
            margin: 0px;
        }
    </style>
    <script>
        $(function () {
            $(".box").boxPlugin({ width: "200px", height: "200px", backgroundColor: "lightblue" });
            $("#boxOne").boxPlugin({ 
            width: "200px", 
            height: "200px", 
            backgroundColor: "lightblue", 
            imageURL: "https://www.w3schools.com/images/w3schools_green.jpg" ,
            imgdisplay: "block"
            });
        })
    </script>
</head>

<body>
    <p class="box"></p>
    <p class="box&-gt;</p>
    <p class="box"></p>
    <p id="boxOne">
        <img src=""  />
    </p>
</body>

</html>

Answer №3

Utilize the tag to neatly organize your data, the image shadow may cause some issues

Answer №4

Simply add:

#boxOne img {
   position: absolute;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
    <title>Box with Plugin</title>
    <script>
        (function ($) {
            $.fn.boxPlugin = function (options) {
                //default settings
                var settings = $.extend({
                    color: "green",
                    width: "100px",
                    height: "100px",
                    backgroundColor: "black",
                    imageURL: "",
                    display: "inline-block"

                }, options);

                $(this).addClass('boxAdded').css({ "color": settings.color, "width": settings.width, "height": settings.height, "background": settings.backgroundColor, "display": settings.display }).find('img').attr('src', settings.imageURL);
            }
        }(jQuery));
    </script>
    <style>
        p {
            margin: 0px;
        }
        #boxOne img {
            position: absolute;
        }
    </style>
    <script>
        $(function () {
            $(".box").boxPlugin({ width: "200px", height: "200px", backgroundColor: "lightblue" });
            $("#boxOne").boxPlugin({ width: "200px", height: "200px", backgroundColor: "lightblue", imageURL: "https://www.w3schools.com/images/w3schools_green.jpg" });
        })
    </script>
</head>

<body>
    <p class="box"></p>
    <p class="box"></p>
    <p class="box"></p>
    <p id="boxOne">
        <img src="" />
    </p>
</body>

</html>

Answer №5

To style #boxOne, apply the rule vertical-align: top;.

For optimal results, combine vertical-align: top; with display: inline-block. By default, the vertical-align property is set to baseline.

Answer №6

Apply the float property to the p tag in order for it to align to the left.

p {
   margin: 0px;
   float: left;
}

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

The page becomes unresponsive once a window is closed

My process in selenium is outlined as follows: Accessing a webpage Clicking on a tab Clicking on the add button within the tab to open a window Closing the window Attempting to click on the same tab again Although I can successfully complete steps 1 to 4 ...

"Introduce a time delay for the hover effect on the navigation dropdown

I've been struggling to find a way to add a delay to this hover effect. If you visit , you'll see a menu at the top that displays panels when hovered over. The CSS code to make them appear is: #navigation li:hover > .panel { display: blo ...

Which is better for handling events - jQuery delegation or function method?

Which approach is quicker and has broader browser support? 1. Utilizing a JavaScript function such as: function updateText(newtext) { $('div#id').text(newtext); } and incorporating it into an element's onclick event: <button onc ...

Exploring the wonders of looping through arrays with JavaScript and jQuery

I am having trouble getting this function to work properly. My goal is to replace certain characters when a key is pressed. The code works fine using the variable `replace_list` instead of `replace_list["russian"]`, but I actually need different "replace ...

Conceal the complete <div> in the event that the <table> contains no data

My task involves managing a div containing a table. The goal is to hide the div if it has no value or is empty, and then show it again once it has a value. <div class="container" id="main_container"> <table id="my_tabl ...

Display background image exclusively on homepage using jQuery

Is there a way to show a fullscreen, resizing background image only on the start page? I have a div in the background that spans the entire background and resizes when the window loads. How can I conceal this div unless the current page is the start page ...

Two identical Jquery ajax calls, but only one is functioning correctly

I encountered an issue where I am calling two jQuery ajax functions using the same PHP function but targeting two different IDs. However, only the first ajax call seems to work. Interestingly, when I switch the order of the calls, the other one starts work ...

Discovering the compiled CSS file in React when using Sass: A step-by-step guide

In my React project, I have incorporated SASS. Now I am looking to find the final compiled CSS file from that SASS code. Whenever I navigate to: While there is the SCSS file visible, where can I locate the CSS version? I am referring to the compiled outp ...

Utilize AJAX to dynamically load a complete HTML page in Flask

In the backend of my web app, I utilize Flask along with Jinja2 and wtforms to dynamically produce content. One of the main pages that is dynamically generated is a table displaying all sales for a specified month. This table includes select fields at the ...

Setting the attribute dynamically for a select box with multiple choices

In my form, I have multiple choice select boxes styled using bootstrap select. Since the app is developed in Express, I am having trouble retrieving the selected values due to bootstrap select creating a div and a list for user interaction. To tackle this ...

Is there a way to display (PHP for loop variables) within an HTML table using echo?

for ($q = 1 ; $q < 7 ; $q++ ) { echo $q ; echo $row; } While the above code functions properly, I am interested in echoing two rows as shown in the image below: https://i.stack.imgur.com/7KmUY.jpg I prefer not to use another for loop. Is there a way t ...

Having trouble getting a button's OnClick event to work with Bootstrap on iOS devices

Here is an example of the issue I'm experiencing: http://jsfiddle.net/d01sm5gL/2/ The onclick event functions correctly on desktop browsers, however when using iOS8, the event does not trigger. I have tried calling the function manually through the d ...

Ways to optimize image focus on a webpage

Upon loading the page, I desire for my image to be automatically focused. Unfortunately, setting the tabindex parameter has not produced the desired outcome. This is likely due to the fact that there are multiple angular2 header components with defined t ...

Content of two li tags in row and column are not aligned properly

I am struggling with ensuring that the two columns and two rows of text in my page section are always aligned correctly. Column 1 and the contents of row 1 should be on the same line, but instead, column 2 is appearing a line or two below column 1. This is ...

Struggling with generating forms using AJAX, Javascript, and HTML depending on the selection made from a drop-down menu

I am in need of a simple web form for work submissions within my organization. These submissions will fit into 4 Categories, each requiring unique information. Currently, I have a basic form set up with fields such as Requested Name, Requested Date, Acquis ...

Invoke a method in a separate class

I am facing issues with passing variables to another file. I have checked my code multiple times but cannot find a solution. It keeps giving me an error in the function. onclick="limit();javascript:AyudaTipos(2)"/> The function is: function limit ( ...

What is the process for clearing CSS position properties?

I am facing an issue with a multiselect dropdown in my kendo grid, which is being used for a kendo modal window. The dropdown box initially appears correctly but when I scroll horizontally due to multiple columns, the selected value still displays at the o ...

The autofill ajax script is unable to populate dynamically added rows

As a newcomer to programming, I am trying to create a table that allows users to add new rows by clicking a button. Each row should have a field for inputting an ID which corresponds to some predefined numbers in another table, and then the other field sho ...

When utilizing the JavaScript createElement() method to create elements on keydown, it will not be compatible with jQuery's draggable() method

I'm currently developing a drag and drop feature for a project, allowing users to add items to a work area and then position them by dragging. I'm facing an issue where I want to create multiple instances of the same element using a key code, but ...

Rows that intersect - Challenging to articulate - Bootstrap 3

I'm at a loss for words when trying to describe what I'm attempting without simply showing you a picture: The issue I'm encountering is that the search box's height isn't fixed, and I might want to include other elements on the le ...