I know this is easy with Javascript but... is there any way with just CSS?
Let's say we have two elements (green and red) within a parent one (beige). The red element should be always to the right of the green one, except if the green one (because of the content) is too big to fit the parent in which case the red one will be over the green one (the normal behaviour would be the red element staying to the right of the green one and therefore being hidden because of the overflow of the parent)
In other words: red.x = min(green.x + green.w, beige.x+beige.w-red.w)
For more info, here's the concrete HTML:
<div class="beige" style="width:250px"> <!-- parent with a given width (unknown until the page is rendered) & overflow hidden -->
<a class="green"> <!-- link with display:inline -->
content
<em class="red"></em> <!-- actually a button, 15 px width -->
</a>
</div>
EDIT: #kyledws answer is awesome but I'll update the question with more info (needed things) such as:
red is only displayed when green:hover (that's why it's inside green)
you don't know beige width in CSS (in the real world beige is inside a with defined width but not known until the page is rendered)
green content is a variable length text, and the reason of red being pushed
if the green content does not fit into the parent, it should show the ellipsis (text-overflow: ellipsis; overflow: hidden)
must work in IE8+
If you're able to wrap the content inside <a> in a span then try this.
HTML
<div class="beige">
<a class="green" href="#">
<span class="content">This is some text.</span><em class="red"></em>
</a>
</div>
CSS
.beige {
background-color: #EBDFA0;
height: 32px;
overflow: hidden;
border: 4px solid #EBDFA0;
white-space: nowrap;
width: 400px;
}
.green {
background-color: #4CA73D;
color: #222;
display: inline-block;
height: 100%;
text-decoration: underline;
vertical-align: middle;
}
.content {
display: inline-block;
height: 100%;
margin-left: 4px;
position: relative;
width: 100%;
max-width: 364px;
top: -50%;
}
.red {
border: 2px solid red;
display: inline-block;
height: 28px;
position: absolute;
width: 28px;
}
Essentially the <a class="green">, <span class="content"> and <em class="red"> need to be display: inline-block and the <span class="content"> has to be width: 100% with a max-width of the different between the <div class="beige"> and the <em class="red" minus any additional padding/margin/border etc. (So in this example, max-width: 364px) By setting a width on the span you force the em outside of it's container but by setting a max-width you stop the em from flowing outside of the main wrapper.
Here is a codepen.io link to an example.
(Note: Most of the CSS above is just to make the example look like your images.)
UPDATE:
To show or hide the <em class="red"> add the :hover pseudo-class to .beige and visibility or opacity to .red. (Use opacity if you want to use a transition.)
.red {
opacity: 0;
}
.beige:hover .red {
opacity: 1;
}
Because the width of <div class="beige"> is unknown you can't use CSS to set max-width on <span class="content">.
(The 100% in max-width: calc(100% - 28px) is width of <a class="green"> not <div class="beige">. I couldn't hack it with pseudo-elements, positioning, floats or different display types like flex either.)
The way around this is to fix the max-width of the <span class="content"> in CSS (as is shown above) or use Javascript to detect the width of <div class="beige"> and then set the max-width.
content.style.maxWidth = beige.clientWidth - red.clientWidth + "px";
I updated the example with visibility and Javascript versions.
Also, I added position: absolute to .red so <span class="content"> doesn't have empty space on the right.
So I found a way that you can handle this in just CSS. I have setup a jsbin with a example. I don't have a fancy slider so you will need to use the inspector tool to resize the width or do it manually.
Basically I set up a dive as a table. Because you can't drop to different rows the right most column is forced to collapse but leaves the block visible because it is position absolute. The size of the container is based on the block which has been changed to a inline block to maintain the coloration and push its parent to become larger. Excuse me not making the styles match exactly what you had int he graphics.
http://jsfiddle.net/93u5E/3/
HTML
<div class="beige">
<div class="table">
<!-- parent with a given width & overflow hidden -->
<ul>
<li><a class="green"> <!-- link with display:inline -->
content
</a>
</li>
<li><em class="red"></em>
<!-- actually a button, 15 px width -->
</li>
</ul>
</div>
</div>
CSS
.beige {
background-color:grey;
overflow:hidden;
white-space:no-wrap;
}
.table{
display:table;
}
ul {
display:table-row;
list-style-type:none;
}
li {
display:table-cell;
position:relative;
}
li:last-child{
width:30px;
}
.green {
display:inline-block;
background-color:green;
}
.red {
border:3px solid red;
display:inline-block;
width:20px;
position:absolute;
right:0px
}
li:hover + li .red{
height:20px;
}
*Updating to include hover
Related
I'm using a parent div which is a flexbox. inside it, i require two child divs, each spanning the complete width of the parent div (width:100%) such that they are on top of each other (overlayed). This way, when you change the width of one child div, it shouldn't re-position the other child div in any way.
for example: i'm trying to create an progress bar of sorts like below:
for this, i'm using two divs - the outer div (grey background) which renders the progress bar outline, and inner div (green background) which will show the progress. I also require to show the numerical percentage in the middle (81%). the inner div (green bg) width value will wary according to the numerical percentage. The numerical percentage should always be at the center of the OUTER DIV regardless of the progress (inner div width).
Could you please tell me how this can be achieved? Thanks.
For styling a basic progress bar, you'd make use of position: absolute to overlay items.
You could make use of flexbox for positioning purposes, however you'd use less lines of code by just using position: absolute, and subsequent rules, on the middle text (e.g. "80%"), enabling both the overlaying need and centering at the same time.
For controlling the progress bar (dynamically), you'd use Javascript. You'd build some logic in JS that follows the progress of something, and then update the CSS rules of the bar accordingly. There's no way of doing this without JS.
If you just want a dummy progress bar with motion, then you could animate it without JS, by using CSS animation rules.
Codepen
#container {
position: relative;
width: 400px;
height: 40px;
background-color: grey;
border: 2px solid black;
}
#text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
#fill {
background-color: green;
width: 80%;
height: 100%;
}
<div id="container">
<div id="text">80%</div>
<div id="fill"> </div>
</div>
No need flexbox or any complex stuff, you can simply use background for this:
.progress {
width:200px;
line-height:50px;
margin:5px;
text-align:center;
font-size:30px;
background:
linear-gradient(green,green) left no-repeat,
grey;
}
<div class="progress">
100%
</div>
<div class="progress" style="background-size:50% 100%">
50%
</div>
<div class="progress" style="background-size:75% 100%">
75%
</div>
You can add more coloration to get closer to the visual you want:
.progress {
width:200px;
line-height:50px;
margin:5px;
text-align:center;
font-size:30px;
background:
/*light overlay on the half top*/
linear-gradient(rgba(255,255,255,0.3),rgba(255,255,255,0.3)) top/100% 50%,
/*the two bars*/
linear-gradient(#539041,#539041) left/5px 100%,
linear-gradient(#539041,#539041) var(--p,100%) 0/5px 100%,
/*the progress*/
linear-gradient(#53e15a,#53e15a) left/var(--p,100%) 100%,
/*the outer coloration*/
#bbbbbb;
background-repeat:no-repeat;
}
<div class="progress">
100%
</div>
<div class="progress" style="--p:50%">
50%
</div>
<div class="progress" style="--p:75%">
75%
</div>
Not sure if I understood well... Is it something like this that you want to achieve?
HTML
<section class="parent">
<article class="child">
</article>
<article class="child">
</article>
</section>
CSS
.parent {
display: flex;
width: 100vw;
justify-content: space-around;
}
.child {
width: 50%;
height: 60px;
}
Link to codePen
How can I set the height of the div to be the same than another div with only CSS with text?
For example I have a div whose max-width is 10px and it contains the text
CSS is one of the most famous Programming Language to design webpages
and have another div whose max-width is also 10px.
<div style="max-width:10px">CSS is one of the most famous Programming Language to design webpages</div>
<div style="max-width:10px"></div>
What I want is set the height of the second div equal to the height of the First Div relative to the content. If the div has more or less text it is adjustable by it.
My fiddle: http://jsfiddle.net/p38hmoz0/
NOTE: I can't use jquery or javascript for this purpose because it would be difficult for me to add it in the polymer as it uses shadow dom.**
Since you said you can make the second div as a child of first div:
Demo: http://jsfiddle.net/p38hmoz0/6/
HTML:
<div class="parent">
CSS is one of the most famous Programming Languages to design webpages
<div></div>
</div>
CSS:
div.parent {
position: relative;
max-width: 10px;
}
div.parent > div {
max-width: 10px;
position: absolute;
top: 100%;
left: 0;
height: 100%;
border: 1px solid #0ff; /* for demo purposes */
}
You can do this with css only if you can add a wrapper and use display: table;: JS Fiddle
Note: I adjusted the max-width and added a blue background to the empty div to show the example.
HTML
<div class="table">
<div style="max-width:100px">CSS is one of the most famous Programming Language to design webpages</div>
<div style="max-width:100px" id="two"></div>
</div>
CSS
.table {
display: table;
}
.table div {
display: table-cell;
}
You can't do it the way you explain as CSS is stateless, you cannot know anything that you didn't previously defined.
I suggest you to use a wrapper and table display
HTML
<div id="wrapper">
<div id="div1" style="max-width:10px">CSS is one of the most famous Programming Language to design webpages</div>
<div id="div2" style="max-width:10px"></div>
</div>
CSS
#wrapper
{
display: table;
}
#div1
{
border: 1px solid blue;
display: table-cell;
}
#div2
{
border: 1px solid yellow;
display: table-cell;
}
JSFiddle
I'm trying to get a vertical navigation list that will allow for elements that are wider than the nav itself. Users can enter whatever names they like for items that will appear here, so I have no control over their width, aside from maybe a very high max character length.
I've tried a few different methods, and seem to be coming up with multiple ways to achieve the same wrong result, once with flexbox once without. In both cases, if I have some "normal" sized elements that don't overflow outside of the nav, they look fine at first. But if I have an oversized element that overflows outside the container, and the user scrolls to the right, they will see the items boundaries don't extend to the right.
If I use the outer "item" for visual styling (light blue), they all end up the same width, but not wide enough to account for the overflow. If instead try styling the inner item (green), it is the correct width for only the overflowing item, and all the rest of the items are different widths based on their length.
Is there a way to:
Have all items appear to be the same width when there is a large item that overflows larger than the container
Without setting some arbitrary width, because I don't have control over how long the user strings might be
CSS only, no javascript
Initial View, looks ok...
Scroll to the right... looks bad!
Codepen
Here's the Codepen
HTML
<div id="container">
<div class="item"><span>Item 1</span></div>
<div class="item"><span>Item 2</span></div>
<div class="item"><span>Item 3</span></div>
<div class="item"><span>Item 4</span></div>
<div class="item"><span>Super Long Item Name of Obliteration</span>
</div>
</div>
<div id="flex-container">
<span class="flex-item"><span>Item 1</span></span>
<span class="flex-item"><span>Item 2</span></span>
<span class="flex-item"><span>Item 3</span></span>
<span class="flex-item"><span>Item 4</span></span>
<span class="flex-item"><span>Super Long Item Name of Obliteration</span>
</span>
</div>
CSS
#container {
width:200px;
height:400px;
background-color: red;
overflow:auto;
}
.item {
height: 40px;
border: 1px solid blue;
background-color:lightblue;
white-space: nowrap;
}
#flex-container {
width:200px;
height:400px;
background-color: red;
overflow:auto;
display: flex;
flex-direction:column;
}
.flex-item {
height: 40px;
border: 1px solid blue;
background-color:lightblue;
white-space: nowrap;
}
/* Content */
span > span,
div > span {
background-color: green;
}
Use overflow and text overflow
.item,
.flex-item {
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%:;
}
Codepen link
I am trying to make these blocks of info the same size regardless of the number of words each one holds. As seen in the example, when one block has less text than the other, one gets a bit smaller and the other remains a different size.
Now my question is, How do I achieve having these blocks the same size regardless of its content or image? I am also going to use another pair right below them.
Here is the CSS code:
/***********All containers**************/
.bottomContainers{
position: absolute;
margin-left: 0%;
display: inline-box;
}
/**********Small Containers*************/
.container{
max-width: 30%;
max-height: 30%;
margin-top:5%;
margin-bottom: 5%;
margin-left: 10%;
padding-left: 2%;
padding-right: 2%;
padding-bottom: 2%;
background-color: #ecf0f1;
color: grey;
display: inline-block;
/*display: inline-block;*/
border-radius: 5px;
border-bottom: 2px solid grey;
}
Here is the HTML code:
<div class="bottomContainers" role="moreInfo">
<!--Small Inner Containers for Information-->
<div class="container" id="firstContainer">
<br />
<center><img src="img/map.png"></center>
<br>
<article>
Some random text is in this block, It doesnt size like the next one
</article>
</div>
<div class="container" id="firstContainer">
<br />
<center><img src="img/money.png"></center>
<br>
this is another block which also doesnt scale to the other block regardless of text inside of it
</div>
What did I possibly do wrong here ?
I am heavily refactoring your original code in this solution. If this is a static width website then having static width cells won't be a problem. If you want this solution to be responsive you will have a lot of issues with it:
http://jsfiddle.net/VET6x/1/
I positioned the image and its corresponding text using absolute. Again that will work with a static layout, once it goes responsive there will be problems.
<div class="bottomContainers">
<div class="container">
<div>
<img src="http://placekitten.com/g/80/80" />
</div>
<div>
Some random text is in this block, It doesnt size like the next one
</div>
</div>
<div class="container">
<div>
<img src="http://placekitten.com/g/80/80" />
</div>
<div>
This is another block which also doesnt scale to the other block regardless of text inside of it
</div>
</div>
</div>
.bottomContainers { overflow:hidden; }
.container {
width:200px;
height:200px;
float:left;
position:relative;
margin:5% 5%;
padding:2%;
background-color: #ecf0f1;
color: grey;
border-radius: 5px;
border-bottom: 2px solid grey;
}
.container > div { position:absolute; bottom:10px; }
.container > div:first-child { position:absolute; top:10px }
If it were me I would find someway to avoid static height cells.
Here is one solution that may work for you:
Demo Fiddle
I changed up your code a bit. Using the center tag is frowned upon, also it looks like the br tags were there for spacing, which could be done with margin. I ended up giving .container a specified height, the main drawback in that being if the window is sized down too far the overflow text will be hidden.
HTML:
<div class="bottomContainers" role="moreInfo">
<div class="container" id="firstContainer">
<img src="http://www.placehold.it/100x100">
<p>
Some random text is in this block, It doesnt size like the next one
</p>
</div>
<div class="container" id="firstContainer">
<img src="http://www.placehold.it/100x100">
<p>
this is another block which also doesnt scale to the other block regardless of text inside of it
</p>
</div>
</div>
CSS:
.container{
// your current styles here
overflow: hidden;
height: 200px;
display: block;
float: left;
}
.container img {
display: block;
margin: 10px auto 0px;
}
This is a quick fix, but setting an explicit height on the objects will have them all be the same height. This requires some playing around with the best size and such but it will fix your problem. I'm curious how a professional would fix this problem.
Some other things with your code. Centering the <img> using HTML is discouraged, use css instead. Also, where are the <br> tags and why are some closed but some aren't?
Maybe you can use display:table;, display:table-row; and display:table-cell;. This way, your div will act like column of a table. They will stay at the same height.
Take a look at this jsfiddle!
I'm using a table for the footer of my web page. I really don't know much about tables because I've always used CSS. The following is the only table I've ever made. It seems to work in Opera, Chrome, and Firefox, but everything goes to the left in IE. I'm not sure what I'm doing wrong with the table because I don't know much about tables. Here is the HTML:
<div id="framecontentBottom">
<div id="container">
<div id="row">
<div id="left">
<!-- Counter Code START --><img src="http://www.e-zeeinternet.com/count.php?page=760915&style=LED_g&nbdigits=4&reloads=1" alt="Web Counter" border="0" ><br>Page Views<!-- Counter Code END -->
</div>
<div id="middle">
Contact me at jacksterdavis<img src="images/#white.png">gmail.com
</div>
<div id="right">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style ">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4f302421558e3fc2"></script>
<!-- AddThis Button END -->
</div>
</div>
<div id="row">
<div id="left">
</div>
<div id="middle">
<p>The internet is the printing press of the 21'st century.</p>
</div>
<div id="right">
</div>
</div>
And here is the CSS:
#container {
display: table;
width: 100%;
}
#row {
display: table-row;
}
#middle {
width:50%;
text-align:center;
display: table-cell;
}
}
#left
{
width:25%;
text-align:left;
display: table-cell;
}
#right
{
width:25%;
text-align:right;
display: table-cell;
padding: 5px;
}
#quote
{
text-align:center;
width:100%;
}
#logoBtm
{
align:middle;
text-align:center;
}
#logoBtmLeft
{
align:left;
}
#logoBtmRight
{
align:right;
}
#framecontentBottom{
clear: both;
position: relative;
z-index: 10;
margin-top: -3em;
top: auto;
bottom: 0;
height: 80px; /*Height of bottom frame div*/
overflow: hidden; /*Disable scrollbars. Set to "scroll" to enable*/
background-color: #585858;
color: white;
width: 100%;
}
If you point out everything wrong with my table it's appreciated, thanks. A CSS fix would be the best but if the HTML must be edited it's fine.
the problem most likely lies in the fact that you have two divs with the same id. use classes for row instead.removed for the comfort of others. This line doesnt help the solution at hand.
also, in referring to your comment, ie 7 does not support table display CSS.
http://caniuse.com/#search=table-cell
use a combination of inline block or float. but beware, as inline block has its own issues with ie7
http://flipc.blogspot.com/2009/02/damn-ie7-and-inline-block.html
Here is a working, valid, example.
http://jsfiddle.net/mRHnW/2/
A couple changes: Ive styled every div inside of .row so that it gets applied once (and if it needs to be fixed, it can be, in one place. Even in CSS, it needs to be DRY.
I removed the margin-top from the #frameContentBottom selector because it was screwing with jsfiddle giving me visible results. Feel free to re-instate it if its important to your layout.
I adjusted the width of your 'columns' to be slightly less than 100%, because you've also included padding. The way the CSS Box Model as specified by W3C works is that the width declaration does not include padding, border, and margin. Thus, if you're creating a 100% div, and want 5px padding, then you need to specify less than 100% to get the padding within the 100% confines.
On a sufficiently wide screen (something bigger than jsfiddle default panes), your footer should look about what you expect.