How to decrease width of the box from both right and left equally when the containing block size changes? - css

I have made a bordered box. Here is the css code:
.Box{
height: 500px;
width: 700px;
border: 2px solid black;
margin: auto;
margin-top: 10px;
}
When I run the following CSS my box appears as shown in the image below.
Now when I try to decrease the size of the containing block of the box, till there is a margin on both sides the box remains in the middle and the margins decrease but once the margin space is over the left border becomes stationary and only the right portion starts to shrink.
In the above image, it could be seen that the margin has fully shrunken and now the left side is stationary and the right side is only shrinking
I want the box to shrink from both sides equally even when the margin is over. Please guide me on how I could achieve this. Please let me know if more information is required.

You can use the CSS function min to make sure the box is 700pa when its container has a width greater than or equal to 700px, and thereafter it will take on the width of the container.
This snipper transitions the container width from 100vw to 100px (and vice versa) when you click the button. It has a backgound color so you can see when it is larger than the box.
body {
width: 100vw;
}
.container {
width: 100vw;
transition: width 5s linear;
margin: 0 auto;
background-color: #eeeeee;
}
.container.shrink {
width: 100px;
}
.Box{
height: 500px;
width: min(700px, 100%);
border: 2px solid black;
margin: auto;
margin-top: 10px;
}
<button onclick="document.querySelector('.container').classList.toggle('shrink');">Click me to make the container shrink/grow</button>
<div class="container">
<div class="Box"></div>
</div>

In your approach if you change your width:700px; to max-width:700px;
when screen shrinks, both left and right borders will shrink with them because when the screen width is smaller your box's width will be equal to screen width.
.Box{
height: 500px;
max-width: 700px;
border: 5px solid red;
margin: auto;
margin-top: 10px;
}
body {
min-height: 100vh;
margin:0;
}
.container {
height: 500px;
max-width: 700px;
border: 5px solid red;
margin: auto;
margin-top: 10px;
}
<div class="container"></div>

Related

Image border overlapping div padding

I have some stupid problem but it bothers me quite a lot. I already tried some things but nothing works so maybe someone of you will have a soultion.
I have a centered picture inside div and as you can see on the picture the border of the image on the right side overlapping a little bit the padding of the div. I tried different things but I canno't "push" the image to the left.
Both img and div width have to be set as 300px.
Overlapping border
and heres some code for img and div
img {
width: 300px;
height: auto;
border: 2px solid rgba(115,186,155,.5);
border-style: dotted;
border-radius:8px;
display: block;
margin-left: auto;
margin-right: auto;
margin-bottom: 20px;
}
#first {
border: 1px solid rgba(115,186,155);
padding:15px;
width: 300px;
}

Why are positioned divs overlapping?

I have a main wrapper div with 2 content divs inside. The position attribute of both content divs is set to relative, but for some reason they're overlapping as shown here:
I want the div outlined in red to be underneath the blue one and am having trouble figuring out how to do so.
#wrap {
height: 500px;
width: 350px;
border: 3px solid black;
}
#upper {
position: relative;
width: 40%;
height: 70%;
top: 5%;
left: 2%;
border: 1px solid blue;
text-align: center;
}
#lower {
position: relative;
width: 40%;
height: 20%;
left: 2%;
border: 1px solid red;
}
<div id="wrap">
<div id="upper"></div>
<div id="lower"></div>
</div>
Can someone please help me figure out how to align them correctly?
The styling of the div#upperDiv has top:5% which causing this to happen. Although relative but div#upperDiv is taking the 5% top to overlap on div#lowerDiv.
Solution: EITHER take that top:5% styling off from upperDiv OR add the same top style to lowerDiv.
For lower div can you try adding clear:both;
#lowerDiv {
position: relative;
clear:both;
width: 40%;
left: 2%;
border: 1px solid red;
text-align: center;
}
It is happening because u r using height in percentage. As you've taken Height of upperDiv is 70%. it is taking 70% of ur main div. and ur lower div is having more data than it can adjust in the same outer div. so ur main div should big enough so that ur lowerDiv can adjust in remaining 30% of space u r providing to it. or u can adjust ur upperDiv's percentage value of height so that both can adjust in that space.

How to fix an element within a bootstrap container?

I have a fixed a button at the right of a bootstrap container.
But in high resolution, the button moves outside of the bootstrap container as it is fixed relative to body, not container.
Note: It must be fixed (not absolute) because I don't want it to scroll with window.
Here is the code:
<div class="body">
<h1>Body</h1>
<div class="container">
<h1>Container</h1>
Enquire Now
</div>
</div>
CSS:
.fixed-btn {
padding: 10px 20px;
background: #000;
color: #FFF !important;
text-decoration: none !important;
line-height: 30px;
position: fixed;
right: 70px;
top: 50px;
}
.body {
background: aquamarine;
min-height: 1000px;
}
.container {
background: antiquewhite;
min-height: 1000px;
max-width: 400px;
}
Is it possible to make it fixed within a bootstrap container? so it would not move outside of the container.
Look a live code at JSFIDDLE.
One possibility is to position the fixed element using calc
.fixed-btn {
position: fixed;
left: calc(50% + (400px/2)) ;
}
In this case the 400px (from your demo) would be the width of the container based on the various widths in Bootstrap.
You would have to adjust this in each media query.
JSfiddle Demo

Stop floating div with "fixed" size from floating out of container

I have a <div> container with a certain max-width,
max-width: 300px;
margin: 4em auto;
border: 1px solid black;
that contains two things: 1) text, 2) a floating div with
float: right;
width: 150px;
See my example at http://jsfiddle.net/uXEBR/.
When you reduce the window’s width, the floating <div>, as expected, extends outside the containing <div>, getting beyond its left border. However, I would like it to decrease in width so that it never leaves the outer <div>’s border. In other words, the width specification of the floating element should be conditional on sufficient width of the outer div. Is there a way to achieve this in CSS?
The other option is a media query. Here is an example based on the code you supplied. Keep in mind that .divright element will only shrink as small as the longest word in the div.
>>jsFiddle<<
CSS:
.divout {
max-width: 300px;
margin: 4em auto;
border: 1px solid black;
}
.divright {
float: right;
max-width: 150px;
margin-right: 1.25em;
border: 1px solid black;
}
#media screen and (max-width: 225px) {
.divright {
margin-left: 1.25em;
}
}
Sure:
width: 50%;
max-width: 150px;

How to add borders to div without messing up the layout?

I have the following elements:
<body>
<div id="container">
<div id="sidebar1"></div>
<div id="content">
<h3>Lorem ipsum</h3>
<p>Whatnot.</p>
</div>
<div id="sidebar2"></div>
</div>
</body>
Following this style:
/* ~~ this fixed width container surrounds all other divs~~ */
#container {
width: 960px;
background-color: #FFF;
margin: 0 auto;
overflow: hidden;
}
#sidebar1 {
float: left;
width: 180px;
/*border: 2px solid black;*/
background-color: #EADCAE;
padding: 0px 0px 100% 0px;
}
#content {
padding: 10px 0;
width: 600px;
float: left;
}
#sidebar2 {
float: left;
width: 180px;
/*border: 2px solid black;*/
background-color: #EADCAE;
padding: 0px 0px 100% 0px;
}
I am trying to achieve this layout: http://jsfiddle.net/QnRe4/
But as soon as I un-comment the borders it turns into this: http://jsfiddle.net/FZxPQ/
** Solved **
The border width was added to each element's total width making them too wide to fit in the container. Removing 2x the border width from each column's width solves the problem: http://jsfiddle.net/FZxPQ/4/
CSS box-sizing to the rescue! This property
alters the default CSS box model used to calculate widths and heights of elements
The border-box value means that
the width and height properties include the padding and border
/* support Firefox, WebKit, Opera and IE8+ */
#container, #sidebar1, #sidebar2 {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
However, browser support is not 100% standardized.
As other answers have already mentioned the extra width which pushes the sidebars out of alignment is because the width calculation includes the border width. box-sizing simply tells the browser that an element with a given width/height should include any border and padding values into the final width/height calculations.
The problem is that when you add in the boarder, the size of the outer divs increased by 4, 2px on each size. So, your container needs to grow in size by 8px.
So change your container to:
#container {
width: 970px;
background-color: #FFF;
margin: 0 auto;
overflow: hidden;
}
See: http://jsfiddle.net/QnRe4/13/
When you apply the borders, that goes outer the divs, so the sidebars will have 184px width which doesn't fits to the container. try addig width: 176px
http://jsfiddle.net/QnRe4/12/
#sidebar1 {
float: left;
width: 176px;
border: 2px solid black;
background-color: #EADCAE;
padding: 0px 0px 100% 0px;
}
Like this? http://jsfiddle.net/QnRe4/3/
What's happening is that your elements are losing their block display properties when you remove the borders.
So, adding display: block to those elements resolves that.
I've also adjusted your element's widths by 4px in width to retain the layout, since removing those borders essentially reduces the space that those elements occupy on-page.

Resources