Button width % seems to jump out of container on smaller screens - css

Using bootstrap3 with mvc5.
Using the grid system, I have 4 across in a row ("col-md-3"). Here is HTML for one box:
<div class="col-md-3">
<div class="index-box">
<h2>Other Reports</h2>
<p>Click this button to go to the report index page.</p>
<button class="big-button">View Reports</button>
</div>
</div>
In full screen, the button sits nicely in the box, but when I view on mobile device, the boxes are stacked (as they should) but the button width becomes a percent of the full screen instead of the .index-box. HEre is current css:
.index-box {
border: 1px #527f03 solid;
border-radius: 5px;
text-align: center;
padding-bottom: 20px;
height: 250px;
max-width: 260px;
}
.index-box h2 {
position: relative;
top: -25px;
background-color: #dfece2;
display: inline-block;
}
.big-button {
position: absolute;
margin-left: -35%;
left: 50%;
width: 70%;
max-width: 70%;
bottom: 20px;
background: #9cc64e;
padding: 5px 10px;
color: #23423a;
font-size: 18px;
text-decoration: none;
}
How it looks on mobile screen width:

Using Bootstrap's classes when available, such as .btn-block will give you a full-width button, then wrap that in a container and add margin or padding to it. And remove the absolute positioning...
This should point you in the right direction....
.big-button {
background: #9cc64e;
padding: 5px 10px;
color: #23423a;
font-size: 18px;
text-decoration: none;
}
.big-button-cont {
padding: 0 15%;
}
</style>
<div class="col-md-3">
<div class="index-box">
<h2>Other Reports</h2>
<p>Click this button to go to the report index page.</p>
<div class="big-button-cont">
<button class="btn big-button btn-block">View Reports</button>
</div>
</div>
</div>
it's a good idea to take bootstrap's classes and modify them in an "override" that loads after the bootstrap.css stylesheet, this way you can keep it within the framework and just change the bits and pieces you need, such as the button color, etc.

Try adding position relative the index-box. An element that is positioned using absolute behaves like fixed except relative to the nearest positioned ancestor instead of relative to the viewport. If it has no positioned ancestor it will position itself relative to the viewport

Related

How is "double border" eliminated for stacked inline-block elements by negative margins?

If we place 3 x 3 inline-block elements together, we know their borders will touch each other and "double up":
span {
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
border: 3px dotted #999;
font: 42px Arial;
line-height: 60px;
}
<div>
<span>1</span><span>2</span><span>3</span>
</div>
<div>
<span>4</span><span>5</span><span>6</span>
</div>
<div>
<span>7</span><span>8</span><span>9</span>
</div>
If we don't use a table to do it, some developers using a margin-top and margin-left to fix it:
margin-top: -3px;
margin-left: -3px;
span {
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
border: 3px dotted #999;
font: 42px Arial;
line-height: 60px;
margin-top: -3px;
margin-left: -3px;
}
<div>
<span>1</span><span>2</span><span>3</span>
</div>
<div>
<span>4</span><span>5</span><span>6</span>
</div>
<div>
<span>7</span><span>8</span><span>9</span>
</div>
The effect with border being 1px:
span {
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
border: 1px dotted #999;
font: 42px Arial;
line-height: 60px;
margin-top: -1px;
margin-left: -1px;
}
<div>
<span>1</span><span>2</span><span>3</span>
</div>
<div>
<span>4</span><span>5</span><span>6</span>
</div>
<div>
<span>7</span><span>8</span><span>9</span>
</div>
But we also know a negative margin "moves the element", similar to
position: relative; top: -3px; left: -3px
And if the borders "double up", moving every element "relatively" is not going to fix it, supposedly. So how does negative margin make it work? Can the claim be substantiated by CSS specs.
You mentioned the following code:
position: relative;
top: -3px;
left: -3px;
top: -3px; moves each element up by 3px.
left: -3px; moves each element left by 3px.
The code therefore won't solve your problem because all elements are shifted – the whole grid moves to the top left, and borders still double up.
margin: -3px; is something different. It positions each box normally, but as if they were all was 3px smaller on each side.
This means each box moves closer to the element adjacent to it, and borders no longer double up.
margin affect the layout while top/left only move the element from its normal flow position without affecting the layout.
Here is a basic example to understand the difference:
.box {
height:50px;
background:red;
border:2px solid;
}
<div class="box"></div>
<div class="box" style="margin-top:-50px;"></div>
<div class="box"></div>
Notice how we shift the second element and the third follow it.
Now let's use top
.box {
height:50px;
background:red;
border:2px solid;
}
<div class="box"></div>
<div class="box" style="top:-50px;position:relative"></div>
<div class="box"></div>
The third will not move. top will simply affect the concerned element
From the specification:
relative
The box's position is calculated according to the normal flow (this is called the position in normal flow). Then the box is offset relative to its normal position. When a box B is relatively positioned, the position of the following box is calculated as though B were not offset.
For the margin I guess it's more trivial since margin is a part of the box model like defined here. You won't find an explicit sentence saying how negative margin behaves but following its logic we can understand that negative margin will affect the box of the concerned element and will affect the adjacent elements.
So negative margin doesn't really move the element the same way as top/left.

Carousel, make a div within an item same height as sibling item's div

I have an responsive Owl Carousel (v2), each item or slide has an image and below that some text of variable length, see image below:
As can be seen, all the images are bottom aligned to the same baseline, regardless of how much text there is. I've done this by setting the text div to a fixed height. The problem is, if there were to be just one line of text, I'd have unnecessary space below the carousel.
If I allow the div to set its own height, I get this:
So my images are no longer lined up.
HTML
<div>
<img class='a4_diary_image' src='sizes/test.png'>
<div class='owl_diary_desc'>
A4 size, this is going on to two lines
</div>
</div>
<div>
<img class='a5_diary_image' src='sizes/test.png'>
<div class='owl_diary_desc'>
A5 size
</div>
</div>
<div>
<img class='a6_diary_image' src='sizes/test.png'>
<div class='owl_diary_desc'>
A6 size
</div>
</div>
</div>
CSS
.owl-carousel .owl-item {
display: table-cell;
float: none;
text-align: center;
vertical-align: bottom;
border: 1px dashed grey;
height: 100%;
padding: 0px 10px;
}
.owl_diary_desc {
font-size: 19px;
border: 1px dashed red;
margin-top:10px;
}
.a4_diary_image {
max-width: 100%;
max-height: 100%;
margin-left: auto;
margin-right: auto;
}
.a5_diary_image {
max-width: 70%;
max-height: 70%;
margin-left: auto;
margin-right: auto;
}
.a6_diary_image {
max-width: 50%;
max-height: 50%;
margin-left: auto;
margin-right: auto;
}
Straight HTML and CSS won't allow you to set equal heights based off siblings. Using a little jquery this can be achieved though.
$maxDesc;
function equalize(){
$('.owl_diary_desc').each(function(i){
if ($(this).height() > $maxDesc) { $maxDesc = $(this).height();}
});
$('.owl_diary_desc').each(function(i){$(this).height($maxDesc);});
}
When I use something like this, I generally move the variable holder to the beginning of the script. Then I call the function on document ready. Sometimes I'll even call it on the window resize function. If you choose to do that, you must call an each function on your object and reset the height to auto before recalling the equalize function.

Can't get element below fixed div to show full height with paddings or margins

Fiddle: http://jsfiddle.net/rg3w8kxc/2/
I have a fixed bar on top and an element below it. Since the top bar is fixed, I need to add some padding to the top of the element below it so that the whole height of that element shows. However, when I add something like padding-top:40px for example, it doesn't move the element down; rather it creates space below the element. Same goes with margin.
I feel like I'm missing something obvious. What's the issue here?
Here's my HTML:
<div id="top-bar">
<div class="section-wrap">
Win a [name of phone]!
</div><!-- .section-wrap -->
</div>
<div id="top-section-page">
<div class="section-wrap">
<span>⇦</span> Back to the mix
</div>
</div>
<p>Some text here</p>
Here's my CSS:
#top-bar {
background: #FAFAFA;
height: 60px;
line-height: 60px;
padding: 0 20px;
position: fixed;
width: 100%;
z-index: 999;
}
#top-section-page {
background: url("http://i.imgur.com/KNYV8j2.jpg") repeat center top #69C9CA;
border-bottom: 10px solid #FFF;
line-height: 185px;
margin-bottom: 100px;
}
You can add add the padding-top on the body and then you need a top 0px on the #top-bar
Add this to your css code:
body{
padding-top: 40px;
}
#top-bar {
top: 0px;
}

Dynamically resize text in Div tag based on page resize and available space

Here is an example what I will try to describe below: http://jsfiddle.net/9E3yJ/17/
When resizing the screen size to anything less than 930px, the right image wraps under the center div tag with text. Is there a way to dynamicly have the center div tag resize itself and wrap the words so that the min-width becomes 67px and the right image does not wrap under the div tag until only the center div tag does not have any more room to wrap(min-width=67px) then ends up looking like this: http://jsfiddle.net/9E3yJ/18/
Thank You!
<style>
#logo {
display: block;
float: left;
line-height: 126px;
height: 130px;
max-width: 100%;
vertical-align: middle;
}
#logo img {
margin: 5px 0 5px 17px;
}
#topText{
width: 210px;
height: 130px;
line-height: 130px;
text-align: center;
float: left;
font-size: 16px;
text-transform: uppercase;
font-weight: bold;
}
#topTextchild{
display:inline-block;
line-height:1;
vertical-align: middle;
}
#longPic {
width: 468px;
height: 60px;
display: block;
float: right;
vertical-align: middle;
padding: 5px;
margin: 30px 17px 0 0;
background: rgb(190, 218, 247);
border: 1px solid rgb(0, 0, 0);
border-radius: 10px 10px 10px 10px;
}
</style
><div class="pageheader">
<div id="logo"><a href="http://www.sitelogo">
<span></span>
<img src="http://www.roirevolution.com/blog/2008/01/04/confusedbird.gif" alt="Logo">
</a>
</div>
<div id="topText">
<div id="topTextchild">
The needed Header Text For the top of the page.
</div>
</div>
<div>
<div id="longPic">
<a href="http://www.sitepicture">
<span></span>
<img src="http://nature-drops.puzl.com/puzl/images/3000/3281/gallery/51c870b940060.jpg" alt="image">
</a>
</div>
</div>
<span class="helper"></span>
</div>
BTW, the second thing I will be trying to do(after the center div tag can no longer shrink and eventually forces the right image under it) is use media tags to have different css for smaller screens and have the center text divs be full width and line up all the text in one row above the right image. This will make the center text be pushed above the right image instead of having the image go under the text.
I see two options unless JavaScript could be used.
First
Use
#media screen and (max-width: 830px) {
...
}
Example here: http://jsfiddle.net/9E3yJ/20/
Second
Use display: table for pageheader and display: table-cell for its children - http://jsfiddle.net/9E3yJ/21/.

float div and keep it at the baseline

I'm working on a chat module for a project, everything is working but the css. I have a global container for the chat elements, this div has fixed position. Inside I have two divs, one for the chat windows and one for the contacts list, both the chat window and the contact list are floating to the right and can be "minimized" by clicking on the title (this hides the body and only leaves the title visible). The problem is if I minimize just one of the divs it remains on the top at the same height as the other div (see the image).
This is what I'm getting:
This is what I want:
Relevant code:
<body>
<!--boring code-->
<div class="chat_container">
<div class="contactos show">
<div class="titulo">contactos</div>
<div class="container">
<div class="contacto online" id="contacto_3">juan an orozco</div>
</div>
</div>
<div class="chat_wdow_container">
<div class="chat_wdow " id="chat_wdow_3">
<div class="title_area">juan an orozco</div>
<div class="container">
<div class="msg_area"></div>
<input type="text" name="msg">
</div>
</div>
</div>
</div>
</body>
and css
div.chat_container
{
position: fixed;
bottom: 0px;
left: 0px;
right: 0px;
border: 1px dashed gold;
}
div.chat_container > div
{
float: right;
}
div.chat_container div.contactos div.titulo
{
text-align: center;
}
div.chat_container div.contactos
{
min-width: 150px;
background: dimgrey;
padding: 5px;
border-radius: 10px 10px 0 0px;
}
div.chat_container div.contactos div.container
{
display: none;
min-height: 145px;
padding: 10px;
}
div.chat_container div.contactos.show div.container
{
display: block;
}
div.chat_container div.chat_wdow
{
margin: 0 5px;
min-width: 190px;
background: dimgrey;
padding: 5px;
border-radius: 10px 10px 0 0px;
float: left;
}
div.chat_container div.chat_wdow div.title_area
{
text-align: center;
}
div.chat_container div.chat_wdow div.container div.msg_area
{
background-color: white;
height: 120px;
padding: 10px;
}
div.chat_container div.chat_wdow div.container
{
display: none;
}
div.chat_container div.chat_wdow.show div.container
{
display: block;
}
.chat_wdow input[type="text"]
{
width: 186px;
}
To collapse the window I toggle via mootools the class .show. When this class is missing the container area of the windows has display:none and when it gets applied it has display:block.
What I have tried so far:
setting the fixed parent to a height of 0 and overflow visible
seting the inner container to position relative and the child to absolute
using clear and overflow hacks
changing margins to auto values
changing vertical sizes and minimun heights of the inner containers and childs
changing display to inline and inline block
changing chat container to absolute and inner containers to relative
I have been searching for a while on google and SO but I have only found the options that I have already tried, I also looked at facebook's chat css but I can't find anything to help me, so I am looking for new ideas to bring down the collapsed div.
One solution is to use display:inline-block or display:inline instead and then set the vertical-align:bottom.
Ex: http://jsbin.com/uhubeh/1/edit
If you know the widths of both, however, you could also just use absolute positioning.

Resources