Left align div to centered container - css

I have a container centered with a max width as follow:
#container {
margin-left: auto;
margin-right: auto;
max-width: 900px;
}
Now I want to create a new div just below the contained one, but I want this new div to align to the left and expand beyond the right side of the container. Something like that:
|||||||||||||||||||||||||||||||||||| #container
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| #div 2
The difficulty is that #container margins are auto, so how can I force #div to follow #container left margin as the browser resizes?!
NOTE: I am looking for a pure CSS solution WITHOUT JAVASCRIPT
EDIT: It was not clear in my explanation but, my goal was to make the #div ADJACENT to the #container. Like this:
<div id="container"></div>
<div id="div"></div>
I ended up refactoring my html to use #bananabran solution with absolute positioning which simply uses parent-child structure:
<div id="container">
<div id="div"></div>
</div>

You don't have to force the div to follow its container's left margin. Divs naturally start at the top-left of their container (unless otherwise specified or affected by). You also do not need to use Grid or FlexBox. CSS3, and even CSS2 can do this natively.
See working CodePen example: https://codepen.io/bananabrann/pen/QWWdXQZ
Assuming you have no other code affecting your code...
<div id="container" />
<div id="my-div" />
#container {
background-color: blue;
position: relative;
margin: 0 auto;
width: 900px;
height: 300px;
}
#my-div {
background-color: red;
position: absolute;
bottom: 0;
width: 500px;
height: 20px;
}

CSS-Grid can do that:
.wrap {
display: grid;
grid-template-columns: 1fr minmax(auto, 400px) 1fr;
/* 400px for demo purposes */
}
.container {
padding: 1em;
background: pink;
grid-column-start: 2;
}
.wide-r {
padding: 1em;
background: lightgreen;
grid-column: 2 / span 2;
}
<div class="wrap">
<div class="container">Container
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates, similique, maxime aspernatur dolorum quod recusandae possimus fuga blanditiis laudantium delectus quis magni. Veniam, consequuntur dolores facilis cupiditate fugiat ullam aspernatur!
Corporis excepturi quos esse voluptatem voluptatibus corrupti ea, tempora culpa magni, hic aspernatur pariatur molestias itaque doloremque assumenda ad fugiat!</p>
</div>
<div class="wide-r">Wide Right</div>
</div>

Related

Display flex, 1 item fixed width, other item overflow and gets cut off [duplicate]

This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 4 years ago.
I have 2 items, 1 is a fixed width of 100px the other fills the space. The other item has a child with long text on one line that gets ellipses. Unfortunately it overflows its parent. How can i make it not exceed the parent??
issue:
https://jsfiddle.net/jv54f6g1/2/
i found one solution but i dont feel its ideal:
https://jsfiddle.net/jv54f6g1/3/
code:
<div class="a">
<div class="b">
<p>im a icon</p>
</div>
<div class="c">
<p>im a teapot that is short and stout, here is my handle Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus, mollitia, obcaecati. Amet, modi. Consequuntur sit asperiores veniam, cupiditate dolores iusto illo alias? Cupiditate, optio delectus voluptatum, ducimus nam inventore doloribus.</p>
</div>
</div>
css
* {
box-sizing: border-box;
}
.a {
width: 100%;
display: flex;
max-width: 500px;
border: 1px solid red;
}
.b {
min-width: 100px;
display: block;
}
.c {
flex: 1;
background: yellow;
max-width: 100%;
p {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
Add overflow: hidden; to that second element (.c)
https://jsfiddle.net/e2am9hj0/

Two columns with one header layout with CSS3 Flexbox

Like at picture above I need such layout. DIV1 contains static text and to DIV2 data (text) coming from other files (EX.JSON) so it's variable. The point is to keep both divs always with same height based on height of heigher div.
Note: I don't want this with float.
Image courtesy: One of Test I Given Online.
Hi
You can do this easly with CSS3 Flexbox like you asking.
Solution
Here is snippet with working example. I used Flexbox and detalils you have in comments in the code.
#main {
/*Styles for sample presentation*/
padding: 20px;
border: 1px solid tomato;
}
#wrapper {
display: flex;
flex-flow: row wrap;
/* The remaining place (horizontaly) will be spread out around divs in wrapper. */
justify-content: space-around;
/*Styles for sample presentation*/
border: 1px solid royalblue;
}
#wrapper>header {
/* To keep header 100% width. */
flex: 0 0 100%;
text-align: center;
/*Styles for sample presentation*/
background-color: sandybrown;
}
#wrapper>div {
margin: 10px;
padding: 20px;
/* To center the text vertically. */
display: flex;
flex-flow: column nowrap;
justify-content: center;
/*Styles for sample presentation*/
border: 1px solid maroon;
text-align: justify;
}
#text-static {
/*Low flex basis values to keep it next to each other divs*/
flex: 1 0 30%;
}
#wrapper>div#text-json {
/*Low flex basis values to keep it next to each other divs*/
flex: 0 0 25%;
margin-left: 0;
}
<div id="main">
<div id="wrapper">
<header>
Sample header
</header>
<div id="text-static">
Lorem Ipsum jest tekstem stosowanym jako przykładowy wypełniacz w przemyśle poligraficznym. Został po raz pierwszy użyty w XV w. przez nieznanego drukarza do wypełnienia tekstem próbnej książki. Pięć wieków później zaczął być używany przemyśle elektronicznym,
pozostając praktycznie niezmienionym. Spopularyzował się w latach 60. XX w. wraz z publikacją arkuszy Letrasetu, zawierających fragmenty Lorem Ipsum, a ostatnio z zawierającym różne wersje Lorem Ipsum oprogramowaniem przeznaczonym do realizacji
druków na komputerach osobistych, jak Aldus PageMaker
</div>
<div id="text-json">
a Lorem Ipsum a Lorem Ipsum a Lorem Ipsum
</div>
</div>
</div>
If you want to try case when right div has more text than left you can edit same snippet as above there.
Knowledge
More informations about CSS3 Flexbox you have e.g. on this W3Schools site.
A nice learning tool that I found recently flexboxfroggy.com .
Hope that was helpful.
Cheers
Here is my implementation using CSS Grid which in my opinion makes this a lot easier especially when working with layouts. I am using SASS for styling. I hope this helps. Here is a link of the snippet on codepen.io
HTML CODE
<div class="main-div">
<div class="wrapper-div">
<div class="sample-header">Header</div>
<div class="div1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Dicta blanditiis, error dolorem, velit tempora, magni ea officiis itaque voluptates aliquid consectetur deserunt quisquam tenetur dolor! Labore assumenda iusto debitis autem. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla, velit cumque quaerat optio vero sed dolores maxime dolorum aut itaque? Asperiores, esse. Nihil dignissimos nisi debitis molestiae facilis accusamus non! Lorem ipsum dolor sit amet consectetur, adipisicing elit. Architecto quisquam corrupti error nesciunt pariatur quidem, voluptates similique obcaecati magni aperiam autem aliquam ex, ducimus, distinctio amet labore vel blanditiis sapiente. </div>
<div class="div2">Little bit of text here </div>
</div>
</div>
CSS STYLES USING CSS GRID
.main-div{
border:1px solid #000;
width:500px;
margin:0 auto;
padding:10px;
.wrapper-div{
display: grid;
grid-gap: 10px;
grid-template-columns: 3fr 1fr;
background-color: #fff;
// align-items:center;
color: #444;
margin:10px 0;
.sample-header {
grid-column: 1 / 3;
grid-row: 1;
background-color:lightgrey;
padding:10px;
text-align:center;
}
.div1,.div2{
border:1px solid #000;
padding:10px;
display:grid;
align-items:center;
}
.div1 {
grid-column: 1 ;
grid-row: 2 ;
}
.div2 {
grid-column: 2;
grid-row: 2;
}
}
}
https://codepen.io/anon/pen/OQZNgX
On the container for Div 1 & Div 2 apply this CSS
display: flex;
justify-content: center;
And then on the child divs, use flex-basis to specify their width
flex-basis: 75%;
/* and / or */
flex-basis: 25%;
See above code pen link for a working demo!
You can use bootstrap to achieve this... You can see below code for this type of design.
<div class='container'>
<div class='col-md-12 customHeaderclass'>
Your header
</div>
<div class='row'>
<div class='col-md-8'>
Your big content
</div>
<div class='col-md-4'>
Your small content
</div>
</div>
</div>
I hope this code will help you
Thanks & Regards.

How to position two divs one on each other at the bottom of parent?

In this example I was able put two divs on each other at the bottom of their parent, but only because I knew the height of last div. The first one was moved a bit to the top. What if the height of #second is dynamic? How can we make them sit on each other at the bottom of parent with dynamic heights? Is it even possible with css? Please do not post JavaScript or jQuery versions.
You can use Flexbox. With justify-content: flex-end and you can move content to end of parent element, so if you want to position some other child element on top of parent you can use margin-bottom: auto. This applies if you set flex-direction: column on parent.
.content {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.main {
margin-bottom: auto;
border: 1px solid black;
}
.box {
background: lightblue;
margin: 5px;
}
<div class="content">
<div class="main">Lorem ipsum dolor.</div>
<div class="box">One</div>
<div class="box">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati vel molestiae dolores, ad, nulla harum tenetur minima aperiam debitis id atque fugit, modi error et magni eius repellendus saepe. Vero?</div>
</div>
You can wrap both of them in a div and set position absolute style for that div rather than applying them individually..
working [Fiddle][1]
[1]: https://jsfiddle.net/gd5pqky7/

Prevent content from wrapping around floated element

I am well aware of the concept of "overflow" in HTML/CSS. But here I am stuck at a very simple issue.
#wrapper {
width: 100%;
}
aside {
width: 30%;
text-align: justify;
float: left;
}
section {
width: 70%;
text-align: justify;
}
<div id="wrapper">
<aside>Aside</aside>
<section>Section</section>
</div>
My wrapper div consists of aside and section.I tried to align them side by side with total width of the container. But it always appear that section overflows.I wonder why? The total width of aside plus section has never crossed width of its wrapper container.It only works if I put overflow:hidden in the section.
All you need to do to overcome the effect of the section overflowing is to set overflow to auto on the section. Now you will not need to set float on the section,...
#wrapper {
width: 100%;
}
aside {
width: 30%;
text-align: justify;
float: left;
background: green;
}
section {
width: 70%;
text-align: justify;
overflow:auto;
background: red;
}
<div id="wrapper">
<aside>Aside</aside>
<section>Section Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facere illo pariatur odit! Nobis impedit quibusdam a explicabo quod in molestias amet nemo fugiat excepturi nisi placeat ex est sequi distinctio.</section>
</div>
but be aware of the fact that if the content (any child elements) of section are extending the boundary of section will cause scrollbars on the section. So keep that in mind. You could as well use overflow:hidden which will work as well in your situation, but then any content that exceeds the boundary of section will be hidden. In case of overflow:hidden you could do the following to prevent this behavior. An example with an image as a child element of section could look like this.
#wrapper {
width: 100%;
}
aside {
width: 30%;
text-align: justify;
float: left;
background: green;
}
section {
width: 70%;
text-align: justify;
background: yellow;
overflow:hidden;
}
section img {
width:100%;
height: auto;
}
<div id="wrapper">
<aside>Aside</aside>
<section>Section Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facere illo pariatur odit! Nobis impedit quibusdam a explicabo quod in molestias amet nemo fugiat excepturi nisi placeat ex est sequi distinctio.
<img src="http://placehold.it/1000x1000" />
</section>
</div>

Having problems with aligning images like I want

Basically want I want is for my large image to be to the left of my two small images, and I want my two small images to be stacked on top of one another. Currently I'm running the class "small-images" with a width of 30% because it makes them stack like I want, but I feel there is a better way to do this. Also these three images are going to run next to some text which is why I have all of them in one big div together so I can float them to the right of the main text. I don't mind if you take my code apart I'm trying to find the most efficient way possible and I'm using HTML5 and CSS3.
HTML5
<div class="images">
<div class="large-image">
<!--This is a larger image-->
<img alt="bussiness lage picture" src="Images/buisness-big.jpg">
<div class="small-image">
<!--These are two smaller images-->
<img alt="bussiness lage picture" src="Images/handshake.jpg">
<img alt="bussiness lage picture" src="Images/calculator.jpg">
</div>
</div>
</div>
CSS 3
/*This deals with the main images right of the main content*/
.images {
float: left;
margin-top: 3%;
}
.large-image {
float: left;
margin-left: 10%;
}
.small-image {
width: 30%;
float: right;
}
You can use this kind of structure :
.images {
float: left;
margin-right: 10px;
}
.images img {
float: left;
}
.images .thumbs {
float: left;
margin-left: 10px;
}
.images .thumbs img {
clear: both;
margin-bottom: 10px;
}
<article>
<aside class="images">
<img src="http://placehold.it/300/2ecc71/fff"/>
<div class="thumbs">
<img src="http://placehold.it/145/3498db/fff"/>
<img src="http://placehold.it/145/e74c3c/fff"/>
</div>
</aside>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis suscipit dolorum repellat eveniet error a aliquam magni laudantium pariatur adipisci quisquam nesciunt unde quo. Quae ab explicabo et iure minus id ad placeat ducimus eos delectus assumenda recusandae labore! Minus modi error. Illo mollitia consequuntur ipsam fugit voluptatibus eos repudiandae!
</article>

Resources