webta.st.ic
webta.st.ic

Reputation: 5179

How to wrap label with max-width

I've got this code:

HTML:

<div class="propertyContainer">
    <div class="property">
        <label>This one is a very long label</label>
        <select></select>
    </div>
    <div class="property">
        <label>Short label</label>
        <select></select>
    </div>
</div>

CSS:

.propertyContainer{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 0 0 450px;
    min-height: 30px;
}
.property {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-bottom: 10px;
    height: 30px;
}
label {
    height: 30px;
    line-height: 30px;
    vertical-align: middle;
}
select {
    width: 200px;
    height: 30px;
}

RESULT:

enter image description here

I've got the problem, when my label is to long, it doesn't wrap it. I tried to use a max-width: 150px; for my label, but than it just overlaps the other label without floating the container:

enter image description here

I have no idea how to fix this. I just would like to set a max-width an than wrap the label and float the conatiner so it should look like this:

enter image description here

I searched and tried a lot but at the moment I havn't got a solution. I also tried it with word-wrap: break-word;, but it also went wrong. Any ideas?

Upvotes: 3

Views: 4151

Answers (1)

Nenad Vracar
Nenad Vracar

Reputation: 122087

You just need to set display: flex on property and flex: 1 on select and label

.propertyContainer {
  border: 1px solid black;
  max-width: 450px;
  padding: 5px;
}
.property {
  display: flex;
  align-items: flex-start;
  padding: 5px;
}
label,
select {
  flex: 1;
}
<div class="propertyContainer">
  <div class="property">
    <label>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minim</label>
    <select></select>
  </div>
  <div class="property">
    <label>Short label</label>
    <select></select>
  </div>
</div>

Upvotes: 2

Related Questions