Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,
I have finished my html/css lessons but I m having hard time making a menu I want with images and text that are not set aligned but in different heights .I m linking an image to make you understand what I want.

http://i64.tinypic.com/1zvv8l4.jpg[^]

I read in stackoverflow that I should not use position properties if I want this menu to be responsive.

Thank you .
Posted
v2
Comments
Andy Lanng 15-Dec-15 9:29am    
What the mean by "responsive" is that when viewed on a browser with restricted width, the menu should shift out of the format you show into a simpler one.

Bootstrap does this often. I suggest you look to see how that css uses cascading media styles to arrange columns and rows.
George Tourtsinakis 15-Dec-15 9:53am    
They told me not to use position properties for responsive design.Anyway the thing is I can't do that without position properties.Maybe I m missing something.You can suggest any code if you think the menu will look like the img I posted.
Sergey Alexandrovich Kryukov 15-Dec-15 10:12am    
What they told you makes perfect sense. Even though absolute position can be used for responsive design, too (it would be more correctly to talk about fluid or liquid designs), it would be bases on calculations of window innerHeight and innerWidth (that is, something which is already done with normal flow of elements) and would be error-prone.

In practice, absolute positioning should only be uses when you need to overlap elements. But you want just the opposite: avoid overlapping and preserve the shape of the pattern created by those text and image elements.

I provided explanation of one of the approaches; please see Solution 1.

—SA

1 solution

Here is a hint for you: if you look properly, you will see a table of 5 columns and 2 rows, with 3 cells left empty.

For implementation, you can use 3 rows of div elements with appropriate CSS styles, in particular, the property display with values "table-row" and "table-cell":
https://developer.mozilla.org/en-US/docs/Web/CSS/display[^].

[EDIT]

Here is a proof of concept for you:
HTML
<html>
   <head>
      <title>Table layout</title>
      <style>
         div { padding-left: 1em; padding-right: 1em;
               padding-top: 0.4em; padding-bottom: 0.4em; }
         div.menu { display: table; }
         div.row { display: table-row; }
         div.cell { display: table-cell;
                    vertical-align:middle; text-align: center;
                    border: solid black thin; }
      </style>
   </head>
<body>

<div class="menu">

   <div class="row">
      <div class="cell">Image 1</div>
      <div class="cell"></div>
      <div class="cell">Text 1</div>
      <div class="cell"></div>
      <div class="cell">Image 2</div>
   </div>
   
   <div class="row">
      <div class="cell"></div>
      <div class="cell">Image 3</div>
      <div class="cell">Text 2<br/>
         more text<br/>more…</div>
      <div class="cell">Image 4</div>
      <div class="cell"></div>
   </div>

</div>

</body>
</html>


For final results, remove the borders; I added them just for demonstration. Pay attention for vertical-align and the fact that the content adjusts itself in both vertical and horizontal direction, depending on the size of the inner content of all cells.

—SA
 
Share this answer
 
v4
Comments
George Tourtsinakis 15-Dec-15 11:34am    
Still the images and text align vertically next to each other.I can't give the space needed or put the text as shown in the link.Only if I put position :absolute go where I went.I definitely miss or don't understand something.Nice page though you provided.
Sergey Alexandrovich Kryukov 15-Dec-15 11:56am    
With the styles I suggested, you don't need to give space. You need to simulate 3-level hierarchy: table, table row (two of them), cells in each row (I forgot to mention display="table".) After all, you can simply use the elements table, tr and td (there is a lot of objections against doing so, but I consider them "religious"). Your empty cells will be represented by empty elements (div or td). No, don't use absolute.

I feel you just did not try the table layout, but it will work, I'll guarantee that. (I also forgot to mention: you may need the style vertical-align; but this is alignment inside a cell.)

—SA
Sergey Alexandrovich Kryukov 15-Dec-15 12:08pm    
Look, this is so obvious. Please see my update to the question, after [EDIT], where I provided a complete working code sample, proof of concept.
—SA
George Tourtsinakis 15-Dec-15 12:27pm    
Interesting approach.You use tables without table elements but with classes and display properties that work like tables.Din;t know about that.Thank you for that I have a lot to learn in html/css still.
Sergey Alexandrovich Kryukov 15-Dec-15 12:33pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900