Say we have this responsive design with 3 columns (all 3 are dynamic contents so we don't know their heights)
For desktop:
For tablet (left column moves to the right)
What's the best way we can achieve this? (I don't mind using flexbox or other modern css specs)
You can make the elements make float: left on desktop with with 1/3 width each, then on tablet/mobile give them 50% width and make 1 and 3 float: right.
HTML:
<div class="b1">
1
</div>
<div class="b2">
2
</div>
<div class="b3">
3
</div>
CSS:
div {
float: left;
width: calc(100%/3);
box-sizing: border-box;
}
#media (max-width: 600px) {
div {
width: 50%;
}
.b1,
.b3 {
float: right;
}
}
Working JSFiddle
div{
border: 1px solid #333;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 20px;
float: left;
}
.div1{
background : yellow;
width: 25%;
}
.div2{
background : red;
width: 50%;
min-height: 400px;
}
.div3{
background : purple;
width: 25%;
}
#media (max-width: 768px){
.div1, .div3{
clear: right;
float: right;
width: 30%;
}
.div2{
width: 70%;
}
}
<div class="div1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo reiciendis sapiente cumque optio incidunt, dolore impedit officiis ut tempore. Pariatur commodi perspiciatis ducimus laudantium atque sed eveniet explicabo animi ipsam!</div>
<div class="div2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo reiciendis sapiente cumque optio incidunt, dolore impedit officiis ut tempore. Pariatur commodi perspiciatis ducimus laudantium atque sed eveniet explicabo animi ipsam!</div>
<div class="div3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo reiciendis sapiente cumque optio incidunt, dolore impedit officiis ut tempore. Pariatur commodi perspiciatis ducimus laudantium atque sed eveniet explicabo animi ipsam!</div>
https://jsfiddle.net/yuywh5gv/
Try this, it might help. You can edit CSS as your wish
HTML code:
<div class="wrapper">
<div class="left"> 1 </div>
<div class="middle">2 </div>
<div class="right"> 3 </div>
</div>
CSS code:
.wrapper div {float:left;margin-right:.5%;text-align:center;}
.wrapper div:last-child {margin-right:0;}
.left {background: yellow; width:33%}
.middle {background: green; width:33%}
.right {background: blue; width:33%}
# Phone only screen and (max-width:320px) # Tablet only screen and (min-width:321px) and (max-width:768px) # Desktop only screen and (min-width:769px)
<div class="b1 col" >1</div>
<div class="b2 con">2</div>
<div class="b3 col">3</div>
add style bellow ::
div {
border: 2px solid black;
padding: 20px;
float: left;
box-sizing: border-box;
}
.con{
width: 66.66%;
}
div.col {
width: 16.5%;
}
.b1 {
height: 50px;
border-color: #ee8;
}
.b2 {
height: 200px;
border-color: #fcc;
}
.b3 {
height: 80px;
border-color: #caf;
}
#media screen and (min-width:321px) and (max-width:768px) {
div.col {
width: 33.33%;
}
.b1,
.b3 {
float: right;
}
}
DEMO
Related
This question already has an answer here:
Why is a flex item limited to parent size?
(1 answer)
Closed 4 months ago.
So I have a flex container with 2 children. And I want to assign a fixed width to the first child. But If I set width: 200px, for some reason it does not work.
Here's my code:
.carousel {
background-color: #087f5b;
color: white;
padding: 32px;
width: 800px;
display: flex;
gap: 88px;
position: relative;
}
.img-container {
width: 200px;
height: 200px;
border: 1px solid;
}
<div class="carousel">
<div class="img-container"> </div>
<blockquote>
<p class="testimonial-text">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum,
sapiente labore! Accusantium, voluptas omnis dicta facere, porro
mollitia minus ad ut debitis consequuntur.
</p>
</blockquote>
</div>
However, if I use min-width instead of just width, it works alright.
I also found out that if I delete some text from the blockquote, then it works fine.
.carousel {
background-color: #087f5b;
color: white;
padding: 32px;
width: 800px;
display: flex;
gap: 88px;
position: relative;
}
.img-container {
width: 200px;
height: 200px;
border: 1px solid;
}
.carousel blockquote {
flex: 1;
}
<div class="carousel">
<div class="img-container"> </div>
<blockquote>
<p class="testimonial-text">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum,
sapiente labore! Accusantium, voluptas omnis dicta facere, porro
mollitia minus ad ut debitis consequuntur.
</p>
</blockquote>
</div>
Here's the setup:
#out {
background-color: blue;
width: 100px;
padding: 5px;
max-height: 250px;
overflow: hidden;
}
#in {
background-color: red;
overflow: scroll-y;
}
input {
width: 100%;
box-sizing: border-box;
}
<div id="out">
<input value="stuff here that i don't know the height of">
<div id="in">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid eius voluptatibus tenetur cumque, incidunt maxime, cum dolorem sed corporis. Iste illum eaque enim cum quo saepe dicta perferendis incidunt. Accusamus.
</div>
</div>
You can see that the red box runs off the bottom. I want a scrollbar to appear instead, and the 5-pixel blue padding should be visible on the bottom.
How can I do this?
Note that I don't know the exact height of the red box. If there's less content, there should be no scrollbar, and the total height will be less than 250px.
Update: Based on your updated question, you can use display:flex instead of the max-height that I initially suggested and keep the overflow:auto.
#out {
background-color: blue;
width: 100px;
padding: 5px;
max-height: 250px;
overflow: hidden;
display: flex;
flex-direction: column;
}
#in {
background-color: red;
max-height: 240px;
overflow: auto;
}
<div id="out">
<input value="stuff here that i don't know the height of">
<div id="in">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid eius voluptatibus tenetur cumque, incidunt maxime, cum dolorem sed corporis. Iste illum eaque enim cum quo saepe dicta perferendis incidunt. Accusamus.
</div>
</div>
Ok so we have a div #out with a max-height to get scrollbars. The #in div will add the blue border and the #scroll div contains the content that will scroll on overflow-y
#out {
width: 100px;
max-height: 250px;
position:relative;
background:blue;
padding:5px;
}
#in {
background-color: red;
height:240px
}
#scroll{
height:100%;
overflow-y: scroll;
overflow-x: hidden;
}
<div id="out">
<div id="in">
<div id="scroll">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid eius voluptatibus tenetur cumque, incidunt maxime, cum dolorem sed corporis. Iste illum eaque enim cum quo saepe dicta perferendis incidunt. Accusamus.
</div>
</div>
</div>
#out {
width: 100px;
max-height: 250px;
overflow: scroll;
background-color: blue;
padding: 5px;
}
#in {
background-color: red;
overflow: scroll;
max-height: 245px;
}
I have a little question due to position of background image in my footer.
As You can see on the picture, my current background image (green dotted line with ball - it is svg image) is placed in the middle if the footer.
I would like to place it in the position of the red line, staying there while resizing window.
Code of it is:
footer{
position: relative;
/* START OF IMAGE BG */
&:before{
content: "";
position: absolute;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: url("/wp-content/themes/company/static/img/line.svg");
-moz-background-size:90%;
background-size:90%;
background-repeat: no-repeat;
}
/* END OF IMG BG */
padding: rem-calc(45 20);
#media #{$medium-up}{
padding: rem-calc(85);
}
background-color: $bluedark;
color: $white;
min-height: rem-calc(500);
p{
font-size: rem-calc(12);
#media #{$medium-up}{
font-size: rem-calc(16);
}
font-weight: 300;
&.section-header{
text-transform: uppercase;
font-weight: 600;
}
&.bold{
font-weight: 600;
}
&.green{
color: $green;
}
}
.social-media{
img{
display: inline-block;
margin: rem-calc(0 5 20 5);
max-height: rem-calc(20);
#media #{$medium-up}{
max-height: rem-calc(40);
}
}
}
.underline{
margin-top: rem-calc(50);
border-top: 1px solid $bluegrey;
padding-top: rem-calc(20);
.logo{
float: left;
width: rem-calc(200);
height: rem-calc(45);
background: url("/wp-content/themes/company/static/img/logo_light.svg");
background-size: contain;
background-repeat: no-repeat;
}
select{
float: right;
}
}
}
And html is:
<footer>
<div class="line"></div>
<div class="flex">
<div class="row">
<div class="large-3 columns">
<p class="section-header">O nas</p>
<p>O Firmie</p>
<p>Zespół</p>
<p class="green">Blog</p>
</div>
<div class="large-3 columns">
<p class="section-header">Pomoc</p>
<p>FAQ</p>
<p>Regulamin</p>
<p>Polityka prywatności</p>
</div>
<div class="large-3 columns">
<p class="section-header">Social Media</p>
<div class="social-media">
<img src="{{site.theme.link}}/static/img/social-media/facebook.png">
<img src="{{site.theme.link}}/static/img/social-media/twitter.png">
<img src="{{site.theme.link}}/static/img/social-media/instagram.png">
</div>
</div>
<div class="large-3 columns">
<p class="section-header">Kontrakt</p>
<div class="button green">Formularz kontaktowy</div>
</div>
</div>
<div class="row underline">
<div class="logo"></div><span>
<select>
<option value="Polski">Polski</option>
<option value="English">English</option>
</select>
</div>
</div>
I know it is wrong placed due to top:0 however I dont know how to make it stay right on the top border of footer.
I would be grateful for any help.
A similar example of yours, I think its usefull for your purpouse:
div {
border: solid 1px green;
margin-bottom: 20px;
}
footer {
position: relative;
height: 50px;
background-color: red;
text-align: center;
padding-top: 20px;
}
footer:before {
content: "";
position: absolute;
display: block;
top: -50%;
left: 0;
width: 100%;
height: 100%;
background: url("http://www.curtainshopsouthport.co.uk/scissors.png");
-moz-background-size: 90%;
background-size: 90%;
background-repeat: no-repeat;
background-position: left 50%;
}
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ullam quis placeat architecto dolore recusandae nam amet, voluptate consequatur tenetur, quibusdam cupiditate culpa perferendis praesentium ab quasi voluptatum doloremque illum suscipit ea. Placeat
aperiam tempore maiores minus harum sint debitis beatae sit, eos voluptas est temporibus animi ullam praesentium voluptates molestiae dolore illo officiis blanditiis dolores. Qui labore asperiores quia dolore amet, culpa consectetur est quibusdam iusto
incidunt maxime aliquid sit eius explicabo aut, possimus corporis temporibus. Alias officia libero repellat veritatis, obcaecati repudiandae at voluptas, maxime doloremque facilis, sunt praesentium voluptatibus eaque provident natus, earum asperiores?
Possimus voluptatem, soluta deserunt.</div>
<footer>footer</footer>
As long as you don't have overflow issues, all you should need to do is replace top:0 with bottom:100%
footer{
position: relative;
&:before{
content: "";
position: absolute;
display: block;
bottom: 100%; /* change here */
left: 0;
width: 100%;
height: 100%;
padding-bottom: 50%;
background: url("img/line.svg");
-moz-background-size:90%;
background-size:90%;
background-repeat: no-repeat;
}
Currently how it looks on page:
The goal: I'm trying to make the orange buttons set at an equal height regardless of the (blurred) text above it.
Relevant SCSS:
.medium-2 {
padding: .5rem;
background-color: white;
box-shadow: 0px 0px 5px 3px rgba(0,0,0,0.10);
margin-left: 2.5em;
text-align: center;
float: left;
width: 20%;
height: 450px;
h4 {
font-size: 16px;
border: none;
color: #5d5d5d;
font-family: $font-family-sans-serif;
}
p {
padding-bottom: 10%;
}
.button {
text-align: center;
}
}
Is there a non-hacky way to do this? Right now my only fix is to go into the html.erb file and add in some additional padding-down to the smaller block of p to push the orange button down in place.
Flexbox can easily solve this for you.
.content {
height: 200px;
width: 200px;
border: 1px solid;
display: inline-flex;
flex-direction: column;
margin-right: 10px;
}
p {
margin: 0;
}
button {
margin-top: auto;
width: 200px;
}
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit rerum neque laboriosam perspiciatis sapiente optio ipsam ea magni, accusantium eos quaerat ullam facilis hic quo aperiam a iure porro inventore.</p>
<button>Button</button>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet</p>
<button>Button</button>
</div>
I have a fixed side bar on the left and a right content area. Is there an alternative to calculating the content width other than calc()? I wanted a more browser safe method.
.left-sidebar {
width: 160px;
height: 100%;
border-right: 1px solid black;
position: fixed;
top: 72px;
}
.right-content {
position: absolute;
left: 160px;
top: 72px;
width: calc(100% - 160px);
overflow: hidden;
}
I have already done a similar example, which I would like to share. You need to use positioning for this case. This is a case of fixed-fluid:
+-------+-----------+
| FIXED | FLUUUUUID |
+-------+-----------+
Or
+-------+-----------+
| FIXED | FLUUUUUID |
| | FLUUUUUID |
+-------+-----------+
Fixed-Fluid Model. In my snippet, I have demonstrated two kinds of examples. In the first case, the fluid is less in size. And the next has too long content.
Snippet
.parent {position: relative; margin: 0 0 15px; border: 1px solid #999; padding: 5px; padding-left: 100px;}
.parent .fixed {position: absolute; left: 5px; width: 90px; background-color: #99f;}
.parent .fluid {background-color: #f99;}
<div class="parent">
<div class="fixed">Fixed</div>
<div class="fluid">Fluid</div>
</div>
<div class="parent">
<div class="fixed">Fixed</div>
<div class="fluid">Fluid Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque animi placeat, expedita tempora explicabo facilis nulla fuga recusandae officia, maiores porro eaque, dolore et modi in sapiente accusamus id aut.</div>
</div>
For a better fixed fluid, I have done with the same kind for you:
.main-content {border: 1px solid #999; padding: 5px; position: relative; min-height: 200px; padding-left: 125px;}
.left-sidebar {position: absolute; left: 0; top: 0px; width: 120px; background-color: #eee; height: 100%;}
<div class="main-content">
<div class="left-sidebar"></div>
<div class="right-fluid">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum libero iure facere quam iste, nostrum laborum in, dolorum beatae optio rem explicabo voluptates qui quos eius accusamus! Accusamus blanditiis, et!
</div>
</div>