Blocked Wrapper

本文介绍了一种使用CSS解决容器内元素宽度溢出问题的方法,通过包裹元素并调整边界盒模型实现精确布局。

Problem:

We have a container element with % to define the width. In the container, we need to display another element that fullfills this container. The conained element need some border or padding. If we define the contained element's width to 100%, then the border and padding would make the contained element bigger than the container, because the actual width of the element would be the width+padding+border.

 

Solution:  

Add a blocked element like <div> to wrap the contained element. Move the padding, margin or border of the contained element to the wrapper <div>. Set the contained element with width 100%, no border, margin, padding. The wrapper div will fullfill the outer container by the block feature.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .container {
            width: 30%;
            height: 200px;
            background-color: #adff2f;
            padding: 10px;
        }

        .blocked_wrapper {
            border: 1px solid #939598;
            height: 20px;
        }

        .contained {
            width: 100%;
            height: 100%;
            border: 0 none;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="blocked_wrapper">
        <input type="text" class="contained"/>
    </div>
</div>
</body>
</html>

 

Try it with jsfiddle

 

 

If you use CSS3, it would be much more easier. Border-box would completely solve this prblem.

 

.sizing {
    padding: 0px 4px;
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */
}

 

Try it with jsfiddle

 

C:\Users\ASUS\.jdks\openjdk-24.0.1\bin\java.exe -Dmaven.multiModuleProjectDirectory=C:\Users\ASUS\Desktop\san\spring_boot_mystarters -Djansi.passthrough=true -Dmaven.home=C:\Users\ASUS\.m2\wrapper\dists\apache-maven-3.9.9-bin\4nf9hui3q3djbarqar9g711ggc\apache-maven-3.9.9 -Dclassworlds.conf=C:\Users\ASUS\.m2\wrapper\dists\apache-maven-3.9.9-bin\4nf9hui3q3djbarqar9g711ggc\apache-maven-3.9.9\bin\m2.conf "-Dmaven.ext.class.path=C:\Users\ASUS\AppData\Local\JetBrains\IntelliJ IDEA 2025.1.1\plugins\maven\lib\maven-event-listener.jar" -Dvaadin.copilot.pluginDotFilePath=C:\Users\ASUS\Desktop\san\spring_boot_mystarters\.idea\.copilot-plugin "-javaagent:C:\Users\ASUS\AppData\Local\JetBrains\IntelliJ IDEA 2025.1.1\lib\idea_rt.jar=64654" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\ASUS\.m2\wrapper\dists\apache-maven-3.9.9-bin\4nf9hui3q3djbarqar9g711ggc\apache-maven-3.9.9\boot\plexus-classworlds-2.8.0.jar;C:\Users\ASUS\.m2\wrapper\dists\apache-maven-3.9.9-bin\4nf9hui3q3djbarqar9g711ggc\apache-maven-3.9.9\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version=2025.1.1 install -P native,nativeTest WARNING: A restricted method in java.lang.System has been called WARNING: java.lang.System::load has been called by org.fusesource.jansi.internal.JansiLoader in an unnamed module (file:/C:/Users/ASUS/.m2/wrapper/dists/apache-maven-3.9.9-bin/4nf9hui3q3djbarqar9g711ggc/apache-maven-3.9.9/lib/jansi-2.4.1.jar) WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module WARNING: Restricted methods will be blocked in a future release unless native access is enabled WARNING: A terminally deprecated method in sun.misc.Unsafe has been called WARNING: sun.misc.Unsafe::objectFieldOffset has been called by com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper (file:/C:/Users/ASUS/.m2/wrapper/dists/apache-maven-3.9.9-bin/4nf9hui3q3djbarqar9g711ggc/apache-maven-3.9.9/lib/guav
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值