Is it possibly to automatically expand a div to maximum width once it's floated sibling style set to display:none?
Please take a look at this example:
It's easy with flexbox - children expand by default:
document.querySelector("button").addEventListener("click", function() {
document.querySelector(".b").classList.toggle("hidden");
});
body {
display: flex; /* this is the important bit */
}
div {
border: 0.1em solid black;
margin: 0.5em;
padding: 0.5em;
}
.b {
width: 15%;
flex-shrink: 0;
}
.hidden {
display: none;
}
<div class="a">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu felis at metus convallis ornare. Pellentesque in porttitor elit. Sed augue augue, vulputate in laoreet quis, vehicula et arcu. Nullam feugiat elit purus, id euismod ligula sodales nec. Vestibulum mattis molestie lacus. <button>Toggle B</button>
</div>
<div class="b"></div>
Browser support is really good for some time now: https://caniuse.com/#feat=flexbox
Another option would be display: table:
document.querySelector("button").addEventListener("click", function() {
document.querySelector(".b").classList.toggle("hidden");
});
body {
display: table;
border-spacing: 1em;
border-collapse: separate;
}
.a, .b {
border: 0.1em solid black;
padding: 0.5em;
display: table-cell;
height: 5em;
}
.a {
width: 85%;
}
.b {
width: 15%;
}
.hidden {
display: none;
}
<div class="a">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu felis at metus convallis ornare. Pellentesque in porttitor elit. Sed augue augue, vulputate in laoreet quis, vehicula et arcu. Nullam feugiat elit purus, id euismod ligula sodales nec.
Vestibulum mattis molestie lacus. <button>Toggle B</button>
</div>
<div class="b"></div>
But… it feels kinda hacky to me. Just use flexbox if you don't need support for ancient browsers.
This is how I'd do it. See if it works for you.
document.querySelector("button").addEventListener("click", function(){
document.querySelector(".a").classList.toggle("hidden");
});
body{
display: block;
}
.container{
width: 96%;
margin-left: 2%;
}
.a, .b {
float: right;
height: 5em;
}
.a {
width: 15%;
background: #00E676;
}
.b {
width: 85%;
background: #1E88E5;
font-family: Calibri;
line-height: 2em;
}
.hidden {
display: none;
}
.hidden ~ .b{
width: 100%;
}
button{
margin: 100px 100px;
}
<div class="container">
<div class="a"></div>
<div class="b"></div>
</div>
<button>Toggle</button>
Related
This question already has answers here:
Why is a flex item limited to parent size?
(1 answer)
Display a div width 100% with margins
(6 answers)
Closed 4 months ago.
I am trying to make a design where there are two columns in a box. One column contains an image and takes up 1/4 of the width under normal circumstances, and 1/2 of the width when hovered over. The other column contains a decent amount of text and takes up whatever space is left.
I am running into an issue where the CSS that tells the first column's width to be either 50% or 25% is not being respected. The first column is less than 1/8 of the width when it is supposed to be 1/4 and less than 1/4 when it is supposed to be 1/2. I have managed to create a minimal example:
html,
body {
height: 100%;
width: 100%;
min-width: 100%;
}
.container {
height: 50%;
width: 100%;
display: flex;
flex-direction: row;
border-radius: 0.25rem;
margin: 1rem;
overflow: hidden;
border-width: 1px;
border-style: solid;
}
.left {
margin: auto;
width: 25%;
height: 100%;
border-width: 1px;
border-style: solid;
transition-property: width;
transition-duration: 300ms;
padding: 1rem;
}
.left:hover {
width: 50%;
}
.right {
width: auto;
margin: auto;
font-size: 1.5rem;
line-height: 2rem;
padding: 1rem 4rem;
border-width: 1px;
border-style: solid;
}
<div class="container">
<div class="left">
Placeholder
</div>
<p class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ligula purus, lobortis luctus malesuada vitae, egestas quis lorem. Mauris ultrices mauris et enim dictum fermentum. Nam nibh nulla, posuere ut egestas a, fringilla ac augue. Vivamus sed
eros eget purus maximus iaculis. </p>
</div>
JSFiddle link
I have looked up previous questions like this and they all seem to state that somehow the parent div's dimensions are not defined. However, in this case they are defined! html and body have defined dimensions, and so does the container div.
Why isn't the width doing what I think it should do in this case?
Flex items (i.e. the children of a flex container) are allowed to grow and shrink by default. To avoid that, you can add flex-shrink: 0; and flex-grow: 0; to their CSS:
html,
body {
height: 100%;
width: 100%;
min-width: 100%;
}
.container {
height: 50%;
width: 100%;
display: flex;
flex-direction: row;
border-radius: 0.25rem;
margin: 1rem;
overflow: hidden;
border-width: 1px;
border-style: solid;
}
.left {
margin: auto;
width: 25%;
height: 100%;
border-width: 1px;
border-style: solid;
transition-property: width;
transition-duration: 300ms;
padding: 1rem;
flex-shrink: 0;
flex-grow: 0;
}
.left:hover {
width: 50%;
flex-shrink: 0;
flex-grow: 0;
}
.right {
width: auto;
margin: auto;
font-size: 1.5rem;
line-height: 2rem;
padding: 1rem 4rem;
border-width: 1px;
border-style: solid;
}
<div class="container">
<div class="left">
Placeholder
</div>
<p class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ligula purus, lobortis luctus malesuada vitae, egestas quis lorem. Mauris ultrices mauris et enim dictum fermentum. Nam nibh nulla, posuere ut egestas a, fringilla ac augue. Vivamus sed
eros eget purus maximus iaculis. </p>
</div>
using flex can distort widths
.left {
width: 25%;
display: inline-block;
border: 1px solid red;
}
.right {
float: right;
width: 50%;
}
.left:hover {
width: calc(50% - 2px);
}
<div class="container">
<div class="left">
Placeholder
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ligula purus, lobortis luctus malesuada vitae, egestas quis lorem. Mauris ultrices mauris et enim dictum fermentum. Nam nibh nulla, posuere ut egestas a, fringilla ac augue. Vivamus sed
eros eget purus maximus iaculis. </div>
</div>
This question already has answers here:
Why is the paragraph not floating beside the profilePic?
(3 answers)
Closed 6 years ago.
Why does making span float:left or display:inline-block or both move the span class below the profilePic?
.content {
margin-top: 30px;
margin-left: 20px;
padding-bottom: 20px;
float: left;
}
.mainContent {
width: 1000px;
float: left;
}
.infoBit {
display: inline-block;
font-size: 1.1em;
padding-right: 10px;
padding-top: 10px;
}
.profilePic {
border: 1px blue solid;
height: 59px;
display: inline-block;
width: 44px;
margin: 0px;
float: left;
margin-right: 3px;
}
span {
margin-top: 0px;
float: left;
}
<div class="content">
<div class="mainContent">
<div class="infoBit">
<div class="profilePic"></div>
<span> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam quis enim ut sapien sodales commodo. Fusce congue, elit a finibus fermentum, diam eros mollis massa, at eleifend sapien dui eget mauris. Donec nec diam enim. Vivamus commodo placerat risus vitae auctor. Cras leo elit, egestas eget dolor vitae, facilisis consequat sem. Mauris facilisis ipsum in porttitor ullamcorper. Nam vel massa sed quam venenatis facilisis. Quisque vitae mollis urna. In egestas nunc sed felis consequat, in malesuada dolor feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</span>
<div class="social">
<div class="Like"></div>
<div class="Dislike"></div>
<div class="share"></div>
</div>
</div>
</div>
</div>
you need to float only one thing
try this
CSS
.content
{
margin-top : 30px;
margin-left: 20px;
padding-bottom: 20px;
}
.mainContent
{
width: 1000px;
float: left;
}
.infoBit
{
display: inline-block;
font-size: 1.1em;
padding-right: 10px;
padding-top: 10px;
}
.profilePic
{
border:1px blue solid;
height: 59px;
display: inline-block;
width: 44px;
margin: 0px;
float: left;
margin-right: 3px;
}
span
{
margin-top: 0px;
}
I'm showing more text on :hover, so the container <div> is changing height automatically but the background-color doesn't expand. Is there any solution please?
Here is the jsFiddle that shows the problem.
#related {
width: 100%;
left: 0;
min-height: 360px;
height: auto;
background-color: #3f5673;
color: white;
}
#related .inner {
width: 63%;
margin: auto;
color: white;
}
#related .inner .abox .thumb {
text-decoration: none;
}
#related .inner .abox .thumb .sgn {
display: block;
padding-left: 15px;
color: #36C7E3;
}
#related .inner a {
color: white;
}
#related .inner h3 {
font-size: 25px;
color: white;
}
#related .inner h4 {
color: white;
}
#related .inner .col {
float: left;
width: 30%;
margin-right: 30px;
}
#related .inner .col strong {
padding-left: 15px;
color: #36C7E3;
}
#related .inner .col p {
white-space: nowrap;
overflow: hidden;
text-overflow: clip;
}
#related .inner .col p::after {
display: block;
float: right;
padding-top: 15px;
content: "..see more..";
color: #36C7E3;
}
#related .inner .col p:hover {
overflow: visible;
width: auto;
white-space: normal;
padding: 0;
}
#related .inner .col p:hover:after {
display: none;
}
#related #references {
float: left;
width: 30%;
margin-right: 30px;
}
#related #didyouknow {
float: left;
width: 30%;
margin-right: 30px;
}
/*------------------------------------------------------*/
#footer {
float: left;
left: 0;
height: 30px;
width: 100%;
background-color: #5f5f5f;
color: white;
padding-bottom: 20px;
}
#footer hr {
display: none;
}
#footer .inner .copy {
padding-left: 15px;
}
#footer .inner .menu li {
float: right;
display: inline;
padding-right: 15px;
}
#footer .inner .menu li a {
color: white;
}
<section id="related">
<div class="inner">
<section class="col" id="news">
<h3>News</h3>
<h4>Some title</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam rhoncus, lacus sed tincidunt mollis, tellus erat mollis sapien, at ullamcorper augue nisi a justo. Praesent et tellus at lorem rhoncus venenatis non id velit. Nullam vestibulum arcu
quis erat fermentum, sed venenatis arcu tristique. Quisque fermentum nisi sed porta fermentum. Nam tincidunt, ipsum et blandit sodales, turpis enim ultricies erat, a viverra tellus elit vitae enim. Etiam placerat enim orci, nec pulvinar lorem
vehicula ac. Etiam eget elementum sem. Integer nisi elit, bibendum vitae leo non, posuere tincidunt neque.
</p>
<strong>10 Dec 2014</strong>
<h4>Some title</h4>
<p>Vestibulum luctus nibh non risus semper consectetur. Sed laoreet eget metus in elementum. Ut mollis faucibus risus a faucibus. Vestibulum eget maximus purus. Maecenas vitae ipsum mattis augue feugiat rutrum. Sed tortor eros, convallis vitae libero
vitae, commodo lobortis lacus. Duis condimentum consectetur augue, vel pharetra orci aliquam sit amet.</p>
<strong>5 Jan 2015</strong>
</section>
<aside>
<div id="references" class="abox">
<h3>References</h3>
<a class="thumb d2" href="#">
<q>FitLayout is very useful for obtaining structured data from the web. We use it every dat
for obtaining statistical data about the products offered by our competitors.</q>
<span class="sgn">John Smith</span>
</a>
</div>
<div id="didyouknow" class="abox">
<h3>Did you know?</h3>
<a class="thumb d2" href="#">
<q>The FitLayout pattern matching algorithms save time and money in the specification phase.
The designers may focus on the main problem instead of manually designing complex extraction
templates.</q>
</a>
</div>
</aside>
</div>
</section>
<footer id="footer">
<div class="inner">
<hr>
<ul class="menu">
<li>Home</li>
<li>News</li>
<li>Contact</li>
</ul>
<div class="copy">Copyright © 2016 FITLayout. All rights reserved.</div>
</div>
</footer>
The issue is not background color, but element size. You need to force the parent to stretch to the height of the child:
#related {
...
overflow: hidden;
}
Demo
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to put three divs on my webpage, like this, the pink div is the container for two other divs and I want to center the div on the webpage (vertically and horizontally). I prepared some jsfiddle, but obviously I suck at css, so the effect is far from the expected one... So far my css looks like this:
#intro2{
background-color: #b0e0e6;
width: 50%;
margin: 0 auto;
font-size: 1.5em;
}
#intro2 .image{
position: absolute;
left: 0px;
background-color: #aaaae6;
}
#intro2 .text{
position: relative;
right: 0px;
background-color: #cccccc;
}
}
Could you help me with that?
Thanks.
Try like this:-
#intro2{
background-color: #b0e0e6;
width: 50%;
margin: 0 auto;
font-size: 1.5em;
padding: 5px;
}
.clear{
clear: both;
}
#intro2 .image{
float:left;
width:50%;
background-color: #aaaae6;
}
#intro2 .text{
float: right;
width:50%;
background-color: #cccccc;
}
<div class="intro" id="intro2">
<div class="image" id="image1">
<img src="http://www.cdc.gov/animalimportation/images/dog2.jpg" alt="simple" />
</div>
<div class="text" id="text1">
<h1>Simple</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis sodales sit amet mauris in blandit. Aenean sodales in dui eget facilisis. Vestibulum tempor risus dui, sed pharetra nulla auctor id. Sed purus odio, tempus et volutpat a, fermentum sit amet ante. Etiam eros mauris, rutrum at vehicula a, vehicula vitae nulla. Suspendisse non mattis turpis. Donec non convallis lacus. Nullam gravida diam et leo tempor vestibulum. Vivamus lorem nunc, bibendum eu lacinia quis, porta vel nisl. Sed vitae euismod augue. In at est lacinia ipsum feugiat feugiat. Praesent mollis posuere ante, eget maximus est mollis suscipit. Donec ullamcorper elit quis cursus gravida. Quisque leo risus, bibendum sed nisi ut, facilisis iaculis arcu. Pellentesque purus augue, fringilla tempus augue eget, ullamcorper condimentum leo.</p>
</div>
<div class="clear"></div>
</div>
Here:
#intro2{
background-color: #b0e0e6;
width: 50%;
left:50%;
font-size: 1.5em;
position:absolute;
margin-left:-25%;
}
That way your container is centered on the page.
I'm not sure I would go with the absaloute positioning method you've gone for on this occasion. I think perhaps a display:table on the wrapper and display:table-cell on the children would give you more control over your style.
#intro2{
background-color: #b0e0e6;
width: 50%;
margin: 0 auto;
font-size: 1.5em;
}
#intro2 .image{
background-color: #aaaae6;
display: table-cell;
vertical-align: top;
}
#intro2 .text{
background-color: #cccccc;
display: table-cell;
vertical-align: top;
}
Here's an example: http://jsfiddle.net/Lfyacy25/2/
http://jsfiddle.net/Lfyacy25/3/
#intro2{
background-color: #b0e0e6;
width: 50%;
margin: 0 auto;
font-size: 1.5em;
}
#intro2 .image{
float: left;
width: 50%;
background-color: #aaaae6;
}
#intro2 .text{
float: left;
width: 50%;
background-color: #cccccc;
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
.intro{
background-color: #b0e0e6;
width: 100%;
margin: 0 auto;
font-size: 1.5em;
padding: 5px;
}
.intro:before,.intro:after {
content: " ";
display: table;
}
.intro:after {
clear: both;
}
.image{
width: 50%;
background-color: #aaaae6;
float: left;
}
img{
margin: 0 auto;
display: block;
}
.text{
width: 50%;
background-color: #cccccc;
float: left;
}
Updated fiddle
try the following:
<div class="outter">
<div class="innerleft">image goes here</div>
<div class="innerright">text goes here</div>
</div>
.outter {width:100%; min-height:120px; background-color:red; padding:1% }
.innerleft {float:left; width:49%;background-color: green;min-height:120px; }
.innerright {float:right; width:49%;background-color: blue;min-height:120px; }
I'm trying to make a few rows like in a table, but with divs.
Each one has an image on the left, a block of text, and "read more"..
I've tried using display:table, but it doesn't seem to be working.. The text is and images are not aligned properly..
http://jsfiddle.net/76a4j/1/
.entry{
width=100%;
display:table;
}
entry-row {
border: 2px #000000 solid;
margin-top:5px;
margin-bottom:5px;
display: table-row;
}
.imgrL {
border: 1px solid #c7c5c8;
padding: 5px;
float:left;
clear:left;
}
Thanks for the answers everyone, I see what I did wrong and have fixed it now :)
You have two problems:
CSS does not use =. Change
width=100%;
to
width: 100%;
You need to use . on all class selectors. Change
entry-row {
to
.entry-row {
With these changes, it looks more like your image.
JSFiddle
Here is the solution you want:
HTML
<div class="entry">
<a href="#">
<img src="#" />
</a>
<div class="text">
<h3 class="title">Article 1</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur scelerisque arcu at accumsan feugiat. Fusce at interdum sapien.
Phasellus nec odio varius ante imperdiet facilisis. Etiam iaculis dui vitae nibh scelerisque fermentum.
Nam iaculis quis purus ac congue. Maecenas sed elit tortor.
Sed gravida velit nulla, sit amet dapibus elit mollis vitae.
In libero libero, mattis et ipsum eu, euismod aliquet diam.
Nulla eu neque interdum, suscipit libero nec, facilisis sapien. Donec consequat porttitor interdum.
Nullam non blandit massa.Read more
</p>
</div>
</div>
CSS
.entry > a {
float: left;
}
.entry img {
width: 130px;
height: 130px;
}
.entry .text {
float: right;
width: 700px;
}
.entry:after {
clear: both;
content: '';
display: block;
}
.entry .title {
color: #FF7A00;
font-family: arial;
font-size: 15px;
}
.entry .text {
margin: 0px 0px 0px 20px;
}
.entry .text * {
margin: 0px;
}
.entry .read-more {
float: right;
}
display: inline-block;
Could be done for you
add this css
div
{
display: inline-block;
}
Please change css code
entry-row {
border: 2px #000000 solid;
margin-top:5px;
margin-bottom:5px;
display: table-row;
}
To
.entry-row {
float:left;
border: 2px #000000 solid;
margin-top:5px;
margin-bottom:5px;
}
You have to add overflow:hidden property to each row.
.entry-row {
overflow:hidden;
margin:0 0 20px;
}
Hope it will help.