This question already has answers here:
Is there a way to make a child DIV's width wider than the parent DIV using CSS?
(15 answers)
Closed 7 years ago.
I am having trouble overriding the parent's width within my CSS.
Essentially, I have a parent and a child div like:
.parent{ width: 768px; background-color: red; }
.child{ background-color:blue; }
<div class="parent">
<div class="child">
//content
</div>
</div>
A lot of elements still use the parents parameter of 768px width, however I wish this one specific child element to extend the entire width of the screen - I have tried doing left: 0, right: 0, clearing the floats and setting the width to auto.
I also wanted to avoid using !important if I can.
Any suggestions ?
An accurate representation of what I want would look like this:
_____
|par. |
_|_____|_
| child |
| |
|_________|
| |
|_____|
Do this, use padding and margin (margin-left and margin-right and padding-left and padding-right) to achieve this.
<div class="parent">
<p>This is parent</p>
<div class="child">
<p>This is child</p>
</div>
<p>This is still parent</p>
</div>
.parent{ width: 468px; background-color: red; margin: 0 auto; }
.child{
background: blue;
margin-left: -300vw;
padding-left: 300vw;
margin-right: -300vw;
padding-right: 300vw;
}
http://cssdeck.com/labs/full/6xljy6pz
Try this https://jsfiddle.net/7txe5eev/. This will calculate and set margin for you. I assumed you are using bootstrap but if you get the logic you can modify this to fit your code.
HTML
<div class="container">
<div class="row">
<div class="col-xs-offset-3 col-xs-6 parent">
<div class="row">
<div class="col-xs-12 divs red"></div>
</div>
<div class="row">
<div class="col-xs-12 divs green special"></div>
</div>
<div class="row">
<div class="col-xs-12 divs blue"></div>
</div>
</div>
</div>
CSS
.divs {
height: 200px;
margin: 5px auto;
}
.red {
background-color: red;
}
.green {
background-color: green
}
.blue {
background-color: blue;
}
.special {
width: 100vw;
}
JS
$(document).ready(calcMargin);
$(window).resize(calcMargin);
function calcMargin() {
var width = $('.parent').width() - $('.special').width();
var leftMargin = width/2;
$('.special').css('margin-left', leftMargin);
}
Kind of hacky, but it works (in browsers that support calc and vw): http://jsfiddle.net/tvg2ocvs/
margin-left: calc(-50vw + (768px/2));
margin-right: calc(-50vw + (768px/2));
Doesn't look nice when viewport is smaller than 768px though, but nothing a media query won't fix :)
Related
This question already has answers here:
How to match width of text to width of dynamically sized image/title?
(2 answers)
Closed 1 year ago.
Please take a look at the code snippet below:
.parent {
background-color: #a7dbff;
width: fit-content;
padding: 5px;
margin: 5px;
display: inline-block;
}
.image {
width: 100%;
aspect-ratio: 1 / 1;
background-image: url(https://i.stack.imgur.com/qV078.jpg);
background-size: contain;
}
<div class="parent">
<h3>Some title</h3>
<div class="image"></div>
<div>Some more content here</div>
</div>
<div class="parent">
<h3>Some title</h3>
<img class="image" src="https://i.stack.imgur.com/qV078.jpg">
<div>Some more content here</div>
</div>
I'm trying to make the image the size of the largest element in the parent.
In the first example the image is set using background-image, this works fine. Using width: 100%, the element gets resized to the width of the parent.
But in the second example the image is an <img> element. In this case the image grows bigger than the parent, causing the parent to grow with it.
Some context: I'd like to use a <picture> element so that the ua automatically downloads the image in the correct format. The <picture> element seems to suffer from this same behaviour unfortunately. It seems like adding an <img> to the parent causes the fit-content value of the parent to grow.
What is causing this behaviour, and is there some way to fix this with css?
Note that this is similar to How to match width of text to width of dynamically sized image/title? but the solutions there don't apply here because I'm working with an <img> rather than a <div>
I added two properties to .parent. I'm not sure how the white-space will work out on all kinds of sizes but it's ok for your example. There's a subtle difference in the snippet-result; I didn't look into that.
.parent {
background-color: #a7dbff;
width: fit-content;
padding: 5px;
margin: 5px;
display: inline-block;
max-inline-size: min-content;
white-space: nowrap;
}
.image {
width: 100%;
aspect-ratio: 1 / 1;
background-image: url(https://i.stack.imgur.com/qV078.jpg);
background-size: contain;
}
<div class="parent">
<h3>Some title</h3>
<div class="image"></div>
<div>Some more content here</div>
</div>
<div class="parent">
<h3>Some title</h3>
<img class="image" src="https://i.stack.imgur.com/qV078.jpg">
<div>Some more content here</div>
</div>
Does this solve your problem?
.parent {
background-color: #a7dbff;
width:100px;
padding: 5px;
margin: 5px;
display: inline-block;
}
.image {
height:100%;
width:100%;
object-fit: cover;
}
<div class="parent">
<h3>Some title</h3>
<img class="image" src="https://i.stack.imgur.com/qV078.jpg">
<div>Some more content here</div>
</div>
You can also specify a height for the image, but then you need to create another parent div for the img and give the div a height property
This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 4 years ago.
I have a parent element (div) with a fixed width of 1200px. There are no borders or padding on this element.
I have three inline child elements (divs) with fixed widths of 400px. Again, no borders, padding or margins.
I want my three child elements to sit on the same line but instead the third one gets pushed down. If I reduce their widths to 397px they all sit on the same line.
Why can't I divide the width of a parent container exactly by the number of children I want to sit abreast within that container? Much the same way that I can't define those child elements as percentage widths that add up to 100% (ie four children of all 25% width)?
This happens due to the extra spacing cause by the white space in the code itself. You can fix it by either writing the markup in a way that makes sure there are no white space or you can set the parent div's font-size to 0 so no white space is visible (make sure you then set the children div font's size back to normal)
In this example I've used the first method as it is cleaner
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
<div class="parent">
<div class="child"></div><div class="child"></div><div class="child"></div>
</div>
style
.parent {
width: 1200px;
background-color: #333;
margin: 20px 0; /* outer margin doesn't matter */
}
.parent .child {
width: 400px;
height: 300px;
display: inline-block;
background-color: #ccc;
}
The first box doesn't work, the second does as I've left no space between the closing and opening tags of the child elements
http://jsbin.com/cifedis/edit?output
You need to use float:left to your children in order to achieve this
.parent {
width: 1200px;
height: 200px;
background: pink;
}
.child {
float: left;
width: 400px;
display: inline-block;
background: red;
}
<div class="parent">
<div class="child">Child1</div>
<div class="child">Child2</div>
<div class="child">Child3</div>
</div>
You can add css like this=>
.parent_container{
width:1200px;
float:left;
}
.child1,
.child2,
.child3{
float:left;
width:400px;
display: inline-block;
}
inline-block elements (which I'm guessing you are using), by default, have a white space after them, which might cause the issue you are seeing.
There are a number of ways to remove this in the html itself, one of them being adding a comment between the two inline-block elements. I prefer this approach, as its more readable.
.parent {
width: 600px;
height: 50px;
background: grey;
}
.child {
display: inline-block;
width: 200px;
height: 50px;
background: pink;
}
<div class="parent">
<div class="child">1</div><!--
--><div class="child">2</div><!--
--><div class="child">3</div>
</div>
You can also start the divs in the same line, like below, forgoing the comment
<div>content</div><div>
content</div
There is lots of solution I prefer flexbox
.parent {
display: flex;
}
.child {
flex:1 1 400px;
background-color:red;
max-width: 400px;
height: 400px;
}
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
</div>
If you really want to use with inline-block either make font-size:0; to the parent or do not change the line while creating children element
.parent{
width:1200px;
}
.child {
background-color:red;
width: 400px;
height: 400px;
display: inline-block;
}
<div class="parent">
<!-- Do Not change line of children-->
<div class="child">1</div><div class="child">2</div><div class="child">3</div>
</div>
please read details https://css-tricks.com/fighting-the-space-between-inline-block-elements/
Just Give Parent Div Font Size 0px Below is the Code,
You Can Also do the same by float Left But This is the Best Way :)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pratice</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
.contaniner {
width:1200px;
font-size: 0px;
}
.threelock {
background: #000;
width: 400px;
height: 400px;
display: inline-block;
}
.yllow {
background: yellow;
}
.red {
background: red;
}
</style>
<body>
<div class="contaniner">
<div class="threelock"></div>
<div class="threelock red"></div>
<div class="threelock yllow"></div>
</div>
</body>
</html>
https://jsfiddle.net/nynd8b4b/
There are purple blocks at the top and bottom of this example, representing a centered site.
I have a module I want to include on the page that spans full width, that are 60% / 40% (seen in the red and blue blocks).
So here is the question:
I want the red block to stay lined up with the purple blocks on the left side as the viewport expands and contracts. I do not know the height of the red & blue blocks, so please don't put any height on them. I can use any number of divs to achieve this.
Ideas? Ideally this works on modern browsers and IE10 and up.
Edit: I'll even take an absolute position answer now... I'd obviously prefer it if I didn't have to do that. Also here is a wireframe of a design that I need this implementation for: https://wireframe.cc/TkX5QF
Html:
<div class="container">
<div class="content">centered content</div>
</div>
<div class="full-width">
<div class="sixty"></div>
<div class="forty"></div>
</div>
<div class="container">
<div class="content">centered content</div>
</div>
Css:
.container {
width: 1000px;
margin: 0 auto;
}
.content {
height: 100px;
background-color: purple;
color: white;
}
.full-width {
overflow: hidden;
}
.sixty{
width: 60%;
height: 100px;
background-color: red;
float: left;
}
.forty {
width: 40%;
height: 100px;
background-color: blue;
float: left;
}
I think this is what you are looking for https://jsfiddle.net/nynd8b4b/4/
<body onresize="marg()" onload="marg()">
<div class="container" id="container">
<div class="content">centered content</div>
</div>
<div class="full-width">
<div class="sixty" id="sixty"></div>
<div class="forty"></div>
</div>
<div class="container">
<div class="content">centered content</div>
</div>
</body>
<script>
function marg() {
var p = document.getElementById("container");
var style1 = p.currentStyle || window.getComputedStyle(p);
var p2 = document.getElementById("sixty").offsetWidth;
var margin = style1.marginLeft;
document.getElementById("sixty").style.marginLeft = style1.marginLeft;
document.getElementById("sixty").style.width = "calc(60% - " + style1.marginLeft + ")";
}
</script>
Why not just set an absolute position to the left column?
I have this site:
http://avocat2.dac-proiect.ro/?page_id=25
At this point my items are centered as they wish. The only problem is that it is not responsive.
This is code HTML:
<div class="parentVerticalCenter">
<div class="childVerticalCenter">
<div class="row sss">
<div class="col-sm-4 col-md-4 col-lg-12 col-lg-offset-0" style="font-size:17px;color:white;">
<div class="container3">
<div class="centered">[Contact_Form_Builder id="10"]</div>
</div>
</div>
</div>
</div>
</div>
This is code CSS:
.container3 {
background-color: green;
}
.centered {
display: table;
margin: 0 auto;
background-color: red;
}
div[wdid="4"] {
width: 40%;
display: inline-block;
}
div[wdid="22"] {
width: 40%;
display: inline-block;
margin-left:-40px;
}
div[wdid="2"] {
width: 40%;
display: inline-block;
}
div[wdid="6"] {
width: 40%;
display: inline-block;
margin-left:-40px;
}
If you delete this code, my elements are responsive but are not aligned properly.
.contactform10 .wdform_column
{
width:50% !important;
}
I tried to use min-width and max width for this but does not work so take one above the other elements
Basically, my div red is divided into two equal parts, each having a width of 50%.
Can you please help me solve this problem?
Items to be displayed as they are now and be responsive.
Thanks in advance!
Using "Push/Pull" with bootstrap will help you reposition elements in a stacked order when the screen resizes... perhaps this is what you meant?
full size:
| ITEM 1 | ITEM 2|
Smaller size:
| ITEM 1 |
| ITEM 2 |
Bootstrap discusses it here: http://getbootstrap.com/css/#grid-column-ordering
If this is the case, this question/answer may also assist you:
Bootstrap 3: Push/pull columns only on smaller screen sizes
I use foundation (not bootstrap) and to do it in that framework you simply set up as similar to this pseudo-code:
<div id="item1" class="small-12-pull medium-12-pull large-6">lorem ipsum</div>
<div id="item2" class="small-12-push medium-12-push large-6">qwerty colec</div>
I think this is an IE9/10 specific issue. I'm trying to create a flexible row layout as follows:
|----------------------------------|
| FIXED HEIGHT |
|----------------------------------|
| FIXED HEIGHT, SOMETIMES HIDDEN|
|----------------------------------|
| |
| FLEXIBLE HEIGHT |
| |
|----------------------------------|
| FIXED HEIGHT |
|----------------------------------|
I also want the outer container for this to have a flexible height and width which fits the browser view port.
Naively, possibly, I thought that since I'm only supporting Chrome, Firefox, Safari, and IE9/10, then display: table, display: table-row, display: table-cell might be a good way to go, so I came up with:
jsFiddle: http://jsfiddle.net/Nugps/2/
HTML:
<div class="stage">
<div class="table">
<div class="row">
<div class="cell">
<div class="content1">one</div>
</div>
</div>
<div class="row">
<div class="cell">
<div class="content1">two</div>
</div>
</div>
<div class="row flexible">
<div class="cell">
<div class="content2">three</div>
</div>
</div>
<div class="row">
<div class="cell">
<div class="content1">four</div>
</div>
</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
.stage {
position: absolute;
top: 15px;
right: 15px;
bottom: 15px;
left: 15px;
}
.table {
display: table;
height: 100%;
width: 100%;
background: lightblue;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
border: 3px solid red;
height: 50px;
}
.row.flexible {
height: 100%;
}
.row.flexible .cell {
height: 100%;
}
.content2 {
background: lightgreen;
height: 100%; /* setting this in IE causes the content to be 100% of the table rather than the table cell height */
}
If you open this jsFiddle in Chrome, everything is as expected. However in IE10, and I suspect IE9 also, the heights are messed up (the .content2 height is 100% of the table rather than the cell).
Is there a good workaround for this other than setting the height of the content manually using javascript? Or is there a better CSS layout that I could use? I can't use the newer flexbox because of IE9.
I was wondering if the content2 class really needs styling, could you not just add the background colour to .row.flexible .cell and get rid of the .content2 style all together.
Here is my solution:
http://cdpn.io/Ewrsa
It works in both IE9/10