skip to Main Content

I have a div containing a paragraph. I want to add a translucent background image to the paragraph but only upto height same as height of the paragraph. I have:

<div id = "phase1box">
    <div id = "phase1content" class = "phasecontent">
        <h1>Phase 1</h1>
        <p1>
        •   some text.<br/>
        •   some text.<br/>
        •   some text.<br/>
        •   some text.<br/>
        •   some text.<br/>
        </p1>
    </div>
</div>

CSS:

#phase1box {
    position: absolute;
    top: 0%;
    width:100%;
    height:18%;
    background-image: url("../assets/phase1box.png");
}

I have hard coded height: 18% but instead i want the background image to take height of the div (paragraph). If i do height: auto no background image shows up.

2

Answers


  1. <script>
    function inserirNaTabela() {
        const textarea = document.querySelector("#result");
        const tabela = document.querySelector("#minha-tabela tbody");
    
        const texto = textarea.value;
        const valoresAntes = texto.toLowerCase().split("values")[0];
        const valoresPos = texto.toLowerCase().split("values")[1];
    
        const regex = /^(.*?)(/;
        const match = regex.exec(valoresAntes);
    
        let label = document.querySelector("#valores-anteriores");
        if (match) {
            label.textContent = `${match[1]}`;
        } else {
            label.textContent = "N/A";
        }
    
        const content = valoresAntes.replace(regex, "");
        const contentArray = splitValues(content);
        for (var i = 0; i < contentArray.length; i++) {
            const valor = contentArray[i].trim();
            if (valor !== "") {
                const row = tabela.insertRow();
                const cell1 = row.insertCell(0);
                cell1.innerHTML = valor;
                const cell2 = row.insertCell(1); // Nova célula para a coluna de valores
                cell2.innerHTML = ""; // Deixar a coluna de valores vazia por enquanto
            }
        }
    
        const valores = valoresPos ? splitValues(valoresPos) : [];
        const rows = tabela.getElementsByTagName("tr");
        let rowIndex = 0;
        let cellIndex = 1; // Iniciar o índice da célula na segunda coluna (coluna de valores)
        for (let i = 0; i < valores.length; i++) {
            const valor = valores[i].trim();
            if (valor !== "") {
                const row = rows[rowIndex];
                let cell = row.cells[cellIndex];
                if (!cell) {
                    cell = row.insertCell(cellIndex); // Adicionar nova célula na coluna de valores, se necessário
                }
                cell.innerHTML = valor;
                rowIndex++;
                if (rowIndex >= rows.length) {
                    rowIndex = 0;
                    cellIndex++; // Passar para a próxima coluna de valores
                }
            }
        }
    
        var imagem = document.getElementById('elefante');
        imagem.classList.add('rotacionar');
    
        var celulas = tabela.getElementsByTagName('td');
        for (var i = 0; i < celulas.length; i++) {
            var conteudo = celulas[i].innerHTML;
            conteudo = conteudo.replace(/[()]/g, '');
            celulas[i].innerHTML = conteudo;
        }
    
        const contadorLinhas = document.querySelector("#contadorLinhas");
        contadorLinhas.textContent = `Linhas geradas: ${rows.length}`;
    };
    
    function splitValues(content) {
        let values = [];
        let isInQuotes = false;
        let currentValue = "";
    
        for (let i = 0; i < content.length; i++) {
            const char = content[i];
            if (char === "'") {
                isInQuotes = !isInQuotes;
            }
            if (char === "," && !isInQuotes) {
                values.push(currentValue);
                currentValue = "";
            } else {
                currentValue += char;
            }
        }
    
        values.push(currentValue); // Adicionar o último valor
    
        return values;
    }
    
    function highlightKeywords() {
        const textarea = document.querySelector("#result");
        const keywords = ["INSERT", "VALUES"];
        const content = textarea.value;
        const highlightedContent = content.replace(
            new RegExp(`\b(${keywords.join("|")})\b`, "gi"),
            '<span class="highlight-$1
    
    Login or Signup to reply.
  2. To make the background image height the same as the height of the div content, you can use the CSS property background-size with the value contain. This will ensure that the background image scales to fit within the container while maintaining its aspect ratio.

    Here’s an updated version of your code with the necessary CSS modifications:

    <div id="phase1box">
        <div id="phase1content" class="phasecontent">
            <h1>Phase 1</h1>
            <p>
                •   some text.<br/>
                •   some text.<br/>
                •   some text.<br/>
                •   some text.<br/>
                •   some text.<br/>
            </p>
        </div>
    </div>
    

    CSS

    #phase1box {
        position: absolute;
        top: 0;
        width: 100%;
        background-image: url("../assets/phase1box.png");
        background-size: 100% auto;
        background-repeat: no-repeat;
        overflow: hidden; 
    }
    
    .phasecontent {
        padding: 20px;
        position: relative; 
    }
    
    .phasecontent:before {
        content: ""; 
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-image: inherit; 
        background-size: 100% 100%; 
        z-index: -1; 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search