Reputation: 3866
I have a parent div which has two div inside one is image and one is description like below snapshot
The parent div has propertytext-align: center
. so its working fine and text image and description is in center.
the description div has a propertymax-width: 580px
. so if the description text increases, the image will slightly move to the left as below snapshot
And it looks good. But when there is some long word which does not fit in the current line it uses the whole 580px width
and long word goes into the next line, like below
since the whole description width is used so the image shifts to the left by using the parent property text-align: center
.
but it does not looks good since its in the center by css logic but not by looks.
Is there a way to take the whole width only when there is some content.
I have used property display: table-caption
and now it takes only the text width
and its in the center of the parent div.
but then it splits the Assistant Manager
into two line like below which i dont want
Is there a way to implement the above task by some css property, or i have to use javascript to implement the above task.
Upvotes: 0
Views: 62
Reputation: 2021
Since you did not provide us with any code, I only could guess.
This is what I came up with: https://jsfiddle.net/9888d4tf/1/
HTML
<div class="headline-wrapper">
<img class="headline-image" src="http://kcdn.at/company/84085/1073352/logo-fairmoney-clever-consulting-gmbh.companybig.gif">
<p class="headline-title">
<span>
Assistant Manager someveryverylonglongtexttext
</span>
</p>
</div>
<br/><br/>
<div class="headline-wrapper">
<img class="headline-image" src="http://kcdn.at/company/84085/1073352/logo-fairmoney-clever-consulting-gmbh.companybig.gif">
<p class="headline-title">
<span>
Assistant Manager
</span>
</p>
</div>
CSS
.headline-wrapper {
background: #fff;
text-align: center;
display: flex;
flex-wrap: nowrap;
justify-content: center;
}
.headline-wrapper > .headline-image {
width: 298px;
height: 92px;
align-self: center;
}
.headline-wrapper > .headline-title {
font: 30px/1.1 Verdana;
display: inline-block;
text-align: left;
}
.headline-wrapper > .headline-title > span {
display: table-caption;
}
The key is, that you previously need to know which words you don't want to wrap. You then either wrap them in a white-space:nowrap
span or replace the spaces in between with
as I did.
Upvotes: 1
Reputation: 149
You can set relative width for description div with percentage(like width: 50%
). I think it would look good based on your images.
Upvotes: 0