What could be causing the CSS style to not take effect following transclusion?

Why isn't the style being applied in this code snippet that uses ng-transclude? The transclusion function works fine, but for some reason the class .box is not applying to the span. Why could this be happening?

transclude.html looks like this:

<html ng-app="myApp">
    <head>
        <script src="js/angular.js" type="text/javascript"></script>
        <script src="js/transclude.js" type="text/javascript"></script>

        <style type="text/css">
            .box{
               background-color: red; 
            }
        </style>

    </head>
    <body>

        <div sidebox title="Links">
            <ul>
                <li>first link</li>
                <li>2nd link</li>
            </ul>
        </div>        
    </body>
</html>

transclude.js looks like this:

var app = angular.module('myApp',[]);
app.directive('sidebox',function(){
    return {
      restrict:'A',
      transclude:true,
      scope:{
          title:'@'
      },
      template:'<div>\n\
        <h1>{{title}}</h1>\n\
        <span class="box" ng-transclude></span></div>\n\
'
    };
});

UPDATE: It seems that the code did apply the .box class, but the div did not turn red as expected. See image https://i.sstatic.net/WZsi7.png.

Answer №1

The default behavior of a <span> element is to be displayed inline, meaning it can only contain plain text or marked up text, not other elements like <ul> which are block elements.

If you insist on using a span (although a div would be more appropriate), you must change its CSS property to display: block in order for it to adjust its size based on block content. Alternatively, you can use display: inline-block if you want some inline properties while still maintaining block-like behavior. However, keep in mind that inline-block may have unintended consequences that you probably want to avoid.

It's important to note that this is not an issue related to Angular, but rather a basic HTML/CSS matter.

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

Troubleshooting Offline.js

I've implemented Offline.js in my project, a powerful library that notifies users when they lose internet connection. Using version 0.5.0.0 of offline.js, I included offline.min.js in the _Layout Page. While it functions flawlessly in Google Chrome, I ...

What is the best way to perfectly center this select box both horizontally and vertically within a table in Bootstrap 4.0?

I've attempted various methods, including this one, but haven't been successful. You can find the Fiddle here. <td class="align-middle"> <select name="D1" style="border-radius: 0.2rem;"> <op ...

Modifying the TR class appears to be ineffective in this instance

I am attempting to update the TR className using JavaScript. However, for some reason it does not appear to be working properly (please note that it must also work in IE8). <html> <head> <style> tr.test {background-color:#000000; ...

Displaying the age figure in JSX code with a red and bold formatting

I have a webpage with a button labeled "Increase Age". Every time this button is clicked, the person's age increases. How can I ensure that once the age surpasses 10, it is displayed in bold text on a red background? This should be implemented below t ...

What is the best way to choose a specific checkbox with a ::before pseudo-element?

Having trouble selecting checkboxes in a table with a ::before tag using Selenium. Even after implementing implicit and explicit waits, as well as action class for a list of elements, I still receive an "element not visible" error. How can I select a speci ...

Using jQuery to include Chinese characters in a request header

When making a jQuery post request, I need to set client information in the header. This is how my call looks: jQuery.ajax({ url : myURL, type : "POST", beforeSend : function(request){ request.setRequestHeader('someInfo', clie ...

What is the reason for the position of div1 not being directly in the top right corner when its position is set to div2's top and left offset plus div2's width?

function handler(ev) { var target = $(ev.target); if( target.is(".item") ) { var brt = $(target).offset().top; var let = $(target).offset().left + $(".test").width(); $('#DivToShow').css({'top':brt,'left':let, 'd ...

What methods would you use to test a Vanilla JS Ajax call handled by a Promise using Jasmine?

I recently came across a great solution on How to make an AJAX call without jQuery? that I wanted to implement. However, I found it challenging to mock the Promise, the AJAX call, or both in my testing. My initial attempt with jasmine-ajax didn't wor ...

Display the div in all browsers except for Internet Explorer

There's a message in a div that needs to be displayed on the webpage, but it's not showing up on Internet Explorer. Even after attempting the usual , as well as other recommendations, the message still remains hidden. What could I be overlooking ...

modify the appearance of HTML controls in real time with C#

Is there a way to dynamically add an additional navigation item to my website's menu list when the admin logs in through admin.aspx? The menu list is created using HTML controls such as <ul>...<li>, and I would like the new navigation item ...

Is there a way to transform standard JSON data into a format that can be interpreted by Google Visualization

Recently, I delved into the world of Google charts and was amazed by its capabilities. However, I encountered a hurdle when trying to integrate data from a SQL server view. To tackle this issue, I initiated a sample project to facilitate the process but ha ...

Send an email containing the entirety of a web page

After selecting a user from a drop-down and running a search, a PHP page displays a time schedule in HTML table format. Now, the goal is to send the entire page to the user via email when a specific button is clicked. The user's email address is sto ...

Unable to locate the hidden file name within the iframe

Currently, I am utilizing an iframe for uploading an image. This setup involves two files - one for the iframe itself and another to display the uploaded image. When the link to display the iframe is clicked, the following code is executed: function ShowU ...

When the add button is clicked, I would like to implement a feature where a checkbox is added

When the user clicks on the link "출력하기", I want the web page to add a checkbox to all images. I wrote this code, but it's not working. Can anyone help me? This is my JS: $(document).ready(function(){ $("#print").on('click', fu ...

Exploring the Power of Ajax with JQuery on the Django Development Server

I am currently setting up an Ajax request using JQuery (version 1.5) on a Django website running version 1.2.5. I am testing this on the Development Server as I intend to demonstrate it before deploying to production. Below is the javascript code snippet ...

Learn the process of synchronously loading forms and data in Angular

Looking to synchronize the loading of data and form... Let's start by reviewing the code: ngOnInit() { if (this.data.fromCalendar) { this.singleTraining(); } }, 200); this.formControl(); } formControl() { this.gib ...

What specific features does CSS3 offer in terms of background-size properties?

Let's delve into my knowledge: $, like in font-size: 14$; em, as in margin: 2em; What other units are out there? ...

The Angular ng-repeat directive is populating a list with items, but the parameters enclosed in double curly braces

After calling an API, the response contains data in the following format: [{name: "ABC", age: 20}, {name: "DEF", age: 25}] Even though the $scope.names variable is assigned with the array from the API, when viewed in the browser, it displays 2 rows but th ...

Utilizing CSS styling in PHP as a functional approach

In my journey of learning CSS, HTML5, and jQuery, I have found a unique way to organize my CSS code into different chapters. I simply call them into the head section when needed. This method allows me to have only the necessary CSS on each page, making it ...

Guide on comparing a string value with a date in the format of YYYY-MM-DD using protractor in a node js environment

In my application, I am retrieving the date field in string format using the getText() function. I need assistance in determining whether the captured date is earlier than the current date. Can someone provide guidance on this? var actDate = locator.getTex ...