I need elements inside a div to be outside of their div for different screen sizes.
I currently have the html repeated and am hiding it in certain viewports, this obviously isn't ideal but I'm not sure if there's another way to do it.
Here's the html desktop and tablet
<div class="container">
<div class="one">
<p>Content 1</p>
</div>
<p>Content 2</p>
</div>
Here's the html needed for mobile
<div class="container">
<p>Content 1</p>
<p>Content 2</p>
</div>
This is so I can use flexbox order on all the items within the container div
This is the perfect use case of display:contents; (https://caniuse.com/#feat=css-display-contents)
display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. This can be useful when a wrapper element should be ignored when using CSS grid or similar layout techniques.
.container {
display:flex;
}
.one {
display:contents;
}
.one p:first-child {
order:2;
}
<div class="container">
<div class="one">
<p>Content 1</p>
<p>Content 3</p>
</div>
<p>Content 2</p>
</div>
You can try this(if you want):
<div class="container">
<div class="one d-none d-md-block">
<p>Content 1</p>
</div>
<p class="d-block d-md-none">Content 1</p>
<p>Content 2</p>
</div>
For CSS part:
.d-none {
display: none;
}
.d-md-none {
display: none;
}
.d-md-block {
display: block;
}
// Extra small devices (portrait phones, less than 576px)
#media (max-width: 575.98px) {
.d-none {
display: none;
}
.d-block {
display: block;
}
}
This method is actually came from Bootstrap. But if you don't want to use it you may try to add the code into your HTML and CSS.
Related
I need elements inside a div to be outside of their div for different screen sizes.
I currently have the html repeated and am hiding it in certain viewports, this obviously isn't ideal but I'm not sure if there's another way to do it.
Here's the html desktop and tablet
<div class="container">
<div class="one">
<p>Content 1</p>
</div>
<p>Content 2</p>
</div>
Here's the html needed for mobile
<div class="container">
<p>Content 1</p>
<p>Content 2</p>
</div>
This is so I can use flexbox order on all the items within the container div
This is the perfect use case of display:contents; (https://caniuse.com/#feat=css-display-contents)
display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. This can be useful when a wrapper element should be ignored when using CSS grid or similar layout techniques.
.container {
display:flex;
}
.one {
display:contents;
}
.one p:first-child {
order:2;
}
<div class="container">
<div class="one">
<p>Content 1</p>
<p>Content 3</p>
</div>
<p>Content 2</p>
</div>
You can try this(if you want):
<div class="container">
<div class="one d-none d-md-block">
<p>Content 1</p>
</div>
<p class="d-block d-md-none">Content 1</p>
<p>Content 2</p>
</div>
For CSS part:
.d-none {
display: none;
}
.d-md-none {
display: none;
}
.d-md-block {
display: block;
}
// Extra small devices (portrait phones, less than 576px)
#media (max-width: 575.98px) {
.d-none {
display: none;
}
.d-block {
display: block;
}
}
This method is actually came from Bootstrap. But if you don't want to use it you may try to add the code into your HTML and CSS.
I need elements inside a div to be outside of their div for different screen sizes.
I currently have the html repeated and am hiding it in certain viewports, this obviously isn't ideal but I'm not sure if there's another way to do it.
Here's the html desktop and tablet
<div class="container">
<div class="one">
<p>Content 1</p>
</div>
<p>Content 2</p>
</div>
Here's the html needed for mobile
<div class="container">
<p>Content 1</p>
<p>Content 2</p>
</div>
This is so I can use flexbox order on all the items within the container div
This is the perfect use case of display:contents; (https://caniuse.com/#feat=css-display-contents)
display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. This can be useful when a wrapper element should be ignored when using CSS grid or similar layout techniques.
.container {
display:flex;
}
.one {
display:contents;
}
.one p:first-child {
order:2;
}
<div class="container">
<div class="one">
<p>Content 1</p>
<p>Content 3</p>
</div>
<p>Content 2</p>
</div>
You can try this(if you want):
<div class="container">
<div class="one d-none d-md-block">
<p>Content 1</p>
</div>
<p class="d-block d-md-none">Content 1</p>
<p>Content 2</p>
</div>
For CSS part:
.d-none {
display: none;
}
.d-md-none {
display: none;
}
.d-md-block {
display: block;
}
// Extra small devices (portrait phones, less than 576px)
#media (max-width: 575.98px) {
.d-none {
display: none;
}
.d-block {
display: block;
}
}
This method is actually came from Bootstrap. But if you don't want to use it you may try to add the code into your HTML and CSS.
Here is my code:
.block {
width: 25%;
float: left;
}
<paper-material elevation="1">
<p>List of blocks:</p>
<div class="block">
<p>Block 1</p>
</div>
<div class="block">
<p>Block 2</p>
</div>
<div class="block">
<p>Block 3</p>
</div>
<div class="block">
<p>Block 4</p>
</div>
</paper-material>
Paper-material element contains only "List of blocks" label, all four divs are outside. How can I fix it so that the divs are inside the paper-material element?
Thank you.
Import iron-flex-layout and add #apply --layout-horiztonal; to your CSS, which currently amounts to
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
You have to add div at the end of all .block divs and set style clear: both
inside style:
.block {
width: 25%;
float: left;
}
.clear {
clear: both;
}
and paper-material:
<paper-material elevation="1">
<p>List of blocks:</p>
<div class="block">
<p>Block 1</p>
</div>
<div class="block">
<p>Block 2</p>
</div>
<div class="block">
<p>Block 3</p>
</div>
<div class="block">
<p>Block 4</p>
</div>
<div class="clear"></div>
</paper-material>
but this is still not the best solution. Since your paper-material should have fixed width. At this moment it is better to use display: flex . Be aware that older IE doesn't support flex. So if you really want to support that 1% of people then you can continue using float:left.. but i still think using display: inline-block would be much more better.
I'm trying to target the class, .textbox-fill to change the background colour and text colour for each individual box element. For example .textbox-fill's first-child should be grey. The second-child should be white. The third child I want to adjust the height slight and so on.
I have tried using the nth-child selector #About-Container .about-inner-content article:nth-child(1n+2) .textbox-fill nothing seems to work. Example here
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
I have encounter this issue in the past and I'm not 100% sure how I resolved it. I have read several articles and posts on this subject. I understand the basics, however something more complex like this I always approached with trial and error method.
Does anyone know how I can solve this issue?
Since your .textbox-fill is inside the article, it is the article you need to start with, as target the text-fill using nth-child will not work as it can't see outside its parent
So do like this instead
article:nth-child(1) .textbox-fill {
color: red;
}
article:nth-child(2) .textbox-fill {
color: lime;
}
article:nth-child(3) .textbox-fill {
color: blue;
}
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
If we talk about the .textbox-fill's .about-textbox's children, the h2 and p, do like this, where you use the global selector * in *:nth-child, as the children is of different types
.textbox-fill .about-textbox *:nth-child(1) {
color: red;
}
.textbox-fill .about-textbox *:nth-child(2) {
color: lime;
}
<article class="col-sm-6 grid-tile">
<div class="textbox-fill">
<div class="about-textbox">
<h2>Some text</h2>
<p>Some text</p>
</div>
</div>
</article>
If you have a .textbox-fill with several children, like:
div.textbox-fill
h1#header
div#firstDiv
div#secondDiv
article#someArticle
div#thirdDiv
Now, .textbox-fill div:first-child will target nothing, because there isn't any div that is a first child of it's parent (.textbox-fill). Although .textbox-fill div:first-of-type will target div#firstDiv. .textbox-fill div:nth-child(2) will also target div#firstDiv. div#thirdDiv will be .textbox-fill div:nth-of-type(3) or .textbox-fill div:nth-child(5) or .textbox-fill div:last-child or .textbox-fill div:last-of-type.
If you want to target specific .textbox-fill located on site, but with different parents, like:
div.something
div.textbox-fill
[...]
article#other
div.textbox-fill
You cannot target it using nth-child nor nth-of-type. You will need to find another way based on your site tree.
I am working in a bootstrap project where i need some text to be centered vertically with another row. Obviously the two rows are not the same size hence the content size. Is there a way to solve this without escalating to flexbox or is this the way to go? Also i would like to maintain the responsiveness.
Fiddle: http://codepen.io/anon/pen/mPwxXa
HTML
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 col-xs-12">
<img src="http://rack.2.mshcdn.com/media/ZgkyMDE1LzA5LzEzLzNjL2dvb2dsZXRodW1iLmIyNGE0LmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/63126c72/af4/google-thumb.jpg" class="img-responsive"/>
</div>
<div class="col-lg-6 col-xs-12">
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
</div>
</div>
</div>
You can do this with Flexbox and media queries.
As for flexbox you can just use display: flex and align-items: center on parent element and that will be row in this case.
Now you just need to adjust min-width in media queries to be same as bootstrap breakpoints So for example if you are using col-sm-6 bootstrap class, then 768px is break point and you will use #media(min-width: 768px) { } in your media queries.
For col-sm- you can use #media(min-width: 768px) Fiddle
For col-md- you can use #media(min-width: 992px) Fiddle
For col-lg- you can use #media(min-width: 1200px) Fiddle
#media(min-width: 768px) {
.flex {
display: flex;
align-items: center;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row flex">
<div class="col-sm-6">
<img src="http://rack.2.mshcdn.com/media/ZgkyMDE1LzA5LzEzLzNjL2dvb2dsZXRodW1iLmIyNGE0LmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/63126c72/af4/google-thumb.jpg" class="img-responsive" />
</div>
<div class="col-sm-6 text-center">
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
</div>
</div>
</div>
Instead of Flexbox you can also get vertical-align with CSS Tables and again you have to adjust your queries with bootstrap breakpoints same as for flexbox. With tables you have to remove float from columns with float: none to get vertical-align.
#media(min-width: 768px) {
.row.display_table {
display: table;
}
.col-sm-6.left_cell, .col-sm-6.right_cell {
display: table-cell;
vertical-align: middle;
float: none;
}
img {
width: 100%;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row display_table">
<div class="col-sm-6 left_cell">
<img src="http://rack.2.mshcdn.com/media/ZgkyMDE1LzA5LzEzLzNjL2dvb2dsZXRodW1iLmIyNGE0LmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/63126c72/af4/google-thumb.jpg" class="img-responsive" />
</div>
<div class="col-sm-6 right_cell text-center">
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
</div>
</div>
</div>
I don't want to be too simple, but you are wondering if flexbox is the way to go. Actually, at this time, it is.
The reason behind the text container is that you don't need to use text-align:center; to center your text if you don't want to, but it is still horizontal centered.
body {
padding: 2em;
}
#media (min-width: 1200px) {
.vcenter {
display: flex;
align-items: center;
}
.text {
display: inline-block;
position: relative;
left: 50%;
transform: translateX(-50%);
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row vcenter">
<div class="col-lg-6 col-xs-12 ">
<img src="http://rack.2.mshcdn.com/media/ZgkyMDE1LzA5LzEzLzNjL2dvb2dsZXRodW1iLmIyNGE0LmpwZwpwCXRodW1iCTk1MHg1MzQjCmUJanBn/63126c72/af4/google-thumb.jpg" class="img-responsive"/>
</div>
<div class="col-lg-6 col-xs-12">
<div class="text">
<p>This is some text some te xt some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
<p>This is some text some text some more text</p>
</div>
</div>
</div>
</div>
You could create a div with relative position like:
<div style='position: relative; top: 50%; transform: translateY(50%); height:100%;'>
To encapsulate all the <p> tags and align middle. There are a lot of methods like (http://vanseodesign.com/css/vertical-centering/):
#parent {display: table;}
#child {
display: table-cell;
vertical-align: middle;
}
but with inline elements didn't work quite well. Note I change the col-lg-6 to col-sm-6, because in codepen you could see the 2 columns.
Extracted from here: https://davidwalsh.name/css-vertical-center
Here a working codepen: http://codepen.io/jpaulet/pen/bpRvyd?editors=1100
Hope it helps!