Advice regarding wrapping text inside a div - html

I'm creating a contact card style layout, with a photo and text next to it, as demonstrated in this fiddle here:
http://jsfiddle.net/L7pWv/5/
HTML:
<div class="container">
<div class="contact-card">
<div class="photo"></div>
<div class="details">
<span class="name">My Name</span>
<span class="description">This is some really long text that should wrap nicely when things all work OK</span>
</div>
<div class="clearfix"></div>
</div>
<div class="contact-card">
<div class="photo"></div>
<div class="details">
<span class="name">My Name 2</span>
<span class="description">Short description</span>
</div>
<div class="clearfix"></div>
</div>
</div>
CSS:
.container {
width: 350px;
}
.contact-card {
background-color: whitesmoke;
border-bottom: 1px solid #ddd;
white-space: nowrap;
}
.contact-card .photo {
float: left;
width: 80px;
height: 50px;
background-color: tan;
margin: 10px;
}
.contact-card .details {
display: inline-block;
margin: 10px 0;
}
.contact-card .name {
display: block;
font-weight: bold;
line-height: 1em;
}
.contact-card .description {
display: block;
font-size: 0.8em;
color: silver;
line-height: 1em;
white-space: normal;
}
.clearfix {
clear: both;
}
As you can see from running the fiddle, when the text is really long, it does wrap eventually, based upon my white-space setting, but it exceeds the size of the contact card before doing so. I could put a right margin of 90px on the "description" class to keep the text within the bounds (which works), but I can't help but feel this is wrong. I'd like it to naturally want to stay within its parent's bounds, but can't think of the best way to achieve that. Any ideas?

Consider making these changes:
.contact-card {
display: inline-block;
}
.contact-card .details {
display: block;
}
This will keep each card displaying inline while keeping the text of the card inside the block without specifying a margin.

Kind of a tricky one, as I don't know what uses you'll be putting this in, but I'd probably do it with these changes.
Get rid of
<div class="clearfix"></div>
It's not needed if you make a simple addition like:
.contact-card {
float:left;
}
Then change .contact-card .details to this:
.contact-card .details {
padding: 10px 0;
}
That should give you the "The width of the details element should really be dictated by the parent." behaviour you're after
http://jsfiddle.net/L7pWv/6/

I suggest just don't use inline-block for this. You don't want the .detail element overflow on it's parent element. Because you already floated your photo, you can just place the element next to the photo element.
Note that you should use padding when you want space inside the element and use margin when you want it outside of the element.
There is no need for white-space: nowrap; as you floated the photo.
jsFiddle
The only thing i changed is the use of padding and margin and removed the white-space .

CSS:
.contact-card .details {
display: inline-block;
margin: 10px 0;
width:70%;
}
Fiddle: http://jsfiddle.net/L7pWv/2/

Related

Display flex and wrapping a text in span tag gives unwanted spacing between words

I have set display flex for parent element because I need pseudo element and the text be next to each other. But I also put part of the text inside span tag to make it more light, but in that case, display flex also distribute spacing between my words outside span tag. Here you see:
Instead I need in products and services be displayed like a normal sentence because it's part of the same p tag div, it's just that the first few words are wrapped in span tag too.
The code:
#page-sub-header p {
margin-left: 0;
font-size: 40px;
font-weight: 400;
text-align: left;
line-height: 1.2;
padding-bottom: 60px;
display: flex;
}
#page-sub-header p:before {
content: "";
display: block;
width: 5px;
height: 150px;
background-image: url(rectangle.jpg);
margin-right: 15px;
float: left;
}
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="sub_container">
<p class="header_tag">
<span>Maintaining only the highest standards of quality</span> in products and services.
</p>
</div>
</div>
</div>
</div>
Two things is going on here:
the rightmost, unwrapped, text becomes an anonymous flex item, which is kind of equal to have it wrapped (which I did in below sample to show how it all behaves).
the space you get is because when the inner left span wraps its text, the span itself aren't aware of that and won't adjust its width.
This is the box models default behavior, and happens whether you use Flexbox or not.
So either you need a script to calculate and adjust the left span's width, or use a media query.
As a note, the display: block, and the edited float: left, has no impact since the pseudo is a flex item, also, the float: left can actually cause issues as well on some browsers.
Stack snippet - with added borders so you can see how it behaves when resize the browsers width.
p {
margin-left: 0;
font-size: 40px;
font-weight: 400;
text-align: left;
line-height: 1.2;
padding-bottom: 60px;
display: flex;
}
p span {
border: 1px solid red;
}
p:before {
content: "";
/*display: block; does not apply on flex item's */
width: 5px;
height: 150px;
background: red;
margin-right: 15px;
}
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="sub_container">
<p class="header_tag">
<span>Maintaining only the highest standards of quality</span> <span>in products and services.</span>
</p>
</div>
</div>
</div>
</div>

Perfect way to align div next to image

I have two divs. First one has a image file and the other one has a username. I just want to align the second div vertically center just like this way.
But this one is not aligned perfectly. This screen already coded but the problem is that I still couldn't figure out the perfect way to align middle the user name div. I just use my naked eye and adjust padding.
Here is the my code
.tag-header {
padding: 12px;
overflow: auto;
}
.tag-header .tag-header-img {
height: 55px;
width: 55px;
border-radius: 50%;
float: left;
}
.tag-header .info {
padding: 14px 11px;
display: inline-block;
font-size: 1.3rem;
line-height: 14px;
}
.tag-header .info p {
margin: 0;
font-weight: 600;
line-height: 1;
font-size: 1.3rem;
}
.tag-header .time {
display: block;
font-size: 1.2rem;
}
.info span {
font-weight: 300;
color: #b9b9b9;
}
<div class="tag-header">
<div class="col-md-6">
<div class="row">
<img class="tag-header-img" src="http://blog.couchbase.com/binaries/content/gallery/speakers/kirkk.jpg" alt="">
<div class="info">
<p>John Stevens</p>
<span class="time">2 minutes ago</span>
</div>
</div>
</div>
fiddle
Any solution?
Heres your fiddle updated https://jsfiddle.net/p4x7d3fq/5/ -- i added borders just so you can see.
Using display:table-cell you can achieve this, if you don't mind the slight changes, including the addition of a height to match that of your image.
You seem to have done an ok job making them both have the same height, the image doesn't have to use float: left;, you can also use display: inline-block; on it and vertical-align: middle; on both the image and the name, this way you don't need them to have the same height.
Also, make sure you use Bootstrap properly, you first need a "container" div, in the container you put a row, and in that row you put columns.
You only put a div with a row class in a column div if you want more columns in that column.
<div class="container">
<div class="row">
<div class="col-md-12">
</div>
</div>
</div>
Try to use flexbox:
.info {
display: flex;
}

Placing an unknown width element on the right of a centered element without moving it

I have a text element centered with text-align: center;, I want to place another element (a small inline <span>) to the right of it without affecting its position.
Neither of the elements (and especially the span) have a known size, so I can't use an offsetting margin on the left of the text element. Is there a way to do that in pure CSS?
Obligatory code that doesn't work:
<div style="text-align: center;">
<h3 id="centered-text">My centered text</h3>
<span class="to-the-right" style="background-color: blue;">BADGE</span>
</div>
how's this?
#centered-text {
display: inline-block;
}
#to-the-right {
display: inline-block;
position: absolute;
margin-left: 4px;
}
<div style="text-align: center;">
<div id="centered-text">My centered text</div>
<div id="to-the-right" style="background-color: blue;">BADGE</div>
</div>
I made your H3 not an H3 because it made the BADGE appear weirdly high above the title, but that could be easily corrected by giving the BADGE an attribute like "top: 10px;"
If you can put the h3 and the span inside a wrapper, you can center that wrapper, and position the span outside the wrapper using absolute positioning.
This may be a bit tricky if the h3 is full page width (the span will be outside of the visible area), or if the span contains a longer text (it may wrap awkwardly). However, it's a start, and those issues may not be issues to you.
.wrapper {
display: inline-block;
position: relative;
}
h3 {
display: inline-block;
margin: 0;
}
.wrapper span {
display: block;
position: absolute;
left: 100%;
top: 0;
}
<div style="text-align: center;">
<div class="wrapper">
<h3 id="centered-text">My centered text</h3>
<span class="to-the-right" style="background-color: blue;">BADGE</span>
</div>
</div>
Your css is off. Give your div an id and then
#divid {margin-left:auto; margin-right:auto; width:100%;}
h3 {text-align:center;}
A way to do this without using positioning is to use display: flex and a pseudo element. I personally think that oxguy3's way is better, but if you want to stay away from positioning then this will also do the trick.
span {
background: blue;
}
div {
display: flex;
justify-content: center;
}
div:before, span {
content: "";
flex: 1 1;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
<div>
<h3>My Centered Text</h3>
<span>BADGE</span>
</div>
This does have a few issues that may or may not matter based on your needs. If any other elements are needed in the div, then some reconfiguration is necessary, because any new elements also become flex-items and mess with the centering of the h3.
Also, as you may notice the span now extends to the edge of the div. If this is not a problem, then the markup is fine as is, but if it is a problem then wrapping it in another element also fixes this problem, like so:
span {
background: blue;
}
.container {
display: flex;
justify-content: center;
margin-bottom: 5%;
}
.container:before,
.badge {
content: "";
flex: 1 1;
}
* {
margin: 0;
padding: 0;
}
<div class="container">
<h3>My Centered Text</h3>
<div class="badge"><span>BADGE</span></div>
</div>
It's not perfect and, as I said, I like oxguy3's answer better, but if you want to stay away from positioning, then I think this is a good alternative.
Here's a codepen with both examples.

CSS float is not working

After thoroughly researching for the way to fix this I have still not found the answer I seek. I finally decide to post my problem on stackoverflow.com because I finally give up trying to find the answer. What I get as a result is two boxes with content on top and one box on the bottom.
Here is the CSS code:
#content_area
{
overflow: hidden;
display: inline-block;
background: white;
margin-top: 20px;
margin-right: 110px;
margin-left: 110px;
margin-bottom: 20px;
}
.box
{
display:inline-block;
width: 33.33%;
float: left;
background: #FFFFFF;
padding: 15px;
}
Here is the HTML Code:
<div>
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
</div>
The problem is your padding, as mentioned above.
Here is a fiddle with the padding removed and colours added: http://jsfiddle.net/gj0wmgym
.box
{
display:inline-block;
width: 33.33%;
float: left;
background: #FFFFFF;
}
The problem with your code is that your .box class assigns a width of 33%, with additional padding. This leads to a total width of more than 100%. Padding is added to the .box's initial width because that's how the default box model works in CSS.
To fix this problem, add this line to the .box's style declarations:
box-sizing: border-box;
You can see a live demo here. If you want to learn more about the box model, this article by Chris Coyier is an excellent reference.
From what I can tell, your floats are working correctly.
Your html was missing the id attribute, so make sure to add that to your html.
What you are probably expecting is for the floats to not wrap to the next line, which is because the padding gets added to the width size (the elements are greater than 33%). You instead need to set the box sizing attribute see this article
* {
box-sizing:border-box;
}
#content_area
{
overflow: hidden;
display: inline-block;
background: white;
margin-top: 20px;
margin-right: 110px;
margin-left: 110px;
margin-bottom: 20px;
}
.box
{
display:inline-block;
width: 33.33%;
float: left;
background: #FFFFFF;
padding: 15px;
}
<div id="content_area">
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
<div class="box">
//enter text here
</div>
</div>

Align H1 Header and Normal Text in Same Line

I'm trying to have a H1 header and regular text on the same line, with a line under it, like so:
I have tried below code, but have been unsuccessful. What am I doing wrong?
<div style="border-bottom:1px;">
<div align="left"><h1>Header</h1></div>
<div align="right">Regular Text Goes Here</div>
</div>
Original answer (still working just fine)
See the snippet below. The idea is to make the <h1> inline to allow the second text to be at the same line.
header { border-bottom: 1px solid #000; }
header > h1 { display: inline-block; }
header span { margin-left: 100px; }
<header>
<h1>Text</h1>
<span>text2</span>
</header>
2020 Update
See the snippet the snippet below that makes use of Flexbox. So instead of setting the h1 to an inline-block, you can make the header a flex container. A flex container will (by default) layout its children on a horizontal axis.
Note that you also need align-items: center to keep the h1 and span on the same vertical axis. Also, note that you might want align-items: baseline if you want the texts to appear on the same baseline (like my original answer).
header {
display: flex;
align-items: center;
/* Remove the next line if you want the span to appear next to the h1 */
justify-content: space-between;
border-bottom: 1px solid #000;
padding: 10px 30px;
}
<header>
<h1>Text</h1>
<span>at the end</span>
</header>
I came up with a simple solution. My requirements are slightly different in that I want my status right aligned.
.my-header h2 {
display: inline;
}
.my-header span {
float: right;
}
<div class="my-header">
<h2>Title</h2>
<span>Status</span>
</div>
<div style="clear:both"></div>
Add this line border-bottom:1px solid #000
<div style="border-bottom:1px solid #000;">
<div align="left"><h1>Header</h1></div>
<div align="right">Regular Text Goes Here</div>
</div>
DEMO
Use class name instead of inline-style.
Try
<div style="float:left;"><h1>Header</h1></div>
<div style="float:right;">Regular Text Goes Here</div>
instead?
There are two methods to accomplish H1 and TEXT inline. To clarify, TXT is in an element container. You suggest DIV, but any symantic element will do. Below, h1 and p illustrate a common use, while showing that you need not hide from element blocking, using DIV's (though divs are pandemic for many javascript coders).
Method 1
.inline { display: inline; float: left; padding-right: 2rem; }
<h5 class="inline">Element a's link family...</h5>
<p class="inline">
Method 2
h5 { display: inline-block; float: left; font-size: 1rem; line-height: 1rem; margin-right: 2rem; }
h5>p { display: inline-block; float: right; }
<h5>Title</h5>
<p>Paragraph</p>
I think you should write like this :-
HTML
<div style="border-bottom:1px solid black; overflow:hidden;">
<h1>Header</h1>
<div class="right">Regular Text Goes Here</div>
</div>
CSS
h1 {
float:left;
margin:0px;
padding:0;
}
.right {
float:right;
margin:0px;
padding:0px;
}
DEMO
EVEN YOU CAN USE THIS METHOD ALSO WITH MINIMIZED MARKUP :- DEMO