2

Here's the mockup of what I want to achieve(3 cases of the same mockup): mockup

So, first div(green) has fixed width. Third div(blue) is always at the right side and it gets as much space as it needs, considering text in it. Second div(orange) takes all remaining space and if text in this orange div doesn't fit, it gets ellipsis style at the end.

Please help me to achieve this(IE8-IE10, Chrome support), I've tried to put first elements into container, tried using float, inline-block, but there are still problems.

Thank you, here's jsfiddle, so you can quickly work with it: http://jsfiddle.net/0tLvyf20/4/

<div class="container">
    <div class="checkbox"></div>
    <div class="name">Some text</div>
    <div class="value">12345</div>
</div>
Aremyst
  • 1,480
  • 2
  • 19
  • 33
  • 2
    What kind of browser support do you need? Also, that sounds like a table to me. What content is there inside of it? – Madara's Ghost Aug 16 '14 at 07:31
  • Chrome, IE8-IE10, Safari. Content is very simple: image for green div, text for orange and text for blue. – Aremyst Aug 16 '14 at 07:34
  • See here: http://stackoverflow.com/questions/773517/style-input-element-to-fill-remaining-width-of-its-container – alexreardon Aug 16 '14 at 07:35
  • @JoeSmith This, indeed, seems to be the case for a table. Do not get confused by whole "table design is bad, you should use divs" - this is not an advice to _always_ follow. In the layouts where you need your view to look like table - use table. – mishik Aug 16 '14 at 07:36
  • @alexreardon, that's not related to my question. There are only 2 divs. – Aremyst Aug 16 '14 at 07:37
  • @mishik, thank you, I'll try that. If it's not hard for you, can you please make changes to JSfiddle I provided to use table like you're suggesting? Thanks. – Aremyst Aug 16 '14 at 07:38

4 Answers4

1

With Flexbox

* {
    box-sizing: border-box;
}
.container {
    display: flex;
    width: 300px;
    height: 30px;
    background-color: #B7B7B7;
    padding: 2.5px; /* This is the remaining gap from the edges */
}

.container div {
    margin: 5px; /* This is for the gap between elements. */
}

.checkbox {
    width: 15px;
    background-color: green;
}

.name {
    flex: 1;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    background-color: orange;
}

.value {
    background-color: LightCyan;
}

With a table (Things get a bit tricky)

HTML

<table class="container">
    <tr>
        <td class="checkbox"></td>
        <td class="name">
            <!-- Necessary Evil -->
            <table>
                <tr>
                    <td>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit eaque autem quos nulla dignissimos corporis pariatur itaque excepturi debitis ratione dolorem iste consectetur accusantium necessitatibus blanditiis labore unde perferendis tempore.</td>
                </tr>
            </table>
        </td>
        <td class="value">12345</td>
    </tr>
</table>

CSS

.container {
    width: 300px;
    height: 30px;
    background-color: #B7B7B7;
    /table-layout: fixed;
    border-spacing: 5px;
}
.checkbox {
    width: 20px;
    background-color: green;
}
.name td {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    background-color: orange;
}
.name table {
    table-layout: fixed;
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    border: 0;
}
.value {
    background-color: LightCyan;
}
Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Thanks. In IE10 it's not as expected :( Please, see screenshot: https://copy.com/H4xL0wT2aX8G79YV – Aremyst Aug 16 '14 at 07:46
  • Yes, I posted this before seeing your IE10 comment. I'm working on a table based solution now. – Madara's Ghost Aug 16 '14 at 07:47
  • 2
    Also @JoeSmith, flexbox does work with IE10 I just didn't bother with the vendor prefixes. It most certainly won't work in IE *NEIN* and below. http://caniuse.com/#search=flexbox – Madara's Ghost Aug 16 '14 at 07:53
  • yeah, yeah, I see that IE8-9 are not supported. I'm not a fan of IEs myself, but have to support them. Thank you for your time, eager to see your table solution. I'm trying to rework to table too. – Aremyst Aug 16 '14 at 07:56
  • 1
    @JoeSmith Added table – Madara's Ghost Aug 16 '14 at 08:23
  • Although I still wholeheartedly recommend that you didn't support IE9 and below. PCs today are shipped with IE11 by defaut, and supporting IE8 means technical baggage that will get out of hand eventually. Check the browser usage statistics in your area and make an educated decision. Don't arbitrarily decide that you have to support IE. And if the client still wants it, charge more. – Madara's Ghost Aug 16 '14 at 08:26
  • Thanks, Second Rikudo. What do you think(and guys) about such solution: http://jsfiddle.net/3bumk/76/ ? Seems to be working... Regarding support - I can't say "I won't support IEs" to my product owner. At least not yet :) – Aremyst Aug 16 '14 at 08:27
  • @JoeSmith Frankly, it looks horrible. Not aligned, full of magical numbers. – Madara's Ghost Aug 16 '14 at 08:28
  • Yeah, regarding margins and top it's horrible, but that's easy to fix and not the main part. It works as in mockup I mean. Anyway, thank you very much, I'll take a look at your table solution and accept it in case everything is ok. Thanks again. – Aremyst Aug 16 '14 at 08:30
  • Here's more cleaned up version without table, but it still uses margin and top. Every solution has some "evil" in it. http://jsfiddle.net/3bumk/77/ – Aremyst Aug 16 '14 at 08:35
1

Demo

css only solution using flex-box

enter image description here

css

.container {
    width: 300px;
    height: 30px;
    background-color: #B7B7B7;
    display: flex;
    flex-wrap: wrap;
    align-items: center
}

.checkbox {
    width: 15px;
    height: 15px;
    margin: 5px;
    background-color: green;
}

.name {
    height: 20px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    background-color: orange;
    flex: 1;
}

.value {
    height: 20px;
    background-color: LightCyan;
    margin-right: 5px;
}
4dgaurav
  • 11,360
  • 4
  • 32
  • 59
  • Thank you. That works great in Chrome, but IE8-IE9 don't support flex style :( I need to support IE8-IE10 unfortunately. – Aremyst Aug 16 '14 at 08:10
1

And one without tables/flexbox.

Slightly different HTML structure, largely same css.

<div class="container">
    <div class="checkbox"></div>
    <div class="value">12345</div>
    <div class='expander'>
       <div class="name">Some text</div>
    </div>
</div>
<div class="container">
    <div class="checkbox"></div>
    <div class="value">123456789</div>
    <div class='expander'>
       <div class="name">Some long text that won't overlap</div>
    </div>
</div>
<div class="container">
    <div class="checkbox"></div>
    <div class="value">12354354354345</div>
    <div class='expander'>
       <div class="name">Some very long text that should elipse text</div>
    </div>
</div>

with the following CSS

.container {
    display: block;
    width: 300px;
    height: 30px;
    background-color: #B7B7B7;
    overflow: hidden;
}

.checkbox {
    display: inline-block;
    margin-top: 5px;
    width: 15px;
    height: 15px;
    background-color: green;
    float: left;
}

.expander {
    background-color: orange;
    overflow: hidden;
    display: block;
}

.name {
    display: inline-block;
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 100%;
    overflow: hidden;
}

.value {
    display: inline-block;
    background-color: LightCyan;
    float: right;
}

and a fiddle to see it (tested this only in chrome, IE 11)

Marvin Smit
  • 4,088
  • 1
  • 22
  • 21
  • Thank you. That works perfectly in IE9-IE10 too. It's a bit similar to what I provided in jsfiddle in one of my comments to "Second Rikudo" answer. If I could accept 2 answers, I would accept yours too. Thanks! – Aremyst Aug 16 '14 at 08:53
0

With a diffrent structure;

<div class="container">
    <div class="checkbox"></div>
    <div class="namevalue">
       Some Text
        <div class="value">1234</div>

    </div>

</div>

*{
    box-sizing:border-box;
}
.container {
    width: 300px;
    height: 30px;
    background-color: #B7B7B7;
}

.checkbox {
    display: inline-block;
    position: relative;
    width: 15px;
    height: 15px;
    margin: 5px 3px 5px 5px;
    top: 3px;
    background-color: green;
    float:left;
}
.namevalue {
    display: inline-block;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    background-color: orange;
    width:275px;
top: 7px;
     float:left;

}
.name {
    display: inline-block;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    background-color: orange;
}
.value {
    display: inline-block;
    position: relative;
    background-color: LightCyan;
    float:right;
    border-left:5px solid #B7B7B7;
}
xuma
  • 53
  • 7
  • Sorry, it doesn't work if you put text that is overlapping. Please, see this screenshot: https://copy.com/Vkx2IJGrg3FiYzaL – Aremyst Aug 16 '14 at 07:48
  • 1
    you can try this one but hard to set dots at the end of the string with old browser support.http://jsfiddle.net/xuma/nvxd20eL/ – xuma Aug 16 '14 at 08:01
  • Thanks, but I kind of need the dots(ellipsis) :) – Aremyst Aug 16 '14 at 08:03
  • Haha, cool. But I intend to make it without JS if possible. You deserve a medal for that :D Thank you, pretty fun solution, thanks for you time. – Aremyst Aug 16 '14 at 08:37