CSS - Listas a lo ancho

La tendencia actual al desarrollar páginas HTML es utilizar CSS.

Con CSS, se debe crear el contenido de la página demarcado adecuadamente con etiquetas HTML, y el diseño de la página se obtiene dandole el estilo por medio de un archivo aparte.

La manera más utilizada actualmente de crear menués es con una lista de enlaces y hacerla ver como un menu con CSS, pero una lista tiene sus limitaciones. Yo quise hacer que un menú generado por software por medio de listas utilizara todo el ancho de la página. Una lista no distribuye sus elementos a lo ancho de la lista, tal como lo hace una tabla, sino que los va agregando a la lista hasta llenar su ancho. La única manera de hacerlo era poner medidas estáticas a los elementos de la lista, pero al agregar otro elemento se corrín y quedaba desordenado. Así que tuve que encontrar una solución dinámica a mi problema.

Esta es una tabla con dos elementos:

A B

Esta es una lista con dos elementos:

  • A
  • B

Nótese que para evitar confusiones, el borde de la tabla es rojo mientras que el de la lista es negro.

Al quitar los marcadores a la lista y desplegarla en una línea, se ve similar a nuestra tabla:

  • A
  • B

Mi problema era que los elementos no son distribuidos a lo ancho de la lista, sino que esta agrupados al inicio, mientras que en la tabla, al darle un ancho fijo, los elementos se distribuyen dentro de la tabla para utilizar todo el ancho:

A B

Mi primera soluci&oacuten fue dar un ancho fijo a la lista, y utilizar medidas estáticas para distribuir los elementos a lo ancho:

  • A
  • B

Pero esta solución resultó ser ineficaz tan pronto se agregó otro elemento más por medio del software:

  • A
  • B
  • C

Esto es lo que pasaría si el software generara una tabla en lugar de una lista, y de hecho, este es el efecto esperado:

A B C

Se me ocurrió modificar el software para que generara una tabla en lugar de una lista, pero hubiera resultado más complicado. Especialmente al actualizar el software, ya que la nueva versión posiblemente volverá a generar este menú como listas, y tendría que volverlo a modificar. También esta la posibilidad de un cambio en el diseño de la página, y ahora el menú perdería todas las ventajas de ser creado como una lista. Así que preferí dejar esta opción como última.

Entonces decidí replantear mi problema. En lugar de pensar que mi problema es que la lista no distribuye sus elementos a lo ancho, empezar a pensarlo como la necesidad de que una lista se comporte como una tabla.

La tabla se comporta como lo hace, porque el navegador crea unas definiciones de estilo por defecto, en el caso de una tabla sencilla, serían las siguientes:

table { display: table }
tr { display: table-row }
td { display: table-cell }

… y el estilo lo que me permite es modificar las definiciones por defecto de los diferentes elementos HTML utilizados en nuestras páginas, tal y como ya lo hice con la lista para que despliegue sus elementos en una sola línea un lugar de hacerlo en líneas aparte. Así que se me ocurrió quitar las medidas estáticas y probar lo siguiente:

ul { display: table }
li { display: table-cell }

… y este fue el resultado:
Con 2 elementos

  • A
  • B

Con tres elementos

  • A
  • B
  • C

Así que esta es la manera sencilla de hacer que una lista horizontal despliegue sus elementos a lo ancho de la misma.

30 respuestas a “CSS - Listas a lo ancho”

  1. gm dijo:

    This design is steller! You definitely know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  2. inkjet cartridges dijo:

    Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say superb blog!

  3. fruit mocking party dijo:

    Wonderful post however , I was wondering if you could write a litte more on this topic? I’d be very thankful if you could elaborate a little bit more. Bless you!

  4. Linguim dijo:

    Exactly where might I get a hold of this Blog platform?

  5. What are some things that help Teenage Acne dijo:

    I’m not sure exactly why but this web site is loading extremely slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later on and see if the problem still exists.

  6. escorts in london dijo:

    I am curious to find out what blog system you are utilizing? I’m having some small security problems with my latest blog and I’d like to find something more safe. Do you have any recommendations?

  7. Sade Qualey dijo:

    Really impressed with the website, sincerely, Dr. Smith,

    Sanford Dental Excellence
    4942 W. SR 46 Suite 1038, Sanford, FL
    (407) 513-4950

  8. project payday review dijo:

    Hmm it seems like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog. I too am an aspiring blog blogger but I’m still new to the whole thing. Do you have any tips and hints for first-time blog writers? I’d genuinely appreciate it.

  9. drinking games for 2 people dijo:

    Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot you an email. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it develop over time.

  10. IP address dijo:

    Hey! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa? My blog goes over a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you are interested feel free to send me an email. I look forward to hearing from you! Fantastic blog by the way!

  11. Lampu kolam renang dijo:

    Audio began playing any time I opened up this webpage, so frustrating!

  12. hack facebook password dijo:

    learn how to hack a facebook = hack-facebook-password.org

  13. finding people dijo:

    The crux of your writing while sounding reasonable initially, did not settle properly with me after some time. Somewhere throughout the sentences you actually were able to make me a believer but just for a while. I however have a problem with your leaps in assumptions and one would do well to help fill in those gaps. When you actually can accomplish that, I could definitely end up being amazed.

  14. cockroach repellent dijo:

    It’s a shame you don’t have a donate button! I’d without a doubt donate to this fantastic blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this website with my Facebook group. Talk soon!

  15. kill fleas in house dijo:

    It’s a pity you don’t have a donate button! I’d definitely donate to this fantastic blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this blog with my Facebook group. Talk soon!

  16. ant exterminator dijo:

    Wow! This blog looks exactly like my old one! It’s on a totally different topic but it has pretty much the same page layout and design. Excellent choice of colors!

  17. how to kill ticks dijo:

    When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Appreciate it!

  18. free calling dijo:

    Internet can now put your phone calls on cable and you can make free pc to phone calls. This free calling service lets you make free calls to India, free calls to China, and free calls to all other countries using internet telephone. Internet telephony is the thing of the future according to a recent research. Free phone calls will be a reality in very soon and wireless service carriers will have a hard time justifying their costs.

  19. Boys fancy dress dijo:

    Fancy dress for boys will always be popular and there are so many different choices out there these days from animals to TV star themed costumes, film stars to traditional costumes such as angels and fairies.

  20. how to astral project dijo:

    Do you have a spam problem on this site; I also am a blogger, and I was curious about your situation; many of us have created some nice methods and we are looking to swap techniques with other folks, be sure to shoot me an e-mail if interested.

  21. Garmin nüvi 2545 dijo:

    great post!!

  22. my IP dijo:

    I’m not sure exactly why but this weblog is loading very slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later and see if the problem still exists.

  23. lewis satloff dijo:

    Woah! I’m really loving the template/theme of this site. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a awesome job with this. Additionally, the blog loads super fast for me on Firefox. Excellent Blog!

  24. calgary mortgage dijo:

    I know this if off topic but I’m looking into starting my own blog and was curious what all is required to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web savvy so I’m not 100% certain. Any suggestions or advice would be greatly appreciated. Thanks

  25. Roofline replacement Norwich dijo:

    Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say excellent blog!

  26. broker dijo:

    Thanks for ones marvelous posting! I genuinely enjoyed reading it, you’re a great author.I will always bookmark your blog and definitely will come back at some point. I want to encourage you to definitely continue your great job, have a nice evening!

  27. future technology predictions dijo:

    This design is steller! You definitely know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  28. makar pekseg dijo:

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog? My blog is in the exact same area of interest as yours and my visitors would certainly benefit from a lot of the information you present here. Please let me know if this ok with you. Thanks!

  29. san diego seo management dijo:

    Hmm it seems like your blog ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I too am an aspiring blog blogger but I’m still new to the whole thing. Do you have any recommendations for rookie blog writers? I’d really appreciate it.

  30. bench jacken damen reduziert dijo:

    Don’t count the chickens before they are hatched.

Leave a Reply