How to style, with CSS, a SELECT that is inside PHP?

Asked

Viewed 1,119 times

0

<?php

    include "conexao.php";

    $sql = "select * from categorias";
    $dados = pg_query($sql);
    $resposta = "";

    while($linha = pg_fetch_array($dados)){
        $nome_categoria = $linha["nome_categoria"];
        $resposta .= "<option>".$nome_categoria."</option>";
    }

    $categoria = "<select>". $resposta."</select>";
    pg_free_result($dados);

?>

HTML

<tr><td><a class="itens">Categoria:</td>
<td><?php echo $categoria;?></a></td></tr>
  • You will be welcome to the stack overflow. The select being generated in php and the styles application in it are two different things. Check whether that question Already answered does not solve your problem.

  • Then there would be no way I could change the source of the results generated in select??

  • Your question is how to apply css to a select/option element, this ?

  • I would like to know how to change the source of what is shown inside a select

  • In the question I put as link shows how to style select, just change the css and change the font-family. I recommend reading: [tour], [help] and [Ask]

  • Yes, but how do I call in CSS, since my select is in PHP ?? You can put a class in PHP ??

Show 1 more comment

1 answer

10


The fact that select is generated by PHP makes no difference as to the stylization of it. Remember, your PHP script is returning HTML, which is interpreted in the browser.

Regarding stylisation, note that the element select can be stylized more freely than the elements option, which has many of its properties limited by the browser used.

To style, you can use a class, an id or the tag itself select (note that the latter will stylize all select, what may or may not be desired).

select {
    font-family: 'Verdana', sans-serif;
    font-size: 14px;
}

If you want to use a class, put it in your PHP:

$categoria = "<select class='minha-classe'>". $resposta."</select>";

Then in CSS:

select.minha-classe {
    font-family: 'Verdana', sans-serif;
    font-size: 14px;
}
  • so how do I call in CSS ?? I’m a beginner.. Thank you..

  • Thank you worked!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.