Solved

Element alignment and word wrapping

Posted on 2016-11-18
9
25 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 250 total points
ID: 41893374
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 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 30

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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

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 30

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 51

Expert Comment

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now