align one div to same length as another [duplicate] - html

This question already has answers here:
Every item to have the same width as the widest element [duplicate]
(3 answers)
Flex box to assign largest child width to other child width
(3 answers)
Vertical buttons with equal width
(2 answers)
Closed 4 years ago.
I have a page with two div tags. One is a text and the other is a button beneath the text. The texts varies so I can not set it to a fixed size and the tekst element rescales as I zoom in and out. I want the button to
1: always be the same size as the text element
2: scale in the same way as the text element.
How can I do this?
.tekst{
padding:1rem;
font-size: 1rem;
border:0.1rem black solid;
margin-left: 2rem;
max-width: 75rem;
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 1rem;
text-align: center;
text-decoration: none;
display: inline-block;
max-width:75rem;
}

CSS Solution
Without knowing the structure of your HTML it's difficult to give an exact solution. However you do say that the button is to be displayed below the text. Therefore a CSS only solution could be to wrap both text and button in a div with display:inline-block so that its width shrinks to its contents. You would then set .button to have width: 100%, thus filling the width of its parent. Because .tekst would be then be setting the width of the parent (remember, the parent is shrinking to fit its content), .button would necessarily have the same width as tekst.
CSS:
.tekst-button-parent {
display: inline-block;
}
.tekst {
...
}
.button {
width: 100%;
...
}
HTML:
<div class="tekst-button-parent">
<div class="tekst"> ... </div>
<div class="button"> ... </div>
</div>
You may need to further style tekst-button-parent to occupy the same position in which your two elements currently reside.
JavaScript Solution
Alternatively you could use JavaScript to set the width of .button to be the same as .tekst everytime the window loads or resizes. If you only have the two occurrences of those divs then identifying the divs by className will be easy.
<script>
var setButtonSize = function(event) {
var width = document.getElementsByClassName( "tekst")[ 0 ].innerWidth;
document.getElementsByClassName( "button")[ 0 ].style.width = width + "px";
};
window.onresize = setButtonSize;
window.onload = setButtonSize;
</script>
If you have more than one pair of .tekst and .button then you'll need to match each pair before resizing.

You should add an javascript function that calculats the width and set the width of the button
example:
$(function(){
resize();
});
$(window).resize(function() {
resize();
});
function resize(){
$(".button").each(function(){
var width = $('.text').width();
var height = $('.text').height();
$(this).css({'width': ''+width , 'height': ''+height })
});
}
.tekst{
padding:1rem;
font-size: 1rem;
border:0.1rem black solid;
margin-left: 2rem;
max-width: 75rem;
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 1rem;
text-align: center;
text-decoration: none;
display: inline-block;
max-width:75rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id soluta dolores eius quaerat quae nihil debitis reprehenderit et atque, repudiandae itaque iste molestiae nostrum eum laboriosam, unde molestias praesentium aperiam? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id soluta dolores eius quaerat quae nihil debitis reprehenderit et atque, repudiandae itaque iste molestiae nostrum eum laboriosam, unde molestias praesentium aperiam? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id soluta dolores eius quaerat quae nihil debitis reprehenderit et atque, repudiandae itaque iste molestiae nostrum eum laboriosam, unde molestias praesentium aperiam?
</div>
<div class="button">
</div>

Related

Why border removes new line created by block level elements in HTML? [duplicate]

This question already has answers here:
How to disable margin-collapsing?
(12 answers)
Why do the top and bottom margins disappear in the child container? [duplicate]
(3 answers)
CSS: Margin-top when parent's got no border
(7 answers)
Closed 26 days ago.
Please check the below HTML code. If I remove border from the first element inside the body, the body starts in a new line, but when I include border the new line just disappears. Someone please explain why it is happening.
article {
border: 3px solid grey;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}
.box {
width: 7rem;
height: 7rem;
background-color: aqua;
float: left;
margin-right: 1rem;
}
<article class="clearfix">
<div class="box"></div>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Delectus dolores, nulla minima dolorum rerum earum ea porro necessitatibus ullam recusandae culpa magni cupiditate dolore nobis quibusdam similique sit. Nostrum, atque.
</p>
</article>
I thought the border style wouldn't affect the block level elements and it's styles, but it seems like I am wrong. Please clarify this for me

Align two elements on top of each other without a container with a width

I have two elements that I would like to be aligned in a column-like way. These elements also need to be floated to the right of the page. If I float the container containing these two elements to the right, they automatically align in a row-like way. My immediate thought is to specify a width of the container so that they will be forced to move downward. The issue with this is that the two elements are different widths.
<div style="float: right; width: 100px;">
<div style="width: 110px; height:50px; background-color: blue;">
Element 1
</div>
<div style="width: 60px; height:50px; background-color: red;
float:right;">
Element 2
</div>
</div>
paragraph text that will not flow into the bottom element because the container's width prevents it. Filler text.....
If the bottom element is not as wide as the top element, the width of the container makes it wider. This is an issue because I have other text / elements that I would like flow around these side elements, and it looks weird because of the whitespace created by the different in widths.
If I try something like making the parent absolute, as not to effect the other elements on the page, the children don't either.
How can I create elements that are floated in a container without the width of the container affecting the other elements on the page as well? Thanks, Levi
One approach is as below, taking advantage of display: contents comments in the code itself:
/* basic CSS reset to reduce all elements to the same
box-sizing, font, margin and padding: */
*,
::before,
::after {
box-sizing: border-box;
font: normal 1rem / 1.5 sans-serif;
margin: 0;
padding: 0;
}
/* a wrapping element to allow for some dynamic sizing of the
contents: */
main {
/* the width of the <main> element is 80vw (viewport-width units)
unless that is less than 30em (the minimum size it will be) or
unless that size exceeds 1000px (at which point it will take
a width of 1000px maximum): */
width: clamp(30em, 80vw, 1000px);
/* a margin of 1em on the top and bottom top-to-bottom languages: */
margin-block: 1em;
/* a margin of auto on the inline axis, left (start) and right (end)
in left-to-right languages, such as English: */
margin-inline: auto;
}
div.wrapper {
/* for those browsers that are yet to implement
logical properties: */
float: right;
/* equivalent to "float: right" in left-to-right
languages, such as English: */
float: inset-inline-end;
width: 100px;
/* effectively removes this element from the
layout, exposing its contents to the layout
engine: */
display: contents;
}
div.wrapper > div {
/* ensuring that the "display: contents" is
unset, which takes it back to the default
display model: */
display: unset;
/* for those browsers that are yet to implement
logical properties: */
float: right;
/* as above, equivalent to "float: right" in
left-to-right languages, such as English: */
float: inset-inline-end;
height: 50px;
/* forces each element to clear the float of its
siblings: */
clear: both;
/* to hide the overflowing text: */
overflow: hidden;
text-overflow: ellipsis;
}
.wrapper > div:first-child {
background-color: blue;
width: 110px;
}
.wrapper > div:last-child {
background-color: red;
width: 60px;
}
<main>
<div class="wrapper">
<div>
Element 1
</div>
<div>
Element 2
</div>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur perferendis corporis itaque, sequi quod commodi explicabo dolore, totam, libero architecto doloremque nisi illo iste quae ea, laboriosam reprehenderit nemo animi! Lorem ipsum dolor
sit amet, consectetur adipisicing elit. Quia reiciendis sapiente blanditiis provident ad ullam consequatur, temporibus ex accusamus est nihil voluptatum totam cupiditate. Ducimus sit deserunt nostrum, dolorem doloremque. Lorem ipsum dolor sit amet,
consectetur adipisicing elit. Consectetur perferendis corporis itaque, sequi quod commodi explicabo dolore, totam, libero architecto doloremque nisi illo iste quae ea, laboriosam reprehenderit nemo animi! Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Quia reiciendis sapiente blanditiis provident ad ullam consequatur, temporibus ex accusamus est nihil voluptatum totam cupiditate. Ducimus sit deserunt nostrum, dolorem doloremque.</p>
</main>
JS Fiddle demo.
References:
CSS logical properties.
display.
float.
Bibliography:
Compatibility of CSS logical properties, from "Can I Use."

Sticky element that doesn't occupy space (like a relative/absolute element) - CSS

Absolutely or relatively positioned elements don't occupy its initial space in the document, so other elements behave as if it wasn't there.
I need this behavior, but with a sticky element.
I hope the code explains it all:
(also have it on JSFiddle)
const myDiv = document.querySelector('#container');
const tooltip = document.querySelector('#tooltip');
let showTooltip = false;
myDiv.addEventListener('click', () => {
showTooltip = !showTooltip;
if (showTooltip) {
tooltip.classList.add('shown');
} else {
tooltip.classList.remove('shown');
}
})
#container {
height: 19rem;
overflow-y: scroll;
}
.info {
background: lightblue;
padding: .5rem;
}
#tooltip {
background: gray;
position: sticky;
bottom: 0;
margin: 0 2rem;
opacity: 0;
padding: 1rem;
}
#tooltip.shown {
opacity: 1;
}
<div id="container">
<div class="content info">
Click in this div to hide/show the tooltip.
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex assumenda, quos, perspiciatis temporibus asperiores, corporis rerum veritatis veniam enim rem repellat doloribus a. Asperiores, perferendis voluptatem, quis non modi quibusdam!</p>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex assumenda, quos, perspiciatis temporibus asperiores, corporis rerum veritatis veniam enim rem repellat doloribus a. Asperiores, perferendis voluptatem, quis non modi quibusdam!</p>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex assumenda, quos, perspiciatis temporibus asperiores, corporis rerum veritatis veniam enim rem repellat doloribus a. Asperiores, perferendis voluptatem, quis non modi quibusdam!</p>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex assumenda, quos, perspiciatis temporibus asperiores, corporis rerum veritatis veniam enim rem repellat doloribus a. Asperiores, perferendis voluptatem, quis non modi quibusdam!</p>
</div>
<div id="tooltip">
This tooltip should not occupy its initial space at the bottom of its parent div...
<br><br><br>
But yet its space is taken into consideration. Scroll down to see...
</div>
</div>
Note: using 'display' instead of 'position' as suggested in answers also doesn't work. It does prevent the tooltip of occupying space when not displayed, but when displayed it's space is still taken in consideration...
I'm afraid I don't have a sample but I was able to achieve this with something like:
.sticky-element
{
position: sticky;
height: 0px;
overflow: visible; // not strictly needed
}
.content
{
position: relative;
top: -100%; // or calc(-20px - 100%) to add margin
}
Then:
<div class="sticky-element">
<div class="content">
....
</div>
</div>
In other words, the actual sticky element has a height of zero so it takes up no space, and you shift up the content by its own height.
The best thing about this is it doesn't require you to know the height of the sticky element.
There may be some side effects but it's working OK for my needs.
I think if you switch between
display:none;
and
display: block;
rather than opacity. Then the initial white space that is being occupied at the end will not appear.
You are using opacity: 0;
to hide your element.
Where it might sound like a cool idea, the element is still there, just transparent. Think of really polished window in real life. You might never acknowledge the window, but it is still there and is taking space, and if you are unaware you might crash into it and harm yourself really bad.
The better idea would be to just get rid of it for the time being:
#tooltip {
display: none;
}
#tooltip.shown {
display: block;
}
Here is working JSFiddle:
https://jsfiddle.net/dyabgve5/26/
EDIT:
I found out what you mean. I think you should override #container divs, because they are interfering with your sticky class divs.
Or.. you can try moving that sticky class behind container like this (it works):
</div> - end of div container
<div id="tooltip">
This tooltip should not occupy it's initial space at the bottom of it's parent div...
<br><br><br>
But yet it's space is taken in consideration. Scroll down to see...
</div>
Working JSFiddle: https://jsfiddle.net/83k1xwt5/29/

Have the container width be as wide as it's first element, and other elements keep that maximum size

<div class="content">
<h2>This is a long heading</h2>
<p>This is a long text but it should be the same width as the h2, not wider nor narrower Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur temporibus commodi expedita debitis nisi accusantium, saepe dolorem sapiente eum suscipit quod vitae!</p>
</div>
Css
.content {
background-color: tomato;
color: #fafafa;
padding: 10px;
margin: 40px;
}
Basically my code is the first example, and i want it to be like in the second example.
The container having the same width as the H2(that is generated from a backend).
If the h2 has 2 words, then the paragraph below it should match the width, if the h2 has 20 words the same should happen(used extremes as guidelines).
Better check fiddle here
Looking for a css only solution
There is a way but it's not intended for general layout..but, FWIW.
For preference, I'd be using a more flexible layout method or javascript. I suspect that this option is not robust.
.content {
display: table;
border: 1px solid grey;
/* note does not extend to paragraph */
}
.content h2 {
display: inline-block;
}
.content p {
display: table-caption;
caption-side: bottom;
text-align: justify;
}
<div class="content">
<h2>This is a long heading</h2>
<p>This is a long text but it should be the same width as the h2, not wider nor narrower Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur temporibus commodi expedita debitis nisi accusantium, saepe dolorem sapiente eum suscipit quod
vitae!</p>
<p>This is a long text but it should be the same width as the h2, not wider nor narrower Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur temporibus commodi expedita debitis nisi accusantium, saepe dolorem sapiente eum suscipit quod
vitae!</p>
</div>
JSfiddle Demo
The only way that ensure a robust backward compatibility is Javascript.
I'd avoid the "table" display; i can't recall all the supports situation but it's somehow problematic.
I'd add that i don't find that nice coding using Divs just to trasform them in Tables again...
All in all it's just 2 lines of code:
function setWidth(id) {
w=$("#header").css("width");
$(id).css( "width", w);
}
setWidth(inner)
css part:
.content {
background-color: tomato;
color: #fafafa;
padding: 10px;
margin: 40px;
display:inline-block
}
h2 {
display:inline
}
HTML:
<div class="content">
<h2 id="header">This is a long text</h2>
<p id="inner">This is a text that expands accordling with H2 header other text, other and other...</p>
</div>
Working Feedle here

How can I use image with a 100% width and a fixed height - and have them not distort?

Google wasn't giving me anything helpful :(
I'm after a way of having an image have a 100% width, and a fixed height, say, 400px, and not stretch horribly, and instead of stretching, zoom in?
I think I'm after something not dissimilar to what backstretch does, but not for full screen backgrounds.
I think this video kind of shows what I'm after in a few instances (I think the eagle picture shows what I'm looking for) http://www.teehanlax.com/resources/img/story/medium/prototypes/feature-header.mp4
100% width picture, that's a fixed height, that shows a cropped image, and that scales with the browser.
http://jsfiddle.net/XcYfS/2/
<style>
img {
width: 100%;
height: 400px; }
h1, p {
width: 80%;
padding-left: 10%; }
</style>
<img src="http://placekitten.com/200/300" alt="">
<h1>Interesting Title!</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat magnam culpa obcaecati numquam iusto recusandae totam voluptatibus temporibus ipsum quasi. Nesciunt maiores sequi quis consectetur labore asperiores eaque hic ipsa!</p>
To avoid Distortion i think its best to use jQuery for this.
You can use jQuery Supersized plugin for this. It's one of the famous plugins mostly used on sites with grounds covering 100% of the width.
Here's the link for the site - http://buildinternet.com/project/supersized/
Try this one. Click Here for Preview
I have edited your sample code
HTML:
<div class="wrapper">
<img src="http://placekitten.com/200/300" alt="" />
</div>
<h1>Interesting Title!</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat magnam culpa obcaecati numquam iusto recusandae totam voluptatibus temporibus ipsum quasi. Nesciunt maiores sequi quis consectetur labore asperiores eaque hic ipsa!</p>
CSS:
div.wrapper{
display: inline-block;
width:500px;
height:400px;
border:1px solid red;
overflow:hidden;
}
img {
width: 100%;
height: auto;
}
h1, p {
width: 80%;
padding-left: 10%;
}
The parent element of your image should be display:inline-block; and the width will be the width of your img.
if you want to position the image, lets say you want to show the center of the image, just add a negative margin-top to the img. Click Here for Preview
img {
width: 100%;
height: auto;
margin-top:-100px;
}