I have a simple HTML structure (jsfiddle):
<li>
<div class="buttons">
<img src="done.png">
</div>
<div class="owners">
Даня Абрамов и Саша Васильев
</div>
<div class="text">
трали-вали трали-вали трали-вали трали-вали
</div>
</li>
buttons, owners and text have display: inline-block.
This looks fine when text is fairly small:
However, as the text grows, inline-block elements extend and eventually fall over the line:
This is ugly, and I would like to avoid that.
What I want to achieve instead is this:
When the text is too large to fit inside the element, I want it to be wrapped by lines.
I tried setting float: left on the elements, but couldn't get it working.
What's the proper way to do this with HTML and CSS (no tables)?
The exact result you desire can be achieved if you use floats instead of display: inline-block.
See: http://jsfiddle.net/thirtydot/CatuS/
li {
overflow: hidden;
}
.buttons, .owners {
float: left;
}
.text {
overflow: hidden;
padding-left: 4px;
}
You have to specify some max-width with percentage:
<li>
<div class="buttons" style="max-width:10%;">
<img src="done.png">
</div>
<div class="owners" style="max-width:30%;">
Даня Абрамов и Саша Васильев
</div>
<div class="text" style="max-width:60%;">
трали-вали трали-вали трали-вали трали-вали
</div>
</li>
<!-- 10+30+60 = 100% -->
There is a very nice flexbox solution if you have the browser support:
/* flexbox additions */
ul li {
display: flex;
}
.buttons {
flex-shrink: 0;
}
.owners {
flex-shrink: 0;
margin-right: 6px;
}
/* original CSS (inline-block is technically no longer necessary) */
.buttons {
display: inline-block;
}
.owners {
display: inline-block;
}
.text {
display: inline-block;
}
/* the rest is visual styling */
ul li {
line-height: 1.5em;
padding: 12px 8px 12px 8px;
margin-bottom: 12px;
margin-top: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
font-size: 15px;
background-color: #DBEAFF;
min-height: 23px;
}
.buttons {
min-width: 13px;
vertical-align: top;
margin-top: 3px;
margin-bottom: -3px;
}
.buttons a {
padding: 13px 9px 5px 9px;
}
<ul>
<li>
<div class="buttons">
<img src="http://clstr.org/static/images/tick.png">
</div>
<div class="owners">
<a>Даня Абрамов</a>
</div>
<div class="text">short text
</div>
</li>
<li>
<div class="buttons">
<img src="http://clstr.org/static/images/tick.png">
</div>
<div class="owners">
<a>Даня Абрамов</a>
</div>
<div class="text">longer text longer text longer text longer text longer text longer text longer text longer text longer text
</div>
</li>
<li>
<div class="buttons">
<img src="http://clstr.org/static/images/tick.png">
</div>
<div class="owners">
<a>Даня Абрамов</a> и <a>Саша Васильев</a>
</div>
<div class="text">
longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text longer text
longer text longer text longer text longer text longer text longer text
</div>
</li>
<li>
<div class="buttons">
<img src="http://clstr.org/static/images/tick.png">
</div>
<div class="owners">
<a>Даня Абрамов</a> и <a>Саша Васильев</a>
</div>
<div class="text">
трали-вали трали-вали трали-вали трали-вали
</div>
</li>
</ul>
I think you need to set max-width with different display mode.
li {overflow:hidden;}
li div { float:left; }
.button{ max-width: 10%;}
.owners{ max-width: 20%;}
.text{ max-width: 70%;}
See the new result here
BTW, if you use inline-block, the owners part won't stay on top.
I modified the code to fit your requirement. :)
FYI, li {overflow:hidden;} is a way to make a container to encompass its floated children.
Related
I want the long text written in Span should wrap and it should be justified. The text should not come under the Icon.
Following is the code I am using,
<style>
.widgets_div .text_div {
display: inline-block;
margin-left: 10px;
}
.widgets_div .icon_div {
display: inline-block;
margin-left: 15px;
}
</style>
<div class="widgets_div">
<div class="icon_div">
<span><i class="fa fa-calendar"></i></span>
</div>
<div class="text_div">
<span>Upcoming Events This Is A Very Long Text Which May Wrap Into The Browser According to the width of the Browser But I am Not Sure how much your browser width will be so I am keep on writing this Dummy text so that at some point of time this whole useless text will be wrapped and my purpose will be fulfill</span><br>
<span>Description</span>
</div>
</div>
Change you CSS Like
.widgets_div .text_div {
display: inline-block;
margin-left: 10px;
width:calc(100% - 60px);
vertical-align:middle;
}
.widgets_div .icon_div {
display: inline-block;
margin-left: 15px;
width:30px;
vertical-align:middle;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="widgets_div">
<div class="icon_div">
<span><i class="fa fa-calendar"></i></span>
</div>
<div class="text_div">
<span>Upcoming Events This Is A Very Long Text Which May Wrap Into The Browser According to the width of the Browser But I am Not Sure how much your browser width will be so I am keep on writing this Dummy text so that at some point of time this whole useless text will be wrapped and my purpose will be fulfill</span><br>
<span>Description</span>
</div>
</div>
Another solution without the vertical align,
.widgets_div .text_div {
display: inline-block;
margin-left: 25px;
margin-top: -20px
}
.widgets_div .icon_div {
display: inline-block;
margin-left: 15px;
}
<div class="widgets_div">
<div class="icon_div">
<span><i class="fa fa-calendar"></i></span>
<div class="text_div">
<span>Upcoming Events This Is A Very Long Text Which May Wrap Into The Browser According to the width of the Browser But I am Not Sure how much your browser width will be so I am keep on writing this Dummy text so that at some point of time this whole useless text will be wrapped and my purpose will be fulfill</span><br>
<span>Description</span>
</div>
</div>
</div>
i'm trying to create a news box where there will be a div containing an image on the left and a div containing lines of text on the right. Whenever i set them to inline-block the text goes right beneath the div image and stays there. I want them to be in a single line.
Here's my code, can you point out what i'm doing wrong?
EDIT: Corrected the TYPO but still...
<div id="newsBox">
<div id="newsImg">
<img src="images/news1.jpg" alt="news1">
</div>
<div class="metaNews">
<div id="newsTitle">
<h3>Text Text Text Text Text Text Text Text .</h3>
</div>
<div class="newsBrief">
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
</div>
</div>
<div style="clear:both"></div>
<div class="newsBy">
<span class="metaDate">Posted on 10/5/2017</span>
<span class="metaBy">By Li</span>
</div>
<div class="newsLink">
View →
</div>
<div style="clear:both"></div>
</div>
And the CSS:
#newsBox {
width: 85%;
margin: 0.5rem auto;
padding: 0.5rem;
border: #333 solid 1px;
background-color: #c2c2c2;
}
#newsImg {
display:inline-block;
}
#newsImg > img{
max-width: 120px;
max-height: 120px;
padding-right:20px;
vertical-align: middle;
}
.metaNews{
text-align:center;
display:inline-block;
}
#newsTitle {
}
.newsBy {
display:inline-block;
}
.newsLink {
float:right;
width: 6rem;
text-align: center;
background-color: #2F4F4F;
}
.newsLink a{
font-family: 'Play', sans-serif;
color: #fff;
text-decoration: none;
}
You got a typo in:
.metaNews{
text-align:center;
display:inline-blockl
}
inline-blockl has extra l instead of a ;
.metaNews{
text-align:center;
display:inline-block;
}
jsfiddle (you should stretch the result window a bit to let it have enough width to see the results you wanted )
Wrap the divs <div id="newsImg"> , <div class="metaNews"> using a div and set its display as flex.
#newsBox {
width: 85%;
margin: 0.5rem auto;
padding: 0.5rem;
border: #333 solid 1px;
background-color: #c2c2c2;
}
#newsImgWrapper {
display: flex;
}
#newsImg {
display: inline-block;
}
#newsImg>img {
max-width: 120px;
max-height: 120px;
padding-right: 20px;
vertical-align: middle;
}
.metaNews {
text-align: center;
display: inline-block;
}
#newsTitle h3{ margin:0;}
.newsBy {
display: inline-block;
}
.newsLink {
float: right;
width: 6rem;
text-align: center;
background-color: #2F4F4F;
}
.newsLink a {
font-family: 'Play', sans-serif;
color: #fff;
text-decoration: none;
}
<div id="newsBox">
<div id="newsImgWrapper">
<div id="newsImg">
<img src="images/news1.jpg" alt="news1">
</div>
<div class="metaNews">
<div id="newsTitle">
<h3>Text Text Text Text Text Text Text Text .</h3>
</div>
<div class="newsBrief">
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
</div>
</div>
</div>
<div style="clear:both"></div>
<div class="newsBy">
<span class="metaDate">Posted on 10/5/2017</span>
<span class="metaBy">By Li</span>
</div>
<div class="newsLink">
View →
</div>
<div style="clear:both"></div>
</div>
The problem is that you are using display: inline-block, Without use width, display: inline-block does not work without using setting width,
So I added these properties: width: 50% to #newsImg selector and to .metaNews,
Here a running example in jsFiddle
I have a code like this: https://jsfiddle.net/qchtngzf/1/
and now, when the browser is too narrow, the div.right (the text on the right) jumps onto a next line. I would like to, however, if the div.left (the text on the left) would get smaller and the text inside would break onto a next line and the div.right would stay in the same place instead.
I found a similar issue here: Making a div float left, but not "fall" if text is too long
but it's a little different and doesn't work for me even with some changes I tried.
Thank you.
You should set width for both of them right and left class:
html, body, header, nav, footer, div, p, ul, li, a {
margin: 0;
padding: 0;
text-decoration: none;
font-family: sans-serif;
font-size: 18px;
}
.left {
width: 80%;
float: left;
position: relative;
}
.right {
width: 10%;
}
.clearfix:after {
content: "";
display: block;
clear: both;
}
hr {
border: 0;
height: 1px;
background-color: #e9e9e9;
margin: 16px 0;
}
section.body {
max-width: 960px;
min-width: 272px;
}
div.left {
color: #1e344c;
float: left;
}
div.left span {
font-size: 14px;
color: #666666;
}
div.right {
float: right;
}
<section class="body">
<div class="item clearfix">
<div class="left">
text text text text text text text text text text text text text text
</div>
<div class="right">text </div>
</div>
<hr>
<div class="item clearfix">
<div class="left">
text text text text text text text text text text <br>
<span>text text text text text </span>
</div>
<div class="right">text </div>
</div>
<hr>
<div class="item clearfix">
<div class="left">
text text text text text text text text text
</div>
<div class="right">text </div>
</div>
<hr>
<div class="item clearfix">
<div class="left">
text text text text text <br>
<span>text text text text text text text text text text text text text text text </span>
</div>
<div class="right">text </div>
</div>
<hr>
<div class="item clearfix">
<div class="left">
text text text text text text text <br>
<span>text text text text text text text </span>
</div>
<div class="right">text </div>
</div>
<hr>
<div class="item clearfix">
<div class="left">
text text text text text text text text text <br>
<span>text text text text text text </span>
</div>
<div class="right">text </div>
</div>
</section>
You can use below css if you want right section not to wrap in next line and left section to get shorter.
html, body, header, nav, footer, div, p, ul, li, a {
margin: 0;
padding: 0;
text-decoration: none;
font-family: sans-serif;
font-size: 18px;
}
.clearfix:after {
content: "";
display: block;
clear: both;
}
hr {
border: 0;
height: 1px;
background-color: #e9e9e9;
margin: 16px 0;
}
section.body {
max-width: 960px;
min-width: 272px;
}
div.left {
color: #1e344c;
width
}
.item {
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
div.left span {
font-size: 14px;
color: #666666;
}
div.right {
white-space:nowrap;
}
1) Set width for both divs
2)set font size in "vh"
3) No need to set position:relative; of div.left;
div.left{
width:90%;
font-size:2vh;
float:left;
}
div.right{
width:10%;
float:right;
}
Actually, a combination of these answers helped. There is a problem with the one from Teuta (https://stackoverflow.com/a/38073935/6522717) that if the screen is too small and the content on the right is bigger than the set percentage, the text then breaks onto another line. This would be solved by another #media width or, as San posted (https://stackoverflow.com/a/38074311/6522717), by assigning white-space: nowrap to the div.right.
Thank you very much for the answers as they helped a lot.
I am trying to align two different divs next to each other using inline-block, but they are instead stacking like a blocked element. Specifically, they are the wrapper_image div containing an image, and the about_div containing some text information.
I have the following HTML:
<body>
<header>
... header information here
</header>
<div class="wrapper_image">
<img src="img/1935323_10153090821883239_4407778661294134622_n.jpg" class="profile-photo">
</div>
<div class="about_div">
<h3 id="about_me">About Me</h3>
<p id="about_me_info">
Text
</p>
<p id="about_me_info">
Some more text
</p>
</div>
<footer>
<p>
© Name
</p>
</footer>
</body>
And CSS:
.wrapper_image {
display: inline-block;
vertical-align: top;
}
.about_div {
display: inline-block;
vertical-align: top;
}
.profile-photo {
max-width: 350px;
border-radius: 100%; /* adds rounded corners to an element */
}
#about_me {
font-size: 2em;
}
#about_me_info {
font-size: 1.5em;
}
everything seems oks.
I put a demo picture and display in chrome and this is what looks like
The image and the text are inline-block and seems ok
I have a list of items and to the end of some items I want to add an icon (or several of them - it's dynamic). Items' container has a fixed width and if item's text size is too big - I'd like to add ellipsis.
So the problem is that icon is added next to text and if text is long - the icon moves off the container. The template for all items has to be the same.
Note: not all items have icons and some icons may have more than one icon.
Note2: javascript solution is not applicable, want to make it in pure CSS.
Actual:
Expected:
Any help is much appreciated.
body {
font-size: 20px;
}
.container {
width: 150px;
background: #ff0;
list-style: none;
padding: 0px;
margin: 0px;
}
.container li {
border-bottom: 1px solid #000;
}
.item {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.icon {
background: #0f0;
}
<ul class="container">
<li>
<div class="item">
<span class="text">Text 1</span>
<span class="icon">12</span>
</div>
</li>
<li>
<div class="item">
<span class="text">Text 2 Text 2</span>
<span class="icon">12</span>
</div>
</li>
<li>
<div class="item">
<span class="text">Text 3 Text 3 Text 3</span>
<span class="icon">12</span>
</div>
</li>
<li>
<div class="item">
<span class="text">Text 4 Text 4 Text 4</span>
<span class="icon"></span>
</div>
</li>
</ul>
If anyone still looking for solution I figured it out:
Markup:
<div class="block-wrap">
<div class="block">
<div class="icon"></div>
<div class="text">Text text text text text text text text text text text text text text text text text text text</div>
</div>
</div>
Styles:
.block-wrap {
display: inline-block;
max-width: 100%;
}
.block {
width: 100%;
}
.text {
display: block;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.icon {
float: right;
margin-left: 10px;
width: 20px;
height: 14px;
background-color: #333;
}
https://jsfiddle.net/rezed/n1qft37L/
Try like this: Demo
Fix the width for the text next to .item class and make display as inline-block to get floated with icon.
CSS:
.item {
width:100%;
overflow: hidden;
}
.item >span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width:70%;
display:inline-block;
}
.icon {
background: #f00;
display:inline-block;
max-width:50px;
}
Hope this helps :)