Is there a pure CSS way to create a div that matches another's div width? There is no parent div.
.green-div-1 {
background-color: #00ba00;
width: 500px;
height: 20px;
}
.blue-div-1 {
background-color: #8888FF;
max-width: 90%;
float: left;
}
.green-div-2 {
background-color: #00ba00;
width: 500px;
height: 20px;
}
.blue-div-2 {
background-color: #8888FF;
max-width: 90%;
float: left;
}
<div class="green-div-1">
<div class="blue-div-1">
<label>long text long text long text long text long text long text </label>
</div>
</div>
<br/>
<div class="green-div-2">
<div class="blue-div-2">
<label>short text</label>
</div>
</div>
Example: https://jsfiddle.net/6kgjnL15/
not sure if this is exactly what you are looking for. but changing max-width to just width then setting that at 100% makes both blue divs identical.
.blue-div-1{
background-color:#8888FF; width:100%; float:left;
}
.blue-div-2{
background-color:#8888FF; width:100%; float:left;
}
Using pseudo elements, you can get the desired result. You can find the code below. However, I highly recommend using JavaScript for your problem, as you can achieve a much more stable and flexible solution.
In case the background-color is not important, try this:
.green-div-1{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1{
max-width:90%; float:left;
}
.green-div-2{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1::after {
content:"short text";
display: block;
height: 20px;
margin-top: 20px;
}
<div class="green-div-1">
<div class="blue-div-1">
<label>long text long text long text long text long text long text </label>
</div>
</div>
<br/>
<div class="green-div-2">
</div>
else, you could add another element to cover the space in between:
.green-div-1{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1{
background-color:#8888FF; max-width:90%; float:left;
}
.green-div-2{
background-color:#00ba00; width:500px;height:20px;
}
.blue-div-1::after {
content:"short text";
display: block;
background-color:#8888FF;
height: 20px;
margin-top: 20px;
}
#space {
height: 20px;
background-color: white;
position: relative;
top: -40px;
}
<div class="green-div-1">
<div class="blue-div-1">
<label>long text long text long text long text long text long text </label>
</div>
</div>
<br/>
<div class="green-div-2">
</div>
<div id="space">
</div>
Actually there is no need of float: left; as they both are in separate div's
try removing the float: left from
.blue-div-1{
background-color:#8888FF; max-width:90%;
}
.blue-div-2{
background-color:#8888FF; max-width:90%;
}
here is the link: https://jsfiddle.net/s8ufxu1m/2/
Also this will help to understand more CSS Box Model: https://css-tricks.com/the-css-box-model/
Related
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 want to display two pieces of text side by side in columns using CSS. The left-hand column text is variable in length, and the right-hand text is fixed and always the same length.
I want the two "columns" to float to the left next to each other e.g.
[Variable Text] [Fixed text]
If the variable text is long I want it to wrap.
eg,
Here is a very long Hello World
piece of variable
text which wraps
My code works if the variable text is short, but I get unwanted whitespaces if the variable text wraps.
eg,
Here is a very long Hello World
piece of variable
text which wraps
Here is my code:
#wrapper {
margin-right: 100px;
}
#left-col {
float: left;
max-width: 100%;
background-color: #CCF;
}
#right-col {
float: left;
width: 100px;
margin-right: -100px;
background-color: #FFA;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="left-col">Here is a very long piece of text which wraps</div>
<div id="right-col">Hello World</div>
<div id="cleared"></div>
</div>
I believe changing #left-col's property max-width to other than 100% it will work. For example:
#wrapper {
margin-right: 100px;
}
#left-col {
float: left;
max-width: 200px;
background-color: #CCF;
}
#right-col {
float: left;
width: 100px;
background-color: #FFA;
}
#cleared {
clear: both;
}
Here is a jsfiddle with various text lengths for clarity.
you could maybe update your html to something more meaningfull and easier to style without class ou id. (a <dl> is not the most efficient but shows alternative tags easy to style)
calc() can be useful here :)
#wrapper {
width:15em;/* demo purpse */
}
dl,dt,dd {
padding:0;
margin:0;
}
dt {
float: left;
clear:left;
text-align:justify;/* to fill up entire first lines when text wraps*/
max-width: calc(100% - 100px);
background-color: #CCF;
margin-top:0.1em;
}
dd {float:left;
width: 100px;
background-color: #FFA;
}
<dl id="wrapper">
<dt>1 Here is a very long piece of text which wraps</dt>
<dd>Hello World 1</dd>
<dt>2 Here is a very long piece of text which</dt>
<dd>Hello World 2</dd>
<dt>3 Here is a very long piece of text</dt>
<dd>Hello World 3</dd>
<dt>4 Here is a very long piece</dt>
<dd>Hello World 4</dd>
<dt>5 short</dt>
<dd>5</dd>
</dl>
I want to build a container that contains an image on the left side and to its right there is supposed to some information about it, like a headline and some description.
I want the container to be able to expand between some minimum and maximum width dynamically. The images can also have different widths between two boundaries and if the container already has a maximum width, but the headline is longer, the headline should be shortened and there should appear some dots.
I found a way to shorten the headline, like here: http://jsfiddle.net/h0452569/
, but therefore I need to limit the width of the container next to the image. I tried this with the code below, but I can't find a way with CSS to dynamically limit the div width to not extend the container's div!
I would be very happy if anyone had an idea out there!
jsfiddle
HTML:
<div class="container">
<div class="image"><img src="https://farm6.staticflickr.com/5238/14184095861_d3787020c7_t.jpg" width="100" height="71" alt="alt_flickr-7"></div>
<div class="meta-container">
<div class="headline">Some very very very long headline</div>
<div class="description">Some description</div>
<div class="description">Other stuff</div>
</div>
<div style="clear:both"></div>
CSS:
.container {
min-width: 200px;
max-width: 250px;
max-height: 100px;
position: absolute;
border: 1px solid #666666;
white-space:nowrap;
}
.image {
float: left;
display: inline-block;
vertical-align: top;
}
.image img {
max-width: 100px;
max-height: 80px;
vertical-align: top;
}
.meta-container {
overflow: hidden;
text-overflow:ellipsis;
display: inline-block;
}
.headline {
width: 100%;
white-space:nowrap;
}
.description {
font-size:.8em;
}
In the example you refer to, those styles are added to the text element itself. In your design, the styles are given to the parent element.
Solution: add the styles to .headline instead of .meta-container.
.container {
min-width: 200px;
max-width: 250px;
max-height: 100px;
border: 1px solid #666666;
overflow: hidden;
}
.image {
float: left;
}
.image img {
max-width: 100px;
max-height: 80px;
vertical-align: top;
}
.headline {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.description {
font-size: .8em;
}
<div class="container">
<div class="image"><img src="https://farm6.staticflickr.com/5238/14184095861_d3787020c7_t.jpg" width="100" height="71" alt="alt_flickr-7"></div>
<div class="meta-container">
<div class="headline">Some very very very long headline</div>
<div class="description">Some description</div>
<div class="description">Other stuff</div>
</div>
</div>
<p>Next element</p>
In order to break the word use "word-wrap: break-word" in your .headline class, you also need to set a width (in px). For example:
.headline{
width:100px;
word-wrap: break-word;
}
I am running with a minor CSS problem. I want to have a dual lined text right aligned but they should be intended in a single line. No matter How long the text in the first line grows, the text "Small" should follow and indent itself to the left with the Long text. Both should be aligned to the right though.
ACTUAL
EXPECTED
My js fiddle here
<div>
<div id="blocker">
ABCD
</div>
<div id="wrapper">
<p id="inner">
<h3>A very very long text</h3>
<h3>Small</h3>
</p>
</div>
</div>
#blocker{
background-color: green;
float: left;
width: 30%;
height: 100px;
}
#wrapper{
text-align: right;
background: grey;
height: 100px;
}
#inner{
text-align: left;
}
just make these changes in your csss and html:
CSS:
#inner{
display: inline-block;
float: right;
text-align: left;
}
HTML:
<div id="inner">
<h3>A very very long text</h3>
<h3>Small</h3>
</div>
Demo: http://jsfiddle.net/ykmupf1r/6/
You need to put a containing div around the text in the right section like below;
<div class="txt">
<p id="inner"> </p>
<h3>A very very long text</h3>
<h3>Small</h3>
<p></p>
</div>
And add styles like below;
.txt {
float: right;
text-align: left;
width: auto;
}
There is no logical reason at all to put an <h3> inside a <p>. That's about as un-semantic as you can get. You should use tags to best suit your content. If you don't have any headers, don't use any header tags. Just don't ever put a header inside a paragraph.
Demo
css
#blocker{
background-color: green;
float: left;
width: 30%;
height: 100px;
}
#wrapper{
background: grey;
height: 100px;
}
#inner{
float: right;
text-align: left;
font-size: 18px;
font-weight: bold;
}
html
<div>
<div id="blocker">
ABCD
</div>
<div id="wrapper">
<p id="inner">
A very very long text
<br/>
Small
</p>
</div>
</div>
try this:
HTML
<div>
<div id="blocker">
ABCD
</div>
<div id="wrapper">
<p id="inner">
<h3>A very very long text</h3>
<h3>Small</h3>
</p>
</div>
</div>
CSS
#blocker{
background-color: green;
float: left;
width: 30%;
height: 100px;
}
#wrapper{
text-align: right;
background: grey;
height: 100px;
}
#wrapper > h3{ text-align:left}
Also you can add a new div for say the margin-left
UPDATE: The answers have got me close, but they still don't align vertically as the text div is larger, how can I make them both the same height and therefore align?
I would like to have two DIVs next to each other, one containing an image and one containing text, both sitting in a container DIV.
The image should be 15% of the width of the container div, with the text using the remaining 85%
The image and text should be aligned vertically within their respective DIVs, so it looks like they are aligned with each other.
I've tried to work this out but can't seem to do it! Can anyone help?
#picture {
float: left;
width: 15%;
line-height: auto;
}
#text {
width: auto;
padding-left: 16%;
line-height: auto;
vertical-align: middle;
margin-top: auto;
margin-bottom: auto;
}
#text p {
display: inline-block;
vertical-align: middle;
line-height: normal;
}
and
<div id="quotes">
<div id="picture">
<img style="width: 100%; vertical-align: middle" src="tom.jpg" >
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Here's a fiddle with your code in it: http://jsfiddle.net/hQ6Vw/1/
The only changes I made was to assign matching top/bottom margins to the img and p tags. I think that will give you the effect you're looking for.
If you use float and verticl-align, those two won'nt work together.
Float extract itself from regular flow and go slide on one side or the other on top of next line right after any content within the regular flow.
Vertical-align works:
in betweem inline-boxes (inline-block-level element or displayed so with display:inline-block;)
inside td or it's CSS default display : display:table-cell;
here jsfiddle #TXChetG updated
Using display:inline-block; http://jsfiddle.net/GCyrillus/hQ6Vw/2/
Using display:table/* table-cell*/;
http://jsfiddle.net/GCyrillus/hQ6Vw/3/
This should get you close:
<div>
<div style="background: grey; width: 15%; float:left"></div>
<div style="background: blue; width: 85%; float:left"></div>
</div>
Replace the grey background div with your image and the blue with your text.
Check this out
HTML:
<section>
<div id="one"></div>
<div id="two"></div>
</section>
CSS:
section {
width: 80%;
height: 200px;
background: aqua;
margin: auto;
padding: 10px;
}
div#one {
width: 15%;
height: 200px;
background: red;
float: left;
}
div#two {
margin-left: 15%;
height: 200px;
background: black;
}
Is this what you mean?
html
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
css
.container {
clear: both;
}
.images {
width: 15%;
float: left;
vertical-align: text-top;
}
.text {
width: 85%;
float: right;
vertical-align:text-top;
}
Why not just set the #text p display to display: inline or display:block; or use margins to align them?
<div id="quotes">
<div id="picture">
<img src="tom.jpg" />
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Display the container div as table and the text and image divs as table-cell to make them the same heights. You can then centre the image vertically through vertical-align:middle.
#quotes {
display:table;
}
#picture {
width: 15%;
display:table-cell;
vertical-align: middle;
}
#text {
display:table-cell;
width:85%;
padding-left: 16%;
}
#picture img {
width: 100%;
}
http://jsfiddle.net/X3WsV/1/