?
Solved

Element alignment and word wrapping

Posted on 2016-11-18
9
Medium Priority
?
162 Views
Last Modified: 2016-11-19
Hi everybody.
I have a div which contains 2 man elements, an anchor tag and a span: the first should contain an image (the user avatar), the second the user name.
I woud like to get 2 results:

1) the user name should be aligned with the vertical center of the image
2) If the user name is too long for the container div should wrap remaining aligned with the vertical center of the image

The result should be something the one you can see in attached image:

alignment and word wrapping
Here you find a jsFiddle to play with. In the fiddle you fins relevant markup and related relevant css rules... which fail :)

Thank you for any suggestion.
0
Comment
Question by:Marco Gasi
  • 3
  • 3
  • 3
9 Comments
 
LVL 16

Assisted Solution

by:SSupreme
SSupreme earned 1000 total points
ID: 41893374
0
 
LVL 62

Accepted Solution

by:
Julian Hansen earned 1000 total points
ID: 41893642
Here is a solution without flex - which has pre IE 10 compatibility issues
CSS
<style type="text/css">
ul {
  list-style: none;
}
ul li {
  padding: 10px;
}
ul li:nth-child(odd) {
  background: #f5f5f5;
}
ul li:nth-child(even) {
  background: #cfcfcf;
}
ul li span {
  display: inline-block;
  vertical-align: middle;
  width: 70px;
  margin-left: 5px;
}
.avatar {
  width: 25px;
  vertical-align: middle;
  display: inline-block;
}
.avatar img {
  width: 100%;
}
</style>

Open in new window

HTML
    <ul>
      <li>
        <a href="" class="avatar"><img src="images/pic2.png" /></a>
        <span>Frederico Stephano Milano Smit</span>
      </li>
      <li>
        <a href="" class="avatar"><img src="images/pic2.png" /></a>
        <span>bob</span>
      </li>
    </ul>

Open in new window

Working sample here
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 41893682
Hi guys, thank you for your answers.
@SSupreme: your solution is really simple to apply to my existing code but I'm wondering if you already know the way to get the username aligned to the vertical center of the image when word-wrap is not applied
@Julian: your solution gets exactly the result I want: wonderful.

I'll split points because both soluutions are good indeed: I've just to choose which solution apply. A really comfortable situation :)
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
LVL 16

Expert Comment

by:SSupreme
ID: 41893920
https://jsfiddle.net/w61k58q3/3/
Not sure why Marco Gasi would want to use unordered list if he already has divs...

P.S. no need to split points
P.S.S. I believe that browser compatibillity isn't and issue anymore, we cannot wait until all of outdated browsers die out. If person uses IE6 he accepts that he wouldn't be able to get full experience of viewing webpages. That is why both of you have high end browsers, and to work for less than 1% of bots, scam browsers and some people isn't energy efficient.
0
 
LVL 62

Expert Comment

by:Julian Hansen
ID: 41893996
Unfortunately there are situations out there where older versions of IE are still in use - especially in corporations. It is not for us to decide whether or not it is relevant or not - but it is necessary to add the disclaimer so that should the target audience include browsers that are not capable of running the solution, the author is aware of this upfront.

When coding in html - it is advisable to use elements that match what it is you are doing. You can use a div as a submit button or a span as a link but it is not necessary to do so and by using the elements for the purpose they were designed helps to add clarity to the design of the document.
<div>'s and <dd><dl> elements are both valid options in this case - but at the end of the day the result is an unordered list of items. There is a certain amount of logic to using the <ul> in this case. That is not to say you can't use <div>'s - just that when reading the html when you see <ul> you know it is a list - as opposed to seeing a bunch of nested <div>'s

Just my 2c worth.
0
 
LVL 31

Author Closing Comment

by:Marco Gasi
ID: 41894021
Thank you, guys.
0
 
LVL 16

Expert Comment

by:SSupreme
ID: 41894112
There is no such corporations in Europe. Even in my poor country, it is impossible to find who use  at least IE9. But as a developer you can make it compatible for stones, but it is not worse it. Do you troubleshot each of your projects, whether it is compatible with each and every browser and version? I don't believe the answer is yes.
Julian, your solution is simple and genius:  vertical-align: middle; for avatar and  width:**px; for username. But redoing whole markup isn't genius, you make it look like that code wouldn't work without whole redo. "your solution is really simple to apply to my existing code", Julian's even more simple.
And I would be fine without assistant solution.
0
 
LVL 62

Expert Comment

by:Julian Hansen
ID: 41894113
You are welcome Marco,
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 41894155
@SSupreme: I splitted points because both solution in jsFiddle work fine, both solve my problem (even if now I'm having problem because evidently there is something soewhere which is preventing the Julian solution to work and I'll try your one).
I have choosen Juian answer as best solution because  his arguments about list markup convinced me but I thiink we have to award points to all answers which solve the question independently on which one we decide to apply to our code :)

I'll let you know if I'll get it working applying your solution - even if I'm still fighting to understand why the Julian list doesn't work once I put it in my app. Unfortunately, I can't give you a ink to a live page because I'm developing a Cordova mobile app...
0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Custom Android App Development is the need of an hour for several businesses. If you go through the right firm for Android Application Development, it can make a huge difference to the ways in which customers interact with your business.
Although a new technology, ReactJs offers multiple benefits to the website owners when it comes to creating interactive websites. Know what aspects make React Js one of the most popular frameworks for building websites.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question