Replacing dynamic % width with scaleX + translateX - css

I am currently transitioning the width property of an element. I would like to replace this with transitions on scaleX and translateX for better rendering performance.
I'm struggling to come up with a proper 1:1 conversion between the two concepts.
Below is a box which contains two lines. Each line has a bar inside of it. The first bar is created using width. The second bar is created using scaleX and translateX. The second bar breaks out of the box. It should appear identical to the width bar for all possible values.
Is this an appropriate way to tackle this problem? If not, how should I approach it? If so, I have some concerns:
I feel like I shouldn't have to use 1% width. I thought I could say 1px and scale that, but maybe that isn't the right idea.
I'm unclear if I should use 1% width and scale up, or 100% width and scale down. Perhaps they're equivalent, but the width of the bar controls the positioning of translateX
* {
box-sizing: border-box;
}
.box {
height: 200px;
width: 200px;
border: 1px solid;
}
.line {
background-color: #ccc;
width: 100%;
height: 5px;
margin: 50px 0;
}
.bar {
height: 5px;
background-color: blue;
}
.bar.width {
width: 66.6%;
}
.bar.scale {
width: 1%;
transform: scaleX(66.6) translateX(33%);
}
<div class='box'>
<div class='line'>
<div class='bar width'>
</div>
</div>
<div class='line'>
<div class='bar scale'>
</div>
</div>
</div>

It's simpler than all that. You're already scaling the element to 66%. Now all you need to do is set the origin to the far left of the element with transform-origin: 0 50%; and drop the translate rule. That should fix the issue.
* {
box-sizing: border-box;
}
.box {
height: 200px;
width: 200px;
border: 1px solid;
}
.line {
background-color: #ccc;
width: 100%;
height: 5px;
margin: 50px 0;
}
.bar {
height: 5px;
background-color: blue;
}
.bar.width {
width: 66.6%;
}
.bar.scale {
width: 1%;
transform: scaleX(66.6);
transform-origin: 0 50%;
}
<div class='box'>
<div class='line'>
<div class='bar width'>
</div>
</div>
<div class='line'>
<div class='bar scale'>
</div>
</div>
</div>

Related

Using transfrom translate property with absolute positioned elements

I noticed something strange when trying to move an absolute positioned element with transform property even when using something like translateY(0) it moves the element abit which it should not move it at all
is this a normal behavior ?? and is there any workaround for it ??
I could not find anything on the internet related to this topic so I'm posting this here
NOTE: this issue happend on (Firefox) and it seems to work just fine on (Chrome)
I tried to reproduce the same problem using the code below
<div class="wrapper">
<div class="box box-1"></div>
<div class="box box-2"></div>
</div>
* {
box-sizing: border-box;
}
body {
margin: 0;
height: 100vh;
display: grid;
place-content: center;
}
.wrapper {
position: relative;
width: 200px;
height: 400px;
border: 1px solid red;
}
.box {
position: absolute;
width: 100%;
border: 5px solid white;
/*
if you toggle the next line you'll notice the bottom border line disappear and appear again
*/
transform: translateY(0);
}
.box-1 {
background-color: red;
height: 25%;
}
.box-2 {
background-color: blue;
height: 75%;
top: 25%;
}
you'll notice a line that has a comment above
as described removing the transform line will make the bottom border to reappear again

CSS margin-top moving element down

I have a context of 3 divs, one parent, and two children.
The two children are placed one on top of the other and I want to add a margin-top on the bottom one to move the one on top 50px up.
What ends up happening is that the one on the bottom moves down 50px instead.
Here is the code:
.container {
background-color: red;
width: 500px;
height: 300px;
position: relative;
margin: auto;
font-size: 30px;
}
.top,
.bottom {
height: 100px;
width: 100px;
}
.top {
background-color: purple;
}
.bottom {
margin-top: 50px;
background-color: blue;
}
<body>
<div class="container">
<div class="top">top</div>
<div class="bottom">bottom</div>
</div>
</body>
Any suggestions?
CSS allows you to move an element relative to its position without affecting other elements' positions if you use transform.
In this case you can translate the top element in the Y direction by -50px to move it up:
.container {
background-color: red;
width: 500px;
height: 300px;
position: relative;
margin: auto;
font-size: 30px;
}
.top,
.bottom {
height: 100px;
width: 100px;
}
.top {
background-color: purple;
transform: translateY(-50px);
}
.bottom {
background-color: blue;
}
<body>
<div class="container">
<div class="top">top</div>
<div class="bottom">bottom</div>
</div>
</body>
gap (grid-gap) Syntax
gap: 50px;
As you can see the first element is already on the highest point inside the parent container.
html
What you can do is in case you want to increase its height is scaling its y position by a negative number.

Change width of mosaic layout to be 50% / 50%

I would like the images in this layout to each take up 50% of the page.
Right now the split is 2/3 for the large image and 1/3 for the 2 smaller images.
Images are here:
https://www.joshungerdesign.com/packed
I can make the large image take up 50% by using this:
.tweak-index-gallery-layout-packed [data-index-gallery-images='3']
.Index-gallery-item:nth-child(3n+1) {
width: 50%;}
I don't know how to make the small images extend to the right side once the large image width is changed.
I just write basic code, I hope it'll help you out. Thanks
body {
margin: 0;
}
.Index-gallery-inner {
background-color: #fff;
display: inline-block;
width: 100%;
}
.Index-gallery-item {
box-sizing: border-box;
float: left;
height: 500px;
border: 5px solid #fff;
width: 50%;
}
.Index-gallery-item:nth-child(2),
.Index-gallery-item:nth-child(3){
height: 250px;
width: 50%;
}
.Index-gallery-item-inner {
background-color: #ccc;
height: 100%;
width: 100%;
}
.Index-gallery-item-image {
background-color: white !important;
padding: 0 !important;
}
<div class="Index-gallery-inner clear">
<div class="Index-gallery-item">
<div class="Index-gallery-item-inner"></div>
</div>
<div class="Index-gallery-item">
<div class="Index-gallery-item-inner"></div>
</div>
<div class="Index-gallery-item">
<div class="Index-gallery-item-inner"></div>
</div>
</div>

Fixed width layout with one child spanning full screen width

Can I create a layout like on the picture below, while setting the fixed width only on the parent container? I also cannot use position: absolute; left: 0; right: 0; on Full screen width child, as I cannot remove it from the flow, because it's size is dynamic.
I can't change the markup.
The only solution I can think of is setting the fixed width on every Fixed-width child separately, but as I have a lot of them, that's not the most comfortable solution - means adding a class for every child that I add into the parent container.
Here is an example markup you can post a solution to.
HTML
<div class="fixed-width-container">
<div class="regular-child"></div>
<div class="full-screen-width-child"></div>
<div class="regular-child"></div>
<div class="regular-child"></div>
</div>
CSS
.fixed-width-container {
width: <some-fixed-width>;
}
you can give a try to the flex layout : https://css-tricks.com/snippets/css/a-guide-to-flexbox/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-align: center;
}
body {
margin: 0;
}
div {
border: 1px solid #333;
}
.fixed-width-container {
width: 400px;/* any width set */
margin: auto;
padding: 10px 10px 0;
background: yellow;
display: flex;
flex-flow: column;
align-items: center;
}
.fixed-width-container>div {
height: 3em;
margin-bottom: 10px;
background: lightblue;
min-width: 100%;
}
.full-screen-width-child {
width: 99vw;/* 100vw is fine too */
}
<div class="fixed-width-container">
<div class="regular-child">Fixed-width child</div>
<div class="full-screen-width-child">Full screen width child with dynamic contents</div>
<div class="regular-child">Fixed-width child</div>
<div class="regular-child">Fixed-width child</div>
</div>
codepen to test and play with
This is just an attempt, and probably not a very good one. But maybe it will spawn some more sophisticated solutions by others, or even yourself.
Idea: negative margins for the full-width child.
* {
box-sizing: border-box;
text-align: center;
}
body {
width: 100%;
background: #fff;
}
div {
border: 1px solid #333;
margin-bottom: 10px;
}
div:last-child {
margin-bottom: 0;
}
.fixed-width-container {
width: 70%;
margin: auto;
padding: 10px;
background: LightYellow;
}
.regular-child,
.full-screen-width-child {
height: 45px;
line-height: 45px;
background: LightBlue;
}
.full-screen-width-child {
margin-left: -24%;
margin-right: -24%;
background: LightGreen;
}
<div class="fixed-width-container">
<div class="regular-child">Fixed-width child</div>
<div class="full-screen-width-child">Full screen width child with dynamic contents</div>
<div class="regular-child">Fixed-width child</div>
<div class="regular-child">Fixed-width child</div>
</div>
The problematic part here is the dimension of the negative margins. If you use %, it will relate to the width of the fixed-width-container. Here, I chose width: 70% for it. Given a body width of 625px (as is the case for the Stack Snippet preview) and a margin of -24%, that would give a negative margin of 625px * 0.7 * 0.24 = 105px. I'm not sure what's the best approach of making this work for any configuration.

How to get CSS table-cell to 100% for responsive design [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do I get the following setup with CSS to work?
jsfiddle
http://jsfiddle.net/16ex38mL/2/
Basically, I intend to put an input box to #header-nav-content-search and let the div and the one below it resize responsively to 100% of the remaining width.
I have two static width columns. One is the first one with 240px, and one is the last one with 200px.
code
#header-nav-content-search {
width: 100%;
}
didn't do the trick.
I have concentrated on reducing the HTML markup needed. The following example is mainly based on that excellent sketch of yours, so it will need some tweaking.
Basic Idea
Create a three "column" CSS table with the center cell remaining fluid:
<div class="table">
<div class="cell"></div>
<div class="cell center">I contain 4 fluid divs with the class ".inner"</div>
<div class="cell"></div>
</div>
The center cell contains your 4 inner boxes with the class .inner
Basic CSS Styles
box-sizing: border-box will allow us to calculate percentage width including padding and borders
The main container, .table, is given a fixed height (could be changed to percentage)
The .inner divs are display: inline-block and are given appropriate percentage widths and fixed heights equal to half the containers height
The left and right columns are given their fixed widths
.table is given an appropriate min-width to prevent the inner divs from overlapping
Note: In the HTML markup, the inner divs closing and opening tags have no space between them. This is important as it prevents a gap that is present with inline elements.
Refer to this article for more information.
CSS / HTML / Demo
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
}
body {
margin: 0;
}
.table {
display: table;
height: 300px;
width: 100%;
min-width: 600px;
table-layout: fixed;
}
.cell {
display: table-cell;
vertical-align: top;
}
.left {
width: 240px;
}
.right {
width: 200px;
border-left: solid 1px #000;
}
.inner {
display: inline-block;
height: 150px;
vertical-align: top;
}
.center-left {
width: 30%;
}
.center-right {
width: 70%;
}
/* Borders */
.table {
border: solid 1px #000;
}
.inner {
border-bottom: solid 1px #000;
border-left: solid 1px #000;
}
.center-right .inner {
border-right: solid 1px #000;
}
.inner:nth-child(3),
.inner:nth-child(4) {
border-bottom: none;
}
<div class="table">
<div class="cell left">
240px width
</div>
<div class="cell center">
<div class="inner center-left">
30% width 50% height
</div><div class="inner center-right">
70% width 50% height
</div><div class="inner center-left">
30% width 50% height
</div><div class="inner center-right">
70% width 50% height
</div>
</div>
<div class="cell right">
200px width
</div>
</div>
I wouldn't do it that way. Here's one way to get you started.
DEMO: http://jsbin.com/faveca/1/
http://jsbin.com/faveca/1/edit
HTML:
<header>
<div class="fixed-width-240 eq">
240px column fixed width what about is it equal to the others, yes it is.
</div>
<div class="fluid eq">
fluid column
</div>
<div class="fixed-width-200 eq">
200px column
</div>
</header>
CSS
body,
html {
margin: 0;
padding: 0;
}
header div,
header div:before,
header div:after {
box-sizing: border-box
}
header {
border: 2px solid #000
}
header:after {
content: "";
display: table;
clear: both;
}
.fixed-width-240 {
z-index: 1;
position: relative;
background: red;
}
.fixed-width-200 {
z-index: 2;
position: relative;
background: orange;
}
.fluid {
position: relative;
z-index: -1;
background: #ccc;
}
#media (min-width:700px) {
header {
overflow: hidden
}
header .eq {
padding-bottom: 99999px;
margin-bottom: -99999px;
}
.fixed-width-240,
.fixed-width-200 {
float: left
}
.fixed-width-240 {
width: 240px;
width: 240px;
margin-right: -240px;
border-right: 2px solid #000;
}
.fixed-width-200 {
float: right;
z-index: 2;
width: 200px;
margin-left: -200px;
border-left: 2px solid #000;
}
.fluid {
float: left;
padding: 0 220px 0 260px;
width: 100%;
}
}

Resources