CSS background diappearing when Overflow is Visible - css

I have a site that has the following basic structure. This site should have a background that is white, with an image that apears once, but instead it just inherits the colour from the html{ } declaration in CSS. All elements below the element that should have the background are transparent, and even though the background is being added (checked in Firebug), it seems that this is below the background defined in html{ }.
This has only happened since I removed the declaration overflow: none; from #content-container, where as before that it worked. I need to remove this however, as changes that are occuring to the site require the nav menu to have dropdowns, so the container below has to allow overflow.
Is there a specific CSS reason why this is happening? Or anything else I need to provide for someone to be able to help? Thanks.
<div id="main-container">
<div id="header-container">
<div id="header-top">
{Code}
</div>
<div id="header-middle">
{Code}
</div>
<div id="header-nav">
{Code}
</div>
</div>
<div id="content-container">
<div id="content-left" class="index">
{Code}
</div>
<div id="content-right">
{Code}
</div>
</div>
<div id="footer-container">
{Code}
</div>
</div>

I'm guessing that #content-left and #content-right elements are floated? In which case the overflow: none on the #content-container was causing the element to self-clear. Without this, the element will not have a height because all the elements within it are floated, and therefore the containers' height cannot be calculated.
If you must use overflow: visible, the workaround is to place a div at the end of the containing element with clear: both set on it:
<div id="content-container">
<div id="content-left" class="index">
{Code}
</div>
<div id="content-right">
{Code}
</div>
<div class="clear"></div>
</div>
.clear { clear: both; }

Related

How to change CSS of child element using attribute selector

I want to hide a div when it belongs to a parent div which has the attribute data-ontarget set to true.
I tried the following, but nothing happens.
If I remove the class name after the attribute selector in the example below, the whole parent div becomes hidden. So the attribute selector works, but not in combination with the search for a child class.
Here is what I tried:
HTML:
<div class="myClass" data-ontarget="false">
<img src="myImage1.png"/>
<div class="myImage">This is my image 1</div>
</div>
<div class="myClass" data-ontarget="true">
<img src="myImage2.png"/>
<div class="myImage">This is my image 2</div>
</div>
CSS:
[data-ontarget="true"] .myImage{
display: none;
}
Replace opacity property with display to hide the element.
.input_container[data="true"] .awsome_input_border{
opacity: .5;
}
.input_container[data="false"] .awsome_input_border{
opacity: 1;
}
<div class="input_container" data="true">
<span class="awsome_input_border"/>
hide me!!
</div>
<div class="input_container" data="false">
<span class="awsome_input_border"/>
hide me!!
</div>
I think you are missing the proper CSS, which makes your target div as hidden.
Refer to the demo here.
Please find the code below:
HTML:
<div class="parent">
<div class="myClass" data-ontarget="false">
<img src="myImage1.png" />
<div class="myImage">This is my image 1</div>
</div>
<div class="myClass" data-ontarget="true">
<img src="myImage2.png" />
<div class="myImage">This is my image 2</div>
</div>
</div>
CSS:
.parent > div[data-ontarget="true"] {
display: none;
}

Bootstrap: Floated divs cause parent elements to collapse. Best way to structure nested columns?

I'm really struggling with Bootstrap regarding the way it handles divs.
It seems to me, that all column classed divs using Bootstrap CSS are floated. For example:
<div class="col-md-3">
property
</div>
This above div has the style: float: left; as defined in Bootstrap CSS.
Now everyone knows about the issue with floating, it causes the parent div to not expand to the height because it doesn't 'see' floated elements as pushing out the container.
I feel like this is a serious flaw in Bootstrap. I have some classes to add margin to divs, for example:
.full-buffer{margin:20px 0}
If I want to use this class on a div that wraps a number of Bootstrap columns, it doesn't work. Because the div has no height. If I wanted to add a background colour, it won't show up. For example:
.background-coloured-div{background-color:#0F0;}
<div class="background-coloured-div">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
The above won't show any background colour, because all of the divs inside are floated, so it'll collapse to 0px in height.
JSFiddle without bootstrap
JSFiddle with bootstrap - fixes one problem and causes another
So, what's the proper way to use Bootstrap columns? If everything is floated, how can you add proper margins, background colours etc to parent divs? Isn't this a massive flaw of the whole system?
Bootstrap has a row class that contains the floats using:
.row:before {
display: table;
content: " ";
}
.background-coloured-div {
background-color: #0F0;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="background-coloured-div row">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
You'll want to wrap those cols in a .row div to contain the floats.
Don't forget to also contain that row in a .container or .container-fluid to counter balance the -15px margin applied to .row.
<div class="container-fluid">
<div class="background-coloured-div row">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
</div>
http://getbootstrap.com/css/#grid-example-fluid
If for whatever reason you don't want to use a .row, you could add the following to your css to contain the floats of that divs children, but I recommend using Bootstraps solution:
background-coloured-div:after {
content:'';
display:table;
clear:both;
}
Rows must be placed within a .container (fixed-width) or
.container-fluid (full-width) for proper alignment and padding.
Use rows to create horizontal groups of columns.
Content should be placed within columns, and only columns may be
immediate children of rows.
In your case, the code should be:
<div class="container-fluid">
<div class="row background-coloured-div">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
</div>

One pixel wide artefact using Bootstrap grid with backgrounds

I'm not going to try to explain this in words, just have a look at this fiddle:
https://jsfiddle.net/fhf8rwno/4/
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="myBox" style="height:100px;background-color:#000;">
<div class="row no-gutter">
<div class="col-xs-7">
blbalbalabla
</div>
<div class="col-xs-5" style="height:100px;background-color:red;">
wowowowo
</div>
</div>
</div>
</div>
</div>
</div>
In either Chrome or Firefox (possibly other browsers too), watch the red column as you resize the browser width. You should notice that on every other change in browser side a one-pixel-wide gap appears at the right edge of the parent container, allowing the parent background to come through.
If I instead use col-xs-6 instead of col-xs-7 and col-xs-5, the issue disappears. So it seems the browser's pixel math may cause this due to the odd/even mix of column ratios.
This may not seem like much, but the site I'm working on uses this pattern a lot and half the users are seeing some very noticeable and unsightly dark lines.
Any thoughts or suggested hacks?
Edit: here's a hacky way of achieving this. http://jsfiddle.net/fhf8rwno/8/
CSS
.row.no-gutter {
margin-left: 0;
margin-right: 0
}
.row.no-gutter [class*='col-'],
.row.no-gutter [class*='col-'] {
padding-right: 0;
padding-left: 0;
}
HTML
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="myBox" style="height:100px;background-color:#000;">
<div class="row no-gutter">
<div class="col-xs-7">
blbalbalabla
</div>
<div class="col-xs-5" style="height:100px;background-color:red; position: relative; right: -1px;">
wowowowo
</div>
</div>
</div>
</div>
</div>
</div>
A colleague and I have come to a semi-solution, but I'm not going to accept this answer just yet in case someone comes up with something less hack-y.
Shifting the last column's margins by one seems to help the issue:
.row.no-gutter [class*='col-']:last-child {
margin-right:-1px;
margin-left:1px;
}
https://jsfiddle.net/fhf8rwno/6/

How to put a div to the right of .container in Bootstrap?

Basically, I need to put a back-to-top button at the right side of the footer.
Something like this:
What I get is this:
You can see that there is a blank space between footer and the end of viewport, that space is the height the back-to-top button, if I remove the button the blank space is removed too.
I'm using bootstrap so my html code is similar to:
<footer class="container-fluid">
<div class="container">
<div class="content1>CONTENT 1</div>
<div class="content2>CONTENT 2</div>
</div>
<div class="back-to-top>TOP</div>
</footer>
You can see an example in Bootply. You can see that the footer has to be 20px height (min-height: 20px) but instead it is 40px.
I think that my problem will be solved if I can put the .back-to-top div beside the .container div.
How can I get this?
You can use helper class pull-right and move TOP link before container:
<footer class="container-fluid">
<div class="back-to-top pull-right">TOP</div>
<div class="container">
<div class="content1>CONTENT 1</div>
<div class="content2>CONTENT 2</div>
</div>
</footer>
You need to remove your CSS bloc:
.back-to-top {
float: right;
position: relative;
top: -20px;
}
Doc: http://getbootstrap.com/css/#helper-classes-floats
Having a min-height proxy doesn't mean you footer is going to be 20px. That just mean its height won't be smaller than that. If you want your height to be 20px, use height property. If for some reason you want it to be variable, you can look to the max-height property.
For your "back-to-top" button, here is my suggestion :
http://jsfiddle.net/Bladepianist/38ne021p/
HTML
<footer class="container-fluid navbar-inverse">
<div class="row">
<div class="col-xs-6">CONTENT 1</div>
<div class="col-xs-5">CONTENT 2</div>
<div class="col-xs-1 text-right" id="back-to-top">TOP</div>
</div>
</footer>
CSS
.container-fluid {
color: white;
}
Basically, I change your "back-tot-top" class to an ID in my model but you're free to adapt it to your liking.
Using the col-system and the text-positions classes, you can achieve the same rendering as you show in your question. That way, the back-to-top button is part of the footer.
Hope that's helping ;).

Div Overflow Scroll

There is a code block like this;
<div class="wrapper">
<div class="title">Title Text</div>
<div class="content">
<div class="block">Block text1</div>
<div class="block">Block text2</div>
</div><!--Content End-->
</div>
I change css overflow attr of content class like "overflow:scroll;" . But when I change this feature, overflow attribute of all div element changing. I want to fixed title class ,that don't scroll. How can I do this?
Try with:
div.content { overflow: auto;}
make sure your css is
div.content { overflow:scroll; }

Resources